help! with simple ATM machine

galva's Avatar, Join Date: Dec 2010
Light Poster
/*Hello. I dont't know what wrong i do. It seems that i can't show the transactions history.
It works well until the 10 transactions. But then the program can't rewrite the first array
so only ten latest transactions should be shown. The problem is that i am stuck and i don't know how to go further. Plese, could someone help me...*/




Code:
import java.util.Scanner;


public class BankoMat 
{
	
    public static void main(String[] args)
    {
         Scanner keyboard = new Scanner(System.in);
         
         int amount = 0;
         int choice = 0;
         int [] transactions = new int[10];
         int sum;
         int balance = 0;
         
         while (choice != 4)
         {
         	choice = menu();
         	switch(choice)
         	{
         		case 1:
         				System.out.print("How much will you deposit? :");
         				sum = keyboard.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 = keyboard.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("End program");
         				break;
         	}	
         	
         }
    }
    
    
    	public static int menu()
    	{
    		Scanner keyboard = new Scanner(System.in);
    		int choice = 0;
    		
    		System.out.println("Simple ATM ");
    		System.out.println();
    		System.out.println("1. Deposit ");
    		System.out.println("2. Withdrawl ");
    		System.out.println("3. Balance ");
    		System.out.println("4. End ");
    		System.out.println();
    		System.out.println("Your choice: ");
    		
    		choice = keyboard.nextInt();
    		return choice;
    	}
    	
		public static void showTransactions(int [] transactions, int balance)
		{
			System.out.println();
			System.out.println("10 last transactions:");
			System.out.println();
			
				for(int i = 0; i < transactions.length-1; i++)
				{
					if(transactions[i] == 0)
					{
						System.out.print("");
					}
					
						else
						{
							System.out.print(transactions[i] + "\n");
							balance = balance + 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;
			}
			else
			{
				transactions[position] = 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[0] = transactions[i + 1] ;
				
		}
		

}

Last edited by shabbir; 8Dec2010 at 19:15.. Reason: Code blocks
0
galva's Avatar, Join Date: Dec 2010
Light Poster
it seems strange. I think the array only save the 10 transactions. But what about the balance?
I Try to make about 15 transactions with deposit and withdrawl. Look att the balance. It seems strange to me????
Can you note something wrong that I don't understand, maybe is the array of only 10 saved index?
Anyway the balance should save all the transactions from the beginning...
Do you have any idea about that, so the balance will save all transactions from beginning?


this is what the program should do:
array of 10 index were the latest 10 transactions will be saved.
The balance should save all the deposit and withdrawl from the beginning.
the program should show the 10 latest transaction and show the final balance from all inputs of the user.

Can someone help me out how i can go thru this problem.
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
try the changes i've made and tell if this is what you want

Code:
import java.util.Scanner;


public class BankoMat{
	private static int position=-1;//i need this global
    public static void main(String[] args){
         Scanner keyboard = new Scanner(System.in);
         int choice = 0;
         int [] transactions = new int[10];
         int sum;
         int balance = 0;
         
         while (choice != 4){
         	choice = menu();
         	switch(choice){
         		case 1:
         				System.out.print("How much will you deposit? :");
         				sum = keyboard.nextInt();
         					if(sum == 0){
         						System.out.print("Wrong amount");
         						System.out.println();
         						System.out.println();
         					}
         					else{
         						balance+=sum;//balance is how much money exists in our bank account
         						makeTransactions(sum, transactions);
         					}
         				break;
         		
         		case 2:
         				System.out.print("How much do you want to withdrawl?: ");
         				sum = keyboard.nextInt();
         					if(sum == 0 || sum>balance){
         						System.out.print("Wrong amount");
         						System.out.println();
         						System.out.println();
         					}else{
         						balance+=-sum;
         					    makeTransactions(-sum, transactions);
         					}
         				 break;
         		
         		case 3:
         				showTransactions(transactions, balance);
         				break;
         				
         		case 4:
         				System.out.println("End program");
         				break;
         	}	
         	
         }
    }
    
    
    	public static int menu(){
    		Scanner keyboard = new Scanner(System.in);
    		int choice = 0;
    		System.out.println("Simple ATM ");
    		System.out.println();
    		System.out.println("1. Deposit ");
    		System.out.println("2. Withdrawl ");
    		System.out.println("3. Balance ");
    		System.out.println("4. End ");
    		System.out.println();
    		System.out.println("Your choice: ");
    		choice = keyboard.nextInt();
    		return choice;
    	}
    	
		public static void showTransactions(int [] transactions, int balance){
			System.out.println();
			System.out.println("10 last transactions:");
			System.out.println();
			
				for(int i = 0; i < transactions.length; i++){
					if(transactions[i] != 0){
						System.out.print(""+(i+1)+")  "+transactions[i] + "\n");
							//balance = balance + transactions[i];//it's for all transactions not only for the visible ones
					}
				}
			System.out.println();
			System.out.println("Balance: " + balance + " kr" + "\n" );
			System.out.println();		

		}
		
		public static void makeTransactions(int amount, int [] transactions){
			if (position<9)
				position++;
			if (position==9){
				moveTrans(transactions);//move all records one element up
			    transactions[position] = amount;
			}else
				transactions[position] = amount;
						
		}
		

		
		public static void moveTrans(int [] transactions)
		{
			for(int i = 1; i < transactions.length; i++)
					transactions[i-1] = transactions[i] ;
		}
		

}

Last edited by virxen; 10Dec2010 at 05:49..
0
galva's Avatar, Join Date: Dec 2010
Light Poster
Thank you very much for you help, virxen!
0
galva's Avatar, Join Date: Dec 2010
Light Poster
How can I print out the transaction array in JOptionPane???
Can someone help me? This code snippet is from a method...
But it just this I want that the transactions will be shown in JoptionPane.

I appriciate all help..

Code:
    

public static void showTransactions(int [] transactions, int balance)
        {
            
            System.out.println();
            System.out.println("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]);
                            }
                              
                            
                        
                        }
            
                }
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
one way is this

Code:
        public static void showTransactions(int [] transactions, int balance){
            JTextArea text=new JTextArea();
            System.out.println();
            text.append("10 last transactions:\n\n");
                for(int i = 0; i < transactions.length; i++){
                    if(transactions[i] != 0)
                        text.append(""+(i+1)+")  "+transactions[i]+"\n");
                }
            text.append("\nBalance: " + balance + " kr\n");
             JOptionPane.showMessageDialog(null,text);
        }
shabbir like this
0
Vanna Guo's Avatar, Join Date: Feb 2010
Newbie Member
I am doing a similar project. it is due in 5 hours...

virxen, if you can help me... thanks. But i warn you, my program is all over the place.

This is the project Iam trying to do:

1) main class who asks for customer's cash. Who execute all the methods.
2) Account class that takes the customer's amount of cash
3)withdraw class who extends from Account, who takes the amount withdrawed (from
either savings or checkings) then returns the totals of customers cash and balance.

4)deposit class who extends from account, who takes theamount deposited (from either savings or checkings) then returns the totals of customers cash and balance.

----

Must have:
1. client class, 2 classes + 1 subclass.
2. one constructor, 3 instance varible per class.
3. the program must loop untill user decides to stop.
4. all input/output in client.

----

want (at least):
1 public static variable
1 static method
1 toString() method
1 random number generator
1 array
3 Enum declarations
-------------------

P.S. Don't mind the NewAccount class I dont think I have enough time to do it.
Attached Files
File Type: zip Banking - Copy.zip (16.1 KB, 7 views)