ATM with 10 accounts?

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

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

#1 Java Neil  Icon User is offline

  • D.I.C Head

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

ATM with 10 accounts?

Posted 30 March 2011 - 01:42 PM

Hi all...I'm almost done with this semesters assignments. I just have three left and two of them has me a bit anxious.

Here is the prompt for the first one:

Create a class to simulate an ATM machine. Create 10 accounts in an array with id 0, 1,........,9 and initial balance $100. The system should prompt the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu/window should be displayed. The main window should have 4 choices.

1. Check Balance
2. Withdraw
3. Deposit
4. Exit

If exit is chosen, the system should prompt for the id again. Which means once the system starts, it will never stop.

Now I wrote a program a few chapters ago that is on the same lines. It just needs a major tweak here and there for the assignment to be fulfilled.

Could someone look at that code and maybe comment out the parts of code that is not needed, and add some comments in the appropriate places for code that is needed to complete the prompt?

   import javax.swing.JOptionPane;
   import java.util.Date;
   public class TestAccount {
      public static void main(String[] args) {
      	    
         Account account = new Account(1122, 20000, 4.5);
      	
         account.setWithdraw(0);
         account.setDeposite(0);
         account.setTotal(0);
         account.setMonthly(0);
         	       
         JOptionPane.showMessageDialog(null, account.toString());
            
         JOptionPane.showMessageDialog(null, "Good Buy");    
      }  
   }
	
   class Account {
   
      private int id = 0;
      private double balance = 0;
      private double annualInterestRate = 0;
      private double withdraw = 0;
      private double deposite = 0;
      private double total = 0;
      private double monthly = 0;
      private Date date = new Date();
      
      public Account() {
      }
      
      public Account(int id, double balance, double annualInterestRate){
         this.id = id;
         this.balance = balance;
         this.annualInterestRate = annualInterestRate;
      }
            
      public int getId(){
      
         return this.id;
      
      }
   	
      public void setId(int newId) {
        
         id = newId;
      }
   	  
      public double getBalance() {
        
         return this.balance;
      }
      
      public void setBalance(double newBalance) {
        
         balance = newBalance;
      }
   		  
      public double getAnnualInterestRate() {
        
         return this.annualInterestRate;
      } 
             
      public void setAnnualInterestRate(double newAnnualInterestRate) {
        
         annualInterestRate = newAnnualInterestRate;
      
      }
     
      public java.util.Date getDate() {
      
         return this.date;
            
      }
   	
      public double getWithdraw() {
      
         return this.withdraw;
      }
   	
      public void setWithdraw(double newWithdraw) {
        
         withdraw = Double.parseDouble(JOptionPane.showInputDialog(null, "Account Balance: $"
            + balance + "\n" + "\nHow much would like to withdraw?"));
      
      }
      
      public double getDeposite() {
      
         return this.deposite;
      }
   	
      public void setDeposite(double newDeposite) {
        
         deposite = Double.parseDouble(JOptionPane.showInputDialog(null, "Account Balance: "
            + (balance - withdraw) + "\n" + "\nHow much would like to deposite?"));
      
      }
   	
      public double getTotal(double balance, double deposite, double withdraw) {
      
         return this.total;
      }
   	
      public void setTotal(double newTotal) {
        
         total = balance - withdraw + deposite;
      
      }
   	
      public double getMonthly(double total) {
      
         return this.monthly;
      }
   	
      public void setMonthly(double newMonthly) {
        
         monthly = total * .045 / 12;
      
      }
   
      public String toString() {
         return "Banking Account Information\n" 
            + "\nYour Customer ID # " + id
            + "\nStarting Balnce $" + balance 
            + "\nAnnual Interest Rate " + annualInterestRate + "%"
            + "\nWithdraw Amount $" + withdraw
            + "\nDeposite Amount $" + deposite
            + "\nSubtotal $" + total
            + "\nMonthly Interest $" 
            + Math.round(monthly * 100.0)/100.0 
            + "\n" + "\n" + "As of " + date;
      }
   }


Thanks a lot for your help. Everyone here has been so helpful on furthering my understanding of this language. Every pointer has added as much knowledge as I receive in my class, so I tell everyone that this is my second classroom.

Is This A Good Question/Topic? 0
  • +

Replies To: ATM with 10 accounts?

#2 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 30 March 2011 - 02:17 PM

When it comes to a bank account, when you withdraw or deposit the balance changes. You're going to want a withdraw method which will subtract from balance and add to balance for a deposit method :)
Was This Post Helpful? 2
  • +
  • -

#3 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 - 02:24 PM

View PostTFoSSDQ, on 30 March 2011 - 02:17 PM, said:

When it comes to a bank account, when you withdraw or deposit the balance changes. You're going to want a withdraw method which will subtract from balance and add to balance for a deposit method :)


I thought there was already something like that in there?

      public double getTotal(double balance, double deposite, double withdraw) {
      
         return this.total;
      }
   	
      public void setTotal(double newTotal) {
        
         total = balance - withdraw + deposite;
      
      }

Was This Post Helpful? 0
  • +
  • -

#4 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 30 March 2011 - 02:36 PM

View PostJava Neil, on 30 March 2011 - 02:24 PM, said:

I thought there was already something like that in there?

      public double getTotal(double balance, double deposite, double withdraw) {
      
         return this.total;
      }
   	
      public void setTotal(double newTotal) {
        
         total = balance - withdraw + deposite;
      
      }

Why do you have total and balance? Just have balance and its getter. Also, it's easier to work with if you have separate methods for withdraw and deposit:
public void withdraw(double amount)
{
    balance -= amount;
}
public void deposit(double amount)
{
    balance += amount;
}


That should work for the withdraw and deposit options. For the check balance option, just show a message dialog with the getter for the balance:
//in main
JOptionPane.showMessageDialog(null, "The balance is: "+account.getBalance());

//if the choice is withdraw
account.withdraw(amount);

//if the choice is deposit
account.deposit(amount);


Also, to stay in the program without exiting you would need a while loop, and you would need another while loop for the chosen account after an ID is entered

This post has been edited by TFoSSDQ: 30 March 2011 - 02:38 PM

Was This Post Helpful? 3
  • +
  • -

#5 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 - 03:31 PM

View PostTFoSSDQ, on 30 March 2011 - 02:36 PM, said:

View PostJava Neil, on 30 March 2011 - 02:24 PM, said:

I thought there was already something like that in there?

      public double getTotal(double balance, double deposite, double withdraw) {
      
         return this.total;
      }
   	
      public void setTotal(double newTotal) {
        
         total = balance - withdraw + deposite;
      
      }

Why do you have total and balance? Just have balance and its getter. Also, it's easier to work with if you have separate methods for withdraw and deposit:
public void withdraw(double amount)
{
    balance -= amount;
}
public void deposit(double amount)
{
    balance += amount;
}


That should work for the withdraw and deposit options. For the check balance option, just show a message dialog with the getter for the balance:
//in main
JOptionPane.showMessageDialog(null, "The balance is: "+account.getBalance());

//if the choice is withdraw
account.withdraw(amount);

//if the choice is deposit
account.deposit(amount);


Also, to stay in the program without exiting you would need a while loop, and you would need another while loop for the chosen account after an ID is entered

I see your point about the two unneeded fields, but how would you get the user input that way? Right now both withdraw and deposit have this...

      public double getWithdraw() {
      
         return this.withdraw;
      }
   	
      public void setWithdraw(double newWithdraw) {
        
         withdraw = Double.parseDouble(JOptionPane.showInputDialog(null, "How much would like to withdraw?"));
      
      }
      
      public double getDeposite() {
      
         return this.deposite;
      }
   	
      public void setDeposite(double newDeposite) {
        
         deposite = Double.parseDouble(JOptionPane.showInputDialog(null, "How much would like to deposite?"));
      
      }


Also I need to think about creating an array for the 10 ids. Any thoughts on that?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9030
  • View blog
  • Posts: 33,501
  • Joined: 27-December 08

Re: ATM with 10 accounts?

Posted 30 March 2011 - 03:58 PM

Your user interface and program should always be separate. Another class should be responsible for getting user input, and invoking the appropriate setter method. The Account objects themselves should not be responsible for user input.

For your array, you should be storing individual Account objects in it, not the individual ids.

You might want to check out my tutorial Moving Away From Parallel Arrays. It covers class design mainly, but also look at how I structure the code to separate the program data from the user interface at a basic level.
Was This Post Helpful? 1
  • +
  • -

#7 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 - 04:19 PM

View Postmacosxnerd101, on 30 March 2011 - 03:58 PM, said:

Your user interface and program should always be separate. Another class should be responsible for getting user input, and invoking the appropriate setter method. The Account objects themselves should not be responsible for user input.

For your array, you should be storing individual Account objects in it, not the individual ids.

You might want to check out my tutorial Moving Away From Parallel Arrays. It covers class design mainly, but also look at how I structure the code to separate the program data from the user interface at a basic level.


Thanks macosxnerd101! I'll read your tutorial right now. I know I need a better understanding of program design, especially when it comes down to unnecessary code and making things more modular.

