Skip to content
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: Updated CvsDataLoader file #312

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove the library files from this PR.

@@ -0,0 +1,27 @@
package com.codedifferently.lesson10.library;

public class DVD implements Media {
private String title;
private String type;

public DVD(String title) {
this.title = title;
this.type = "DVD";
}

@Override
public String getTitle() {
return title;
}

@Override
public String getType() {
return type;
}

@Override
public boolean canBeCheckedOutBy(Patron patron) {
// Implement logic for checking if a DVD can be checked out by a patron
return true; // For example, DVDs can be checked out by anyone
}
}
@@ -0,0 +1,23 @@
package com.codedifferently.lesson10.library;

import com.codedifferently.lesson10.library.exceptions.BookCheckedOutException;

public class Librarian {

public void checkOutBook(Book book, Patron patron) throws BookCheckedOutException {
if (book.isCheckedOut()) {
throw new BookCheckedOutException("Book is already checked out.");
}
book.setLibrary(null);
book.setLibrary(patron.getLibrary());
patron.getLibrary().checkOutBook(book, patron);
}

public void checkInBook(Book book, Patron patron) throws BookCheckedOutException {
if (!patron.getLibrary().isCheckedOut(book)) {
throw new BookCheckedOutException("Book is not checked out.");
}
patron.getLibrary().checkInBook(book, patron);
book.setLibrary(null);
}
}
@@ -0,0 +1,27 @@
package com.codedifferently.lesson10.library;

public class Magazine implements Media {
private String title;
private String type;

public Magazine(String title) {
this.title = title;
this.type = "Magazine";
}

@Override
public String getTitle() {
return title;
}

@Override
public String getType() {
return type;
}

@Override
public boolean canBeCheckedOutBy(Patron patron) {
// Implement logic for checking if a magazine can be checked out by a patron
return false; // Magazines cannot be checked out
}
}
@@ -0,0 +1,9 @@
package com.codedifferently.lesson10.library;

public interface Media {
String getTitle();

String getType();

boolean canBeCheckedOutBy(Patron patron);
}
@@ -0,0 +1,27 @@
package com.codedifferently.lesson10.library;

public class Newspaper implements Media {
private String title;
private String type;

public Newspaper(String title) {
this.title = title;
this.type = "Newspaper";
}

@Override
public String getTitle() {
return title;
}

@Override
public String getType() {
return type;
}

@Override
public boolean canBeCheckedOutBy(Patron patron) {
// Implement logic for checking if a newspaper can be checked out by a patron
return false; // Newspapers cannot be checked out
}
}
Expand Up @@ -59,6 +59,18 @@ public Set<Book> getCheckedOutBooks() throws LibraryNotSetException {
return this.library.getCheckedOutByPatron(this);
}

<<<<<<< HEAD
/**
* Get the library associated with the patron.
*
* @return The library associated with the patron.
*/
public Library getLibrary() {
return this.library;
}

=======
>>>>>>> 22ce3dee9fbec444ab04ff845cb6c7fdd2f19932
@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
@@ -0,0 +1,61 @@
package com.codedifferently.lesson12.factory.jamirabailey;

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 org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class CsvDataLoader implements LibraryCsvDataLoader {

@Override
public LibraryDataModel loadData() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're making progress, so feel free to submit an updated version if you'd like more credit for this assignment.

LibraryDataModel model = new LibraryDataModel();
model.mediaItems = readMediaItems("/workspaces/code-differently-24-q1/lesson_12/io/io_app/src/main/resources/csv/media_items.csv");
model.guests = readGuests("/workspaces/code-differently-24-q1/lesson_12/io/io_app/src/main/resources/csv/guests.csv");
model.checkoutsByGuestEmail = readCheckoutsByGuestEmail("/workspaces/code-differently-24-q1/lesson_12/io/io_app/src/main/resources/csv/checkouts_by_guest_email.csv");
return model;
}

private List<MediaItemModel> readMediaItems(String filePath) {
List<MediaItemModel> mediaItems = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
MediaItemModel item = new MediaItemModel();
item.setType(parts[0]);
item.setId(UUID.fromString(parts[1]));
item.setTitle(parts[2]);
item.setType(parts[3]);
mediaItems.add(item);
}
} catch (IOException e) {
throw new RuntimeException("Failed to read media items", e);
}
return mediaItems;
}

private List<LibraryGuestModel> readGuests(String filePath) {
List<LibraryGuestModel> guests = new ArrayList<>();
// Implement reading guests from CSV file
return guests;
}

private Map<String, List<CheckoutModel>> readCheckoutsByGuestEmail(String filePath) {
Map<String, List<CheckoutModel>> checkoutsByGuestEmail = new HashMap<>();
// Implement reading checkouts from CSV file
return checkoutsByGuestEmail;
}
}