-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookInventory.java
More file actions
62 lines (45 loc) · 1.95 KB
/
BookInventory.java
File metadata and controls
62 lines (45 loc) · 1.95 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
import java.util.HashMap;
import java.util.Map;
public class BookInventory implements BookInventoryAPI {
private Map<Book, Integer> bookCatalogue;
public BookInventory() {
bookCatalogue = new HashMap<>();
}
public void addBook(Book book, int quantity) {
bookCatalogue.put(book, quantity);
}
public void updateBookQuantity(Book book, int newQuantity) {
if (bookCatalogue.containsKey(book)) {
bookCatalogue.put(book, newQuantity);
} else {
System.out.println("Book not found in inventory.");
}
}
public void deserializeInventoryFromJson(String json) {
try {
bookQuantityMap = objectMapper.readValue(json, HashMap.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
public int getBookQuantity(Book book) {
return bookCatalogue.getOrDefault(book, 0);
}
public static void main(String[] args) {
// Create a BookInventory instance
BookInventory bookInventory = new BookInventory();
// Add books to the inventory
bookInventory.addBook(new Book("1", "Book 1", "Author 1", 2021), 10);
bookInventory.addBook(new Book("2", "Book 2", "Author 2", 2022), 20);
// Update the quantity of a book
bookInventory.updateBookQuantity(new Book("1", "Book 1", "Author 1", 2021), 15);
// Get the quantity of a book
System.out.println(bookInventory.getBookQuantity(new Book("1", "Book 1", "Author 1", 2021)));
// Serialize the inventory to JSON
String json = bookInventory.serializeInventoryToJson();
System.out.println(json);
// Deserialize the JSON back into the inventory
bookInventory.deserializeInventoryFromJson(json);
}
}