patternjavaModerate
Hotel Booking Simulation
Viewed 0 times
hotelbookingsimulation
Problem
I am learning java and I successfully wrote one small console application. I would love to get reviews and possible bug sources on my code. I'm particularly concerned with my object structure.
Question
A well renowned hotel has three branches in Miami. Namely x,y and
z. Each has two types of customers: Regular and Rewardee. Also each branch has its own ratings x is given a 3 star rating while y has 5 star rating and z has 4 star rating.
Each hotel has specific rates for weekend and weekdays. x charges $100
for regular customers on weekdays and $120 on weekends While it is $90
for rewardee on weekdays and $60 on weekends. Similarly y charges $130
for regular customers on weekdays and $150 on weekends. While its $100
for rewardee on weekdays and $95 on weekends. While z charges $195 for
regular customers on weekdays and $150 on weekends. While its $120 for
rewardee on weekdays and $90 on weekends. Now when the customer
requests for a particular detail you need to find which hotel would
yield the customer profit. In case of tie between hotels compare the
ratings and provide the result.
Input format:
Output format:
Solution:
`import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class HotelFactory {
String hotelName;
private int regularWeekDay;
private int regularWeekEnd;
private int rewardeeWeekDay;
private int rewardeeWeekEnd;
HotelFactory(String name) {
this.hotelName = name;
}
public int getRegularWeekDay() {
return regularWeekDay;
}
public void setRegularWeekDay(int regularWeekDay) {
this.regularWeekDay = regularWeekDay;
}
public int getRegularWeekEnd() {
return regularWeekEnd;
}
public void setRegularWeekEnd(int regularWeekEnd) {
this.regularWeekEnd = regularWeekEnd;
}
public int getRewa
Question
A well renowned hotel has three branches in Miami. Namely x,y and
z. Each has two types of customers: Regular and Rewardee. Also each branch has its own ratings x is given a 3 star rating while y has 5 star rating and z has 4 star rating.
Each hotel has specific rates for weekend and weekdays. x charges $100
for regular customers on weekdays and $120 on weekends While it is $90
for rewardee on weekdays and $60 on weekends. Similarly y charges $130
for regular customers on weekdays and $150 on weekends. While its $100
for rewardee on weekdays and $95 on weekends. While z charges $195 for
regular customers on weekdays and $150 on weekends. While its $120 for
rewardee on weekdays and $90 on weekends. Now when the customer
requests for a particular detail you need to find which hotel would
yield the customer profit. In case of tie between hotels compare the
ratings and provide the result.
Input format:
regular: 16Mar2010(sun), 19Mar2010(wed), 21Mar2010(Fri)
Output format:
320
410
540
LakeWood
Solution:
`import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class HotelFactory {
String hotelName;
private int regularWeekDay;
private int regularWeekEnd;
private int rewardeeWeekDay;
private int rewardeeWeekEnd;
HotelFactory(String name) {
this.hotelName = name;
}
public int getRegularWeekDay() {
return regularWeekDay;
}
public void setRegularWeekDay(int regularWeekDay) {
this.regularWeekDay = regularWeekDay;
}
public int getRegularWeekEnd() {
return regularWeekEnd;
}
public void setRegularWeekEnd(int regularWeekEnd) {
this.regularWeekEnd = regularWeekEnd;
}
public int getRewa
Solution
There are a couple of issue with your code:
There's a special hell for people who do that :D
-
What if more hotels are added, say 10? What about 100 more? 1000? Your algorithm doesn't scale well, meaning you have to duplicate a lot of code for every hotel you would add. You should think of an
Ask yourself, what if there are more types of customers? More types of days in a weekend? Festival days perhaps? While duplicating code would certainly work, it becomes a maintenance nightmare. Consider a different data-structure that allows for easy lookups based on a combination of keys (the keys being customer type and day type).
HotelFactoryis not a factory, it's a domain object. It should be called simply "Hotel"
- Never ever name your variables or methods
x,yandz. Never ever name your parametersa,bandc. You should specifically never ever combine these two.
There's a special hell for people who do that :D
- Your Hotels have properties that would not change, correct? Then your Hotel objects should be immutable, meaning there are no setter methods for those properties, which should then be initialized via the constructor
static class HotelFactory {
private final String hotelName;
private final int regularWeekDay;
private final int regularWeekEnd;
private final int rewardeeWeekDay;
private final int rewardeeWeekEnd;
public HotelFactory(String hotelName, int regularWeekDay, int regularWeekEnd, int rewardeeWeekDay, int rewardeeWeekEnd) {
this.hotelName = hotelName;
this.regularWeekDay = regularWeekDay;
this.regularWeekEnd = regularWeekEnd;
this.rewardeeWeekDay = rewardeeWeekDay;
this.rewardeeWeekEnd = rewardeeWeekEnd;
}
public String getHotelName() {
return hotelName;
}
public int getRegularWeekDay() {
return regularWeekDay;
}
public int getRegularWeekEnd() {
return regularWeekEnd;
}
public int getRewardeeWeekDay() {
return rewardeeWeekDay;
}
public int getRewardeeWeekEnd() {
return rewardeeWeekEnd;
}
}
- The
minfunction doesn't actually do a min operation. It does a minPlusSomStuffs operation. That would be related to breaking ties. You should add a javadoc comment describing how the priority order works with ties. There is a Java built-in min functionMath.min(), meaning you don't have to manually perform theseifchecks. But better yet, you can let Java do the sorting for you using a TreeMap:
TreeMap sortedMap = new TreeMap();
sortedMap.put(a, x);
sortedMap.put(b, y);
sortedMap.put(c, z);
return sortedMap.firstEntry().getValue();
-
What if more hotels are added, say 10? What about 100 more? 1000? Your algorithm doesn't scale well, meaning you have to duplicate a lot of code for every hotel you would add. You should think of an
min() algorithm that takes a list of hotels. For totaling days based on weekdays and weekends, regular and non-regular, you are duplicating that logic. Ask yourself, what if there are more types of customers? More types of days in a weekend? Festival days perhaps? While duplicating code would certainly work, it becomes a maintenance nightmare. Consider a different data-structure that allows for easy lookups based on a combination of keys (the keys being customer type and day type).
Context
StackExchange Code Review Q#62262, answer score: 13
Revisions (0)
No revisions yet.