Repository navigation
Expand file tree
/
Copy pathATM.java
More file actions
196 lines (127 loc) · 5.1 KB
/
Copy pathATM.java
File metadata and controls
196 lines (127 loc) · 5.1 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.*;
public class ATM {
private static final Logger logger = Logger.getLogger(ATM.class.getName());
private static final String LOG_FILE = "atm.log";
public static void main(String[] args) {
setupLogger();
logger.info("ATM Application Started");
Scanner scanner = new Scanner(System.in);
double balance = 1000.0;
while (true) {
System.out.println("1. Check Balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Print Statement");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
checkBalance(balance);
break;
case 2:
balance = deposit(balance, scanner);
break;
case 3:
balance = withdraw(balance, scanner);
break;
case 4:
printStatement(balance);
break;
case 5:
logger.info("ATM Application Closed");
scanner.close();
return;
default:
System.out.println("Invalid option.");
logger.warning("Invalid menu choice: " + choice);
}
}
}
private static void setupLogger() {
try {
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.ALL);
FileHandler fileHandler = new FileHandler(LOG_FILE, true);
fileHandler.setLevel(Level.ALL);
Formatter formatter = new SimpleFormatter() {
@Override
public synchronized String format(LogRecord record) {
return String.format(
"%1$tF %1$tT | %-7s | %-20s | %s%n",
record.getMillis(),
record.getLevel(),
record.getSourceMethodName(),
record.getMessage()
);
}
};
consoleHandler.setFormatter(formatter);
fileHandler.setFormatter(formatter);
logger.addHandler(consoleHandler);
logger.addHandler(fileHandler);
} catch (IOException e) {
System.err.println("Failed to configure logger.");
e.printStackTrace();
}
}
private static void checkBalance(double balance) {
logger.fine("Entered checkBalance()");
System.out.printf("Current Balance: $%.2f%n", balance);
logger.info("Balance checked: $" + balance);
}
private static double deposit(double balance, Scanner scanner) {
logger.fine("Entered deposit()");
System.out.print("Enter amount to deposit: ");
double amount = scanner.nextDouble();
if (amount <= 0) {
System.out.println("Invalid deposit amount.");
logger.severe("Invalid deposit amount: $" + amount);
return balance;
}
balance += amount;
System.out.printf("$%.2f deposited successfully.%n", amount);
logger.info("Deposited $" + amount + ". Current balance: $" + balance);
return balance;
}
private static double withdraw(double balance, Scanner scanner) {
logger.fine("Entered withdraw()");
System.out.print("Enter amount to withdraw: ");
double amount = scanner.nextDouble();
if (amount <= 0) {
System.out.println("Invalid withdrawal amount.");
logger.severe("Invalid withdrawal amount: $" + amount);
return balance;
}
if (amount > balance) {
System.out.println("Insufficient balance.");
logger.warning("Withdrawal failed. Requested: $" + amount +
", Available: $" + balance);
return balance;
}
balance -= amount;
System.out.printf("$%.2f withdrawn successfully.%n", amount);
logger.info("Withdrawn $" + amount + ". Remaining balance: $" + balance);
return balance;
}
private static void printStatement(double balance) {
logger.fine("Entered printStatement()");
try {
FileWriter writer = new FileWriter("statement.txt");
writer.write("===== ATM Statement =====\n");
writer.write("Current Balance: $" + balance + "\n");
writer.close();
System.out.println("Statement saved successfully.");
logger.info("Statement written to statement.txt");
} catch (IOException e) {
logger.log(Level.SEVERE,
"Failed to write statement file.",
e);
}
}
}