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

2021/7/29

Tell me what is wrong here, what I am missing.

1
2
divinity divinity

2021/7/29

#
hi, this is another one of the many program that I am practicing with. i have to build an atm machine that has 10 acounts. think i might have but i have hit a snagg, dont know what it is. can someone pinpoint it to me please. this is the Account class;
package bankatm;

import java.util.Date;

/**
 *
 * @author Luana Taylor
 */
public class Account {
    
    private int acctID;
    private double acctbalance = 0;
    private double annualint_rate = 0;
    private Date dateCreated;
   
    
    
    public Account(){
        
    }
    public Account(Date dateCreated[], int acctID, double acctbalance){
        
        this.acctID = acctID;
        this.acctbalance = acctbalance;        
        this.dateCreated = new Date();
    
   }
    public Date getDate(){
        return dateCreated;
    }
    public int getacctID(){
        return acctID;
    }
    public double getacctbalance(){
        return acctbalance;
    }
    public double getannint_rate(){
        return annualint_rate;
    }
    public double setannualint_rate(){
        acctbalance = acctbalance * annualint_rate/100;
        return annualint_rate;
    }
    public void setDate(Date dateCreated){
        this.dateCreated = new Date();
    }
    public void setacctID(int acctID){
        this.acctID = acctID;
    }
    public void setacctbalance(double acctbalance){
        this.acctbalance = acctbalance;
    }
    public void getannualint_rate(double annualint_rate){
        this.annualint_rate = annualint_rate;
    }
    
    public double withdraw(double total_amt){
        
        acctbalance = acctbalance - total_amt;
        return acctbalance;
    }
    public double deposit(double total_amt){
        acctbalance = acctbalance + total_amt;
        return total_amt;
    }
and this is the main
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bankatm;

import java.util.Scanner;

/**
 *
 * @author User
 */
public class BankATM {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       Scanner scannerin = new Scanner(System.in);
       
       int mychoice=0;
       double withdraw=0;
       
       Account[] newacct = new Account[10];
       
       for(int i = 0; i <newacct.length; i++){
           newacct = new Account[i];
           
           
         while(true){
             System.out.print("Enter your enter account ID");
             int acctID = scannerin.nextInt();
             
             while(acctID != newacct[acctID].getacctID()){
                 System.out.print("Enter the correct account ID");
                 acctID = scannerin.nextInt();
             }   
            
             if(acctID == newacct[acctID].getacctID()){
                 System.out.print("**************Main Menu*********");
                 System.out.print("* 1: Check account balance     *");
                 System.out.print("* 2:Withdraw                   *");
                 System.out.print("* 3 Deposit                    *");
                 System.out.print("* 4: Exit                      *");
             }    
                if(mychoice == 1){
                    System.out.print("The account balance is: $"+newacct[acctID].getacctbalance());
                
            }else if(mychoice == 2){
                System.out.print("Enter the amount to be withdrawn");
                double total_amt = scannerin.nextDouble();
                newacct[acctID].withdraw(total_amt);                
                if(withdraw > total_amt){
                    System.out.print("Insufficient funds available");
                }
            
            }else if(mychoice == 3){
                System.out.print("Enter the amount to be deposit");
                double total_amt = scannerin.nextDouble();
                newacct[acctID].deposit(total_amt);
            }else if(mychoice == 4){
                 System.out.print("Enter the account ID");
                 acctID = scannerin.nextInt();
            }
       }
    }
 }
    
}
and this is the result when i ran it
Enter your enter account ID
1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
	at bankatm.BankATM.main(BankATM.java:35)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 4 seconds)
any help will be appreciated. thanks
RcCookie RcCookie

2021/7/29

#
In line 28, you are trying to initialize the array values, but instead you are creating a new array every time. Your code
newacct = new Account[i];
should actually be
newacct[i] = new Account();
divinity divinity

2021/7/29

#
hi RcCookie, I changeed it but i am still getting the same runtime error, what do i do
danpost danpost

2021/7/29

#
You need to create all the accounts before asking for an account number from the user. Put a closing bracket at line 29. Note: remember that the first account will be at:
Account acct = newacct[0];
(with emphasis on the '0' part)
divinity divinity

