Skip to content

Code suggestion using SOLID principles #1

Description

@Levin514

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!**********"); 

    } 

} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions