0% found this document useful (0 votes)
4 views6 pages

Oop-Mini Project Code

Uploaded by

hdjsuehhc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Oop-Mini Project Code

Uploaded by

hdjsuehhc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;

// -------------------- USER CLASS --------------------


/*
OOP Concept: CLASS & ENCAPSULATION
- Data (username, password) is kept private
- Accessed using public methods (getters)
*/
class User {
private String username;
private String password;
private List<Book> issuedBooks; // Association (User HAS-A Book)

public User(String username, String password) {


[Link] = username;
[Link] = password;
[Link] = new ArrayList<Book>();
}

public String getUsername() {


return username;
}

public boolean checkPassword(String password) {


return [Link](password);
}

public List<Book> getIssuedBooks() {


return issuedBooks;
}
}

// -------------------- BOOK CLASS --------------------


/*
OOP Concept: CLASS & ENCAPSULATION
- Book data is hidden and controlled via methods
*/
class Book {
private int id;
private String name;
private boolean isIssued;
private String issuedTo;

public Book(int id, String name) {


[Link] = id;
[Link] = name;
[Link] = false;
[Link] = null;
}
public int getId() {
return id;
}

public String getName() {


return name;
}

public boolean isIssued() {


return isIssued;
}

public String getIssuedTo() {


return issuedTo;
}

// OOP Concept: ABSTRACTION (hiding internal logic)


public void issue(String username) {
isIssued = true;
issuedTo = username;
}

public void returnBook() {


isIssued = false;
issuedTo = null;
}
}

// -------------------- LIBRARY CLASS --------------------


/*
OOP Concept: ABSTRACTION
- Handles all book-related operations
- User doesn't need to know internal implementation
*/
class Library {
private List<Book> books;

public Library() {
books = new ArrayList<Book>();

// Your required books


[Link](new Book(1, "Object Oriented Programming"));
[Link](new Book(2, "Communication Engineering"));
[Link](new Book(3, "Control Systems"));
[Link](new Book(4, "Signals and Systems"));
[Link](new Book(5, "Financial Management"));
}

public void displayBooks() {


[Link]("\n--- Book List ---");
for (Book b : books) {
if ([Link]()) {
[Link]([Link]() + ". " + [Link]() +
" (Issued to " + [Link]() + ")");
} else {
[Link]([Link]() + ". " + [Link]() +
" (Available)");
}
}
}

public Book getBookById(int id) {


for (Book b : books) {
if ([Link]() == id) {
return b;
}
}
return null;
}
}

// -------------------- MAIN SYSTEM --------------------


/*
OOP Concept: MODULARITY
- System divided into multiple classes
*/
public class Main {

private static Scanner sc = new Scanner([Link]);


private static List<User> users = new ArrayList<User>();
private static Library library = new Library();

public static void main(String[] args) {

while (true) {
[Link]("\n==== Library System ====");
[Link]("1. Login");
[Link]("2. New User");
[Link]("3. Exit");

int choice = getIntInput();

switch (choice) {
case 1:
login();
break;
case 2:
register();
break;
case 3:
[Link]("Exiting system...");
return;
default:
[Link]("Invalid choice.");
}
}
}

// Safe input handling


public static int getIntInput() {
while (![Link]()) {
[Link]("Enter valid number:");
[Link]();
}
int num = [Link]();
[Link]();
return num;
}

public static void register() {


[Link]("Enter username: ");
String uname = [Link]().trim();

[Link]("Enter password: ");


String pass = [Link]().trim();

if ([Link]("") || [Link]("")) {
[Link]("Fields cannot be empty!");
return;
}

// Prevent duplicate users


for (User u : users) {
if ([Link]().equals(uname)) {
[Link]("Username already exists!");
return;
}
}

[Link](new User(uname, pass));


[Link]("Registration successful!");
}

public static void login() {


[Link]("Enter username: ");
String uname = [Link]();

[Link]("Enter password: ");


String pass = [Link]();
for (User u : users) {
if ([Link]().equals(uname) && [Link](pass)) {
[Link]("Login successful!");
userMenu(u);
return;
}
}

[Link]("Invalid username or password.");


}

public static void userMenu(User user) {


while (true) {
[Link]("\n--- User Menu ---");
[Link]("1. View Books");
[Link]("2. Issue Book");
[Link]("3. Return Book");
[Link]("4. My Books");
[Link]("5. Logout");

int choice = getIntInput();

switch (choice) {
case 1:
[Link]();
break;

case 2:
[Link]("Enter Book ID: ");
int issueId = getIntInput();
Book book1 = [Link](issueId);

if (book1 == null) {
[Link]("Book not found.");
} else if ([Link]()) {
[Link]("Book already issued.");
} else {
[Link]([Link]());
[Link]().add(book1);
[Link]("Book issued successfully!");
}
break;

case 3:
[Link]("Enter Book ID: ");
int returnId = getIntInput();
Book book2 = [Link](returnId);

if (book2 == null) {
[Link]("Book not found.");
} else if (![Link]() ||
![Link]().equals([Link]())) {
[Link]("You cannot return this book.");
} else {
[Link]();
[Link]().remove(book2);
[Link]("Book returned successfully!");
}
break;

case 4:
[Link]("\n--- My Books ---");
if ([Link]().isEmpty()) {
[Link]("No books issued.");
} else {
for (Book b : [Link]()) {
[Link]([Link]() + ". " + [Link]());
}
}
break;

case 5:
[Link]("Logged out.");
return;

default:
[Link]("Invalid choice.");
}
}
}
}

You might also like