-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
115 lines (94 loc) · 3.46 KB
/
Library.java
File metadata and controls
115 lines (94 loc) · 3.46 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
package Books;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
public class Library {
private HashMap<String, Author> allAuthors = new HashMap<String, Author>();
private HashMap<String, Book> allBooks = new HashMap<String, Book>();
private double earnings = 0;
public boolean loadAuthors(String dir) {
try {
File selectedFile = new File(dir);
Scanner reader = new Scanner(selectedFile);
while (reader.hasNextLine()) {
String data = reader.nextLine();
String[] parsedData= data.split(",");
char actualGender= parsedData[2].charAt(0);
Author actualAuthor = new Author(parsedData[0],parsedData[1],actualGender);
allAuthors.put(parsedData[0], actualAuthor);
}
reader.close();
return true;
} catch (FileNotFoundException e) {
return false;
}
}
public boolean loadBooks(String dir) {
try {
File selectedFile = new File(dir);
Scanner reader = new Scanner(selectedFile);
while (reader.hasNextLine()) {
String data = reader.nextLine();
String[] parsedData= data.split(",");
String bookName = parsedData[0];
String[] authors = parsedData[1].split(";");
double price = Double.parseDouble(parsedData[2]);
int qty = Integer.parseInt(parsedData[3]);
Author[] foundAuthors = new Author[authors.length];
for(int i = 0; i < authors.length; i++) {
if(allAuthors.containsKey(authors[i])) {
foundAuthors[i] = allAuthors.get(authors[i]); // Checks if loaded authors are valid and get their instances.
}
}
Book actualBook = new Book(bookName, foundAuthors, price, qty);
allBooks.put(parsedData[0], actualBook);
}
reader.close();
return true;
} catch (FileNotFoundException e) {
return false;
}
}
public Book findBook(String bookName) {
return allBooks.containsKey(bookName) ? allBooks.get(bookName) : null;
}
public Author findAuthor(String authorName) {
return allAuthors.containsKey(authorName) ? allAuthors.get(authorName) : null;
}
public void addBook(String newName, Author[] newAuthors, double newPrice, int newQty) {
if(!allBooks.containsKey(newName)) {
Book newBook = new Book(newName, newAuthors, newPrice, newQty);
allBooks.put(newName, newBook);
}
}
public void addAuthor(String newName, String newEmail, char newGender) {
if(!allAuthors.containsKey(newName)) {
Author newAuthor = new Author(newName, newEmail, newGender);
allAuthors.put(newName, newAuthor);
}
}
public double sellBook(String bookName) {
Book actualBook = findBook(bookName);
if(actualBook == null) return 0;//Means that the book was not found.
double newIncome = actualBook.getPrice();
earnings+=newIncome;
int actualQty = actualBook.getQty() - 1;
actualBook.setQty(actualQty);
if(actualQty < 1) {
allBooks.remove(bookName);
}
return earnings;
}
public double showEarnings() {
return earnings;
}
public Book[] showAllBooks() {
Book[] result = new Book[allBooks.size()];
int i = 0;
for(Book actualBook : allBooks.values()) {
result[i] = actualBook;
i++;
}
return result;
}
}