Hello.
Can someone help me with this ATM program.
The program is working good but I have to show the 10 last transactions
Instead the program prints only out 9 lasttransactions, what do I do wrong?
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; 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] ;
}
}