Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   help! Atm bank machine problem (http://www.go4expert.com/forums/help-atm-bank-machine-t24400/)

galva 6Jan2011 02:27

help! Atm bank machine problem
 
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] ;

}


}



All times are GMT +5.5. The time now is 16:33.