This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2016/12/9

reflection

1
2
3
divinity divinity

2016/12/9

#
hi pple I have one more request and it goes like this in both of my assignment I and assignment II, the last part of the instruction states that The balance should reflect all changes on one execution of the program. Example if id entered was 2 and a withdrawal of 30 was made then a deposit of 100, at the end of the program if balance was checked, it should reflect 120. hoiw can i get my calculation to do just as what it says in the statement above how can i accomplish that. here are my codes. her is the main part of the program, this is assignment I and will have to do the same for assignment II
import java.text.DecimalFormat;
import java.util.Scanner;


public class ATM {
    DecimalFormat df = new DecimalFormat("#.00");
        public static void main(String[] args) {
            createAccount();
    }
        public static void createAccount()
        {
            Scanner input = new Scanner(System.in);
            int i = 0; 
            double bal= 50.0;
            //creation of the accounts
            Account[] acc = new Account[10];
            Person[] p = new Person[i++];
            
            //gathering the user information
            for(i=0; i<acc.length; i++){
                System.out.println("Enter first name");
                String fn = input.next();
                System.out.println("Enter last name");
                String ln = input.next();            
                System.out.println("Enter address");
                String ad = input.next(); 
                System.out.println("Enter contact");
                String c = input.next();                
                
                //this object hold the information for each user
                Person person = new Person(fn, ln, ad, c);
                acc[i]= new Account(i, 50, person);//creating a new account for all of the account
            }
            while(true)
            {
                //asking the user to enter their id
                System.out.println("Enter your id");
                int id = input.nextInt();
                //this will break out of the loop once the user enter the last transaction 
                   if(id == -1)
                   {
                       break;
                   }
                while(id < 0 || id > 9)
                {
                    //letting the user know that the wrong id was entered
                    System.out.println("id is incorect, try again");
                    id = input.nextInt();
                }
                while(true)
                {
                    displayMenu();
                    
                    if(!useMenu(acc[id],input))
                    {
                        System.out.println();
                        break;
                    }
                    
                    System.out.println("\n");
                }
            }
        }
        //construction that holds the account and a sccanner input instead of importing another scanner methods to read input
        public static boolean useMenu(Account acc, Scanner input)
        {
            int userchoice = input.nextInt();
            
            double amt=0;
            //this is to let the user know if they enter choice 5 it would return a false value
            if(userchoice == 5)
            {
                return false;
            }
            else if(userchoice == 1)
            {
                //user indicating that they are accessing to view their balance
                System.out.println("The account balance is: "+ acc.getbalance());
                return true;
            }
            else if(userchoice == 2)
            {
                //user asked for the amount to withdraw
                System.out.println("Enter the amount to withdraw");
                amt = input.nextDouble();//this hold the amount the user enter
                // if the amt in the account is greater than what it has
                if(amt > acc.getbalance())
                {
                    //acknowledging that there is insufficient amount and getting the balance 
                    System.out.println("Insufficient amount "+ " Balance is: "+acc.getbalance());
                } 
                else
                {
                    //letting the user know that there is enough to be withdrawn
                    acc.withdraw(amt);
                    System.out.println("Balance is: "+ acc.getbalance());
                    
                }
            }
            else if(userchoice == 3)
            {
                //asking the user what amt to be deposited
                System.out.println("Enter the amount to deposit");
                acc.deposit(input.nextDouble());//hold the amt that was deposited
                acc.deposit(amt);
                System.out.println("Balance is "+ acc.getbalance());//printing out the balance after deposited the amt
                return true;
            }
            else if(userchoice == 4)
            {
                //gathering the owner information after all transaction taken place
                System.out.println(acc.getowner().toString());
            }
            else
            {
                //let the user that they entered an invalid choice
                System.out.println("invalid choice");
            }
            return true;
        }
        
        public static void displayMenu()
        {
            //giving the user the option to choose from
            System.out.println("Welcome to the Automated Teller Machine!\n");
            System.out.println("Select from the following menu option below:\n");
            System.out.println("===================================");
            System.out.println("|       Main Menu                 |");
            System.out.println("===================================");
            System.out.println("| [1] Check Balance               |");
            System.out.println("| [2] Withdraw                    |");
            System.out.println("| [3] Deposit                     |");
            System.out.println("| [4] Display User Information    |");
            System.out.println("| [5] Exist                       |");
            System.out.println("===================================");
            System.out.println("Please select your choice now!!");
        }
}


this is the account class
public class Account
{
    
    private int ID;// this is the id variable 
    private double balance;
    private Person owner;
    
