-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
145 lines (90 loc) · 3.42 KB
/
Account.java
File metadata and controls
145 lines (90 loc) · 3.42 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.util.Date;
import java.util.Scanner;
public class Account {
private float balance;
private long created;
private String name;
private static double INTEREST_RATE = 0.0005;
public Account(Scanner scanner) {
this.created = new Date().getTime();
this.balance = 0;
System.out.print("\nWelcome to Bank of Black and White, What's your name? : ");
name = scanner.next();
}
public void showBalance() {
System.out.printf("\n Your balance is: %.2f #", this.balance);
}
public void withdraw(Scanner scanner) {
System.out.print("\n Enter an amount to withdraw: ");
float amount = scanner.nextFloat();
if (amount <= 0) {
System.out.println(
"\n Amount to withdraw must be greater than 0 #");
return;
}
if (amount > this.balance) {
System.out.printf(
"\n You do not have sufficient balance, please make a deposit first. Your current balance is %.2f # \n",
this.balance);
}
else {
this.balance -= amount;
System.out.printf("\n Withdrawal of %.2f # was successful, your new balance is %.2f #\n", amount,
this.balance);
}
}
public void deposit(Scanner scanner) {
System.out.print("\n Enter an amount to deposit: ");
float amount = scanner.nextFloat();
if (amount <= 0) {
System.out.println(
"\n Amount to deposit must be greater than 0 #");
}
else {
this.balance += amount;
System.out.printf("\n Deposit of %.2f # was successful, your new balance is %.2f #\n", amount,
this.balance);
}
}
public void computeInterest() {
float diffInSecs = (new Date().getTime() - this.created) / 1000;
double interest = this.balance * INTEREST_RATE * diffInSecs;
System.out.printf(
"\nYour account is %.2f seconds old, hence, Interest is: %.2f # at %f percent per second. \n",
diffInSecs,
interest,
INTEREST_RATE);
}
// Main Function
public static void main(String args[]) {
Scanner loopscanner = new Scanner(System.in);
Account account = new Account(loopscanner);
int prompt = 1;
while (prompt != 0) {
System.out
.print(
"\n\nDear " + account.name
+ ", choose an operation: \n1 to show balance\n2 to deposit\n3 to withdraw\n4 to compute interest\n0 to exit\n\n$ ");
prompt = loopscanner.nextInt();
switch (prompt) {
case 1:
account.showBalance();
break;
case 2:
account.deposit(loopscanner);
break;
case 3:
account.withdraw(loopscanner);
break;
case 4:
account.computeInterest();
case 0:
break;
default:
System.out.println("\nYour selection is invalid, try again.");
}
}
loopscanner.close();
System.out.println("\n Thank you for banking with us " + account.name + "!");
}
}