Vacation Home Handling, v2013.5.30.20.18
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.leon.hfu.vacationHomeCalculation; | |
import java.util.Arrays; | |
import java.util.Scanner; | |
import java.util.Vector; | |
import com.leon.hfu.customDate.Date; | |
import com.leon.hfu.customDate.DateFormatException; | |
/** | |
* Main class containing the whole program. | |
* | |
* @author Stefan Hahn | |
*/ | |
public final class Main { | |
/** | |
* Private constructor, not needed for utility classes | |
*/ | |
private Main() { } | |
/** | |
* Starts the program. | |
* Asks for the date of arrival and nights to sleep over. | |
* Prints various information about the three predefined | |
* vacation homes and the price for every vacation home. | |
* | |
* @param args Console parameters | |
*/ | |
public static void main(String[] args) { | |
Date arrivalDate = null; | |
int nights = 0; | |
Vector<VacationHome> vacationHomes = new Vector<>(Arrays.asList(new VacationHome[] { | |
new VacationHome("Abstellkammer", 23, 1, "Hinterhof 17", 2), | |
new VacationHome("Abendruh", 56.7, 4, "Sonnenweg 3", 4), | |
new VacationHome("Schwarzwaldpalast", 79.9, 5, "Baumallee 1", 5) | |
})); | |
try (Scanner scanner = new Scanner(System.in)) { | |
System.out.print("Gib das Datum der Ankunft ein.\n > "); | |
arrivalDate = new Date(scanner.nextLine()); | |
System.out.print("Gib die Dauer des Aufenthalts in Tagen an.\n > "); | |
nights = Integer.parseInt(scanner.nextLine()) - 1; | |
for (VacationHome vacationHome: vacationHomes) { | |
System.out.println("-------------------------------------------------"); | |
vacationHome.reserve(arrivalDate, nights); | |
vacationHome.printInformation(); | |
} | |
} | |
catch (DateFormatException | ReservationException e) { | |
System.err.println(e.getMessage()); | |
System.exit(1); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.leon.hfu.vacationHomeCalculation; | |
import java.text.NumberFormat; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.UUID; | |
import com.leon.hfu.customDate.Date; | |
/** | |
* Represents a vacation home and handles reservations on this vacation home. | |
* | |
* TODO: Add checks in calculation methods if this vacation home is reserved | |
* TODO: Add ability to reserve this vacation home multiple times if dates don't overleap | |
* TODO: Add person count handling | |
* | |
* @author Stefan Hahn | |
*/ | |
public class VacationHome { | |
/** | |
* VAT rate | |
*/ | |
public static final double VAT_RATE = 0.07; | |
private String name = ""; | |
private double pricePerNight = 0; | |
private int bedCount = 0; | |
private String address = ""; | |
private int stars = 0; | |
private Date arrival = null; | |
private Date departure = null; | |
private int nightsReserved = 0; | |
/** | |
* Creates a new vacation home object with given attributes. | |
* | |
* @param name Name of this vacation home | |
* @param pricePerNight Price for one night in this vacation home | |
* @param bedCount Amount of bed in this vacation home | |
* @param address Address of this vacation home | |
* @param stars Stars rating this vacation home received, maximum of 5 | |
*/ | |
public VacationHome(String name, double pricePerNight, int bedCount, String address, int stars) { | |
this.name = name; | |
this.pricePerNight = pricePerNight; | |
this.bedCount = bedCount; | |
this.address = address; | |
this.stars = stars; | |
} | |
/** | |
* Adds a reservation to this vacation home. | |
* | |
* @param arrival Date of arrival | |
* @param nights Amount of nights to sleep over | |
* @throws ReservationException Thrown if this vacation home is already reserved | |
*/ | |
public void reserve(Date arrival, int nights) throws ReservationException { | |
if (Date.getCurrentDate().compareTo(arrival) > 0) { | |
throw new ReservationException("Buchung in der Vergangenheit nicht möglich"); | |
} | |
if (nights < 0) { | |
throw new ReservationException("Ungültige Anzahl an Übernachtungen"); | |
} | |
if (this.nightsReserved > 0) { | |
throw new ReservationException("Belegt"); | |
} | |
this.arrival = arrival; | |
this.nightsReserved = nights; | |
this.departure = this.arrival.getFollowingDate(this.nightsReserved); | |
} | |
/** | |
* Cancels a reservation for this vacation home. | |
* | |
* @throws ReservationException Thrown if this vacation home isn't reserved anymway | |
*/ | |
public void cancel() throws ReservationException { | |
if (this.nightsReserved < 1) { | |
throw new ReservationException("Nicht belegt"); | |
} | |
this.arrival = null; | |
this.departure = null; | |
this.nightsReserved = 0; | |
} | |
/** | |
* Calculates pre tax price for reservation. | |
* | |
* @return Rounded pre tax price | |
*/ | |
public double getPreTaxPrice() { | |
return VacationHome.roundPrice(this.pricePerNight * this.nightsReserved - this.getDiscount()); | |
} | |
/** | |
* Calculates sales tax for reservation. | |
* | |
* @return Rounded sales tax | |
*/ | |
public double getSalesTax() { | |
return VacationHome.roundPrice(this.pricePerNight * VacationHome.VAT_RATE * this.nightsReserved); | |
} | |
/** | |
* Calculates after tax price for reservation. | |
* | |
* @return Rounded after tax price | |
*/ | |
public double getAfterTaxPrice() { | |
return VacationHome.roundPrice(this.getPreTaxPrice() + this.getSalesTax()); | |
} | |
/** | |
* Calculates discount for reservation. | |
* | |
* @return Rounded discount | |
*/ | |
public double getDiscount() { | |
return VacationHome.roundPrice(this.getEarlyBirdDiscount() + this.getQuantityDiscount()); | |
} | |
/** | |
* Calculates early bird discount for reservation. | |
* | |
* @return Rounded early bird discount | |
*/ | |
public double getEarlyBirdDiscount() { | |
double discountRate = 0; | |
int daysTillArrival = Date.getCurrentDate().delta(this.arrival); | |
if (daysTillArrival >= 180) { | |
discountRate = 0.1; | |
} | |
else if (daysTillArrival >= 90) { | |
discountRate = 0.05; | |
} | |
return VacationHome.roundPrice(this.pricePerNight * this.nightsReserved * discountRate); | |
} | |
/** | |
* Calculates quantity discount for reservation. | |
* | |
* @return Rounded quantity discount | |
*/ | |
public double getQuantityDiscount() { | |
return VacationHome.roundPrice(this.pricePerNight * this.nightsReserved - this.getQuantityDiscountedPrice(this.nightsReserved)); | |
} | |
/** | |
* Calculates quanity discounted price for reservation. | |
* | |
* @param n Recursion parameter, must be amount of nights in initial call | |
* @return Quantity discounted price | |
*/ | |
private double getQuantityDiscountedPrice(int n) { | |
if (n <= 1) { | |
return this.pricePerNight; | |
} | |
else { | |
return (1 - 0.12 / (n - 1)) * (this.pricePerNight + this.getQuantityDiscountedPrice(n - 1)); | |
} | |
} | |
/** | |
* Prints various information about this vacation home on stdout. | |
* If reserved, prints information about this reservation, too. | |
*/ | |
public void printInformation() { | |
System.out.println("Der Name dieser Ferienwohnung lautet »" + this.name + "«."); | |
System.out.println("Adresse: " + this.address); | |
System.out.println("Sie bietet Platz für " + this.bedCount + " " + ((this.bedCount != 1) ? "Personen" : "Person") + "."); | |
System.out.println("Die Ferienwohnung wurde mit " + this.stars + " Sternen bewertet."); | |
System.out.println("Der Preis für eine Übernachtung liegt bei " + VacationHome.formatPrice(this.pricePerNight) + "."); | |
if (this.nightsReserved > 0) { | |
double earlyBirdDiscount = this.getEarlyBirdDiscount(); | |
double quantityDiscount = this.getQuantityDiscount(); | |
String preTaxpriceNoDiscounts = VacationHome.formatPrice(this.pricePerNight * this.nightsReserved); | |
String preTaxPrice = VacationHome.formatPrice(this.getPreTaxPrice()); | |
String salesTax = VacationHome.formatPrice(this.getSalesTax()); | |
String afterTaxPrice = VacationHome.formatPrice(this.getAfterTaxPrice()); | |
System.out.println("Die Ferienwohnung ist vom " + this.arrival + " bis zum " + this.departure + " belegt."); | |
System.out.println("Bis zur Ankunft sind es noch " + Date.getCurrentDate().delta(this.arrival) + " Tage."); | |
System.out.println("Die Anzahl der zu bezahlenden Nächte beträgt " + this.nightsReserved + " Stück."); | |
if (earlyBirdDiscount > 0) { | |
System.out.println("Für diese Buchung gibt es zum jetztigen Zeitpunkt einen Frühbucherrabatt in Höhe von " + VacationHome.formatPrice(earlyBirdDiscount)); | |
} | |
if (quantityDiscount > 0) { | |
System.out.println("Für dise Buchung gibt es einen Mengenrabatt in Höhe von " + VacationHome.formatPrice(quantityDiscount)); | |
} | |
System.out.println("Der Preis für diese Buchung ohne Steuern und Rabatte beträgt " + preTaxpriceNoDiscounts + "."); | |
System.out.println("Der Preis für diese Buchung ohne Steuern inkl. Rabatte beträgt " + preTaxPrice + "."); | |
System.out.println("Die Höhe der Mehrwertsteuer beträgt " + salesTax + "."); | |
System.out.println("Der Gesamtpreis inkl. Steuern beträgt " + afterTaxPrice + "."); | |
} | |
} | |
/** | |
* Returns the name of this vacation home | |
* | |
* @return Name of this vacation home | |
*/ | |
public String getName() { | |
return this.name; | |
} | |
/** | |
* Sets the name of this vacation home. | |
* | |
* @param name New name of this vacation home | |
*/ | |
public void setName(String name) { | |
this.name = name; | |
} | |
/** | |
* Returns the price per night of this vacation home. | |
* | |
* @return Price per night of this vacation home | |
*/ | |
public double getPricePerNight() { | |
return this.pricePerNight; | |
} | |
/** | |
* Sets the price per night of this vacation home. | |
* | |
* @param pricePerNight New price per night of this vacation home | |
*/ | |
public void setPricePerNight(double pricePerNight) { | |
this.pricePerNight = pricePerNight; | |
} | |
/** | |
* Returns the amount of beds in this vacation home. | |
* | |
* @return Amount of beds in this vacation home | |
*/ | |
public int getBedCount() { | |
return this.bedCount; | |
} | |
/** | |
* Sets the amount of beds in this vacation home. | |
* | |
* @param bedCount New amount of beds in this vacation home | |
*/ | |
public void setBedCount(int bedCount) { | |
this.bedCount = bedCount; | |
} | |
/** | |
* returns the address of this vacation home. | |
* | |
* @return Address of this vacation home | |
*/ | |
public String getAddress() { | |
return this.address; | |
} | |
/** | |
* Sets the address of this vacation home. | |
* | |
* @param address New address of this vacation home | |
*/ | |
public void setAddress(String address) { | |
this.address = address; | |
} | |
/** | |
* Returns the amount of stars of this vacation home. | |
* | |
* @return Amount of stars of this vacation home | |
*/ | |
public int getStars() { | |
return this.stars; | |
} | |
/** | |
* Sets the amount of stars of this vacation home. | |
* | |
* @param stars New amount of stars of this vacation home | |
*/ | |
public void setStars(int stars) { | |
this.stars = stars; | |
} | |
/** | |
* @see Object#equals(Object) | |
*/ | |
@Override | |
public boolean equals(Object obj) { | |
if ((null == obj) || (obj.getClass() != VacationHome.class)) { | |
return false; | |
} | |
return this.toString().toLowerCase().equals(((VacationHome) obj).toString().toLowerCase()); | |
} | |
/** | |
* @see Object#toString() | |
*/ | |
@Override | |
public String toString() { | |
StringBuilder s = new StringBuilder(); | |
s.append(this.getName()); | |
s.append(" - "); | |
s.append(this.getAddress()); | |
s.append(" - "); | |
s.append(this.getStars()); | |
s.append(" Sterne - "); | |
s.append(this.getBedCount()); | |
s.append(" Betten - "); | |
s.append(VacationHome.formatPrice(this.getPricePerNight())); | |
s.append(" pro Nacht"); | |
return s.toString(); | |
} | |
/** | |
* Rounds a double to two decimal places. | |
* | |
* @param price Any double number | |
* @return price rounded to two decimal places | |
*/ | |
public static double roundPrice(double price) { | |
return ((double) Math.round(price * 100)) / 100; | |
} | |
/** | |
* Formats a double as price | |
* @param price Any double number | |
* @return Formatted price | |
*/ | |
public static String formatPrice(double price) { | |
return NumberFormat.getCurrencyInstance().format(price); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment