Giter Club home page Giter Club logo

hotel-reservation-system's Issues

Conditional Logic in setDetails Methods Should Be Replaced with Polymorphism

The setDetails methods in both the Laundry and Transportation classes currently rely on lengthy if-else statements to determine specific behaviors based on type. This conditional logic is a code smell and can be replaced with polymorphism, which would simplify the code and make it more maintainable.

Example of Current Code:

public class Laundry extends Service {
    public void setDetails() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter type of wash (1/2/3): ");
        type = in.nextInt();
        System.out.print("Enter quantity of clothes: ");
        quantity = in.nextInt();

        if (type == 1) {
            cost = 10 * quantity;
        } else if (type == 2) {
            cost = 20 * quantity;
        } else if (type == 3) {
            cost = 30 * quantity;
        } else {
            cost = 0;
        }
    }
}

public class Transportation extends Service {
    public void setDetails() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter type of transportation (1/2/3): ");
        type = in.nextInt();
        System.out.print("Enter number of people: ");
        quantity = in.nextInt();

        if (type == 1) {
            cost = 5 * quantity;
        } else if (type == 2) {
            cost = 15 * quantity;
        } else if (type == 3) {
            cost = 25 * quantity;
        } else {
            cost = 0;
        }
    }
}

Proposed Refactoring:

The if-else logic should be replaced with a polymorphic structure. This can be achieved by introducing subclasses that represent each type of laundry or transportation. Each subclass will implement its own calculateCost method, eliminating the need for conditional checks.

Refactored Code Example:

public abstract class Service {
    protected int quantity;
    protected int cost;

    public abstract void setDetails();
    public abstract void calculateCost();
}

public class Laundry extends Service {
    private int type;

    @Override
    public void setDetails() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter type of wash (1/2/3): ");
        type = in.nextInt();
        System.out.print("Enter quantity of clothes: ");
        quantity = in.nextInt();

        calculateCost();
    }

    @Override
    public void calculateCost() {
        switch (type) {
            case 1 -> cost = 10 * quantity;
            case 2 -> cost = 20 * quantity;
            case 3 -> cost = 30 * quantity;
            default -> cost = 0;
        }
    }
}

public class Transportation extends Service {
    private int type;

    @Override
    public void setDetails() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter type of transportation (1/2/3): ");
        type = in.nextInt();
        System.out.print("Enter number of people: ");
        quantity = in.nextInt();

        calculateCost();
    }

    @Override
    public void calculateCost() {
        switch (type) {
            case 1 -> cost = 5 * quantity;
            case 2 -> cost = 15 * quantity;
            case 3 -> cost = 25 * quantity;
            default -> cost = 0;
        }
    }
}

Benefits of Refactoring:

  • Simplifies Code: Polymorphism reduces the need for complex conditional logic, making the code easier to read and understand.
  • Improves Maintainability: Each class handles its own logic, making future changes more localized and less prone to errors.
  • Encourages Extensibility: Adding new types of services would simply involve creating new subclasses rather than modifying existing code.

Let me know if you need assistance with implementing this refactoring or have any further questions!

Code Duplication in Deluxe and SuperDeluxe Classes

There is a significant code duplication issue between the Deluxe and SuperDeluxe classes. Both classes contain identical methods (set, getRate, getStatus, getWifi, and statusChange). This code duplication increases the risk of errors and makes maintenance more difficult, as any change to the duplicated logic would need to be applied in multiple places.

Example of Current Code:

public class Deluxe extends Room {
    public void set(int r, boolean w, boolean s) {
        rate = r;
        wifi = w;
        status = s;
    }

    public int getRate() {
        return rate;
    }

    public boolean getStatus() {
        return status;
    }

    public boolean getWifi() {
        return wifi;
    }

    public void statusChange() {
        status = !status;
    }
}

public class SuperDeluxe extends Room {
    public void set(int r, boolean w, boolean s) {
        rate = r;
        wifi = w;
        status = s;
    }

    public int getRate() {
        return rate;
    }

    public boolean getStatus() {
        return status;
    }

    public boolean getWifi() {
        return wifi;
    }

