-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise10.java
More file actions
171 lines (146 loc) · 6.97 KB
/
Copy pathExercise10.java
File metadata and controls
171 lines (146 loc) · 6.97 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
package chapter10;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
/**
* ============================================================
* EXERCISE 10 — Persistent Contact & Data Storage Engine 💾
* ============================================================
*
* Build a Persistent Data Storage Engine for the CRM that supports:
* 1. CSV Export/Import using `BufferedReader` and `BufferedWriter`.
* 2. Binary Snapshot Persistence with Object Serialization (`Serializable`).
* 3. File backup and integrity verification using Java NIO `Files`.
* ============================================================
*/
class StoredContact implements Serializable {
private static final long serialVersionUID = 100L;
private String id;
private String name;
private String email;
private String phone;
private String department;
public StoredContact(String id, String name, String email, String phone, String department) {
this.id = id;
this.name = name;
this.email = email;
this.phone = phone;
this.department = department;
}
public String getId() { return id; }
public String getName() { return name; }
public String getEmail() { return email; }
public String getPhone() { return phone; }
public String getDepartment() { return department; }
public String toCsvRow() {
return String.format("%s,%s,%s,%s,%s", id, name, email, phone, department);
}
public static StoredContact fromCsvRow(String csvLine) {
String[] parts = csvLine.split(",");
if (parts.length >= 5) {
return new StoredContact(parts[0].trim(), parts[1].trim(), parts[2].trim(), parts[3].trim(), parts[4].trim());
}
return null;
}
@Override
public String toString() {
return String.format("StoredContact[ID=%s, Name='%-15s', Email='%-22s', Dept='%-10s']",
id, name, email, department);
}
}
class ContactStorageEngine {
// 1. Export list to CSV
public static void exportToCsv(List<StoredContact> contacts, String filePath) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath))) {
bw.write("id,name,email,phone,department");
bw.newLine();
for (StoredContact c : contacts) {
bw.write(c.toCsvRow());
bw.newLine();
}
System.out.println(" ✓ Exported " + contacts.size() + " records to CSV: " + filePath);
}
}
// 2. Import list from CSV
public static List<StoredContact> importFromCsv(String filePath) throws IOException {
List<StoredContact> list = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line = br.readLine(); // skip header
while ((line = br.readLine()) != null) {
if (!line.trim().isEmpty()) {
StoredContact c = StoredContact.fromCsvRow(line);
if (c != null) list.add(c);
}
}
}
System.out.println(" ✓ Imported " + list.size() + " records from CSV: " + filePath);
return list;
}
// 3. Save Binary Snapshot (Serialization)
public static void saveBinarySnapshot(List<StoredContact> contacts, String binaryFilePath) throws IOException {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(binaryFilePath))) {
oos.writeObject(contacts);
System.out.println(" ✓ Saved full binary snapshot to: " + binaryFilePath);
}
}
// 4. Load Binary Snapshot (Deserialization)
@SuppressWarnings("unchecked")
public static List<StoredContact> loadBinarySnapshot(String binaryFilePath) throws IOException, ClassNotFoundException {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(binaryFilePath))) {
List<StoredContact> loaded = (List<StoredContact>) ois.readObject();
System.out.println(" ✓ Loaded binary snapshot (" + loaded.size() + " records) from: " + binaryFilePath);
return loaded;
}
}
}
public class Exercise10 {
public static void main(String[] args) {
System.out.println("╔══════════════════════════════════════════════════╗");
System.out.println("║ 💾 PERSISTENT CRM FILE STORAGE ENGINE ║");
System.out.println("╚══════════════════════════════════════════════════╝");
String csvPath = "contacts_export.csv";
String binPath = "contacts_snapshot.dat";
List<StoredContact> initialRoster = new ArrayList<>();
initialRoster.add(new StoredContact("C-101", "Yodha Raja", "yodha@domain.com", "555-0101", "Engineering"));
initialRoster.add(new StoredContact("C-102", "Alex Mercer", "alex@domain.dev", "555-0102", "Operations"));
initialRoster.add(new StoredContact("C-103", "Sarah Connor", "sarah@cyberdyne.org", "555-0103", "Security"));
try {
// Step 1: Export and re-import via CSV
System.out.println("\n--- 1. CSV EXPORT & RECOVERY TEST ---");
ContactStorageEngine.exportToCsv(initialRoster, csvPath);
List<StoredContact> fromCsv = ContactStorageEngine.importFromCsv(csvPath);
for (StoredContact c : fromCsv) {
System.out.println(" • " + c);
}
// Step 2: Binary Serialization snapshot
System.out.println("\n--- 2. BINARY SNAPSHOT TEST ---");
ContactStorageEngine.saveBinarySnapshot(initialRoster, binPath);
List<StoredContact> fromBin = ContactStorageEngine.loadBinarySnapshot(binPath);
System.out.println("Restored from binary snapshot matches: " + (fromBin.size() == initialRoster.size()));
// Step 3: NIO File Info
System.out.println("\n--- 3. FILE SYSTEM STORAGE METADATA ---");
Path p = Paths.get(csvPath);
System.out.printf(" CSV File Size: %d bytes | Absolute: %s%n",
Files.size(p), p.toAbsolutePath());
// Cleanup files
Files.deleteIfExists(Paths.get(csvPath));
Files.deleteIfExists(Paths.get(binPath));
System.out.println("\n🧹 All test storage files cleaned up successfully.");
} catch (Exception e) {
System.out.println("❌ Storage Engine Error: " + e.getMessage());
}
}
}