-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.java
More file actions
91 lines (73 loc) · 2.68 KB
/
Copy pathDatabase.java
File metadata and controls
91 lines (73 loc) · 2.68 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
package login;
import app.Customer;
import exceptions.CorruptedFileException;
import java.io.*;
import java.util.*;
public class Database {
// public static Map<String, User> load() {
// Map<String, User> user_map = new HashMap<String, User>();
// try {
// Scanner data_store = new Scanner(new File("fake-people-db.txt"));
//
// while (data_store.hasNextLine()) {
// String[] split_string = data_store.nextLine().split(",");
// User u = new User(split_string);
// user_map.put(u.username, u);
// }
//
// } catch (FileNotFoundException e) {
// System.out.println(e.getMessage());
// }
// return user_map;
// }
public void saveEmpUser(User user) throws IOException {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("employee.txt")));
if (user != null) {
pw.println(user.toString());
}
pw.close();
}
public void saveCusUser(User user) throws IOException {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("customer.txt")));
if (user != null) {
pw.println(user.toString());
}
pw.close();
}
public User loadUser(String userType, String username, String password) throws IOException {
String fileName;
if (userType.equals("customer")) {
fileName = "customer.txt";
} else {
fileName = "employee.txt";
}
// Create File Reading Objects
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
User user = null;
while ((line = br.readLine()) != null) {
user = createUser(line, userType, username, password);
}
br.close();
return user;
}
private User createUser(String line, String userType, String inputUsername, String inputPassword) {
StringTokenizer inReader = new StringTokenizer(line, ",");
String username = inReader.nextToken();
String password = inReader.nextToken();
String firstName = inReader.nextToken();
String lastName = inReader.nextToken();
String email = inReader.nextToken();
String suburb = inReader.nextToken();
String type = inReader.nextToken();
if (username.equals(inputUsername) && password.equals(inputPassword)) {
if (userType.equals("customer")) {
String[] parts = {firstName, lastName, email, suburb, username, password};
return new Customer(parts, type);
} else {
return null;
}
}
return null;
}
}