-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibFileReader.java
154 lines (116 loc) · 4.42 KB
/
LibFileReader.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
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
package library;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.time.LocalDate;
import java.time.Year;
/**
* Provides a platform for a library to read from text files
*
* @author 091388, 094954
* @version 1.0
*/
public class LibFileReader{
/**
* Reads all lines in a file and appends each line to an ArrayList
*
* @param path Path of file to be read
* @return ArrayList containing all lines from file
*/
public ArrayList<String> ReadFile(String path){
ArrayList<String> allLinesInFile = new ArrayList<>();
try {
Scanner scanner = new Scanner(new File(path));
// Add all lines
while (scanner.hasNextLine()) {
allLinesInFile.add(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("* File could not be found");
e.printStackTrace();
}
return allLinesInFile;
}
/**
* Creates Book objects from a comma separated .txt file containing book data
*
* @param path Path of file to be read
* @return ArrayList of books created
*/
public ArrayList<Book> ReadBooks(String path){
// Get book data from file
ArrayList<String> bookData = ReadFile(path);
ArrayList<Book> allBooks = new ArrayList<>();
for (String item : bookData){
String[] stringArray = item.split(",");
// Build book
Book newBook = new Book();
newBook.setIdNumber(Integer.parseInt(stringArray[0]));
newBook.setBookTitle(stringArray[1]);
newBook.setPublishYear(Year.parse(stringArray[3]));
newBook.setNumberCopies(Integer.parseInt(stringArray[4]));
// If more than one author
if (stringArray[2].contains(":")){
String[] authorArray = stringArray[2].split(":");
ArrayList<String> splitAuthors= new ArrayList<>();
splitAuthors.addAll(Arrays.asList(authorArray));
newBook.setBookAuthors(splitAuthors);
}
// If one author
else {
ArrayList<String> author = new ArrayList<>();
author.add(stringArray[2]);
newBook.setBookAuthors(author);
}
allBooks.add(newBook);
}
return allBooks;
}
/**
* Creates Member objects from a comma separated .txt file containing member data
*
* @param path Path of file to be read
* @return ArrayList of members created
*/
public ArrayList<Member> ReadMembers(String path){
// Get member data from file
ArrayList<String> memberData = ReadFile(path);
ArrayList<Member> allMembers = new ArrayList<>();
for (String item : memberData) {
String[] stringArray = item.split(",");
// Build Member
Member newMember = new Member();
newMember.setIdNumber(Integer.parseInt(stringArray[0]));
newMember.setFirstName(stringArray[1]);
newMember.setLastName(stringArray[2]);
newMember.setDateJoined(LocalDate.parse(stringArray[3]));
allMembers.add(newMember);
}
return allMembers;
}
/**
* Creates BookLoan objects from a comma separated .txt file containing book loan data
*
* @param path Path of file to be read
* @return ArrayList of book loans created
*/
public ArrayList<BookLoan> ReadBookLoans(String path){
// Get book loan data from file
ArrayList<String> bookLoanData = ReadFile(path);
ArrayList<BookLoan> allBookLoans = new ArrayList<>();
for (String item: bookLoanData){
String[] stringArray = item.split(",");
// Build loan
BookLoan loan = new BookLoan();
loan.setLoanId(Integer.parseInt(stringArray[0]));
loan.setBookId(Integer.parseInt(stringArray[1]));
loan.setMemberId(Integer.parseInt(stringArray[2]));
loan.setBorrowDate(LocalDate.parse(stringArray[3]));
allBookLoans.add(loan);
}
return allBookLoans;
}
}