Skip to content

try to use various design patterns to implement a simple Console-Based Library Management System(LMS) using java

Notifications You must be signed in to change notification settings

qkrwoghd04/Design_Patterns_Implementation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

Console-Based Library Management System(LMS)

1. How to run java code on cmd

Step 1:

Compile whole java classes in library package

1

Step 2:

Using "-cp ." command

When using the "-cp ." option, it instructs the Java runtime environment to include the current directory in the classpath. This allows Java programs to recognize and use .class files located in the current directory

2

2. Design Patterns for LMS

Singleton Pattern for Database class

// The DatabaseConnection class implements the Singleton Design Pattern.
// It ensures that only one instance of the DatabaseConnection class can exist.
public class DatabaseConnection {
  private static DatabaseConnection instance;
  private Map<String, User> users;
  private Map<String, Book> books;
  private Map<String, List<Book>> userBorrowedBooks = new HashMap<>();
  private Map<String, LocalDate> borrowedDates = new HashMap<>();

  private DatabaseConnection() {
    users = new HashMap<>();
    books = new HashMap<>(); 
    userBorrowedBooks = new HashMap<>();
  }

  public static DatabaseConnection getInstance() {
    if (instance == null) {
      instance = new DatabaseConnection();
    }
    return instance;
  }

Factory Pattern for User class

// The UserFactory class implements the Factory Design Pattern.
// It provides a static method to create objects of User subclasses (Student or Faculty) based on the type parameter.
class UserFactory{
  // This static method creates and returns User objects.
  // The type of User object (Student or Faculty) depends on the type argument.
  public static User createUser(String type, String name, String phoneNum){
    switch(type.toLowerCase()){
      case "student":
        return new Student(name, phoneNum);

      case "faculty":
        return new Faculty(name, phoneNum);
        
      default:
        throw new IllegalArgumentException("Unknown user type.");
    }
  }
}

Decorator Pattern for managing overdue books

// BookDecorator class serves as a decorator for the Book class. It follows the Decorator Pattern,
// allowing dynamic addition of new behaviors (in this case, tracking overdue status) to Book objects.
public class BookDecorator {
    private Book book;
    private boolean isOverdue;

    // Constructor wraps a Book object, enabling dynamic extension of its behavior without modifying the original class
    public BookDecorator(Book book) {
        this.book = book;
        this.isOverdue = false; 
    }

    
    public void setOverdue(boolean isOverdue) {
        this.isOverdue = isOverdue;
    }

Observer Pattern for state changes

// The Observer interface declares the update method that is called by the subject.
// Observers implement this interface to react to changes in the subject's state.
public interface Observer {
    void update(String availability);
}

// The Subject interface declares a set of methods for managing observers.
// It allows subjects to register, remove, and notify observers about the state changes.
public interface Subject {
    void registerObserver(Observer o);
    void removeObserver(Observer o);
    void notifyObservers();
}

About

try to use various design patterns to implement a simple Console-Based Library Management System(LMS) using java

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages