Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ATM simmulation...

Posted on 2011-10-24
10
Medium Priority
?
253 Views
Last Modified: 2012-05-12
In the attached code, I have tried to simulate the basic ATM operations. All is working, but I want to be looked at to see if there are areas that could be improved.

Thank you.
import java.util.*;
import java.text.SimpleDateFormat;

public class ATM {
    
    static int id;
    static boolean terminate;
    static int acctIndex;

    public static void main(String[] args){
        // create an array variable for 10 accounts
        Account[] myAccount = new Account[10];
        
        Scanner in = new Scanner(System.in);
        
        // create 10 accounts each with initial $100 in them
        for(int i=0;i<myAccount.length;i++)
            myAccount[i]=new Account(i+11,100);
        
        while(!terminate){
            System.out.println("Eneter customer ID:");
            id = in.nextInt();
            
            terminate = checkId(myAccount, id);
            
            if(!terminate)
                System.out.println("\n"+id + " is not a valid id.");   
        }
    
        terminate=false;
        
        while(!terminate){    
        System.out.println("\nMain menu\n1: check balance\n2: withdraw\n"+
                "3. deposit\n4. exit\nEnter a choice: ");
                                
            terminate=respondToCustomerChoice(myAccount,in.nextInt());
        }
         
    }
    
    public static boolean respondToCustomerChoice(Account myAccount[], int customerChoice){
        
        Scanner inAmt = new Scanner(System.in);
        
        switch (customerChoice){
            case 1: // chack balance
                break;
            case 2: // withdraw
                System.out.println("Enter an amount to withdraw: ");
                myAccount[acctIndex].withdraw(inAmt.nextDouble());
                break;
            case 3: // deposit
                System.out.println("Enter an amount to withdraw: ");
                myAccount[acctIndex].deposit(inAmt.nextDouble());        
                break;
            case 4: // exit
                System.exit(0);
        }
        
        System.out.println("The balance is "+myAccount[acctIndex].getBalance());
        
        for(int i=0;i<myAccount.length;i++){
            if(customerChoice==4)return true;
        }
         
        return false;        
    }
    
     public static boolean checkId(Account myAccount[], int id){
        
         boolean response=false;

         for(int i=0;i<myAccount.length;i++){
            if(myAccount[i].getId()==id){
                acctIndex=i;
                response= true;
            }
        }   
         return response;
    }
}

class Account
{
    private int id;
    private double balance;
    private double annualInterestRate;
    private String dateCreated;
    
    public Account(){
        CurrentDate();
    }
    
    public Account(int id, double balance){
        this.id=id;
        this.balance=balance;
        CurrentDate();
    }
    
    private void CurrentDate() {
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        dateCreated=dateNow;
    }
            
    public void setId(int id)
    {
        if(id>100)this.id=id;
    }
    
    public int getId()
    {
        return id;
    }
    
    public void setBalance(double balance)
    {
        this.balance=balance;
    }
    
    public double getBalance()
    {
        return balance;
    }
 
    public void setAnualInterestRate(double annualInterestRate)
    {
        this.annualInterestRate=annualInterestRate;
    }
    
    public double getAnualInterestRate()
    {
        return annualInterestRate;
    }
    
    public String getDateCreated()
    {
        return dateCreated;
    }
    public double getMonthlyInterestRate()
    {
        return annualInterestRate/12;
    }
    
    public void withdraw(double amount)
    {
        if(amount<=balance) {
            balance=balance-amount;
        }else{
            System.out.println("The withdraw ammont '"+amount
                    +"' is larger than the account balance, operation is cancelled.");
        }
    }
    
    public void deposit(double amount)
    {
        if(amount>0) balance=balance+amount;
//        System.out.println(balance);
    }
}

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 9
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37021642
It is better to keep your accounts in the ArrayList rather than in the array
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37021644
For array you have to sopecify the total number for the very beginning
Then when you want to add newa accounts, remove accounts - that all cretaed problems and incoonveniences.
Use array only for absolutely static sets of objects, say like fields on the chessboard
For most real life sets better to use ArrayList  
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37021656

can you explain why is this condition   if(id>100)this.id=id;

and if(id < 100) what will haappen?

 public void setId(int id)
    {
        if(id>100)this.id=id;
    }

you'll not assign id, and it will happen silently - the caller will even not know - is
this correct behaviour?

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 47

Expert Comment

by:for_yan
ID: 37021668

This is a minor thing.
This you can write shorter
balance=balance+amount;  ---> balance += amount;
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37021677


  if(customerChoice==4)return true;   compiler reports warning: customerChoice == 4 is always false
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37021692
I am selecting option 3 - deposit, it writes "Enter amount to withdraw"
It then does deposit, but prompt is incorrect
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37021709
Indeed what is this piece of code doing :

  for(int i=0;i<myAccount.length;i++){
            if(customerChoice==4)return true;
        }

if customerChoice would have been 4 it would have exited
even before not reaching this place
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37021815


public void setAnualInterestRate(double annualInterestRate) <--- it is a good idea to spell words correctly
                                             otherwise it is difficult to remember, like "Anual" should be "Annual".
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 37021828
it would look more natural
if you had a method which returns dateString and assignement happens
in constructor:
 public Account(int id, double balance){
        this.id=id;
        this.balance=balance;
        CurrentDate();

  }

    private void CurrentDate() {
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        dateCreated=dateNow;
    }

Open in new window



public Account(int id, double balance){
        this.id=id;
        this.balance=balance;
       this. dateCreated= getCurrentDateString();
   }

    private String  getCurrentDateString() {
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        return dateNow;
    }

Open in new window



0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 37038319
Thank you very much for very good point.

Mike
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month20 days, 20 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question