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] ;

            }


}

suspected
for(int i = 0; i <= transactions.length-1; i++)

...
Use the following method to reach a proper solution.
1. shorten length of the array, eg to 3
2. testing and limitation to one option
3. resigning from the keyboard to enter data
4. printing the entire array

static void dump(int[] transactions) {
        for (int i = 0; i < transactions.length; i++) {
            System.out.print(transactions[i] + ",");
        }
        System.out.println();
    }

    public static void main(String[] args) {

        int[] transactions = new int[3];
        dump(transactions);
        makeTransactions(1, transactions);
        dump(transactions);
        makeTransactions(2, transactions);
        dump(transactions);
        makeTransactions(3, transactions);
        dump(transactions);
        makeTransactions(4, transactions);
        dump(transactions);
    }

run:
0,0,0,
1,0,0,
1,2,0,
2,3,0,
3,4,0,
BUILD SUCCESSFUL (total time: 2000 seconds)

for(int i = 0; i <= transactions.length-1; i++)

I agree with quuba. using transactions.length is different then using

for(int i = 0; i <= 10; i++)

using length looks at the number of values in transactions. So if you do the following transaction.length-1 you are basically removing a value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.