2021/7/29

#
hi danpost, when i put the zero in, I got an error
newacct[i] = new Account(0)
;
the error says; no suitable constructor found for Account(int) constructor Account.Account() is not applicable (actual and formal argument lists differ in length) constructor Account.Account(int,double) is not applicable (actual and formal argument lists differ in length) ----
danpost danpost

2021/7/29

#
You do not have an Account constructor that takes in an account number. In for loop create account, then assign account number:
newacct[i] = new Account();
newacct[i].setacctID(i);
divinity divinity

2021/7/30

#
hi danpost am still getting the same runtime error, I believe there is something that I am missing but cant seem to spot it at all something within the codes that is causing the runtime error
danpost danpost

2021/7/30

#
divinity wrote...
am still getting the same runtime error,
Please show current main and error message.
divinity divinity

2021/7/30

#
public class BankATM {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       Scanner scannerin = new Scanner(System.in);
       
       
       int mychoice=0;
       double withdraw=0;
       double acctbalance = 50;
       
       Account[] newacct = new Account[10];
       
       for(int i = 0; i<newacct.length; i++){
           newacct[i] = new Account();
           newacct[i].setacctID(i);
           
        
             System.out.print("Enter your enter account ID");
             int acctID = scannerin.nextInt();
          
             while(acctID != newacct[acctID].getacctID()){
                 System.out.print("Enter the correct account ID");
                 acctID = scannerin.nextInt();
             }   
            
             if(acctID == newacct[acctID].getacctID()){
                 System.out.print("**************Main Menu*********");
                 System.out.print("* 1: Check account balance     *");
                 System.out.print("* 2:Withdraw                   *");
                 System.out.print("* 3 Deposit                    *");
                 System.out.print("* 4: Exit                      *");
            }    
             if(mychoice == 1){
                 System.out.print("The account balance is: $"+newacct[acctID].getacctbalance());
                
            }else if(mychoice == 2){
                System.out.print("Enter the amount to be withdrawn");
                double total_amt = scannerin.nextDouble();
                newacct[acctID].withdraw(total_amt);                
                if(withdraw > total_amt){
                    System.out.print("Insufficient funds available");
                    System.out.print("The balance is: $"+ acctbalance);
                }
            }else if(mychoice == 3){
                System.out.print("Enter the amount to be deposited");
                double total_amt = scannerin.nextDouble();
                newacct[acctID].deposit(total_amt);
                
            }else if(mychoice == 4){
                 System.out.print("Enter  account ID");
                 acctID = scannerin.nextInt();
            }
       
    }
    
 }
    
}
error message
Enter your enter account ID
2
Exception in thread "main" java.lang.NullPointerException
	at bankatm.BankATM.main(BankATM.java:37)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 11 seconds)
danpost danpost

2021/7/30

#
You still need a closing bracket at line 19.
divinity divinity

2021/7/30

#
okay danpost I fix he bracket and this is thee result i get, I am still missing something
Enter your enter account ID
1
***Main Menu**** 1: Check account balance** 2:Withdraw ** 3 Deposit  ** 4: Exit *BUILD SUCCESSFUL (total time: 5 seconds)
danpost danpost

2021/7/30

#
After the menu (ending at line 35), you need to scan in an int (for mychoice).
divinity divinity

2021/7/30

#
hey danpost: when I added it in this is the error am getting: variable mychoice is already defined in method main(String)
danpost danpost

2021/7/30

#
divinity wrote...
hey danpost: when I added it in this is the error am getting: variable mychoice is already defined in method main(String)
Remove 'int' from beginning of new line.
divinity divinity

2021/7/30

#
hi danpost: again I am missing something. because it is in a for loop and it supposed to run until the user press 4 the exit button and ask for the user id. but when i ran it this is the result
run:
Enter your enter account ID
1
***Main Menu**** 1:Check balance* 2:Withdraw * 3 Deposit  * 4: Exit *
1
The account balance is: $50.0BUILD SUCCESSFUL (total time: 9 seconds)
it stop right there
There are more replies on the next page.
1
2