ATM with 10 accounts?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

55 Replies - 7192 Views - Last Post: 31 March 2011 - 07:59 AM Rate Topic: -----

#31 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: ATM with 10 accounts?

Posted 30 March 2011 - 08:35 PM

Man! You are a pro!

My next quandary is how to get the input for the withdraw and deposit?

Updated code...

   import javax.swing.JOptionPane;
   import java.util.Date;
   public class TestATM {
      public static void main(String[] args) {
      	    
      //Creates 10 accounts
         Account[] accounts = new Account[10];
         for (int i = 0; i < accounts.length; i++) { 
            accounts[i] = new Account(); 
         }
         
         while(true) {
         
            boolean using = true;
         
            int idInput = Integer.parseInt(JOptionPane.showInputDialog("Enter your account ID"));
            int index = -1; //holder position for the Account index
         	
            for(int i = 0; i < accounts.length; i++) {
               if(accounts[i].getId() == idInput) {
                  index = i; //we have the index;
                  i = accounts.length;
               }				
            }
            
            if(index != -1) {
               while(using) {
               
 //display menu and do everything in here.  If exit is selected set using to false and it will go back to the ID input					
               
               }
            }
            else {
            
               JOptionPane.showMessageDialog(null, "invalid ID");
            }
         	
            JOptionPane.showMessageDialog(null, 
               "Your balance is: $" + accounts[0].getBalance());
         }   
      }
   }  
	
   class Account {
   
      private int id = 0;
      private double balance = 100;
      private double withdraw = 0;
      private double deposit = 0;
      private double amount = 0;
      private Date date = new Date();
      
      public Account() {
      }
      
      public Account(int id, double balance){
      
         this.id = id;
         this.balance = balance;
      }
            
      public int getId(){
      
         return this.id;
      }
   	
      public void setId(int newId) {
        
         id = newId;	
      }
   	  
      public double getBalance() {
        
         return this.balance;   
      }		  
     
      public java.util.Date getDate() {
      
         return this.date;      
      }
   	
      public double getWithdraw() {
      
         return this.withdraw;   
      }
   	
      public void withdraw(double amount) {
      
         balance -= amount;   
      }
      
      public double getDeposit() {
      
         return this.deposit;   
      }
   	
      public void deposit(double amount) {
      
         balance += amount;
      }
   
      public String toString() {
         return "Banking Account Information\n" 
            + "\nStarting Balnce $" + balance 
            + "\nYour Customer ID # " + id
            + "\nWithdraw Amount $" + withdraw
            + "\nDeposite Amount $" + deposit
            + "\n" + "\n" + date;
      }
   }

Was This Post Helpful? 0
  • +
  • -

#32 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: ATM with 10 accounts?

Posted 30 March 2011 - 08:52 PM

I've setup the menu but the program never displays it? Also the program always says invalid id?

   import javax.swing.JOptionPane;
   import java.util.Date;
   public class TestATM {
      public static void main(String[] args) {
      	    
      //Creates 10 accounts
         Account[] accounts = new Account[10];
         for (int i = 0; i < accounts.length; i++) { 
            accounts[i] = new Account(); 
         }
         
         while(true) {
         
            boolean using = true;
         
            int idInput = Integer.parseInt(JOptionPane.showInputDialog("Enter your account ID"));
            int index = -1; //holder position for the Account index
         	
            for(int i = 0; i < accounts.length; i++) {
               if(accounts[i].getId() == idInput) {
                  index = i; //we have the index;
                  i = accounts.length;
               }				
            }
            
            if(index != -1) {
               while(using) {
               
               //display menu and do everything in here.  If exit is selected set using to false and it will go back to the ID input					
                  JOptionPane.showInputDialog(null, 
                     "ATM main menu:\n"
                     + "1 - View account balance\n"
                     + "2 - Withdraw funds\n"
                     + "3 - Add funds\n"
                     + "4 - Terminate transaction");
               }
            }
            else {
            
               JOptionPane.showMessageDialog(null, "invalid ID");
            }
         	
            JOptionPane.showMessageDialog(null, 
               "Your balance is: $" + accounts[0].getBalance());
         }   
      }
   }  

This post has been edited by Java Neil: 30 March 2011 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#33 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,555
  • Joined: 27-December 08

Re: ATM with 10 accounts?

Posted 30 March 2011 - 08:57 PM

Take a look at your Account class. You are using the no-args constructor, which doesn't modify the instance variables at all. So all your Accounts have an id of 0. And when I ran your program, I saw the JOptionPane with the menu of the four options.
Was This Post Helpful? 1
  • +
  • -

#34 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: ATM with 10 accounts?

