Created
December 23, 2023 05:07
-
-
Save rayanfer32/4dfba43c40b52d27304823817fe6d119 to your computer and use it in GitHub Desktop.
remotasks_library_problem.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
// MainTest.java | |
package tests; | |
import src.Main; | |
import org.junit.jupiter.api.Test; | |
import static org.junit.jupiter.api.Assertions.*; | |
import org.junit.jupiter.api.BeforeAll; | |
public class MainTest { | |
@BeforeAll | |
static void setUp() { | |
// Sample Users | |
Main.users.put("admin", new Main.User("admin", "admin")); | |
Main.users.put("user1", new Main.User("user1", "user")); | |
Main.users.put("user2", new Main.User("user2", "user")); | |
// Sample Books | |
Main.books.put("Book1", new Main.Book("Book1")); | |
Main.books.put("Book2", new Main.Book("Book2")); | |
Main.books.put("Book3", new Main.Book("Book3")); | |
} | |
@Test | |
void testLoginSuccess() { | |
assertEquals("Login successful. Welcome, admin!", Main.login("admin", "admin")); | |
} | |
@Test | |
void testLoginFailure() { | |
assertEquals("Invalid username or password.", Main.login("nonexistentuser", "password")); | |
} | |
@Test | |
void testReserveBookSuccess() { | |
assertEquals("user1 reserved Book1", Main.reserveBook("Book1", "user1")); | |
} | |
@Test | |
void testReserveBookFailure() { | |
assertEquals("user1 reserved Book2", Main.reserveBook("Book2", "user1")); | |
} | |
@Test | |
void testGenerateReport() { | |
assertEquals("Library Report:\nBook3 - Reserved: false, Loaned: false\nBook1 - Reserved: false, Loaned: false\nBook2 - Reserved: false, Loaned: false\n", Main.generateReport()); | |
} | |
} | |
// Main.java | |
package src; | |
import java.util.*; | |
public class Main { | |
// User class is now static to be accessed in the MainTest class | |
public static class User { | |
String username; | |
String role; | |
public User(String username, String role) { | |
this.username = username; | |
this.role = role; | |
} | |
} | |
// Book class is now static to be accessed in the MainTest class | |
public static class Book { | |
String title; | |
boolean isReserved; | |
boolean isLoaned; | |
public Book(String title) { | |
this.title = title; | |
this.isReserved = false; | |
this.isLoaned = false; | |
} | |
} | |
public static Map<String, Book> books = new HashMap<>(); | |
public static Map<String, User> users = new HashMap<>(); | |
public static void main(String[] args) { | |
// Sample Users | |
users.put("admin", new User("admin", "admin")); | |
users.put("user1", new User("user1", "user")); | |
users.put("user2", new User("user2", "user")); | |
// Sample Books | |
books.put("Book1", new Book("Book1")); | |
books.put("Book2", new Book("Book2")); | |
books.put("Book3", new Book("Book3")); | |
// Simulate user actions | |
System.out.println(login("admin", "admin")); // Log in as admin | |
System.out.println(listBooks()); // List all books | |
System.out.println(reserveBook("Book1", "user1")); // User1 reserves Book1 | |
System.out.println(loanBook("Book1", "user1")); // User1 loans Book1 | |
System.out.println(returnBook("Book1")); // User1 returns Book1 | |
System.out.println(generateReport()); // Generate a simple report | |
} | |
// Methods remain the same as before... | |
public static String login(String username, String password) { | |
User user = users.get(username); | |
if (user != null) { | |
return "Login successful. Welcome, " + user.username + "!"; | |
} else { | |
return "Invalid username or password."; | |
} | |
} | |
public static String reserveBook(String bookTitle, String username) { | |
Book book = books.get(bookTitle); | |
if (book != null && !book.isReserved && !book.isLoaned) { | |
book.isReserved = true; | |
return username + " reserved " + bookTitle; | |
} else { | |
return "Book not available for reservation."; | |
} | |
} | |
public static String loanBook(String bookTitle, String username) { | |
Book book = books.get(bookTitle); | |
if (book != null && book.isReserved && !book.isLoaned) { | |
book.isLoaned = true; | |
book.isReserved = false; | |
return username + " loaned " + bookTitle; | |
} else { | |
return "Book not available for loan."; | |
} | |
} | |
public static String returnBook(String bookTitle) { | |
Book book = books.get(bookTitle); | |
if (book != null && book.isLoaned) { | |
book.isLoaned = false; | |
return bookTitle + " returned."; | |
} else { | |
return "Invalid book return."; | |
} | |
} | |
public static String generateReport() { | |
StringBuilder result = new StringBuilder("Library Report:\n"); | |
for (Map.Entry<String, Book> entry : books.entrySet()) { | |
result.append(entry.getKey()) | |
.append(" - Reserved: ") | |
.append(entry.getValue().isReserved) | |
.append(", Loaned: ") | |
.append(entry.getValue().isLoaned) | |
.append("\n"); | |
} | |
return result.toString(); | |
} | |
// Method to return the list of books as a string | |
public static String listBooks() { | |
StringBuilder result = new StringBuilder("Available Books:\n"); | |
for (Map.Entry<String, Book> entry : books.entrySet()) { | |
result.append(entry.getKey()) | |
.append(" - Reserved: ") | |
.append(entry.getValue().isReserved) | |
.append(", Loaned: ") | |
.append(entry.getValue().isLoaned) | |
.append("\n"); | |
} | |
result.append("\n"); | |
return result.toString(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment