Skip to content

Commit 4b54cd2

Browse files
Moibrahi7Mohamed
andauthored
Feat: adds Mohamed's CsvDataLoader to display current Library items, checked out items, and a guest list (#310)
* Feat: added a CsvDataLoader to display current Library items, checked out items, and a guest list * Fix: Changed formating * Fix: Changed file path to look more clean --------- Co-authored-by: Mohamed <“[email protected]”>
1 parent dc2ae77 commit 4b54cd2

File tree

1 file changed

+163
-0
lines changed
  • lesson_12/io/io_app/src/main/java/com/codedifferently/lesson12/factory/mohamedibrahim

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package com.codedifferently.lesson12.factory.mohamedibrahim;
2+
3+
import com.codedifferently.lesson12.factory.LibraryCsvDataLoader;
4+
import com.codedifferently.lesson12.models.CheckoutModel;
5+
import com.codedifferently.lesson12.models.LibraryDataModel;
6+
import com.codedifferently.lesson12.models.LibraryGuestModel;
7+
import com.codedifferently.lesson12.models.MediaItemModel;
8+
import java.io.BufferedReader;
9+
import java.io.FileReader;
10+
import java.io.IOException;
11+
import java.time.Instant;
12+
import java.util.ArrayList;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.UUID;
17+
import org.springframework.core.io.ClassPathResource;
18+
import org.springframework.stereotype.Service;
19+
20+
/** LibraryCsvDataLoaderSolid */
21+
@Service
22+
public class CsvDataLoader implements LibraryCsvDataLoader {
23+
public static void main(String[] args) throws IOException {
24+
25+
new CsvDataLoader().loadData();
26+
}
27+
28+
@Override
29+
public LibraryDataModel loadData() throws IOException {
30+
LibraryDataModel libraryData = new LibraryDataModel();
31+
32+
libraryData.guests = readGuestList("csv/guests.csv");
33+
libraryData.mediaItems = readMediaItems("csv/media_items.csv");
34+
Map<String, List<CheckoutModel>> checkoutsByGuestEmail =
35+
readCheckedOutByEmail("csv/checked_out_items.csv");
36+
// Gives each guest a list of their checked out books.
37+
for (var guest : libraryData.guests) {
38+
var checkouts = checkoutsByGuestEmail.get(guest.email);
39+
if (checkouts != null) {
40+
guest.checkedOutItems = checkouts;
41+
} else {
42+
guest.checkedOutItems = new ArrayList<>();
43+
}
44+
}
45+
46+
return libraryData;
47+
}
48+
49+
/**
50+
* Gets data from csv file and makes a list of all media items in the library.
51+
*
52+
* @param path The file path
53+
* @return
54+
*/
55+
public static List<MediaItemModel> readMediaItems(String path) {
56+
try (var reader = new BufferedReader(new FileReader(new ClassPathResource(path).getFile()))) {
57+
// The list that will be returned at the end of the function.
58+
var items = new ArrayList<MediaItemModel>();
59+
String line;
60+
// Skips the headers in the csv file.
61+
line = reader.readLine();
62+
while ((line = reader.readLine()) != null) {
63+
// Puts the line read from the csv in an array.
64+
var parts = line.split(",", 8);
65+
// Temp item that will latter be added to a list.
66+
var item = new MediaItemModel();
67+
68+
// Puts data in the temp item.
69+
item.type = parts[0];
70+
item.id = UUID.fromString(parts[1]);
71+
item.title = parts[2];
72+
item.isbn = parts[3];
73+
item.authors = List.of(parts[4]);
74+
75+
// Checks if the index for this variable is empty and sets it to 0.
76+
if (parts[5].equals("")) item.pages = 0;
77+
else item.pages = Integer.parseInt(parts[5]);
78+
79+
// Checks if the index for this variable is empty and sets it to 0.
80+
if (parts[6].equals("")) item.runtime = 0;
81+
else item.runtime = Integer.parseInt(parts[6]);
82+
item.edition = parts[7];
83+
84+
items.add(item);
85+
}
86+
reader.close();
87+
return items;
88+
} catch (IOException e) {
89+
throw new RuntimeException("Failed to read media items", e);
90+
}
91+
}
92+
93+
/**
94+
* Gets data from csv file and makes list of guests in the library.
95+
*
96+
* @param path The file path
97+
* @return
98+
*/
99+
private List<LibraryGuestModel> readGuestList(String path) {
100+
try (var reader = new BufferedReader(new FileReader(new ClassPathResource(path).getFile()))) {
101+
// The list that will be returned at the end of the function.
102+
var guests = new ArrayList<LibraryGuestModel>();
103+
String line;
104+
// Skips the headers in the csv file.
105+
line = reader.readLine();
106+
while ((line = reader.readLine()) != null) {
107+
// Puts the line read from the csv in an array.
108+
var parts = line.split(",");
109+
// Temp guest that will latter be added to a list.
110+
var guest = new LibraryGuestModel();
111+
112+
// Puts data in the guest item.
113+
guest.type = parts[0];
114+
guest.name = parts[1];
115+
guest.email = parts[2];
116+
117+
guests.add(guest);
118+
}
119+
reader.close();
120+
return guests;
121+
} catch (IOException e) {
122+
throw new RuntimeException("Failed to read media items", e);
123+
}
124+
}
125+
126+
/**
127+
* Gets data from csv file and makes a map of items checked out by guest email.
128+
*
129+
* @param path The file path.
130+
* @return
131+
*/
132+
private static Map<String, List<CheckoutModel>> readCheckedOutByEmail(String path) {
133+
try (var reader = new BufferedReader(new FileReader(new ClassPathResource(path).getFile()))) {
134+
// The hashmap that will be returned at the end of the function.
135+
var checkedOutItems = new HashMap<String, List<CheckoutModel>>();
136+
String line;
137+
// Skips the headers in the csv file.
138+
line = reader.readLine();
139+
while ((line = reader.readLine()) != null) {
140+
// Puts the line read from the csv in an array.
141+
var parts = line.split(",");
142+
// Temp item that will latter be added to a list.
143+
var item = new CheckoutModel();
144+
145+
// Puts data in the temp item.
146+
item.itemId = UUID.fromString(parts[1]);
147+
item.dueDate = Instant.parse(parts[2]);
148+
// Checks if the map already had this email.
149+
if (checkedOutItems.containsKey(parts[0])) {
150+
checkedOutItems.get(parts[0]).add(item);
151+
} else {
152+
List<CheckoutModel> checkOut = new ArrayList<>();
153+
checkOut.add(item);
154+
checkedOutItems.put(parts[0], checkOut);
155+
}
156+
}
157+
reader.close();
158+
return checkedOutItems;
159+
} catch (IOException e) {
160+
throw new RuntimeException("Failed to read media items", e);
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)