    public Account()
    {
    //no arg constructor    
    }
    //constructor method with 
    public Account(int ID, double balance, Person owner)
    {
        this.ID = ID;
        this.balance = balance;
        this.owner = owner;
    }
    public int getID()
    {
        return ID;
    }
    public double getbalance()
    {
        return balance;
    }
    public Person getowner()
    {
        return owner;
    }
    public void setID(int ID)
    {
        this.ID = ID;
    }
    public void setbalance(double balance)
    {
        this.balance = balance;
    }
    public void setowner(Person owner)
    {
        this.owner = owner;
    }
    public void deposit(double amt)
    {
        balance = balance + amt;
    }
    public void withdraw(double amt)
    {
        balance = balance - amt;
    }
    @Override
    public String toString()
    {
        String getdetails =
                "/nID: "+ID+" "
                + "\nBalance: "+balance;
        
        return getdetails;
    }
}
here is the person class
public class Person {

    private String FirstName;
    private String LastName;
    private String address;
    private String contact;

    public Person(String f, String l, String a, String c) {
        FirstName = f;
        LastName = l;
        address = a;
        contact = c;
    }

    //get first name

    public String getFirstName() {
        return FirstName;
    }

    //get last name

    public String getLastName() {
        return LastName;
    }

    //get address

    public String getaddress() {
        return address;
    }

    //get telephone number

    public String getcontact() {
        return contact;
    }

    //accessor method with the person name and with parameters

    public void setFirstName(String FirstName) {
        this.FirstName = FirstName;
    }

    //accessor method with the person last name with parameters

    public void setLastName(String LastName) {
        this.LastName = LastName;
    }

    //accessor method the person address with parameters

    public void setaddress(String address) {
        this.address = address;
    }

    //accessor method that take one parameter

    public void setcontact(String contact) {
        this.contact = contact;
    }

    @Override
    public String toString() {
        String getdetails = 
                 "\nFirst Name " + FirstName + " "
                + "\nLast Name " + LastName + " "
                + "\nAddress " + address + " "
                + "\nContact " + contact;

        return getdetails;
    }

}
danpost danpost

2016/12/9

#
Move line 16 to before line 7 (to give scope beyond the execution of the 'createAccount' method) and remove line 17 (not used). With the scope of the Account array expanded to the life of the ATM object, the balances in the accounts will be accessible at the end of the program (provided a reference to the ATM object is maintained).
divinity divinity

2016/12/9

#
hi danpost when i move it ah got an error
divinity divinity

2016/12/9

