In: Computer Science
Java homework problem. This is my hotel reservation system. I'm trying to add a few things to it.
You will be changing your Hotel Reservation system to allow a user to serve more rooms and the rooms will be created as objects.
For this project you will be modifying the Hotel Reservation system to allow a user to serve more rooms and the rooms will be created as objects.
You will be create a Room object that will allow the user to set the type of room, if they want pets, and if they want Oceanview.
OV is $50 more
Pets $25 more
King, Suite and Queen style rooms and you can set the prices
You will have an array for username and password that hold 3 userNames and 3 passwords. These will be parallel arrays. I am allowed to enter username and password 3 times and then I get kicked out.
Main
The main method will keep track of information for 5 room reservations objects all for 1 night
Be sure to use looping, somewhere in the main java file.
Create a method that will catch the object and create it. Remember you can pass by reference or return the object back to the main.
Create a method to handle printing out each of the objects to the screen and the total for each room. (You can set this total in the Room Class if you wish.)
Finally Create a method that will show out the grand total.
Here is my original code to be modified:
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class hotelreservation {
public static double
roomSelection(Scanner scan)
{
String roomSelection;
System.out.print("Please enter
the type of room desired for your stay (King/Queen/Suite/Two
doubles): ");
roomSelection =
scan.nextLine();
if(roomSelection.equalsIgnoreCase("Suite"))
return
275;
else
if(roomSelection.equalsIgnoreCase("Queen"))
return
150;
else
if(roomSelection.equalsIgnoreCase("King"))
return
150;
else
return
125;
}
public static double roomOceanView(Scanner
scan)
{
String response;
System.out.print("Would you
like an oceanview room (Yes/No) ? ");
response =
scan.nextLine();
if(response.equalsIgnoreCase("Yes"))
return
45;
else
return
0;
}
public static double roomPets(Scanner scan)
{
String response;
System.out.print("Do you have
any pets (Yes/No) ? ");
response =
scan.nextLine();
if(response.equalsIgnoreCase("Yes"))
return
50;
else
return
0;
}
public static void main(String[] args) {
DecimalFormat decFormat = new
DecimalFormat("0.00");
Scanner scan = new
Scanner(System.in);
double roomReservation[] = new
double[3];
double subTotal = 0;
for(int x=0;x
{
System.out.println("Welcome to our Hotel Room Reservation Pricing
System.");
System.out.println("Please answer the following questions regarding
your reservation of room #"+(x+1)+" : ");
roomReservation[x] = roomPets(scan);
roomReservation[x] += roomSelection(scan);
roomReservation[x] += roomOceanView(scan);
}
for(int x=0;x
{
System.out.println("Total cost for room #"+(x+1)+" :
$"+decFormat.format(roomReservation[x]));
subTotal += roomReservation[x];
}
System.out.println("Subtotal:
$"+decFormat.format(subTotal));
double tax =
subTotal*0.05;
System.out.println("Total Tax
(5%): $"+decFormat.format(tax));
System.out.println("Grand
Total: $"+(decFormat.format(subTotal+tax)));
scan.close();
}
}
/*
* The java program that prompts user to enter user name and
password that
* are stored in parallel arrays. If user name and password matched
then
* print login successful . Allow user to enter 3 attempt otherwise
terminate the program.
* Then prompts user to enter the room type, yes /no
* for the ocean view and yes/ no for the pets for the Room class
objects.
* Then find the the sub total, tax and net price of the total room
objects.
* */
//Hotelreservation.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class Hotelreservation
{
public static void main(String[] args)
{
DecimalFormat decFormat = new
DecimalFormat("0.00");
Scanner scan = new
Scanner(System.in);
double subTotal = 0;
/*Create an array of
Room class of size,5*/
Room[] rooms=new Room[5];
String roomType;
String oceanViewChoice;
String petChoice;
boolean
oceanChoice=false;
boolean havePets=false;
final int MAX=3;
boolean
login_status=false;
String userNames[]=
{"john","kumar","samantha"};
String passwords[]=
{"1234","abcd","xyz"};
int count=0;
while(count<MAX &&
!login_status)
{
System.out.println("Enter user name : ");
String
userName=scan.nextLine();
int
userNameLocation=-1;
for (int i = 0;
i <MAX; i++)
{
if(userNames[i].equalsIgnoreCase(userName))
userNameLocation=i;
}
if(userNameLocation!=-1)
{
System.out.println("Enter password: ");
String password=scan.nextLine();
if(password.equalsIgnoreCase(passwords[userNameLocation]))
login_status=true;
}
else
System.out.println("Invalid user name");
count++;
}
if(login_status==false
&& count==MAX)
{
System.out.println("Login failed");
System.exit(0);
}
else
System.out.println("Login successful");
System.out.println("Welcome to our Hotel Room Reservation Pricing
System.");
//read 5 room choice
values
for(int
x=0;x<rooms.length;x++)
{
System.out.print("Please enter the type of room desired for your
stay (King/Queen/Suite/Two doubles): ");
roomType =
scan.nextLine();
System.out.print("Do you want ocean view(yes /no): ?");
oceanViewChoice
= scan.nextLine();
//checking if ocean choice is yes then set ocaenChoice is
true
if(oceanViewChoice.equalsIgnoreCase("yes"))
oceanChoice=true;
System.out.print("Do you have pet(yes /no): ?");
petChoice =
scan.nextLine();
//checking if petChoice is yes then set petChoice is
true
if(petChoice.equalsIgnoreCase("yes"))
havePets=true;
//create
an object of room class with user given values
rooms[x]=new
Room(roomType, oceanChoice, havePets);
}
//find the sub total of
rooms
for(int
x=0;x<rooms.length;x++)
{
subTotal+=rooms[x].getRoomCost()+rooms[x].getOceanViewCost()+
rooms[x].getPetCharge();
}
//print the results
to console window
System.out.println("Subtotal:
$"+decFormat.format(subTotal));
double tax = subTotal*0.05;
System.out.println("Total Tax (5%):
$"+decFormat.format(tax));
System.out.println("Grand Total:
$"+(decFormat.format(subTotal+tax)));
scan.close();
}
}
-----------------------------------------------------------------------------------------------------------------------
//Room class declaration
//Room.java
public class Room
{
//private data members
private double roomCost;
private double oceanViewCost;
private double petsCharge;
/*constructor */
Room(String roomType , boolean oceanView , boolean
havePets)
{
setSelection(roomType);
setOceanView(oceanView);
setPets(havePets);
}
public void setPets(boolean havePets)
{
if(havePets)
petsCharge=
50;
else
petsCharge=
0;
}
public void setOceanView(boolean
oceanView)
{
if(oceanView)
oceanViewCost=
45;
else
oceanViewCost=
0;
}
public void setSelection(String roomType)
{
if(roomType.equalsIgnoreCase("Suite"))
roomCost=
275;
else
if(roomType.equalsIgnoreCase("Queen"))
roomCost=
150;
else
if(roomType.equalsIgnoreCase("King"))
roomCost=
150;
else
roomCost=
125;
}
public double getRoomCost()
{
return roomCost;
}
public double getOceanViewCost()
{
return oceanViewCost;
}
public double getPetCharge()
{
return petsCharge;
}
}
-----------------------------------------------------------------------------------------------------------------------
Sample Output:
Enter user name :
john
Enter password:
sdfsf
Enter user name :
john
Enter password:
sdfsdf
Enter user name :
john
Enter password:
1234
Login successful
Welcome to our Hotel Room Reservation Pricing System.
Please enter the type of room desired for your stay
(King/Queen/Suite/Two doubles): King
Do you want ocean view(yes /no): ?yes
Do you have pet(yes /no): ?yes
Please enter the type of room desired for your stay
(King/Queen/Suite/Two doubles): Queen
Do you want ocean view(yes /no): ?yes
Do you have pet(yes /no): ?yes
Please enter the type of room desired for your stay
(King/Queen/Suite/Two doubles): Suite
Do you want ocean view(yes /no): ?yes
Do you have pet(yes /no): ?yes
Please enter the type of room desired for your stay
(King/Queen/Suite/Two doubles): King
Do you want ocean view(yes /no): ?yes
Do you have pet(yes /no): ?yes
Please enter the type of room desired for your stay
(King/Queen/Suite/Two doubles): King
Do you want ocean view(yes /no): ?yes
Do you have pet(yes /no): ?yes
Subtotal: $1350.00
Total Tax (5%): $67.50
Grand Total: $1417.50