Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Answers/java project/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Answers/java project/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Answers/java project/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Answers/java project/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions Answers/java project/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Answers/java project/Admin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Admin extends User {
private String password;

public Admin(String name, String phoneNumber, String password) {
super(name, phoneNumber);
this.password = password;
}

public String getPassword() {
return password;
}
}
42 changes: 42 additions & 0 deletions Answers/java project/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class Book {
private static int idCounter = 1;
private int bookID;
private String title;
private String author;
private boolean isAvailable;
private String description;

public Book(String title, String author, String description) {
this.bookID = idCounter++;
this.title = title;
this.author = author;
this.isAvailable = true;
this.description = description;
}

// Getters and Setters
public int getBookID() {
return bookID;
}

public String getTitle() {
return title;
}

public String getAuthor() {
return author;
}

public boolean isAvailable() {
return isAvailable;
}

public void setAvailable(boolean available) {
isAvailable = available;
}

public String getDescription() {
return description;
}
}

76 changes: 76 additions & 0 deletions Answers/java project/CLI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.util.List;
import java.util.Scanner;

public class CLI {
private static Library library = new Library("City Library", 1000, "9 AM - 5 PM");

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter command: ");
String command = scanner.nextLine();
processCommand(command);
}
}

private static void processCommand(String command) {
String[] parts = command.split(" ");
switch (parts[0]) {
case "lib":
handleLibraryCommand(parts);
break;
default:
System.out.println("Unknown command");
}
}

private static void handleLibraryCommand(String[] parts) {
switch (parts[1]) {
case "add":
if (parts[2].equals("book")) {
String title = parts[3];
String author = parts[4];
String description = parts[5];
library.addBook(title, author, description);
System.out.println("Book added successfully");
} else if (parts[2].equals("member")) {
String name = parts[3];
String phone = parts[4];
String password = parts[5];
library.addUser(new Admin(name, phone, password));
System.out.println("Member added successfully");
}
break;
case "get":
if (parts[2].equals("hrs")) {
System.out.println("Library operating hours: " + library.getOperatingHours());
} else if (parts[2].equals("available") && parts[3].equals("books")) {
List< Book> availableBooks = library.getAvailableBooks();
for (Book book : availableBooks) {
System.out.println(book.getTitle() + " by " + book.getAuthor());
}
}
break;
case "rent":
int bookID = Integer.parseInt(parts[2]);
int userID = Integer.parseInt(parts[3]);
library.rentBook(bookID, userID);
System.out.println("Book rented successfully");
break;
case "return":
bookID = Integer.parseInt(parts[2]);
library.returnBook(bookID);
System.out.println("Book returned successfully");
break;
case "remove":
if (parts[2].equals("member")) {
userID = Integer.parseInt(parts[3]);
library.removeUser(userID);
System.out.println("Member removed successfully");
}
break;
default:
System.out.println("Unknown library command");
}
}
}
66 changes: 66 additions & 0 deletions Answers/java project/Library.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.ArrayList;
import java.util.List;

public class Library {
private String name;
private int capacity;
private String operatingHours;
private List<Book> books;
private List<User> users;
private List<Rent> rentals;

public Library(String name, int capacity, String operatingHours) {
this.name = name;
this.capacity = capacity;
this.operatingHours = operatingHours;
this.books = new ArrayList<>();
this.users = new ArrayList<>();
this.rentals = new ArrayList<>();
}

// Method to add a book
public void addBook(String title, String author, String description) {
books.add(new Book(title, author, description));
}

public void addUser(User user) {
users.add(user);
}

public void removeUser(int userID) {
users.removeIf(user -> user.getUserID() == userID);
}

public void rentBook(int bookID, int userID) {
Book book = books.stream().filter(b -> b.getBookID() == bookID && b.isAvailable()).findFirst().orElse(null);
if (book != null) {
User user = users.stream().filter(u -> u.getUserID() == userID && u instanceof NormalUser).findFirst().orElse(null);
if (user != null) {
book.setAvailable(false);
rentals.add(new Rent(book, (NormalUser) user));
}
}
}


public void returnBook(int bookID) {
Rent rent = rentals.stream().filter(r -> r.getBook().getBookID() == bookID).findFirst().orElse(null);
if (rent != null) {
rent.getBook().setAvailable(true);
rentals.remove(rent);
}
}

public List<Book> getAvailableBooks() {
List<Book> availableBooks = new ArrayList<>();
for (Book book : books) {
if (book.isAvailable()) {
availableBooks.add(book);
}
}
return availableBooks;
}
public String getOperatingHours() {
return operatingHours;
}
}
5 changes: 5 additions & 0 deletions Answers/java project/MyApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class MyApp {
public static void main(String[] args) {
CLI.main(args);
}
}
15 changes: 15 additions & 0 deletions Answers/java project/NormalUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.util.Date;

public class NormalUser extends User {
private Date registrationDate;

public NormalUser(String name, String phoneNumber) {
super(name, phoneNumber);
this.registrationDate = new Date();
}

public Date getRegistrationDate() {
return registrationDate;
}
}

32 changes: 32 additions & 0 deletions Answers/java project/Rent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.Date;

public class Rent {
private static int idCounter = 1;
private int rentalID;
private Book book;
private NormalUser user;
private Date rentalDate;

public Rent(Book book, NormalUser user) {
this.rentalID = idCounter++;
this.book = book;
this.user = user;
this.rentalDate = new Date();
}

public int getRentalID() {
return rentalID;
}

public Book getBook() {
return book;
}

public NormalUser getUser() {
return user;
}

public Date getRentalDate() {
return rentalDate;
}
}
26 changes: 26 additions & 0 deletions Answers/java project/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class User {
private static int idCounter = 1;
private int userID;
private String name;
private String phoneNumber;

public User(String name, String phoneNumber) {
this.userID = idCounter++;
this.name = name;
this.phoneNumber = phoneNumber;
}

// Getters and Setters
public int getUserID() {
return userID;
}

public String getName() {
return name;
}

public String getPhoneNumber() {
return phoneNumber;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading