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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /* * 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(); } } } } } |
1 2 3 4 5 6 | 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) |