java - (Simple placement issue) How do I make my ATM program not withdraw below $0? -
this java 1 final. want kick saying insufficient funds if balance go below $0 (and not create object action -- added arraylist actions).
i believe use conditions in 2 places. 1 in getdouble() method in class account, in object action() in class action. perhaps in main method though... don't know conditions put, , in simplest way won't mess program..
i have separate issue...
if huge number, 7 or 8 digits, return different value, 200e7jk(example). assume double reaching kind of maximum. how can fix this?
main class:
public class atm_larrabee {
public static void main(string[] args) { scanner in = new scanner(system.in); arraylist<action> actions = new arraylist(); account acc = new account("n/a", "n/a", 0.0); acc.displaywelcome(); acc.createusernamepassword(); boolean options = true; while(options){ acc.disconnect(); system.out.println("\nwhat action take?"); system.out.println("[ account balance: $" + account.balance + " ]"); system.out.println("\n- type 'deposit' make deposit account."); system.out.println("- type 'withdrawal' make withdrawal account."); system.out.println("- type 'history' see history of transactions."); system.out.println("- type 'exit' if finished.\n"); string input = in.nextline(); if("exit".equals(input)){ break; } if("history".equals(input)){ action actcopy = new action("n/a", 0); system.out.println("\nbelow history of transactions made:\n"); for(int x = 0; x < actions.size(); x++){ actcopy = actions.get(x); system.out.println(actcopy.gettype() + ": $" + actcopy.getamount()); } } if("deposit".equals(input) || "withdrawal".equals(input)){ double actionamount; actionamount = acc.getdouble(); action act = new action(input, actionamount); actions.add(act); system.out.println("\naction accepted."); } } system.out.println("\nyour account balance is: $" + account.balance); system.out.println("\nthank using boring bank!"); } }
account class:
public class account { scanner in = new scanner(system.in); static string username = "n/a"; static string password = "n/a"; static double balance = 0.0; public account(string u, string p, double b){ username = u; password = p; balance = b; } public void displaywelcome(){ string[] welcome = new string[3]; welcome[0] = "welcome boring banking!"; welcome[1] = "hi there! welcome boring banking!"; welcome[2] = "hello! welcome boring banking!"; random rand = new random(); int r = rand.nextint(3); if(r == 0){ system.out.println(welcome[0]); } if(r == 1){ system.out.println(welcome[1]); } if(r == 2){ system.out.println(welcome[2]); } } public void createusernamepassword(){ system.out.println("\nas new member, first need create username , password."); system.out.println("\n[ enter username account: ]"); string input = in.nextline(); setusername(input); system.out.println("\n[ enter password account: ]"); input = in.nextline(); setpassword(input); } public void disconnect(){ random rand = new random(); int r = rand.nextint(5); if(r == 1){ system.out.println("\n[ connection server lost. please log account. ]"); boolean loggedin = false; boolean correctusername = false; boolean correctpassword = false; while(!loggedin){ while(!correctusername){ system.out.println("\n[ enter username: ]"); string input = in.nextline(); if(account.username.equals(input)){ correctusername = true; system.out.println("\nusername accepted"); } else{ system.out.println("[ incorrect username. please try again. ] "); } } while(!correctpassword){ system.out.println("\n[ enter password: ]"); string input = in.nextline(); if(account.password.equals(input)){ correctpassword = true; system.out.println("\npassword accepted"); loggedin = true; system.out.println("\nsuccessfully logged account: "+ account.username + "."); } else{ system.out.println("[ incorrect password. please try again. ] "); } } } } } public double getdouble(){ boolean validinput = false; double doub = 0.0; while (!validinput) { system.out.println("\n[ please enter amount: ]\n"); string input = in.nextline(); try { doub = double.parsedouble(input); validinput = true; } catch (numberformatexception e) { validinput = false; } } return doub; } public string getusername(){ return account.username; } public void setusername(string u){ username = u; } public string getpassword(){ return account.password; } public void setpassword(string p){ password = p; } public double getbalance(){ return account.balance; } public void setseed(double b){ balance = b; } }
action class:
public class action { double amount = 0; string type; // object withdrawals , deposits (saved arraylist elsewhere) public action(string t, double a){ type = t; amount = a; if("deposit".equals(type)){ type = "deposit"; account.balance = account.balance + a; } if("withdrawal".equals(type)){ type = "withdrawal"; account.balance = account.balance - a; } } // gets , sets public string gettype(){ return this.type; } public void settype(string t){ type = t; } public double getamount(){ return this.amount; } public void setamount(double a){ amount = a; } }
making balance
(and other fields) static
in account
makes global (and means can have 1 account
). but, in context of question,
account.balance = account.balance - a;
needs test make sure a
isn't greater account.balance
like
if (account.balance >= a) { // account.balance = account.balance - a; account.balance -= a; } else { system.err.printf("%d greater %d , overdraft%n", a, account.balance); }
in long term though you'll need instance(s) of account
(so make them instance fields , create instance of account
).
Comments
Post a Comment