madhuv-sharma / hotel-reservation-system Goto Github PK
View Code? Open in Web Editor NEWHotel Reservation System, in Java, having all basic functionalities, following Object Oriented Programming and having easy-to-use interface.
Hotel Reservation System, in Java, having all basic functionalities, following Object Oriented Programming and having easy-to-use interface.
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:
Let me know if you need assistance with implementing this refactoring or have any further questions!
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:
Let me know if you need help with implementing this refactoring or have any questions!
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.
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.
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.
El patrón Singleton asegura que una clase tenga solo una instancia y proporciona un punto de acceso global a ella.
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.
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.
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.
interface Service {
void setDetails();
int getTotalCost();
boolean getStatus();
}
interface Room {
int getRate();
void setDetails(int rate, boolean wifi, boolean status);
boolean getStatus();
boolean getWifi();
void changeStatus();
}
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;
}
}
class StandardRoom extends AbstractRoom {
public StandardRoom() {
this.rate = 5000;
this.wifi = false;
this.status = true;
}
}
class DeluxeRoom extends AbstractRoom {
public DeluxeRoom() {
this.rate = 7000;
this.wifi = true;
this.status = true;
}
}
class SuperDeluxeRoom extends AbstractRoom {
public SuperDeluxeRoom() {
this.rate = 9000;
this.wifi = true;
this.status = true;
}
}
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;
}
}
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;
}
}
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!**********");
}
}
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:
Let me know if you need help with implementing this refactoring or have any further questions!
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.
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.
Attack
instances.A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.