    public void statusChange() {
        status = !status;
    }
}

Proposed Refactoring:

To address this issue, I recommend using the Pull Up Method refactoring technique. This involves moving the duplicated methods to a common superclass, allowing both Deluxe and SuperDeluxe to inherit these methods and thus eliminating the duplication.

Refactored Code Example:

public abstract class Room {
    protected int rate;
    protected boolean wifi;
    protected boolean status;

    public void set(int r, boolean w, boolean s) {
        rate = r;
        wifi = w;
        status = s;
    }

    public int getRate() {
        return rate;
    }

    public boolean getStatus() {
        return status;
    }

    public boolean getWifi() {
        return wifi;
    }

    public void statusChange() {
        status = !status;
    }
}

public class Deluxe extends Room {
    // No need to redefine methods here, they are inherited from Room
}

public class SuperDeluxe extends Room {
    // No need to redefine methods here, they are inherited from Room
}

Benefits of Refactoring:

  • Eliminates Redundancy: The duplicated code will be consolidated in one place, reducing the risk of inconsistencies and errors.
  • Improves Reusability: By moving the shared methods to a superclass, any other future classes that might share similar functionality can also inherit from this superclass.
  • Enhances Maintainability: With the logic centralized in a single location, making updates or bug fixes becomes much simpler and less error-prone.

Let me know if you need help with implementing this refactoring or have any questions!

Implement Adapter Pattern for Service Interfaces

Implement Adapter Pattern

Objective

The Adapter pattern converts the interface of a class into another interface that the client expects. The Adapter allows classes that could not work together due to incompatible interfaces to collaborate.

Motivation

In the context of your system, you have different services (Laundry and Transportation) with specific interfaces. By using the Adapter pattern, you can provide a common interface (Service) for these services, allowing them to be used uniformly and simplifying the client code that interacts with these services.

Advantages

  • Facilitates collaboration between classes with incompatible interfaces.
  • Allows reuse of existing code with non-matching interfaces.
  • You can add new adapters for new classes without modifying the client code.

Attachments

  • UML Solution

adapter

Implementar Patrón Factory Method para la creación de objetos

Objetivo:

El patrón Factory Method define una interfaz para crear un objeto, pero permite a las subclases alterar el tipo de objetos que se crearán. Separa la creación de objetos de su uso.

Motivación:

En el proyecto se tienen diferentes tipos de habitaciones (Luxury, Deluxe, SuperDeluxe). Al usar el patrón Factory Method, se puede centralizar la creación de objetos de manera que la lógica de creación esté encapsulada en una clase de fábrica, reutilizando y la facilitando el mantenimiento.

Ventajas:

Permite que las subclases determinen qué clase concreta instanciar.
Desacopla la creación de objetos de su utilización, facilitando el mantenimiento y la escalabilidad.
Centraliza la lógica de creación, lo que puede ser reutilizado por múltiples clientes.

SOLUCION UML
image

Implementing Singleton Design Pattern

Objetivo:

El patrón Singleton asegura que una clase tenga solo una instancia y proporciona un punto de acceso global a ella.

Motivación:

En el contexto del programa, puede haber situaciones donde se necesita asegurar que solo exista una instancia de ciertas clases (por ejemplo, configuraciones de la aplicación, gestores de recursos, servicios etc.). Singleton garantiza que no se creen múltiples instancias que podrían llevar a inconsistencias y problemas de sincronización.

Ventajas:

  • Garantiza control sobre cómo y cuándo se accede a la instancia.  
    
  • Evita la creación de múltiples instancias, ahorrando memoria y recursos.  
    
  • Asegura un único punto de acceso, manteniendo la consistencia del estado compartido. 
    

SOLUCION UML

SingletonHRS

Code suggestion using SOLID principles

Introduction

Greetings,

After examining the code for your Hotel Reservation System project, I have pinpointed several areas where improvements can be made to boost efficiency, address specific errors and even some code improvements following SOLID design principles.

UML Diagram

UML Diagram Hotel Reservation System

Modified Code

Interfaces

Service


interface Service { 

    void setDetails(); 

    int getTotalCost(); 

