New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adds chukwumaibezim folder with CsvLoader class #314
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
...o/io_app/src/main/java/com/codedifferently/lesson12/factory/chukwumaibezim/CsvLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.codedifferently.lesson12.factory.mohamedibrahim; | ||
|
||
import com.codedifferently.lesson12.factory.LibraryCsvDataLoader; | ||
import com.codedifferently.lesson12.models.CheckoutModel; | ||
import com.codedifferently.lesson12.models.LibraryDataModel; | ||
import com.codedifferently.lesson12.models.LibraryGuestModel; | ||
import com.codedifferently.lesson12.models.MediaItemModel; | ||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CsvLoader implements LibraryCsvDataLoader { | ||
|
||
@Override | ||
public LibraryDataModel loadData() throws IOException { | ||
LibraryDataModel libraryData = new LibraryDataModel(); | ||
|
||
libraryData.guests = readGuestList("csv/guests.csv"); | ||
libraryData.mediaItems = readMediaItems("csv/media_items.csv"); | ||
libraryData.checkedOutItems = readCheckedOutByEmail("csv/checked_out_items.csv"); | ||
|
||
// Gives each guest a list of their checked out books. | ||
libraryData.guests.forEach( | ||
guest -> | ||
guest.checkedOutItems = | ||
libraryData.checkedOutItems.getOrDefault(guest.email, new ArrayList<>())); | ||
anthonydmays marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return libraryData; | ||
} | ||
|
||
private List<MediaItemModel> readMediaItems(String path) throws IOException { | ||
try (BufferedReader reader = new BufferedReader(new FileReader(path))) { | ||
return reader | ||
.lines() | ||
.skip(1) | ||
.map( | ||
line -> { | ||
String[] parts = line.split(",", 8); | ||
MediaItemModel item = new MediaItemModel(); | ||
item.type = parts[0]; | ||
item.id = UUID.fromString(parts[1]); | ||
item.title = parts[2]; | ||
item.isbn = parts[3]; | ||
item.authors = List.of(parts[4]); | ||
item.pages = parts[5].isEmpty() ? 0 : Integer.parseInt(parts[5]); | ||
item.runtime = parts[6].isEmpty() ? 0 : Integer.parseInt(parts[6]); | ||
item.edition = parts[7]; | ||
return item; | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
} | ||
|
||
private List<LibraryGuestModel> readGuestList(String path) throws IOException { | ||
try (BufferedReader reader = new BufferedReader(new FileReader(path))) { | ||
return reader | ||
.lines() | ||
.skip(1) | ||
.map( | ||
line -> { | ||
String[] parts = line.split(","); | ||
LibraryGuestModel guest = new LibraryGuestModel(); | ||
guest.type = parts[0]; | ||
guest.name = parts[1]; | ||
guest.email = parts[2]; | ||
return guest; | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
} | ||
|
||
private Map<String, List<CheckoutModel>> readCheckedOutByEmail(String path) throws IOException { | ||
try (BufferedReader reader = new BufferedReader(new FileReader(path))) { | ||
return reader | ||
.lines() | ||
.skip(1) | ||
.map( | ||
line -> { | ||
String[] parts = line.split(","); | ||
CheckoutModel item = new CheckoutModel(); | ||
item.itemId = UUID.fromString(parts[1]); | ||
item.dueDate = Instant.parse(parts[2]); | ||
return Map.entry(parts[0], item); | ||
}) | ||
.collect( | ||
Collectors.groupingBy( | ||
Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList()))); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug here.
checkedOutItems
doesn't exist onLibraryDataModel
.