1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

help! with simple ATM machine

Discussion in 'Java' started by galva, Dec 8, 2010.

  1. galva

    galva New Member

    Joined:
    Dec 8, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    /*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 a moderator: Dec 8, 2010
  2. galva

    galva New Member

    Joined:
    Dec 8, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  3. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    try the changes i've made and tell if this is what you want

    Code:
    import java.util.Scanner;
    
    
    public class BankoMat{
    	[COLOR="Red"]private static int position=-1;[/COLOR]//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{
             						[COLOR="Red"]balance+=sum;[/COLOR]//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 [COLOR="Red"]|| sum>balance)[/COLOR]{
             						System.out.print("Wrong amount");
             						System.out.println();
             						System.out.println();
             					}else{
             						[COLOR="Red"]balance+=-sum;[/COLOR]
             					    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 < [COLOR="Red"]transactions.length[/COLOR]; i++){
    					if(transactions[i] != 0){
    						System.out.print(""+(i+1)+")  "+transactions[i] + "\n");
    							[COLOR="Red"]//balance = balance + transactions[i];[/COLOR]//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){
    			[COLOR="Red"]if (position<9)
    				position++;
    			if (position==9){
    				[COLOR="Red"]moveTrans(transactions);//move all records one element up[/COLOR]
    			    transactions[position] = amount;
    			}else
    				transactions[position] = amount;[/COLOR]
    						
    		}
    		
    
    		
    [COLOR="Red"]		public static void moveTrans(int [] transactions)
    		{
    			for(int i = 1; i < transactions.length; i++)
    					transactions[i-1] = transactions[i] ;
    		}[/COLOR]
    		
    
    }
    
     
    Last edited: Dec 10, 2010
  4. galva

    galva New Member

    Joined:
    Dec 8, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thank you very much for you help, virxen!
     
  5. galva

    galva New Member

    Joined:
    Dec 8, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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]);
                                }
                                  
                                
                            
                            }
                
                    }
    
    
     
  6. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    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 likes this.
  7. Vanna Guo

    Vanna Guo New Member

    Joined:
    Feb 9, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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:

Share This Page