-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankAccount.java
106 lines (91 loc) · 4.47 KB
/
bankAccount.java
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
import java.util.Scanner;
public class bankAccount {
/* The class has 4 instance variables:
1. balance to store the current balance of the account
2. previousTransaction to store the amount of the last transaction
(either deposit or withdraw)
3. customerName to store the name of the customer
4. customerID to store the ID of the customer */
int balance;
int previousTransaction;
String customerName;
String customerID;
bankAccount(String cname, String cID) {
customerName = cname;
customerID = cID;
}
void deposit(int amount) {
if (amount != 0) {
balance = balance + amount;
previousTransaction = amount;
}
}
void withdraw (int amount) {
if (balance != 0) {
balance = balance - amount;
previousTransaction = -amount;
}
}
void getPreviousTransaction() {
if (previousTransaction > 0) {
System.out.println("DEPOSITED: " + previousTransaction);
} else if (previousTransaction < 0) {
System.out.println("WITHDRAW: " + Math.abs(previousTransaction));
} else {
System.out.println("No Transaction Occurred");
}
}
void showMenu() {
char option;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome " +customerName);
System.out.println("Your ID " + customerID);
System.out.println("\n");
System.out.println("A : Check your balance");
System.out.println("B : Deposit");
System.out.println("C : Withdraw");
System.out.println("D : Previous Transaction");
System.out.println("E : EXIT");
do {
System.out.println("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*");
System.out.println("Enter Your Option");
System.out.println("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*");
option = scan.next().toUpperCase().charAt(0); // what doe this mean? - charAt(0) ? - how is it different from "next().char"
System.out.println("\n");
switch (option) {
case 'A' -> {
System.out.println("--------------------------------------------------------------------------");
System.out.println("Balance = " + balance);
System.out.println("--------------------------------------------------------------------------");
}
case 'B' -> {
System.out.println("--------------------------------------------------------------------------");
System.out.println("Enter an amount to deposit ");
System.out.println("--------------------------------------------------------------------------");
int amount = scan.nextInt(); // will program still work if this & next line are moved above case A?
deposit(amount); /* ANSWER: NO! - because we need the deposit() to run EXACTLY here at case B - aka
'deposit functionality' */
System.out.println("\n");
}
case 'C' -> {
System.out.println("--------------------------------------------------------------------------");
System.out.println("Enter an amount to withdraw ");
System.out.println("--------------------------------------------------------------------------");
int amount2 = scan.nextInt();
withdraw(amount2); // Just like case B, we plug in withdraw() func here.
System.out.println("\n"); // see how the program looks w/out this line break.
}
case 'D' -> {
System.out.println("--------------------------------------------------------------------------");
getPreviousTransaction();
System.out.println("--------------------------------------------------------------------------");
System.out.println("\n");
}
case 'E' ->
System.out.println("============================================================================");
default -> System.out.println("Invalid! - Please choose again");
}
} while (option !='E');
System.out.println("Thank You!");
}
}