    boolean getStatus(); 

} 

Room

interface Room { 

    int getRate(); 

    void setDetails(int rate, boolean wifi, boolean status); 

    boolean getStatus(); 

    boolean getWifi(); 

    void changeStatus(); 

} 

Abstract Class

AbstractRoom

abstract class AbstractRoom implements Room { 

    protected int rate; 

    protected boolean wifi; 

    protected boolean status; 

 

    public int getRate() { 

        return rate; 

    } 

 

    public void setDetails(int rate, boolean wifi, boolean status) { 

        this.rate = rate; 

        this.wifi = wifi; 

        this.status = status; 

    } 

 

    public boolean getStatus() { 

        return status; 

    } 

 

    public boolean getWifi() { 

        return wifi; 

    } 

 

    public void changeStatus() { 

        this.status = !this.status; 

    } 

} 

Classes

StandartRoom

class StandardRoom extends AbstractRoom { 

    public StandardRoom() { 

        this.rate = 5000; 

        this.wifi = false; 

        this.status = true; 

    } 

} 

DeluxeRoom

class DeluxeRoom extends AbstractRoom { 

    public DeluxeRoom() { 

        this.rate = 7000; 

        this.wifi = true; 

        this.status = true; 

    } 

} 

SuperDeluxeRoom

class SuperDeluxeRoom extends AbstractRoom { 

    public SuperDeluxeRoom() { 

        this.rate = 9000; 

        this.wifi = true; 

        this.status = true; 

    } 

} 

LaundryService

class LaundryService implements Service { 

    private int type; 

    private int cost; 

    private int quantity; 

    private boolean status; 

 

    public LaundryService() { 

        this.type = 0; 

        this.cost = 0; 

        this.quantity = 0; 

        this.status = false; 

    } 

 

    @Override 

    public void setDetails() { 

        Scanner in = new Scanner(System.in); 

        System.out.println("Enter type of wash (1/2/3): "); 

        type = in.nextInt(); 

        System.out.println("Enter quantity of clothes: "); 

        quantity = in.nextInt(); 

 

        if (type == 1) 

            cost = 100; 

        else if (type == 2) 

            cost = 200; 

        else if (type == 3) 

            cost = 300; 

        else 

            cost = 0; 

 

        status = true; 

    } 

 

    @Override 

    public int getTotalCost() { 

        return quantity * cost; 

    } 

 

    @Override 

    public boolean getStatus() { 

        return status; 

    } 

} 

TransportationService

class TransportationService implements Service { 

    private int type; 

    private int cost; 

    private int quantity; 

    private boolean status; 

 

    public TransportationService() { 

        this.type = 0; 

        this.cost = 0; 

        this.quantity = 0; 

        this.status = false; 

    } 

 

    @Override 

    public void setDetails() { 

        Scanner in = new Scanner(System.in); 

        System.out.println("Enter type of transportation (1/2/3): "); 

        type = in.nextInt(); 

        System.out.println("Enter number of people: "); 

        quantity = in.nextInt(); 

 

        if (type == 1) 

            cost = 100; 

        else if (type == 2) 

            cost = 200; 

        else if (type == 3) 

            cost = 300; 

        else 

            cost = 0; 

 

        status = true; 

    } 

 

    @Override 

    public int getTotalCost() { 

        return quantity * cost; 

    } 

 

    @Override 

    public boolean getStatus() { 

        return status; 

    } 

} 

HRS_Main

public class HRS_Main { 

    private Room room; 

    private List<Service> services; 

 

    public HRS_Main(Room room) { 

        this.room = room; 

        this.services = new ArrayList<>(); 

    } 

 

    public void addService(Service service) { 

        services.add(service); 

    } 

 

    public double calculateTotalCost() { 

        double totalCost = room.getRate(); 

        for (Service service : services) { 

            totalCost += service.getTotalCost(); 

        } 

        double gst = totalCost * 0.18; 

        return totalCost + gst; 

    } 

 

