Reputation: 1
My code skips the lines where I have to input the checking and savings values. I was supposed to create this: "Write a program to simulate a bank transaction. There are two bank accounts: checking and savings. • First ask for the initial balances of the bank accounts; reject negative balances. •Then ask for the transaction; options are deposit, withdrawal and transfer. • Then ask for the account; options are checking and savings. • Then ask for the amount; reject transactions that overdraw an account. • At the end print the balances of both accounts.
Here is my code:
import java.util.Scanner;
public class BankTransaction {
static Scanner keyb = new Scanner(System.in);
public static void main(String[] args) {
double checkings=0, savings=0,depAmount,withdrawAmount,transferAmount;
int n=0, oper, oper2;
System.out.print("Please specify what type of account you have. Type 1 for checkings, type 2 for savings.");
//checkings= keyb.nextDouble();
if (n==1) { //case 1 of the transaction
System.out.print("Please enter the intitial value of your checkings account");
checkings= keyb.nextDouble();
while (checkings<=0){
System.out.print("Your initial account should be greater than zero. Please type a positive number.");
checkings=keyb.nextDouble();}
}//end of choice 1
else
if (n==2){
System.out.print("Please enter the intitial value of your savings account");
savings= keyb.nextDouble();}//end of choice 2
while (savings<=0){
System.out.print("Please type a positive number.");
savings=keyb.nextDouble();}
System.out.print("Please type 1 for operation within your checking account or 2 for operations within your savings account.");
oper=keyb.nextInt();
while((oper < 1) || (oper > 2)) {
System.out.print("Invalid input.");
System.out.print ("Type 1 for operation within deposit account, type 2 for operation within savings account");
oper= keyb.nextInt();}
if (oper==1){
System.out.print("Please specify what type of transaction you want to perform: 1 for deposit, 2 for transaction, 3 for transfer");
n = keyb.nextInt();
while((n < 1) || (n > 3)) {//input validation for the type of transaction the user wants to perform
System.out.println("Invalid input.");
System.out.print ("Type 1 for deposit, 2 for transaction, 3 for transfer");
n = keyb.nextInt();
}
if (n==1) { //case 1 of the transaction
System.out.print("Please type the amount you want to deposit");
depAmount= keyb.nextDouble();//initial value inserted for the depositing amount
while (depAmount<=0){//positive input validation of the depositing amount
System.out.print("Invalid input. ");
System.out.print("Please type an amount greater than 0.");
depAmount = keyb.nextDouble();
}// end of while of depAmount
checkings= checkings + depAmount;
System.out.println("Your new deposit amount = " + checkings );
}// end of if deposit
else
if (n==2){
System.out.print("Please type the amount you want to withdraw ");
withdrawAmount=keyb.nextDouble();
while (withdrawAmount<=0 && withdrawAmount>checkings){
System.out.print("The amount can not be bigger than the inital balance or smaller or equal with zero.");
System.out.print("Please type the amount you want to withdraw");
withdrawAmount=keyb.nextDouble();
}
checkings= checkings-withdrawAmount;
System.out.println("Your new deposit amount = " + checkings );
}//end of if
else {
System.out.print ("Please type the amount you want to transfer");
transferAmount=keyb.nextDouble();
while (transferAmount <0 && transferAmount > checkings); {
System.out.println("The amount you typed is invalid.The transfer amount can't be bigger than the initial checkings acount, nor smaller than zero");
System.out.print("Please type the amount you want to transfer.");
transferAmount=keyb.nextDouble();
}// end of input validation
checkings= checkings + transferAmount;
System.out.println("Your new deposit amount = " + checkings );
}// end of nested else
}//end of if oper 1
//*******************************************************************************************************
else {
if (oper==2){
System.out.print("Please specify what type of transaction you want to perform: 1 for deposit, 2 for transaction, 3 for transfer");
n = keyb.nextInt();
while((n < 1) || (n > 3)) {//input validation for the type of transaction the user wants to perform
System.out.println("Invalid input.");
System.out.print ("Type 1 for deposit, 2 for transaction, 3 for transfer");
n = keyb.nextInt();
}
if (n==1) { //case 1 of the transaction
System.out.print("Please type the amount you want to deposit");
depAmount= keyb.nextDouble();//initial value inserted for the depositing amount
while (depAmount<=0){//positive input validation of the depositing amount
System.out.print("Invalid input. ");
System.out.print("Please type an amount greater than 0.");
depAmount = keyb.nextDouble();
}// end of while of depAmount
savings= savings + depAmount;
System.out.println("Your new deposit amount = " + checkings );
}// end of if deposit
else
if (n==2){
System.out.print("Please type the amount you want to withdraw ");
withdrawAmount=keyb.nextDouble();
while (withdrawAmount<=0 && withdrawAmount>checkings){
System.out.print("The amount can not be bigger than the inital balance or smaller or equal with zero.");
System.out.print("Please type the amount you want to withdraw");
withdrawAmount=keyb.nextDouble();
}
savings= savings-withdrawAmount;
System.out.println("Your new deposit amount = " + checkings );
}//end of if
else {
System.out.print ("Please type the amount you want to transfer");
transferAmount=keyb.nextDouble();
while (transferAmount <0 && transferAmount > savings); {
System.out.println("The amount you typed is invalid.The transfer amount can't be bigger than the initial savings acount, nor smaller than zero");
System.out.print("Please type the amount you want to transfer.");
transferAmount=keyb.nextDouble();
}// end of input validation
savings= savings + transferAmount;
System.out.println("Your new deposit amount = " + checkings );
}// end of nested else
}//end of if oper 1
} // end of else oper 2
}//end of main
}// end of class
Upvotes: 0
Views: 1043
Reputation: 31204
your while loop will never be entered because n
will never be 1.
If you're using n
to represent the user's choice, than you should at least let the user enter n
. you can do this similarly to how you enter in oper
later in your code.
int n=0, oper, oper2;
System.out.print("Please specify what type of account you have. Type 1 for checkings, type 2 for savings.");
//checkings= keyb.nextDouble();
if (n==1) {
... do stuff
Upvotes: 0