-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankBackendSimulation.java
More file actions
131 lines (113 loc) · 4.45 KB
/
Copy pathBankBackendSimulation.java
File metadata and controls
131 lines (113 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.Scanner;
class BankBackendSimulation {
private String name;
private String username;
private String password;
private double balance;
public BankBackendSimulation (String name, String username, String password, double balance){
this.name = name;
this.username = username;
this.password = password;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
System.out.println("Your new balance is " + balance);
}
public void withdraw(double amount){
if (amount > balance) {
System.out.println("Withdrawal of " + amount + " cannot be made. Balance is less than withdrawal amount.");
System.out.println("Please try again.");
} else {
balance -= amount;
System.out.println("Withdrawal of " + amount + " was successful.");
System.out.println("Your new balance is " + balance);
}
}
public void checkBalance() {
System.out.println("Checking bank account balance: " + balance);
}
public boolean loginAccount(String enteredUsername, String enteredPassword) {
return this.username.equals(enteredUsername) && this.password.equals(enteredPassword);
}
public static void main(String[] args) {
System.out.println("Welcome to the bank! What is your name?");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("Nice to meet you, " + name + "!" );
System.out.println("Please create an account. Enter your username.");
String username = scanner.nextLine();
System.out.println("Please enter your password now.");
String password = scanner.nextLine();
System.out.println("Account creation successful! Remember to write down your account information and do not share this information with anyone else!");
System.out.println("Would you like to continue or log out?");
String decision = scanner.nextLine();
if (decision.equalsIgnoreCase("log out")) {
System.out.println("You are now logging out, goodbye!");
return;
}
boolean hasStartingBalance = false;
double startingBalance = 0;
while (!hasStartingBalance) {
System.out.println("Please enter your starting balance.");
startingBalance = scanner.nextDouble();
if (startingBalance < 0) {
System.out.println("Invalid starting balance. Balance cannot be negative.");
} else {
System.out.println("Success! You now have a balance of $" + startingBalance);
hasStartingBalance = true;
}
scanner.nextLine();
}
BankBackendSimulation account = new BankBackendSimulation(name, username, password, startingBalance);
boolean loggedIn = false;
while (!loggedIn) {
System.out.println("Please log in again to continue.");
System.out.println("Enter your username: ");
String enteredUsername = scanner.nextLine();
System.out.println("Enter your password.");
String enteredPassword = scanner.nextLine();
if (account.loginAccount(enteredUsername, enteredPassword)) {
System.out.println("Login successful!");
loggedIn = true;
} else {
System.out.println("Invalid login. Please try again.");
}
}
boolean menu = true;
while (menu) {
System.out.println("Bank Menu");
System.out.println("Deposit Money");
System.out.println("Withdraw Money");
System.out.println("Check Balance");
System.out.println("What do you want to do today?");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("Deposit Money") || answer.equalsIgnoreCase("Deposit")) {
System.out.println("How much money would you like to deposit?");
double amount = scanner.nextDouble();
scanner.nextLine();
account.deposit(amount);
}
else if (answer.equalsIgnoreCase("Withdraw Money") || answer.equalsIgnoreCase("Withdraw")) {
System.out.println("How much money would you like to withdraw?");
double amount = scanner.nextDouble();
scanner.nextLine();
account.withdraw(amount);
}
else if (answer.equalsIgnoreCase("Check Balance") || answer.equalsIgnoreCase("Check")){
account.checkBalance();
}
else {
System.out.println("System error, please try again");
}
System.out.println("Would you like to do anything else today?");
String response = scanner.nextLine();
if (!response.equalsIgnoreCase("Yes")) {
menu = false;
System.out.println("Thank you for coming, have a good day!");
}
}
scanner.close();
}
}