    public static void main(String[] args) { 

        Scanner sc = new Scanner(System.in); 

        Room room = null; 

 

        System.out.println("Select room type (1: Standard, 2: Deluxe, 3: Super Deluxe): "); 

        int roomType = sc.nextInt(); 

        switch (roomType) { 

            case 1: 

                room = new StandardRoom(); 

                break; 

            case 2: 

                room = new DeluxeRoom(); 

                break; 

            case 3: 

                room = new SuperDeluxeRoom(); 

                break; 

            default: 

                System.out.println("Invalid room type selected."); 

                System.exit(0); 

        } 

 

        HRS_Main booking = new HRS_Main(room); 

 

        System.out.println("Do you want laundry service? (Y/N): "); 

        char laundryOption = sc.next().charAt(0); 

        if (laundryOption == 'Y' || laundryOption == 'y') { 

            Service laundryService = new LaundryService(); 

            laundryService.setDetails(); 

            booking.addService(laundryService); 

        } 

 

        System.out.println("Do you want transportation service? (Y/N): "); 

        char transportOption = sc.next().charAt(0); 

        if (transportOption == 'Y' || transportOption == 'y') { 

            Service transportationService = new TransportationService(); 

            transportationService.setDetails(); 

            booking.addService(transportationService); 

        } 

 

        double totalCost = booking.calculateTotalCost(); 

        System.out.println("Total cost: " + totalCost); 

 

        System.out.println("**********Thank you for your visit.**********\n**********We hope to see you again!**********"); 

    } 

} 

Encapsulate Related Data Fields in `Room` and `Service` Classes

There is a Data Clumps code smell present in the Room and Service classes. Certain groups of variables frequently appear together and are often manipulated in tandem. Specifically, the variables rate, wifi, and status in the Room class, as well as type, cost, quantity, bno, and status in the Service class, are strongly related and should be encapsulated into their own classes.

Example of Current Code:

public abstract class Service {
    int type;
    int cost;
    int quantity;
    String bno;
    boolean status;
}

public abstract class Room {
    int rate;
    boolean wifi;
    boolean status;
}

Proposed Refactoring:

To address this issue, I suggest using the Extract Class refactoring technique. This involves creating new classes to encapsulate these related data fields, which will improve the cohesion and maintainability of the code.

Refactored Code Example:

public class ServiceDetails {
    private int type;
    private int cost;
    private int quantity;
    private String bno;
    private boolean status;

    // Getters and setters for ServiceDetails fields
}

public abstract class Service {
    protected ServiceDetails details;

    public Service() {
        this.details = new ServiceDetails();
    }

    // Methods to interact with ServiceDetails
}

public class RoomDetails {
    private int rate;
    private boolean wifi;
    private boolean status;

    // Getters and setters for RoomDetails fields
}

public abstract class Room {
    protected RoomDetails details;

    public Room() {
        this.details = new RoomDetails();
    }

    // Methods to interact with RoomDetails
}

Benefits of Refactoring:

  • Improves Cohesion: By encapsulating related fields into their own classes, each class now has a single responsibility, which makes the code cleaner and easier to understand.
  • Enhances Maintainability: Changes to the related data can be made in one place (in the new class), reducing the risk of bugs and making the code easier to maintain.
  • Reduces Code Duplication: Common functionality related to these fields can be centralized in the new classes, reducing the likelihood of code duplication.

Let me know if you need help with implementing this refactoring or have any further questions!

Implement Composite Pattern for Attack System

Implement Composite Pattern

Objective

The Composite pattern allows treating both individual objects and compositions of objects uniformly. This is useful when individual elements and composite elements need to be handled in the same way by the client code.

Motivation

In the context of the attack system (Attack) and characters (Character), there is a need to manage both simple attacks and combinations of attacks. The Composite pattern allows these attacks to be handled uniformly and flexibly, without needing to differentiate between simple and composite attacks in the client code.

Details of Implementation

  • Attack Interface: Defines common methods that all attacks must implement.
  • Attack1 Class: Represents a simple attack.
  • AttackComposite Class: Represents a composite attack that can contain multiple Attack instances.

Advantages

  • Allows treating individual and composite objects uniformly.
  • Facilitates the composition of complex objects from simple objects.

Attachments

  • UML Solution Diagram

composite

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.