Below is my latest code trying to implement the suggestions so far. Please set me straight.


   import javax.swing.JOptionPane;
   import java.util.Date;
   public class TestATM {
      public static void main(String[] args) {
      	    
         Account account = new Account();
      	
         account.setId(0);
         account.withdraw(0);
         account.deposit(0);
      
         JOptionPane.showMessageDialog(null, "Your balance is: " + account.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 = Integer.parseInt(JOptionPane.showInputDialog(null, 
            "Enter your customer ID #"));
      }
   	  
      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 getDeposite() {
      
         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
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9030
  • View blog
  • Posts: 33,501
  • Joined: 27-December 08

Re: ATM with 10 accounts?

Posted 30 March 2011 - 04:23 PM

Your setId() method should not process user input at all. It accepts a parameter. That is the value you want to assign to your id instance variable.
Was This Post Helpful? 1
  • +
  • -

#9 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 - 04:34 PM

View Postmacosxnerd101, on 30 March 2011 - 04:23 PM, said:

Your setId() method should not process user input at all. It accepts a parameter. That is the value you want to assign to your id instance variable.


Like so...?

      public void setId(int newId) {
        
         id = newId;
      }


By the way, that was a great tutorial! A lot to take in, but fantastic!

This post has been edited by Java Neil: 30 March 2011 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9030
  • View blog
  • Posts: 33,501
  • Joined: 27-December 08

Re: ATM with 10 accounts?

Posted 30 March 2011 - 04:41 PM

Exactly! I'm glad you found my tutorial helpful as well! :)
Was This Post Helpful? 1
  • +
  • -

#11 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 - 04:47 PM

Well I tried to setup the array but I got many errors.

   import javax.swing.JOptionPane;
   import java.util.Date;
   public class TestATM {
      public static void main(String[] args) {
      	    
         Account[] account = new Account[10];
      	
         account.setId(0);
         account.withdraw(0);
         account.deposit(0);
      
         JOptionPane.showMessageDialog(null, "Your balance is: " + account.getBalance());  
      }  
   }


TestATM.java:8: cannot find symbol
symbol : method setId(int)
location: class Account[]
account.setId(0);
^
TestATM.java:9: cannot find symbol
symbol : method withdraw(int)
location: class Account[]
account.withdraw(0);
^
TestATM.java:10: cannot find symbol
symbol : method deposit(int)
location: class Account[]
account.deposit(0);
^
TestATM.java:12: cannot find symbol
symbol : method getBalance()
location: class Account[]
JOptionPane.showMessageDialog(null, "Your balance is: " + account.getBalance());
^
4 errors

I'm not used to classes and arrays. No experience...
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9030
  • View blog
  • Posts: 33,501
  • Joined: 27-December 08

Re: ATM with 10 accounts?

Posted 30 March 2011 - 04:49 PM

You should check out this arrays tutorial. :)
Was This Post Helpful? 1
  • +
  • -

#13 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 - 05:14 PM

View Postmacosxnerd101, on 30 March 2011 - 04:49 PM, said:

You should check out this arrays tutorial. :)


Another good tutorial. It however doesn't help me with the errors I I'm getting?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9030
  • View blog
  • Posts: 33,501
  • Joined: 27-December 08

Re: ATM with 10 accounts?

Posted 30 March 2011 - 05:44 PM

Basically, an array is a variable that stores other variables. Rather than using different names, numbers are used to allow us to easily access and iterate through a Collection of elements. So you have to access the methods from the elements in the array, not the array itself. That's why I linked the above tutorial. :)

As an example, if I want to access the balance from the first Account in the array, I could simply do:
double balance = accounts[0].getBalance();



Note that accounts[0] is an Account, while accounts is the variable that refers to the group of Account objects.
Was This Post Helpful? 1
  • +
  • -

#15 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 - 05:59 PM

I'm starting to get it I think...

I got this to compile, but when I ran it I got a null error suggesting it was not accessing anything? I'm almost starting to understand??? Maybe...LOL

   import javax.swing.JOptionPane;
   import java.util.Date;
   public class TestATM {
      public static void main(String[] args) {
      	    
         Account[] account = new Account[10];
      	
         double balance = account[0].getBalance();
      
         JOptionPane.showMessageDialog(null, "Your balance is: " + balance);  
      }  
   }
	
   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 getDeposite() {
      
         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;
      }
   }


Exception in thread "main" java.lang.NullPointerException
at TestATM.main(TestATM.java:8)
Was This Post Helpful? 0
  • +
  • -

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