public void deposit (double amount){ if (amount >= 0) { balance = balance + amount; } else { system.out.println("your deposit negative number, if withdraw please enter '0' , select withdraw."); } while (!double || !int) { system.out.println("invalid input. try again"); } }
this code, trying test whether or not user input entered in class file has character other integer or double. need piece of code work here , withdraw class have created underneath one, figured if can work in 1 work in other. in advance!
here code requested:
import java.util.scanner; public class bank { double balance = 0; scanner in = new scanner(system.in); int userchoice; bankaccount account1 = new bankaccount(); boolean quit = false; { { system.out.println("your choice: "); system.out.println("for deposit type 1"); system.out.println("for withdraw type 2"); system.out.println("for check balance type 3"); system.out.println("type 0 quit"); userchoice = in.nextint(); switch (userchoice){ case 1: //deposit money system.out.println("how deposit?"); double amount; amount = in.nextdouble(); system.out.println("depositing: " + amount); account1.deposit(amount); //balance = amount + balance; break; case 2: //withdraw money system.out.println("how withdraw?"); amount = in.nextdouble(); system.out.println("withdrawing: " + amount); account1.withdraw(amount); //balance = balance - amount; break; case 3: //check balance system.out.println("checking balance."); account1.getbalance(); system.out.println(account1.balance); break; case 0: system.out.println("thanks using bankaccount banking system!"); quit = true; break; default: system.out.println("error: choice not recognized please choose again."); continue; } if (userchoice == 0) quit = true; }while (!quit); } }
and here entirety of first portion of code:
public class bankaccount { public double balance; public int sufficientfunds = 1; public int insufficientfunds = -1; public bankaccount(){ balance = 0; } public bankaccount (double initialbalance){ balance = initialbalance; } public void deposit (double amount){ if (amount >= 0) { balance = balance + amount; } else { system.out.println("your deposit negative number, if withdraw please enter '0' , select withdraw."); } while (!double || !int) { system.out.println("invalid input. try again"); } } public double withdraw (double amount){ balance = balance - amount; if (balance == amount){ return sufficientfunds; }else if (balance > amount){ return sufficientfunds; }else if (balance < amount){ system.out.println("insufficent funds"); return insufficientfunds; } return amount; } public double getbalance(){ return balance; }
}
java typed language, there no chance amount variable contain other double, number.