shouryaj98 / Hotel-Management-Project-Java

It is a Hotel Management tool which can be used to manage activites like storing customer details, booking rooms of four different types, ordering food for particular rooms, unbooking rooms and showing the bill.
MIT License
335 stars 426 forks source link

Refactoring Suggestion - Addressing Primitive Obsession #48

Open lborja04 opened 8 months ago

lborja04 commented 8 months ago

Hi shoryaj98,

I trust this message finds you well. In my recent review of your code, I observed a potential instance of "Primitive Obsession" in the bill method, specifically with the use of primitive type codes to represent room types. I'd like to propose a refactoring using the "Replace Type Code with Class" technique.

Why Refactor?

Code Clarity: Using primitive type codes for room types can lead to confusion and makes the code less self-explanatory. Maintainability: By replacing type codes with a dedicated class, the code becomes more maintainable and adaptable to future changes.

Refactoring Proposal:

public class Room {
    private double roomCharge;

    public Room(double roomCharge) {
        this.roomCharge = roomCharge;
    }

    public double getRoomCharge() {
        return roomCharge;
    }
}

public class BillProcessor {
    public static void bill(Room room, int roomNumber) {
        double amount = room.getRoomCharge();
        // ... rest of the billing logic
    }
}

// Usage:
Room luxuryDoubleRoom = new Room(4000);
Room deluxeDoubleRoom = new Room(3000);
// ... create instances for other room types

BillProcessor.bill(luxuryDoubleRoom, roomNumber);

In this refactoring, I did a rework to the Room class that encapsulates the behavior related to room types. Each room type is represented by an instance of the Room class with its specific room charge.

Feel free to consider this suggestion and adapt it to your specific needs. If you have any questions or need further clarification, please don't hesitate to reach out.

Best regards, [Your Name]