Posted 30 March 2011 - 09:14 PM

I'm feeling stupid now. I'm not sure what you mean by no-arg constructor and when I run the program I see no JOptionPane with the four options?
Was This Post Helpful? 0
  • +
  • -

#35 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,555
  • Joined: 27-December 08

Re: ATM with 10 accounts?

Posted 30 March 2011 - 09:16 PM

The Account constructor here accepts no parameters. Thus it is called the no-args (or no arguments) constructor.
accounts[i] = new Account(); 



Keep in mind all the Account ids are 0. So if you don't select 0 as the id initially, you won't see the prompt.
Was This Post Helpful? 1
  • +
  • -

#36 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: ATM with 10 accounts?

Posted 30 March 2011 - 09:24 PM

View Postmacosxnerd101, on 30 March 2011 - 09:16 PM, said:

The Account constructor here accepts no parameters. Thus it is called the no-args (or no arguments) constructor.
accounts[i] = new Account(); 



Keep in mind all the Account ids are 0. So if you don't select 0 as the id initially, you won't see the prompt.


So what's the trick in allowing parameters?

Thanks for the heads up about the menu by the way. Yes I can see it.
Was This Post Helpful? 0
  • +
  • -

#37 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,555
  • Joined: 27-December 08

Re: ATM with 10 accounts?

Posted 30 March 2011 - 09:28 PM

Take a look at your Account class and the constructors you defined for it. You just pass values or variables to the constructor as you would when invoking a method. Pass the values of corresponding type in the specified order.
Was This Post Helpful? 2
  • +
  • -

#38 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: ATM with 10 accounts?

Posted 30 March 2011 - 09:36 PM

Like this?

accounts[i] = new Account(0, 100);

Was This Post Helpful? 0
  • +
  • -

#39 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,555
  • Joined: 27-December 08

Re: ATM with 10 accounts?

Posted 30 March 2011 - 09:38 PM

You're still declaring all the Accounts with the same id. But syntactically, that's legal.
Was This Post Helpful? 1
  • +
  • -

#40 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: ATM with 10 accounts?

Posted 30 March 2011 - 09:45 PM

Well I need 10 ids. How would I do that?
Was This Post Helpful? 0
  • +
  • -

#41 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,555
  • Joined: 27-December 08

Re: ATM with 10 accounts?

Posted 30 March 2011 - 09:49 PM

The counter variable in your for loop (i) goes from 0-9. Wouldn't it make sense to pass that instead of 0 on each iteration?
Was This Post Helpful? 0
  • +
  • -

#42 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: ATM with 10 accounts?

Posted 30 March 2011 - 10:05 PM

Got it...

Now I'm trying to move on to the choices. Does this seem like the right approach?

                  Integer.parseInt(JOptionPane.showInputDialog(null, 
                     "      ATM main menu:\n" + "\n"
                     + "1 - View account balance\n"
                     + "2 - Withdraw funds\n"
                     + "3 - Deposit funds\n"
                     + "4 - Terminate transaction\n" + "\n"));
                     
                  if(idInput == 1){
                  JOptionPane.showInputDialog(null, 
                  "As of " + accounts[0].getDate() + "|n"
                  "you have a balance of " + accounts[0].getBalance());
                  
                  }

Was This Post Helpful? 0
  • +
  • -

#43 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: ATM with 10 accounts?

Posted 31 March 2011 - 04:31 AM

Well I was up late trying to get the framework in place. What I need is for someone to check my work and help me take the next steps. The program will now respond the the user input and if exit is chosen it will go back to the id prompt. Now I need to have the withdrawal and deposit part of the code hashed out. It's a bit confusing keeping up with the logic.

