-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicro_project2.java
More file actions
142 lines (125 loc) · 6.21 KB
/
Micro_project2.java
File metadata and controls
142 lines (125 loc) · 6.21 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
import java.util.*;
import java.io.*;
class Exception1 extends Exception {
Exception1(String msg) {
super(msg);
}
}
public class Micro_project2 {
public static void main(String args[]) throws Exception {
Scanner s = new Scanner(System.in);
String accountNumber = "";
String pin = "";
double balance = 0;
try {
// Enter crediantials
System.out.print("\n\n\t\tEnter Account Number : \t\t");
accountNumber = s.nextLine();
System.out.print("\n\t\tEnter Pin : \t\t\t");
pin = s.nextLine();
FileReader f1 = new FileReader("C:\\Users\\sanke\\Desktop\\tp\\Java ATM MicroProject\\Data.txt");
BufferedReader br = new BufferedReader(f1);
String str;
mainLoop: while ((str = br.readLine()) != null) {
String username = str.split(" ")[0];
if (accountNumber.equalsIgnoreCase(username)) {
String password = str.split(" ")[1];
if (pin.equals(password)) {
System.out.println("\n\t\tCorrect Pin");
} else {
System.out.println("\n\t\tWrong Pin Or Account Number");
}
String amount = str.split(" ")[2];
balance = Double.parseDouble(amount);
while (true) {
System.out.println("\t------------------------------------------------------------------------------");
System.out.println("\t\t|\t\t\t\t\t\t\t|");
System.out.println("\t\t| \t\t Automated Teller Machine\t\t|");
System.out.println("\t\t| \t\tChoose 1 for Withdraw\t\t\t|");
System.out.println("\t\t| \t\tChoose 2 for Deposit\t\t\t|");
System.out.println("\t\t| \t\tChoose 3 for Check Balance\t\t|");
System.out.println("\t\t| \t\tChoose 4 for EXIT\t\t\t|");
System.out.println("\t\t|\t\t\t\t\t\t\t|");
System.out.println(
"\t------------------------------------------------------------------------------");
System.out.print("\n\tChoose the operation you want to perform: ");
int n = s.nextInt();
switch (n) {
case 1:
try {
System.out.print("\n\t\tEnter money to be withdrawn: \t");
double withdraw = s.nextInt();
if (balance >= withdraw) {
balance = balance - withdraw;
System.out.println("\n\t\tPlease collect your money .........\n");
} else {
throw new Exception1("\n\t\tInsufficant balance");
}
} catch (Exception1 e) {
System.out.println(e);
}
break;
case 2:
System.out.print("\n\t\t\tEnter money to be deposited: \t");
double deposit = s.nextInt();
balance = balance + deposit;
System.out.println("\n\t\tYour Money has been successfully depsited ......");
System.out.println("");
break;
case 3:
System.out.println("\n\t\t\t\tBalance : " + balance);
System.out.println("");
break;
case 4:
break mainLoop;
}
}
}
}
f1.close();
s.close();
try {
// Account Class
class Account {
public String username;
public String password;
public String balance;
public Account(String username, String password, String balance) {
this.username = username;
this.password = password;
this.balance = balance;
}
}
// Accounts Array
ArrayList<Account> accounts = new ArrayList<Account>();
// Load Accounts from Data.txt file
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\sanke\\Desktop\\tp\\Java ATM MicroProject\\Data.txt"));
String temp;
while ((temp = reader.readLine()) != null) {
String[] strParts = temp.split(" ");
accounts.add(new Account(strParts[0], strParts[1], strParts[2]));
}
reader.close();
// Change Account Balance
for (Account account : accounts) {
if (account.username.equalsIgnoreCase(accountNumber)) {
account.balance = String.valueOf(balance);
}
}
// Save accounts to Data.txt
FileWriter fw = new FileWriter("Data.txt");
for (Account account : accounts) {
fw.write(String.join(" ", Arrays.asList(account.username, account.password, account.balance)));
fw.write("\n");
}
fw.close();
System.out.println("Hurray!!");
} catch (IOException ioException) {
System.out.println("Unexpected Error Occured");
System.out.println(ioException.getMessage());
}
} catch (Exception e) {
System.out.println(e);
}
}
}