1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

help! Atm bank machine problem

Discussion in 'Java' started by galva, Jan 5, 2011.

  1. galva

    galva New Member

    Joined:
    Dec 8, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hello.

    Can somebody help me with this program code that contains a ATM program.
    The program should display the 10 latest transactions.
    In the array of int [] transactions = new int[10];
    The problem is that it only shows or prints only the 9 last transactions, when I chose to show the balance.
    What am I doing wrong with this code? Can somebody help me...
    Thanks for my attention.

    Code:
    import java.util.Scanner;
    
    
    public class Atm 
    {	
    
    private static int balance = 0;
    
    
    public static void main(String[] args)
    {
    
    Scanner input = new Scanner(System.in);
    
    
    int amount = 0;
    int choice = 0;
    int [] transactions = new int[10];
    int sum;
    
    
    while (choice != 4)
    {
    
    choice = menu();
    
    
    switch(choice)
    {
    
    case 1:
    
    System.out.print("Hur much you want to deposit? :");
    
    sum = input.nextInt();
    if(sum == 0)
    {
    System.out.print("Wrong amount");
    System.out.println();
    System.out.println();
    }
    else
    {
    
    amount = (int) + sum;
    
    makeTransactions(amount, transactions);
    }
    break;
    
    
    case 2:
    
    System.out.print("How much do you want to withdrawl : ");
    sum = input.nextInt();
    if(sum == 0)
    {	
    
    System.out.print("Wrong amount");
    System.out.println();
    System.out.println();
    }
    else
    {
    amount = (int) - sum;
    
    makeTransactions(amount, transactions);
    }
    break;
    
    
    case 3:
    
    showTransactions(transactions, balance);
    break;
    
    
    case 4:
    
    System.out.println("Program exit");
    break;
    }	
    
    }
    }
    
    public static int menu()
    {	
    Scanner input = new Scanner(System.in);
    
    int choice = 0;
    
    
    System.out.println("Enkel BankoMat ");
    System.out.println();
    System.out.println("1. Deposit ");
    System.out.println("2. Withdrawl ");
    System.out.println("3. balance ");
    System.out.println("4. Exit ");
    System.out.println();
    System.out.println("Your choice: ");
    
    
    choice = input.nextInt();
    
    return choice;
    
    } 
    
    
    
    public static void showTransactions(int [] transactions, int balance)
    {
    
    System.out.println();
    System.out.println("The 10 last transactions:");
    System.out.println();
    
    
    for(int i = 0; i <= transactions.length-1; i++)
    {
    if(transactions[i] == 0)
    {
    System.out.print("");
    }
    else
    {	
    for( i = 0;i < transactions.length-1; i++)
    {	
    System.out.println(transactions[i]);
    }
    }
    
    }
    
    System.out.println();
    System.out.println("balance: " + balance + " kr" + "\n" );
    System.out.println();	
    
    } 
    
    
    
    public static void makeTransactions(int amount, int [] transactions)
    {
    int position = findNr(transactions);
    if(position == -1)
    {
    moveTrans(transactions);
    position = findNr(transactions);
    transactions[position] = amount;
    }
    transactions[position] = amount;
    balance += amount;
    
    }
    
    
    public static int findNr(int [] transactions)
    {
    int position = -1;
    
    for(int i = 0; i < transactions.length-1; i++)
    {
    if(transactions[i] == 0)
    {
    position = i;
    break;
    }	
    }
    return position;
    }
    
    
    public static void moveTrans(int [] transactions)
    {
    for(int i = 0; i < transactions.length-1; i++)
    
    transactions[i] = transactions[i + 1] ;
    
    }
    
    
    }
     
    Last edited by a moderator: Jan 6, 2011

Share This Page