-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.java
More file actions
137 lines (120 loc) · 3.1 KB
/
Database.java
File metadata and controls
137 lines (120 loc) · 3.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
import java.util.*;
import java.io.*;
public class Database implements Serializable
{
private Hashtable<String, Entry> data = new Hashtable<String, Entry>();
public Database()
{}
public void addEntry(String email, String file) //Adds Entry to Hashtable if it does not exist and adds File to it, otherwise adds File to existing Entry
{
if (data.containsKey(email))
{
Entry entry = data.get(email);
entry.addFile(file);
return;
}
data.put(email, new Entry(file));
}
public Entry getEntry(String email) //Returns Entry of specified Email
{
if (data.containsKey(email))
{
return data.get(email);
}
System.out.println("Email is not in Database");
return null;
}
public String getFile(String email, int file) //Gets specified File from specified Entry in Hashtable
{
if (data.containsKey(email))
{
Entry entry = data.get(email);
return entry.getFile(file);
}
System.out.println("Email is not in Database");
return null;
}
public String[] getAllFiles(String email) //Gets all Files from specified Entry in Hashtable
{
if (data.containsKey(email))
{
Entry entry = data.get(email);
return entry.getAllFiles();
}
System.out.println("Email is not in Database");
return new String[0];
}
public void printAll() //Prints all Entries and corresponding Files in Hashtable
{
System.out.println("Emails in Database: " + data.size());
System.out.println("-------------------------------------------------------------------");
Set<String> emails = data.keySet();
for (String email : emails)
{
printEntry(email);
}
}
public void printEntry(String email) //Prints all Files in specified Entry
{
if (data.containsKey(email))
{
Entry entry = data.get(email);
System.out.println(email + ":");
entry.printFiles();
return;
}
System.out.println("Email is not in Database");
}
public void simplePrint()
{
System.out.println(data);
}
public int getNumEntries() //Returns number of Entries in Hashtable
{
return data.size();
}
public void removeEntry(String email) //Clear specified entry from table
{
if (data.containsKey(email))
{
data.remove(email);
return;
}
System.out.println("Email is not in Database");
}
public void clearData() //Clears all Entries from Hashtable
{
data.clear();
}
private Hashtable<String, Entry> getHashtable() //Returns Hashtable
{
return data;
}
public void saveDatabase() //Saves Hashtable to HashData.ser
{
try
{
FileOutputStream fs = new FileOutputStream("HashData.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(this);
os.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void getDatabase() //Copies Hashtable from HashData.ser
{
try
{
ObjectInputStream is = new ObjectInputStream(new FileInputStream("HashData.ser"));
Database restored = (Database) is.readObject();
data = restored.getHashtable();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}