java - Why is my withdraw method allowing me to take out more then has been deposited? -
i have write atm program computer science class. works part, apart couple logic problems within programs itself. far, can tell, atm deposit correct amount of money tell however, when withdraw money, not withdraw correct amount if purposefully make error (such trying take out amount not multiple of 20). running issue if try take out more money in account, become negative value, not want allowed. want not subtract value causes become negative , prompt user until value less or equivalent balance able taken out. rather new coding please excuse rather messy code. error around else if statement option == 3. here code:
import java.io.*; import java.util.*; public class atmlauncher { public static int options=0; public static void main(string [] args) { scanner input = new scanner(system.in); atmtester atm = new atmtester(); system.out.println("login: \n(case sensitive)"); system.out.print("username > "); string usernameinput=input.nextline(); int count=4; while (!usernameinput.equals(atmtester.getusername())) { system.out.println("\nincorrect input. please try again. have " + (count-1) + " trys remaining attempts."); system.out.println("login: \n(case sensitive)"); system.out.print("username > "); count--; if(count==0) { system.out.println("no remain attempts, system exit"); system.exit(0); } usernameinput = input.nextline(); } system.out.print("pin > "); int pininput=input.nextint(); int count1=4; while (pininput<atmtester.getpin() || pininput>atmtester.getpin()) { system.out.println("\nincorrect input. please try again. have " + (count1-1) + " trys remaining"); system.out.print("pin > "); count1--; pininput=input.nextint(); if(count1==0) { system.out.println("no remain attempts, system exit"); system.exit(0); } } system.out.println("\nwelcome, " + atmtester.getusername() +"!"); while(pininput==2468) { atm.main(); options = input.nextint(); if (options==4) { atm.logout(); } else if(options==2) { system.out.println("how money deposit?"); system.out.print("$ "); atm.deposit = input.nextint(); atm.balance+=atm.deposit; system.out.println("your new balance $" + atm.balance +"0"); } else if(options==3) { system.out.println("how money withdrawing? \n(only amounts divisible 20 accepted) \n$"); atm.withdraw= input.nextint(); atm.balance-=atm.withdraw; if(atm.withdraw%20==0.0) { system.out.println("you took out " +atm.withdraw); system.out.println("your new balance $" + atm.balance); } while(atm.withdraw%20>0) { system.out.println("invalid withdraw amount. please retry."); system.out.println("\nhow money withdrawing? \n(only amounts divisible 20 accepted)"); atm.withdraw= input.nextint(); system.out.println("you took out " +atm.withdraw); system.out.println("your new balance $" + atm.balance); } if(!(atm.balance>0.0)) { system.out.println("you not allowed take out more have in account \n please retry"); atm.withdraw= input.nextint(); } } else if(options==1) { system.out.println("your account balance $"+atm.balance+"0"); } } } } public class atmtester { private static final string username = "david"; private static final int pin = 2468; public static int deposit, withdraw; public static double balance=0.00; private string menu; /* * default constructor */ public atmtester() { } public static string getusername() { return username; } public static int getpin() { return pin; } public static void main() { system.out.println("\n+++++++++++account: "+ username +"+++++++++++"); system.out.println("1. check account balance"); system.out.println("2. deposit checks"); system.out.println("3. withdraw money"); system.out.println("4. logout"); system.out.print("\nwhat next?\n"); } public static void logout() { system.out.println("thanks usinging david vachlon inc. atm. hope expierence fast , simple! have great day."); system.exit(0); } public static double getbalance() { return balance; } public static void deposit() { balance+=deposit; } public static void withdraw() { balance-=withdraw; } }
you should atm.balance -= atm.withdraw;
after checks passed. instance, may restructure code follows:
boolean ok = false; while (!ok) { system.out.println("\nhow money withdrawing? \n(only amounts divisible 20 accepted)"); atm.withdraw = input.nextint(); if (atm.withdraw % 20 != 0) { system.out.println("invalid withdraw amount. please retry."); } else if (atm.balance < atm.withdraw) { system.out.println("you not allowed take out more have in account \n please retry"); } else if (atm.withdraw < 0) { // should not allow withdraw negative amount of money } else { ok = true; } } atm.balance -= atm.withdraw; system.out.println("you took out " + atm.withdraw); system.out.println("your new balance $" + atm.balance);
Comments
Post a Comment