Thanks for your help!

   import javax.swing.JOptionPane;
   import java.util.Date;
   public class TestATM {
      public static void main(String[] args) {
      	    
      //Creates 10 accounts
         Account[] accounts = new Account[10];
         for (int i = 0; i < accounts.length; i++) { 
            accounts[i] = new Account(i, 100); 
         }
         
         while(true) {
         
            boolean using = true;
         
            int idInput = Integer.parseInt(JOptionPane.showInputDialog("Enter your account ID"));
            int index = -1; //holder position for the Account index
         	
            for(int i = 0; i < accounts.length; i++) {
               if(accounts[i].getId() == idInput) {
                  index = i; //we have the index;
                  i = accounts.length;
               }				
            }
            
            if(index != -1) {
               while(using) {
               
               //display menu 					
                  index = Integer.parseInt(JOptionPane.showInputDialog(null, 
                       "      ATM main menu:\n" + "\n"
                     + "1 - View account balance\n"
                     + "2 - Withdraw funds\n"
                     + "3 - Deposit funds\n"
                     + "4 - Terminate transaction\n" + "\n"));  
                     
                  if (index == 1){
                     JOptionPane.showMessageDialog(null, 
                        accounts[0].getDate() + "\n" + "\n" +
                        "You have a balance of $" + accounts[0].getBalance() + "\n" + "\n");
                  
                  }
                  if (index == 2) {
                     JOptionPane.showInputDialog(null, 
                        "Balance " + accounts[0].getBalance() + "\n" + "\n"
                        + "Enter a withdrawal amount\n" + "\n");
                     if (index > accounts[0].getBalance()) {
                        JOptionPane.showMessageDialog(null, "Isuficient Funds");
                        using = true;
                     }
                  }
                  if (index == 3){
                     JOptionPane.showInputDialog(null, 
                        "Balance " + accounts[0].getBalance() + "\n" + "\n"
                        + "Enter a deposit amount\n" + "\n");
                  
                  }
                  if (index == 4){
                     using = false;
                  }
               }
            }
            
            else {
            
               JOptionPane.showMessageDialog(null, "Invalid ID");
            }
         }   
      }
   }  
	
   class Account {
   
      private int id = 0;
      private double balance = 0;
      private double withdraw = 0;
      private double deposit = 0;
      private double amount = 0;
      private Date date = new Date();
      
      public Account() {
      }
      
      public Account(int id, double balance){
      
         this.id = id;
         this.balance = balance;
      }
            
      public int getId(){
      
         return this.id;
      }
   	
      public void setId(int newId) {
        
         id = newId;	
      }
   	  
      public double getBalance() {
        
         return this.balance;   
      }		  
     
      public java.util.Date getDate() {
      
         return this.date;      
      }
   	
      public double getWithdraw() {
      
         return this.withdraw;   
      }
   	
      public void withdraw(double amount) {
      
         balance -= amount;   
      }
      
      public double getDeposit() {
      
         return this.deposit;   
      }
   	
      public void deposit(double amount) {
      
         balance += amount;
      }
   
      public String toString() {
         return "Banking Account Information\n" 
            + "\nStarting Balnce $" + balance 
            + "\nYour Customer ID # " + id
            + "\nWithdraw Amount $" + withdraw
            + "\nDeposite Amount $" + deposit
            + "\n" + "\n" + date;
      }
   }

Was This Post Helpful? 0
  • +
  • -

#44 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 122
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: ATM with 10 accounts?

Posted 31 March 2011 - 04:36 AM

Don't redefine index, index has to stay the same so we still know what Account we're using, change the menu variable to something else :). We do this so we can access accounts[index] and call withdraw method accounts[index].withdraw(amount) and same for deposit accounts[index].deposit(amount) :)
Was This Post Helpful? 2
  • +
  • -

#45 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: ATM with 10 accounts?

Posted 31 March 2011 - 04:49 AM

View PostTFoSSDQ, on 31 March 2011 - 04:36 AM, said:

Don't redefine index, index has to stay the same so we still know what Account we're using, change the menu variable to something else :). We do this so we can access accounts[index] and call withdraw method accounts[index].withdraw(amount) and same for deposit accounts[index].deposit(amount) :)


I'm with you on the first part...

                  int choice = Integer.parseInt(JOptionPane.showInputDialog(null, 
                       "      ATM main menu:\n" + "\n"
                     + "1 - View account balance\n"
                     + "2 - Withdraw funds\n"
                     + "3 - Deposit funds\n"
                     + "4 - Terminate transaction\n" + "\n"));  
                     
                  if (choice == 1){
                     JOptionPane.showMessageDialog(null, 
                        accounts[0].getDate() + "\n" + "\n" +
                        "You have a balance of $" + accounts[0].getBalance() + "\n" + "\n");
                  
                  }
                  if (choice == 2) {
                     JOptionPane.showInputDialog(null, 
                        "Balance " + accounts[0].getBalance() + "\n" + "\n"
                        + "Enter a withdrawal amount\n" + "\n");
                     if (choice > accounts[0].getBalance()) {
                        JOptionPane.showMessageDialog(null, "Isuficient Funds");
                        using = true;
                     }
                  }
                  if (choice == 3){
                     JOptionPane.showInputDialog(null, 
                        "Balance " + accounts[0].getBalance() + "\n" + "\n"
                        + "Enter a deposit amount\n" + "\n");
                  
                  }
                  if (choice == 4){
                     using = false;
                  }
               }
            }
            


I'm not sure where to invoke the accounts[index].withdraw(amount) and accounts[index].deposit(amount) calls to keep score, so to speak.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4