how to write while loop - java

I am currently making a simple ATM program in java.
I want to write a while loop where when user enters wrong pin it will prompt the user to enter again until the pin is matched. When the pin is matched, it will display the main menu.
I tried by myself, but I don't know how to fix it.
while(userPIN != savedPIN)
{
System.out.print("Please enter your correct PIN : ");
Scanner again = new Scanner(System.in);
int pass = again.nextInt();
break;
}

Remove the `break;' statement and update userPIN with the new pin as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int savedPIN = 4444;
Scanner input = new Scanner(System.in);
System.out.println("Enter password");
int userPIN = input.nextInt();
double withdraw = 0.0, amount = 0.0, deposit = 0.0;
while (userPIN != savedPIN) {
System.out.print("Please enter your correct PIN : ");
Scanner again = new Scanner(System.in);
userPIN = again.nextInt();
}
while (userPIN == savedPIN) {
System.out.println(" 1 - Inquire Balance \n 2 - Withdraw \n 3 - Deposit \n 0 - Quit ");
Scanner inputNum = new Scanner(System.in);
int number = inputNum.nextInt();
switch (number) {
case 1:
System.out.println("The current balance is $" + amount);
break;
case 2:
System.out.println("Enter the amount to withdraw : ");
withdraw = input.nextDouble();
if (amount >= withdraw) {
amount = amount - withdraw;
System.out.println("The current balance is $" + amount);
} else {
System.out.println("Insufficient balance");
}
break;
case 3:
System.out.println("Enter the amount to deposit : ");
deposit = input.nextDouble();
amount = amount + deposit;
System.out.println("The current balance is $" + amount);
break;
case 0:
System.exit(4);
}
}
}
}

Ok 2 errors:
1)you test userPIN != savedPIN but you accept the value into variable pass with which you do nothing.
2)remove the break in the first loop it will always exit without looping.
it should look like :
while(pass!= savedPIN)
{
System.out.print("Please enter your correct PIN : ");
Scanner again = new Scanner(System.in);
int pass = again.nextInt();
}

Related

How to pass parameters values from one method to another one in the same class

I'm writing this code for an ATM simulator, with deposit, withdrawal and balance inquiry functionality. The code needs to be written with methods and not switch statements.
My deposit method works, but I have issues with both the withdraw and balanceInquiry methods. I would like to have checkAc_bal accessible to all methods, in order to perform calculations. I'm new on Java, and I'm trying to wrap my head on methods behaviors. Thanks so much.
...
import java.util.Scanner;
public class Main
{
public static void showMenu()
{
Scanner sc = new Scanner(System.in) ;
String input = null ;
do
{
input = showOptions(sc) ;
if(input != null)
{
switch(input.trim())
{
case "1" : deposit(sc) ;
break ;
case "2" : withdraw(sc);
break ;
case "3" : balanceInquiry() ;
break ;enter code here
case "4" : System.exit(0);
default : System.out.println("Wrong option entered. Exiting application") ;
System.exit(0);
}
}
}while(true) ;
}
public static String showOptions(Scanner sc)
{
System.out.println("********************Welcome to ATM Machine******************** ");
System.out.println("Enter Option");
System.out.println("1. Deposit");
System.out.println("2. Withdrawal");
System.out.println("3. Balance Inquiry");
System.out.println("4. Exit\n");
String input = sc.nextLine() ;
return input ;
}
public static void deposit (Scanner sc) {
int checkAc_bal = 0;
System.out.print("Enter amount to be deposited:");
int deposit;
Scanner s = new Scanner(System.in);
deposit = s.nextInt();
//int checkAc_bal = 0;
checkAc_bal = checkAc_bal + deposit;
System.out.println("Your Money has been successfully deposited");
System.out.println("Balance: " +checkAc_bal);
}
public static void withdraw (Scanner sc){
System.out.print("Enter money to be withdrawn:");
int withdraw;
Scanner s = new Scanner(System.in);
withdraw = s.nextInt();
if(withdraw<=checkAc_bal)
{
checkAc_bal = checkAc_bal - withdraw;
System.out.println("Please collect your money");
}
else
{
System.out.println("Insufficient Balance");
}
System.out.println("");
}
public static void balanceInquiry () {
System.out.println("Balance: " + checkAc_bal);
}
public static void main(String[] args) {
showMenu();
}
}
If you want your int to be accessible to other methods, you need to declare it in the scope of the whole class and not inside a method. Try declaring checkAc_bal in the Main class.
define it as a class member:
import java.util.Scanner;
public class Main
{
static int checkAc_bal = 0; //<------------------------add this
public static void showMenu()
{
Scanner sc = new Scanner(System.in) ;
String input = null ;
do
{
input = showOptions(sc) ;
if(input != null)
{
switch(input.trim())
{
case "1" : deposit(sc) ;
break ;
case "2" : withdraw(sc);
break ;
case "3" : balanceInquiry() ;
break ;enter code here
case "4" : System.exit(0);
default : System.out.println("Wrong option entered. Exiting application") ;
System.exit(0);
}
}
}while(true) ;
}
public static String showOptions(Scanner sc)
{
System.out.println("********************Welcome to ATM Machine******************** ");
System.out.println("Enter Option");
System.out.println("1. Deposit");
System.out.println("2. Withdrawal");
System.out.println("3. Balance Inquiry");
System.out.println("4. Exit\n");
String input = sc.nextLine() ;
return input ;
}
public static void deposit (Scanner sc) {
// int checkAc_bal = 0; <---------------- remove this
System.out.print("Enter amount to be deposited:");
int deposit;
Scanner s = new Scanner(System.in);
deposit = s.nextInt();
//int checkAc_bal = 0;
checkAc_bal = checkAc_bal + deposit;
System.out.println("Your Money has been successfully deposited");
System.out.println("Balance: " +checkAc_bal);
}
public static void withdraw (Scanner sc){
System.out.print("Enter money to be withdrawn:");
int withdraw;
Scanner s = new Scanner(System.in);
withdraw = s.nextInt();
if(withdraw<=checkAc_bal)
{
checkAc_bal = checkAc_bal - withdraw;
System.out.println("Please collect your money");
}
else
{
System.out.println("Insufficient Balance");
}
System.out.println("");
}
public static void balanceInquiry () {
System.out.println("Balance: " + checkAc_bal);
}
public static void main(String[] args) {
showMenu();
}
}
You have three queries, here are the answers :
Declare the variable globally to be accessible from all the methods.
Not to use switch case (U need to use if-else in that case)
How to pass parameters to methods in same class ?
I see you already did this in your code. You called other methods by parameters and received them as well.
Or frame your question well , as what exactly you need.
So here is the complete code :
import java.util.Scanner;
public class Main{
static int checkAc_bal = 0; // <---- declare on class scope
public static void showMenu() {
Scanner sc = new Scanner(System.in) ;
String input = null ;
do {
input = showOptions(sc) ;
if(input != null) {
// removed the switch-case and if-else used
if(input.trim().equals("1")) {deposit();}
else if(input.trim().equals("2")) {withdraw();}
else if(input.trim().equals("3")) {balanceInquiry();}
else if(input.trim().equals("4")) {System.exit(0);}
else {
System.out.println("Wrong option entered. Exiting application") ;
System.exit(0);
}
}
}while(true) ;
}
public static String showOptions(Scanner sc){
System.out.println("********************Welcome to ATM Machine******************** ");
System.out.println("Enter Option");
System.out.println("1. Deposit");
System.out.println("2. Withdrawal");
System.out.println("3. Balance Inquiry");
System.out.println("4. Exit\n");
String input = sc.nextLine() ;
return input ;
}
public static void deposit () {
System.out.print("Enter amount to be deposited:");
int deposit;
Scanner s = new Scanner(System.in);
deposit = s.nextInt();
checkAc_bal = checkAc_bal + deposit;
System.out.println("Your Money has been successfully deposited");
System.out.println("Balance: " +checkAc_bal);
}
public static void withdraw (){
System.out.print("Enter money to be withdrawn:");
int withdraw;
Scanner s = new Scanner(System.in);
withdraw = s.nextInt();
if(withdraw<=checkAc_bal) {
checkAc_bal = checkAc_bal - withdraw;
System.out.println("Please collect your money");
} else {
System.out.println("Insufficient Balance");
}
System.out.println("");
}
public static void balanceInquiry () {
System.out.println("Balance: " + checkAc_bal);
}
public static void main(String[] args) {
showMenu();
}
}
Output :
********************Welcome to ATM Machine********************
Enter Option
1. Deposit
2. Withdrawal
3. Balance Inquiry
4. Exit
1
Input : 1
Enter amount to be deposited:100
Your Money has been successfully deposited
Balance: 100
********************Welcome to ATM Machine********************
Enter Option
1. Deposit
2. Withdrawal
3. Balance Inquiry
4. Exit
1
Input : 1
Enter amount to be deposited:200
Your Money has been successfully deposited
Balance: 300
********************Welcome to ATM Machine********************
Enter Option
1. Deposit
2. Withdrawal
3. Balance Inquiry
4. Exit
1
Input : 1
Enter amount to be deposited:200
Your Money has been successfully deposited
Balance: 500
********************Welcome to ATM Machine********************
Enter Option
1. Deposit
2. Withdrawal
3. Balance Inquiry
4. Exit
3
Input : 3
Balance: 500
********************Welcome to ATM Machine********************
Enter Option
1. Deposit
2. Withdrawal
3. Balance Inquiry
4. Exit

how can i update balance after doing withdrawal and deposits(withdraw,deposit,balance inquiry) java

I have a homework in java. I am tasked to build a bank that can withdraw, deposit and inquire balance. My problem is that I couldn't get to update my balance after deposits and withdrawals... I've tried everything I could do but still cant get the logic. Can someone help add to my program... thanks
import java.util.Scanner;
public class bankJava
{
Scanner input = new Scanner(System.in);
double balance;
double amount;
public void withdraw()
{
System.out.println("Enter amount: ");
amount = input.nextInt();
balance = balance - amount;
}
public void deposit()
{
System.out.println("Enter amount: ");
amount = input.nextInt();
balance = balance + amount;
}
public void accBalance()
{
}
}
---------------------------------MAIN--------------------------------
import java.util.Scanner;
public class bankJavaTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int action;
bankJava wdraw = new bankJava();
bankJava dposit = new bankJava();
bankJava balanceInquiry = new bankJava();
bankJava amount = new bankJava();
do{
System.out.println("Choose Action: ");
System.out.println("(1) Withdraw");
System.out.println("(2) Deposit");
System.out.println("(3) Balance Inquiry");
System.out.println("(4) Exit");
action = input.nextInt();
switch(action){
//---------WITHDRAW------------//
case 1 :
System.out.println("******Withdraw******");
wdraw.withdraw();
System.out.println("***************************");
break;
//---------DEPOSIT------------//
case 2 :
System.out.println("******Deposit******");
dposit.deposit();
System.out.println("***************************");
break;
//-----------Balance Inquiry-------//
case 3 :
System.out.println("******Balance Inquiry******");
balanceInquiry.accBalance();
System.out.println("***************************");
break;
case 4 :
System.out.println("Thank you for choosing our bank!");
break;
default :
System.out.println("Invalid action.");
break;
}
}while(action != 4);
}
}
Why you are instatiating 4 different JavaBank? Doing this for each operation you will execute each method in a different object. If I understand well your question I think you could resolve your problem easily working in the same object.
import java.util.Scanner;
public class bankJavaTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int action;
bankJava myJavaBank = new bankJava(); //creating the bank
do{
System.out.println("Choose Action: ");
System.out.println("(1) Withdraw");
System.out.println("(2) Deposit");
System.out.println("(3) Balance Inquiry");
System.out.println("(4) Exit");
action = input.nextInt();
switch(action){
//---------WITHDRAW------------//
case 1 :
System.out.println("******Withdraw******");
myJavaBank.withdraw(); //withdrawing from it
System.out.println("***************************");
break;
//---------DEPOSIT------------//
case 2 :
System.out.println("******Deposit******");
myJavaBank.deposit(); //deposit from it
System.out.println("***************************");
break;
//-----------Balance Inquiry-------//
case 3 :
System.out.println("******Balance Inquiry******");
myJavaBank.accBalance();
//You don't post this method but I suppose it will refer to the same bank
System.out.println("***************************");
break;
case 4 :
System.out.println("Thank you for choosing our bank!");
break;
default :
System.out.println("Invalid action.");
break;
}
}while(action != 4);
}
}
Now should work. With your code you had 4 different bank, one only for deposit, one only for withdraw and so on. So one bank will continue to increase money, and one continue to decrease in negative.
Morover the amount parameter should not be a JavaBank parameter, bur a local variable inside each method so that it does not define a Bank.
Something like
public class bankJava
{
Scanner input = new Scanner(System.in);
double balance;
public void withdraw()
{
System.out.println("Enter amount: ");
double amount = input.nextInt();
balance = balance - amount;
}
public void deposit()
{
System.out.println("Enter amount: ");
double amount = input.nextInt();
balance = balance + amount;
}
I also suggest to change the input.nextInt() with input.nextDouble() so that you create the amount as a double.
If you don't see the Balance Inquiry is because obviously you have the accBalance() method blank. Edit it like this:
public void accBalance(){
System.out.println("Your balance is: "+this.balance);
}
import java.util.Scanner;
public class BankJava {
double balance = 0;
double amount;
public void withdraw(int amount) {
balance = balance - amount;
}
public void deposit(int amount) {
balance = balance + amount;
}
public double showBalance() {
return balance;
}
public static void main(String[] args) {
BankJava bank = new BankJava();
Scanner input = new Scanner(System.in);
int action;
int amount;
do{
System.out.println("Choose Action: ");
System.out.println("(1) Withdraw");
System.out.println("(2) Deposit");
System.out.println("(3) Balance Inquiry");
System.out.println("(4) Exit");
action = input.nextInt();
switch(action){
//---------WITHDRAW------------//
case 1 :
System.out.println("******Withdraw******");
System.out.println("enter amount:");
amount = input.nextInt();
bank.withdraw(amount);
System.out.println("***************************");
System.out.println("Your balance is now: " + bank.showBalance());
break;
//---------DEPOSIT------------//
case 2 :
System.out.println("******Deposit******");
System.out.println("enter amount:");
amount = input.nextInt();
bank.deposit(amount);
System.out.println("***************************");
System.out.println("Your balance is now: " + bank.showBalance());
break;
//-----------Balance Inquiry-------//
case 3 :
System.out.println("******Balance Inquiry******");
System.out.println("Your balance is: " + bank.showBalance());
System.out.println("***************************");
break;
case 4 :
System.out.println("Thank you for choosing our bank!");
break;
default :
System.out.println("Invalid action.");
break;
}
}while(action != 4);
}
}
Try this code, compare with yours and figure out whats wrong, also you can ask me if you need more help

counter for errors

when user logs out the user may choose to log back in again. For Logging back in: If the user get the pin code wrong 3 times, the program terminates.
System.out.println("You have logged out");
System.out.print("Please Enter Pin: ");
pin2 = sc.nextInt();
while (pin != pin2){
while (ctr < 2){
System.out.print("Please Enter Pin: ");
pin2 = sc.nextInt();
ctr++;
}
}
If I understand your problem correctly you will want to have something like that:
while (pin == pin2) {
System.out.println("What would you like to do?");
System.out.println("1 - Check Balance");
System.out.println("2 - Deposite");
System.out.println("3 - Withdraw");
System.out.println("4 - Change Pin");
System.out.println("5 - End Transaction");
sel = sc.nextInt();
switch (sel) {
case 1:
System.out.println("Your current balance is " + bal);
break;
case 2:
System.out.println("How much would you want to deposite? ");
dep = sc.nextInt();
bal = dep + bal;
System.out.println("Your new current balance is " + bal);
break;
case 3:
System.out.println("How much would you want to Withdraw? ");
with = sc.nextInt();
if (with > bal) {
System.out.println("You do not have that amount on your account! Please enter again.");
} else {
System.out.println("You withdrew " + with);
bal = bal - with;
System.out.println("Your new current balance is " + (bal));
}
break;
case 4:
System.out.print("Please enter a new pin: ");
pin = sc.nextInt();
System.out.println("Please verify your new pin: ");
pin2 = sc.nextInt();
break;
case 5:
System.out.println("Please Enter Pin: ");
pin = sc.nextInt();
break;
default:
break;
}
}
Basically, I've deleted the loop label, it is not necessary and I consider it a bad style. I've also changed the while condition, so the program runs as long as user enters exactly the same pin as he confirmed at the beginning. Moreover, I think it is better to read the value of sel after printing instructions, not before as you did.
Like what been said in the comments, avoid using Labels and/or goto rep.
Use something like that :
import java.util.Scanner;
public class AtmMachine {
public static void main(String[] args){
int actualPin = -1;
int sel = 0, pin, pin2, check, ctr = 0, dep, with, bal = 10000;
Scanner sc = new Scanner(System.in);
while(actualPin == -1)
{
System.out.print("Please enter a new pin: ");
pin = sc.nextInt();
System.out.print("Please verify your new pin: ");
pin2 = sc.nextInt();
if(pin == pin2) actualPin = pin;
else System.out.println("Error");
}
boolean logged = false;
while (true){
if(logged){
System.out.println("What would you like to do?");
System.out.println("1 - Check Balance");
System.out.println("2 - Deposite");
System.out.println("3 - Withdraw");
System.out.println("4 - Change Pin");
System.out.println("5 - End Transaction");
sel = sc.nextInt();
switch (sel){
case 1:
System.out.println("Your current balance is "+ bal);
break;
case 2:
System.out.println("How much would you want to deposite? ");
dep = sc.nextInt();
bal= dep+bal;
System.out.println("Your new current balance is "+ bal);
break;
case 3:
System.out.println("How much would you want to Withdraw? ");
with = sc.nextInt();
if(with > bal){
System.out.println("You do not have that amount on your account! Please enter again.");
}
else{
System.out.println("You withdrew "+ with);
bal = bal-with;
System.out.println("Your new current balance is "+ (bal));
}
break;
case 4:
System.out.print("Please enter a new pin: ");
pin = sc.nextInt();
System.out.println("Please verify your new pin: ");
pin2 = sc.nextInt();
if(pin == pin2) actualPin = pin;
else System.out.println("Error");
break;
case 5:
logged = false;
break;
default:
break;
}
else{
System.out.println("Please Enter Pin: ");
sel = sc.nextInt();
logged = actualPin == sel;
}
}
}
}

How would I have test code to test if an input has characters instead of numbers

public void deposit (double amount){
if (amount >= 0) {
balance = balance + amount;
} else {
System.out.println("Your deposit is a negative number, If you would like to withdraw please enter '0' and select Withdraw.");
}
while (!double || !int) {
System.out.println("Invalid Input. Try again");
}
}
This is the code, I am trying to get to test whether or not a user input entered in another class file has a character other than an integer or double. I need for this piece of code to work here and with a withdraw class that I have already created underneath this one, I figured if it I can get it to work in one it will work in the other.
Thanks in advance!
Here is the 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; {
do {
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 to quit");
userChoice = in.nextInt();
switch (userChoice){
case 1:
//Deposit Money
System.out.println("How Much would you like to 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 much would you like to 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 for 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 is the entirety of the 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 is a negative number, If you would like to withdraw please enter '0' and 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 is a strongly typed language, so there is no chance that the amount variable could contain anything other than a double, which is a number.
Use this for bank
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;
public void thisShouldBeAMethod(){
do {
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 to quit");
//userChoice = in.nextInt();
String regex = "[0-9]+";
String input = in.next();
if(input.matches(regex)){
userChoice = Integer.parseInt(input);
switch (userChoice) {
case 1:
// Deposit Money
System.out.println("How Much would you like to 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 much would you like to 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 for Using BankAccount Banking System!");
quit = true;
break;
default:
System.out
.println("Error: Choice not recognized please choose again.");
continue;
}
if (userChoice == 0)
quit = true;
}
else{
System.out.println("Invalid Input. Try again");
}
} while (!quit);
}
}
update this in BankAccount
public void deposit (double amount){
if (amount >= 0) {
balance = balance + amount;
} else {
System.out.println("Your deposit is a negative number, If you would like to withdraw please enter '0' and select Withdraw.");
}
// while (!double || !int) {
// System.out.println("Invalid Input. Try again");
// }
}
And test
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
Bank bank = new Bank();
bank.thisShouldBeAMethod();
}
}
Ok, so when accepting the input for amount, surround it with a try-catch block so that instead of simply
amount = in.nextDouble();
you'll get
try{
amount = in.nextDouble();
}catch(InputMismatchException ime){
//What to do if this is not a numerical value
}
Additionally you'll want to wrap that in a do-while loop so that the program asks again until a valid input is accepted:
boolean inputInvalid;
double amount;
do{
System.out.println("How Much would you like to deposit?");
try{
amount = in.nextDouble();
inputInvalid = false;
}catch(InputMismatchException ime){
//What to do if this is not a numerical value
inputInvalid = true;
}
}while(inputInvalid);
Edit: due to the in.nextDouble(), the program may be continuously trying to read the \n in the buffer (which is when the user presses Enter). To avoid this, right after initializing the Scanner object (or first thing in a method if it's a class variable), add
in.useDelimiter("\n");
To the Java to end the input at that and ignore the newline character.

Java - Running totals(using do while and if else statements)

I'm not very adept in getting the running totals using Java as I've started recently. I have to display and hold the running total of the bank balance and for some strange reason, it's resetting back to 100, which is what I declared it as to start with. Is there any way for me to stop the bank balance from being reset every time it loops?
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int choice, totBal = 0, totWith = 0, totDep = 0;
double with, remBal = 0, deposit, bankBal = 100;
char reply = 0;
do
{
System.out.println("");
System.out.println("Bank online\n");
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Balance");
System.out.println("4. Account Details");
System.out.println("5. Exit\n");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
if(choice == 1)
{
System.out.print("How much do you wish to withdraw?\n");
with = sc.nextInt();
remBal = bankBal - with;
System.out.println("Your new balance is: " + remBal);
totWith++;
}
else if(choice == 2)
{
System.out.print("How much do you wish to deposit?\n");
deposit = sc.nextInt();
remBal = remBal + deposit;
System.out.println("Your new balance is: " + remBal);
totDep++;
}
else if(choice == 3)
{
System.out.println("Your balance is: " + remBal);
totBal++;
}
else if(choice == 4)
{
System.out.println("You made " + totWith + " withdrawls from your account.");
System.out.println("You made " + totDep + " deposits to your account.");
System.out.println("You made " + totBal + " balance checks on your account.");
}
else if(choice == 5)
{
}
System.out.println("");
System.out.print("Do you want to enter another option?(y/n): ");
reply = sc.next().charAt(0);
}while(reply == 'Y' || reply == 'y');
System.out.println("Thank you and goodbye!");
}
}
Also, I feel that I have WAY too many variables. How can I cut back on these?
Your problem is with following statement:
double with, remBal = 0, deposit, bankBal = 100;
Here you are initialising remBal as 0, while when one deposits amount/checks balance you do:
remBal = remBal + deposit;//you use remBal for blaance check
So on first attempt it will try to add 0 with say $100 which will be 100 while bankBal is 100 it should be 100. So initialize remBal same as bankBal (or use just one variable for bankBalance i.e. either of one).
You set the bankBal value to 100 at the start of the program.
When doing withdrawals, you always do
remBal = bankBal - with
which will always equate to
remBal = 100 - with
since you never change bankBal to reflect the updated balance after each loop.
One approach to solve this is to remove the
bankBal
variable altogether and simply set your
remBal
variable to your desired starting value.
Finally change the withdrawal computation mentioned above to
remBal = remBal - with
One thing you can do is implement switch-cases to call methods specific to Depost, Withdraw, etc. An example of this roughly would be:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please Enter what you would like to do: ");
String enterPrompt = input.next();
int options = 5;
switch (options) {
case 1: enterPrompt = "Deposit";
doDeposit();
break;
case 2: enterPrompt = "Withdrawel";
doWithdrawel();
break;
case 3: enterPrompt = "Balance";
viewBalance();
break;
case 4: enterPrompt = "Account Details";
viewAccount();
break;
case 5: enterPrompt = "Exit";
System.exit(1);
break;
}
public void doDeposit(){
//local variables here
//Do stuff
}
public void doWithdrawel(){
//local variables here
//Do stuff
}
public void viewBalance(){
//local variables here
//Do stuff
}
public void viewAccount(){
//local variables here
//Do stuff
}
}

Categories