Skip to content

Instantly share code, notes, and snippets.

@jinx1281255
Created December 21, 2020 17:37
Show Gist options
  • Save jinx1281255/6d19303f16646c084d22ab9c871621a1 to your computer and use it in GitHub Desktop.
Save jinx1281255/6d19303f16646c084d22ab9c871621a1 to your computer and use it in GitHub Desktop.
Hoteless - Booking Tool - Java
# Default ignored files
/shelf/
/workspace.xml
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<ScalaCodeStyleSettings>
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
</ScalaCodeStyleSettings>
</code_scheme>
</component>
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/HoteLess.iml" filepath="$PROJECT_DIR$/HoteLess.iml" />
</modules>
</component>
</project>
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="783bf269-2ee7-4344-ab50-ccd793109ee4" name="Default Changelist" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="CodeStyleSettingsInfer">
<option name="done" value="true" />
</component>
<component name="ProjectCodeStyleSettingsMigration">
<option name="version" value="1" />
</component>
<component name="ProjectId" id="1lyQqkBJfLvuoF8SrJ5JtC235ix" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application" temporary="true">
<option name="MAIN_CLASS_NAME" value="com.company.Main" />
<module name="HoteLess" />
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$" />
<RunnerSettings RunnerId="Run" />
<ConfigurationWrapper RunnerId="Run" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Main" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="783bf269-2ee7-4344-ab50-ccd793109ee4" name="Default Changelist" comment="" />
<created>1608567046643</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1608567046643</updated>
</task>
<servers />
</component>
<component name="WindowStateProjectService">
<state width="1641" height="332" key="GridCell.Tab.0.bottom" timestamp="1608567775845">
<screen x="-525" y="-2160" width="3840" height="2120" />
</state>
<state width="1641" height="332" key="GridCell.Tab.0.bottom/0.0.1920.1040/-525.-2160.3840.2120@-525.-2160.3840.2120" timestamp="1608567775845" />
<state width="1641" height="332" key="GridCell.Tab.0.center" timestamp="1608567775845">
<screen x="-525" y="-2160" width="3840" height="2120" />
</state>
<state width="1641" height="332" key="GridCell.Tab.0.center/0.0.1920.1040/-525.-2160.3840.2120@-525.-2160.3840.2120" timestamp="1608567775845" />
<state width="1641" height="332" key="GridCell.Tab.0.left" timestamp="1608567775845">
<screen x="-525" y="-2160" width="3840" height="2120" />
</state>
<state width="1641" height="332" key="GridCell.Tab.0.left/0.0.1920.1040/-525.-2160.3840.2120@-525.-2160.3840.2120" timestamp="1608567775845" />
<state width="1641" height="332" key="GridCell.Tab.0.right" timestamp="1608567775845">
<screen x="-525" y="-2160" width="3840" height="2120" />
</state>
<state width="1641" height="332" key="GridCell.Tab.0.right/0.0.1920.1040/-525.-2160.3840.2120@-525.-2160.3840.2120" timestamp="1608567775845" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
package com.company;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
bookRoom();
}
public static void bookRoom() {
Double bedCost;
String bedType;
Scanner s = new Scanner(System.in);
// create room types hashmap <associated with roomCost hashmap by key value>
Map<Integer, String> roomTypes = new HashMap<>();
roomTypes.put(1, "King");
roomTypes.put(2, "Double Queen");
roomTypes.put(3, "Single Queen");
roomTypes.put(4, "Full");
// create room cost hashmap <associated with roomTypes hashmap by key value>
Map<Integer, Double> roomCost = new HashMap<>();
roomCost.put(1,159.99);
roomCost.put(2,139.99);
roomCost.put(3,129.99);
roomCost.put(4,109.99);
System.out.println();
System.out.println("Welcome to our room booking tool! Here is our current availability:\n");
// print hashmap of room types
for (Integer i : roomTypes.keySet()) {
System.out.printf("%s - %s\n", i, roomTypes.get(i));
}
System.out.println();
System.out.println("Please enter a number corresponding to your preferred room type:");
// user input : request room preference <pulls from hashmap for room type and price>
int preferredRoom = s.nextInt();
while (preferredRoom < 1 || preferredRoom > 4)
{
System.out.println("Your entry " + preferredRoom + " is invalid. Please choose a number between 1 and " +
roomTypes.size() + " which corresponds with your desired room configuration.");
preferredRoom = s.nextInt();
}
// set room roomCost value from hashmap based on preferredRoom value
bedCost = roomCost.get(preferredRoom);
// set room roomType value from hashmap based on preferredRoom value
bedType = roomTypes.get(preferredRoom);
// print cost of room per night
System.out.printf("For the %s room it will cost $%s per night.\n" +
"How many nights would you like to stay?\n", bedType.toLowerCase(),bedCost);
// user input : ask for length of stay
int nights = s.nextInt();
s.nextLine();
System.out.println("Enter your desired check-in date in mm/dd/yy format:");
DateTimeFormatter format = DateTimeFormatter.ofPattern("M/d/yy");
LocalDate checkIn = null;
// create boolean for try/catch block to handle parsing exceptions
boolean parseError;
do {
try {
// user input : request check-in date in mm/dd/yy format
parseError = false;
checkIn = LocalDate.parse(s.nextLine(), format);
} catch (Exception e) {
// if incorrect format, repeat request for proper date format
parseError = true;
System.out.println("Your entry is invalid. Please enter a valid date in mm/dd/yy format:");
}
} while (parseError);
// compute checkOut date
LocalDate checkOut = IncrementDate(checkIn, nights);
// calculate total cost of stay based on bedCost and # of nights
double total = bedCost * nights;
// print out details of reservation
System.out.printf("Here are the details of your reservation:\n" +
"Room Type: %s\n" +
"Nights: %s\n" +
"Cost Per Night: %s\n" +
"Check-in: %s\n" +
"Checkout: %s\n" +
"Total Cost: $%.2f\n\n" +
"Type Y to confirm your reservation or N to cancel:\n",bedType.toLowerCase(),nights,bedCost,checkIn,checkOut,total);
// user input : confirm reservation details
String confirmation = s.nextLine();
// validate "Y" or "N" response to confirmation
while (!confirmation.equalsIgnoreCase("N") && !confirmation.equalsIgnoreCase("Y"))
{
System.out.println("Your entry of " + confirmation + " is not valid. Please enter Y or N:");
confirmation = s.nextLine();
}
if (confirmation.equalsIgnoreCase("N")) {
System.out.println("Your reservation has been cancelled.");
} else {
// once confirmed, create new reservation object using Reservation method
Reservation newReservation = new Reservation(new Date(),nights,checkIn,checkOut,bedCost,bedType,total);
System.out.println("Your reservation has been confirmed!");
// print reservation confirmation including reservation id
newReservation.show();
}
}
// method: increment dates for check-out calculation
public static LocalDate IncrementDate(LocalDate initialDate, int daysToIncrement) {
LocalDate incrementedDate = initialDate.plusDays(daysToIncrement);
return incrementedDate;
}
static class Reservation {
int id;
Date made;
int stayDays;
double dailyRate;
LocalDate in;
LocalDate out;
String bedType;
double total;
static int totalReservations;
Reservation(Date m, int s, LocalDate i, LocalDate o, double d, String b, double t) {
id = totalReservations + 10000;
made = m;
stayDays = s;
in = i;
out = o;
dailyRate = d;
bedType = b;
total = t;
totalReservations++;
}
void show() {
System.out.printf(
" - Your confirmation number is %s.\n" +
" - We've reserved a %s room for %d days starting on %s and ending on %s\n" +
" - Based on your daily rate of %s, the total cost of your stay will be $%.2f \n",
id, bedType, stayDays, in, out, dailyRate,total);
}
static void showReservations() {
System.out.println(totalReservations);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment