Reputation: 133
I have been working on making a program to simulate bank transactions. I have to ask the user if they want to deposit, withdrawal, or transfer. Right now I am working on the deposit and withdrawal options of the account.
When the user selects a transaction (for example deposit) and enters a number, I made it so the program asks "Would you like to continue this transaction. Obviously, if yes the program will continue with the transaction and if no, it will not deposit the number the user entered.
My problem is, I have no clue what I need to put in the no option. I don't know if to reject the transaction means I have to exit the loop or what but at the moment if I hit no, the transaction will still go through. Below is a visual of what happens when I enter a transaction but dont want to continue:
below is my entire code. the part of code I don't know what to put has ** by it It probably don't help my organization I am sorry for that!
import java.util.Scanner;
public class BankTransactions {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num;
double balance = 0;
double checkingBalance= 0, savingsBalance =0;
do {
double amount;
System.out.println("------------------------");
System.out.println("Select a Transaction by typing number");
System.out.println("1. Deposit");
System.out.println("2. Withdrawal");
System.out.println("3. Balance");
System.out.println("4. Exit");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) { //if DEPOSIT is selected
//ask to deposit from checking or savings
System.out.println("------------------------");
System.out.println("Would you like to deposit in checking or savings?");
System.out.println("1. Checking");
System.out.println("2. Savings");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) { //if CHECKING is selected
//enter amount to be deposited
System.out.println("------------------------");
System.out.println("Enter amount to deposit in checking account: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
// Add the amount to the checking balance
checkingBalance += amount;
System.out.println("------------------------");
System.out.println("Your checking account's balance is " + checkingBalance);
System.out.println("------------------------");
} else if (num == 2) { //if SAVINGS is selected
//enter amount to be deposited
System.out.println("------------------------");
System.out.println("Enter amount to deposit in savings account: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) {
// Add the amount entered to the savings balance
savingsBalance += amount;
System.out.println("------------------------");
System.out.println("Your savings account's balance is " + savingsBalance);
System.out.println("------------------------");
**} else if (num == 2) {
//EMPTY NEEDS CODE
}**
}
} else if (num == 2) { //if withdrawal is selected
//ask to withdrawal from checking or savings
System.out.println("------------------------");
System.out.println("Would you like to withdrawal from checking or savings?");
System.out.println("1. Checking");
System.out.println("2. Savings");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) { //if checking is selected
//enter amount to be withdrawn
System.out.println("------------------------");
System.out.println("Enter amount to withdrawal: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) { //if you say yes to continuing
// Remove the amount from the balance
checkingBalance -= amount;
System.out.println("------------------------");
System.out.println("Your checking account's balance is " + checkingBalance);
System.out.println("------------------------");
} else if (num == 2) { //if you say no to continuing
//Do not remove amount from savings balance
//EMPTY NEEDS CODE
}
} else if (num == 2) { //if savings is selected
//enter amount to be withdrawn
System.out.println("------------------------");
System.out.println("Enter amount to withdrawal: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) { //if you say yes to continuing
// Remove the amount from the savings balance
savingsBalance -= amount;
System.out.println("------------------------");
System.out.println("Your savings account's balance is " + savingsBalance);
System.out.println("------------------------");
} else if (num == 2) { //if you say no to continuing
//Do not remove amount from savings balance
//EMPTY NEEDS CODE
}
}
} else if (num == 3) { //if balance is selected
//ask to see balance of checking or savings
System.out.println("------------------------");
System.out.println("Your Checking balance is " + checkingBalance);
System.out.println("Your Savings balance is " + savingsBalance);
System.out.println("------------------------");
num = scan.nextInt();
//needs to return to transaction options
}
} while (num != 4);
System.out.println("------------------------");
System.out.println("Good Bye!");
System.out.println("------------------------");
}
}
I am stuck and I want to figure it out. Please don't post the entire code corrected. I want to fix it myself and learn!
Upvotes: 2
Views: 31655
Reputation: 23049
The biggest problem I see is the way how you programming the options. The art of programming is usually in make the problem as simple as possible and easy to expand that problem.
(Like if you make program for adding numbers, you dont care, if you are adding 3 numbers or 3 milions numbers).
If you are gonna expand options, it will be pain in the ass :). No matter if your "decesion tree" would be too big, it will be impossible to know what is happening there.
You can create Option class :
public class Option {
private List<Option> options = new ArrayList<>();
private String text;
public Option(String text) {
this.text = text;
}
public void addOption(Option option) {
getOptions().add(option);
}
/**
* @return the options
*/
public List<Option> getOptions() {
return options;
}
/**
* @return the text
*/
public String getText() {
return text;
}
public String tellUsWhatWeCanDo() {
String ret = "------------------------\n";
int count = 0;
for (Option option : options) {
count++;
ret += count + ". " + option.getText() + "\n";
}
ret += "------------------------\n";
return ret;
}
public Option whereToGo() {
while (1<2) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
if ((num >= 0) && (num < options.size())){
return options.get(num);
} else {
System.out.println("wrong number");
}
}
}
}
Then you can use this like this :
public static void main(String[] args) throws IOException {
Option option1 = new Option("Start");
Option option2 = new Option("Deposit");
Option option3 = new Option("Withdrawal");
Option option4 = new Option("Balance");
Option option5 = new Option("Exit");
option1.addOption(option2);
option1.addOption(option3);
option1.addOption(option4);
option1.addOption(option5);
Option actualOption = option1;
while (1 < 2) {
System.out.println(actualOption.tellUsWhatWeCanDo());
actualOption = actualOption.whereToGo();
}
}
The output is :
------------------------
1. Deposit
2. Withdrawal
3. Balance
4. Exit
------------------------
Note, that you can create automatic scanner option, which can iterate through the options the same way as it is printlined (=automated). (EDIT : I added that iterating)
You can also create interface with "do" method and implements it inside and in every option you run the "do" method which can do the more complex work like "deposit, withdraw" etc.
Upvotes: 3
Reputation: 1526
The transaction still goes through for the checking , not for the savings though.
Here is the reason :
if (num == 1) { //if CHECKING is selected
//enter amount to be deposited
System.out.println("------------------------");
System.out.println("Enter amount to deposit in checking account: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
// Add the amount to the checking balance
checkingBalance += amount;
System.out.println("------------------------");
System.out.println("Your checking account's balance is " + checkingBalance);
System.out.println("------------------------");
} else if (num == 2) { //if SAVINGS is selected
//enter amount to be deposited
System.out.println("------------------------");
System.out.println("Enter amount to deposit in savings account: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) {
// Add the amount entered to the savings balance
savingsBalance += amount;
System.out.println("------------------------");
System.out.println("Your savings account's balance is " + savingsBalance);
System.out.println("------------------------");
**} else if (num == 2) {
//EMPTY NEEDS CODE
}**
}
Notice the difference after the "num = scan.nextInt();" line in both cases? In the first one you are instructing it to go ahead and add anyway regardless of the input, in the second case, you have an if/else statement that would only add it if they user enters 1, and if he enters 2 , you would do nothing.
As for your question about what to do in case of any other option than 1 or 2. I would use an if statement to check if num is 1 without using an else statement, so if any other option is entered it would go to the beginning again. (But if you insist on 2 being "No", you could use else if(num != 2){System.out.println("Invalid Option. Going to the beginning";}
Upvotes: 3
Reputation: 21
You used do while loop. You need some terminate condition. Try with this code.
Put num=4 in your program. It will solve your problem.
else if (num == 2) { //if you say no to continuing
//Do not remove amount from savings balance
//EMPTY NEEDS CODE
num=4;
}
Upvotes: 0