#
hi danpost
for(i=0; i<acct.length; i++){(got the error by acct(cannot find symbol and create a field called acct in atm)
                
                System.out.println("Enter first name");
                String fn = input.next();
                
                System.out.println("Enter last name");
                String ln = input.next(); 
                
                System.out.println("Enter address");
                String ad = input.next(); 
                
                System.out.println("Enter contact");
                String c = input.next();                
                
                //this object hold the information for each user
                Person person = new Person(fn, ln, ad, c);
                acct[i]= new Account(i, 50, person);//creating a new account for all of the account(got an error here by acct that say create a local variable 
            }
            while(true)
            {
                //asking the user to enter their id
                System.out.println("Enter your acctID");
                int acctID = input.nextInt();
                //this will break out of the loop once the user enter the last transaction 
                   if(acctID == -1)
                   {
                       break;
                   }
                while(acctID < 0 || acctID > 9)
                {
                    //letting the user know that the wrong id was entered
                    System.out.println("id is incorect, try again");
                    acctID= input.nextInt();
                }
                while(true)
                {
                    displayMenu();
                    
                    if(!useMenu(acct[acctID],input))(got another error by acct that says to create a local variable
                    {
                        System.out.println();
                        break;
                    }
how do i fix these three errors, this occurs when i remove line 16
danpost danpost

2016/12/9

#
You were only supposed to move line 16 (cut and paste) -- not remove it Read my last post again.
divinity divinity

2016/12/10

#
it is exactly what i did, it affected those three lines hence the reason for the errors
danpost danpost

2016/12/10

#
divinity wrote...
it is exactly what i did, it affected those three lines hence the reason for the errors
It looks like you tried to changed the name of the field from 'acc' to 'acct' (your original code was consistently using 'acc'; but, now those lines you are pointing to are trying to use 'acct'). Be consistent -- use one or the other.
divinity divinity

2016/12/10

#
hi and good morning danpost it doesnt matter which ever one i used may be 'acc' or 'acct' am getting the same error
divinity divinity

2016/12/10

#
hi is there anywhere i can get rid of the error and make this work at the same time
danpost danpost

2016/12/11

#
divinity wrote...
hi is there anywhere i can get rid of the error and make this work at the same time
Please show the current code of the ATM class; again, specify where the errors are currently at.
divinity divinity

2016/12/11

#
public class ATM {
    DecimalFormat df = new DecimalFormat("#.00");
    
        public static void main(String[] args) {
            
              Account[] acc = new Account[10];
              createAccount();
           
    }
        public static void createAccount()
        {
            Scanner input = new Scanner(System.in);
            int i = 0; 
            double bal= 50.0;
            //creation of the accounts
              //Person[] p = new Person[i++];
            
            //gathering the user information
            for(i = 0; i < acc.length; i++){and it say here create a field in atm here. acc
                
                System.out.println("Enter first name");
                String fn = input.next();
                
                System.out.println("Enter last name");
                String ln = input.next(); 
                
                System.out.println("Enter address");
                String ad = input.next(); 
                
                System.out.println("Enter contact");
                String c = input.next();                
                
                //this object hold the information for each user
                Person person = new Person(fn, ln, ad, c);
                acc[i]= new Account(i, 50, person);//creating a new account for all of the account it also says here to create a local variable for acc, the error is here
            }
            while(true)
            {
                //asking the user to enter their id
                System.out.println("Enter your acctID");
                int acctID = input.nextInt();
                //this will break out of the loop once the user enter the last transaction 
                   if(acctID == -1)
                   {
                       break;
                   }
                while(acctID < 0 || acctID > 9)
                {
                    //letting the user know that the wrong id was entered
                    System.out.println("id is incorect, try again");
                    acctID= input.nextInt();
                }
                while(true)
                {
                    displayMenu();
                    
                    if(!useMenu(acc[acctID],input))(this is the third error here, it says create a local variable for acc
                    {
                        System.out.println();
                        break;
                    }
                    
                    System.out.println("\n");
                }
            }
        }
        //construction that holds the account and a sccanner input instead of importing another scanner methods to read input
        public static boolean useMenu(Account acc, Scanner input)
        {
            int userchoice = input.nextInt();
            
            double amt=0;
            //this is to let the user know if they enter choice 5 it would return a false value
            if(userchoice == 5)
            {
                return false;
            }
            else if(userchoice == 1)
            {
                //user indicating that they are accessing to view their balance
                System.out.println("The account balance is: "+ acc.getbalance());
                return true;
            }
            else if(userchoice == 2)
            {
                //user asked for the amount to withdraw
                System.out.println("Enter the amount to withdraw");
                amt = input.nextDouble();//this hold the amount the user enter
                // if the amt in the account is greater than what it has
                if(amt > acc.getbalance())
                {
                    //acknowledging that there is insufficient amount and getting the balance 
                    System.out.println("Insufficient amount "+ " Balance is: "+acc.getbalance());
                } 
                else
                {
                    //letting the user know that there is enough to be withdrawn
                    acc.withdraw(amt);
                    System.out.println("Balance is: "+ acc.getbalance());
                    
                }
            }
            else if(userchoice == 3)
            {
                //asking the user what amt to be deposited
                System.out.println("Enter the amount to deposit");
                acc.deposit(input.nextDouble());//hold the amt that was deposited
                acc.deposit(amt);
                System.out.println("Balance is "+ acc.getbalance());//printing out the balance after deposited the amt
                return true;
            }
            else if(userchoice == 4)
            {
                //gathering the owner information after all transaction taken place
                System.out.println(acc.getowner().toString());
            }
            else
            {
                //let the user that they entered an invalid choice
                System.out.println("invalid choice");
            }
            return true;
        }
        
        public static void displayMenu()
        {
            //giving the user the option to choose from
            System.out.println("Welcome to the Automated Teller Machine!\n");
            System.out.println("Select from the following menu option below:\n");
            System.out.println("===================================");
            System.out.println("|       Main Menu                 |");
            System.out.println("===================================");
            System.out.println("| [1] Check Balance               |");
            System.out.println("| [2] Withdraw                    |");
            System.out.println("| [3] Deposit                     |");
            System.out.println("| [4] Display User Information    |");
            System.out.println("| [5] Exist                       |");
            System.out.println("===================================");
            System.out.println("Please select your choice now!!");
        }
}


danpost danpost

2016/12/11

#
You placed the line AFTER line 7 (line 4 in the last post) -- not BEFORE like I suggested.
divinity divinity

2016/12/11

#
hi danpost can u show me whay you mean because when I put it at line 4 am getting a major error
Super_Hippo Super_Hippo

2016/12/11

#
I think you should put this line
Account[] acc = new Account[10];
outside methods, so let's say line 3 in the last post.
divinity divinity

2016/12/11

#
in my netbeans ide it say line 8 or nine and when i put it there am getting an error that says non static variable acc cannot be referenced from a static context there error is both here
for(i = 0; i < acc.length; i++){

and 

acc[i]= new Account(i, 50, person);//creating a new account for all of the account

 if(!useMenu(acc[acctID],input))
There are more replies on the next page.
1
2
3