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;
and this is the main
and this is the result when i ran it
any help will be appreciated. thanks
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; }
/* * 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(); } } } } }
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)