-
Notifications
You must be signed in to change notification settings - Fork 1
/
BookStorage.java
125 lines (110 loc) · 3.4 KB
/
BookStorage.java
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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Hashtable;
public class BookStorage {
//hashtable items stored by ISBN as key and book object as value
Hashtable<String, Book> table;
BufferedWriter logFile = null;
private int items = 0;
//constructor
public BookStorage(){
table = new Hashtable<String, Book>();
try {
//create log file
logFile = new BufferedWriter(new FileWriter("logFile.txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
//getters and setters
public Hashtable<String, Book> getBooks(){
return table;
}
//return an entire book
private Book queryBook(String isbn){
return table.get(isbn);
}
//get isbn of a book
private String retrieveISBN (String isbn){
return table.get(isbn).getIsbn();
}
//get title of a book
private String retrieveTitle(String isbn){
return table.get(isbn).getTitle();
}
//get author of book
private String retrieveAuthor(String isbn){
return table.get(isbn).getAuthor();
}
//get genre of book
private String retrieveGenre(String isbn){
return table.get(isbn).getGenre();
}
//get price of book
private String retrievePrice(String isbn){
return table.get(isbn).getPrice();
}
//get items
public int getItems() {
return items;
}
//set items
public void setItems(int i) {
items = i;
}
//adding new book into storage
public void storeData(String isbn, String title, String author, String genre, String price){
items++;
//create new book instance
Book newBook = new Book(isbn, title, author, genre, price);
//put book in hashtable
table.put(isbn, newBook);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
try {
//write to log file
logFile.write(timestamp + "/" + "INSERT/" + isbn + "/" + title + "/" + author + "/" + genre + "/" + price + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
//delete a book from storage
public void deleteData(String isbn){
items--;
//time stamp for log file
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
//get book information
String title = retrieveTitle(isbn);
String author = retrieveAuthor(isbn);
String genre = retrieveGenre(isbn);
String price = retrievePrice(isbn);
//write to log file
try {
logFile.write(timestamp + "/" +"DELETE/"+ isbn + "\n");
} catch (IOException e) {
e.printStackTrace();
}
//remove from hashtable
table.remove(isbn);
}
//modify a book in storage
public void modifyData(String isbn, String field, String modifiedValue){
//time stamp for log file
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
//get book object from hashtable
Book modifiedBook = table.get(isbn);
//field modified field to set the value
if(field.equals("isbn")) modifiedBook.setIsbn(modifiedValue);
else if(field.equals("title")) modifiedBook.setTitle(modifiedValue);
else if(field.equals("author")) modifiedBook.setAuthor(modifiedValue);
else if(field.equals("genre")) modifiedBook.setGenre(modifiedValue);
else if(field.equals("price")) modifiedBook.setPrice(modifiedValue);
//write to log file
try {
logFile.write(timestamp + "/" +"MODIFY/"+ isbn + "/" + field + "/" + modifiedValue + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
} //end class BookStorage