-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSER
132 lines (117 loc) · 3.6 KB
/
USER
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
132
import java.util.Scanner;
public class User {
private String name;
private String pin;
private BankAccount account;
public User(String name, String pin, BankAccount account) {
this.name = name;
this.pin = pin;
this.account = account;
}
public String getName() {
return name;
}
public String getPin() {
return pin;
}
public BankAccount getAccount() {
return account;
}
}
public class BankAccount {
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public boolean withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
return true;
} else {
return false;
}
}
}
public class ATM {
private User user;
public ATM(User user) {
this.user = user;
}
public void start() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Please choose an option:");
System.out.println("1. Check balance");
System.out.println("2. Withdraw");
System.out.println("3. Deposit");
System.out.println("4. Quit");
int option = scanner.nextInt();
scanner.nextLine(); // Consume newline not consumed by nextInt()
switch (option) {
case 1:
checkBalance();
break;
case 2:
withdraw(scanner);
break;
case 3:
deposit(scanner);
break;
case 4:
quit();
break;
default:
System.out.println("Invalid option.");
}
}
}
private void checkBalance(){
System.out.printf("Your current balance is $%.2f\n", user.getBalance());
}
private void withdraw(Scanner scanner){
if (!user.hasAccount()) {
System.out.println("You do not have a checking account!");
return;
}
double amount = getAmountFromUser(scanner, "Withdrawal Amount");
if (amount > user.getCheckingBalance()) {
System.out.println("Insufficient funds for that transaction.");
return;
}
user.withdraw(amount);
System.out.printf("$%.2f has been withdrawn from your account.\n", amount);
}
private void deposit(Scanner scanner){
if (!user.hasAccount()) {
System.out.println("You do not have a checking account!");
return;
}
double amount = getAmountFromUser(scanner, "Deposit Amount");
user.deposit(amount);
System.out.printf("$%.2f has been deposited into your account.\n", amount);
}
public static double getAmountFromUser(Scanner scanner, String prompt){
boolean validInput = false;
while(!validInput){
try{
System.out.print(prompt + ": ");
double input = scanner.nextDouble();
if (input < 0) throw new Exception();
validInput = true;
return input;
} catch (Exception e){
System.out.println("That was an invalid entry! Please enter a positive number.");
}
}
return -1; //This should never be reached
}
}