Advertisement
desislava_topuzakova

4. Hotel Reservation

Oct 25th, 2022
1,238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. Main.java
  2. package hotel;
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         //"{pricePerDay} {numberOfDays} {season} {discountType}"
  10.         String input = scanner.nextLine();
  11.         String [] splittedInput = input.split("\\s+");
  12.  
  13.         //["{pricePerDay}", "{numberOfDays}", "{season}", "{discountType}"]
  14.         double pricePerDay = Double.parseDouble(splittedInput[0]);
  15.         int numberOfDays = Integer.parseInt(splittedInput[1]);
  16.         Season season = Season.valueOf(splittedInput[2].toUpperCase()); //"Summer" -> "SUMMER"
  17.         DiscountType discountType = DiscountType.valueOf(splittedInput[3].toUpperCase()); //"SecondVisit" -> "SECONDVISIT"
  18.  
  19.         double holidayPrice = PriceCalculator.calculateHolidayPrice(pricePerDay, numberOfDays, season, discountType);
  20.  
  21.         System.out.printf("%.2f", holidayPrice);
  22.  
  23.     }
  24. }
  25.  
  26. PriceCalculator.java
  27. package hotel;
  28.  
  29. public class PriceCalculator {
  30.  
  31.     public static double calculateHolidayPrice (double pricePerDay, int days, Season season, DiscountType discountType) {
  32.         //сума за всички дни
  33.         double priceForAllDays = pricePerDay * days;
  34.         //умножение спрямо сезона
  35.         priceForAllDays *= season.getMultiplyCoefficient();
  36.         //отстъпка
  37.         priceForAllDays = priceForAllDays - priceForAllDays * (discountType.getPercent() / 100);
  38.  
  39.         return priceForAllDays;
  40.     }
  41. }
  42.  
  43.  
  44. Season.java
  45. package hotel;
  46.  
  47. public enum Season {
  48.     //4 сезона -> 4 константни обекта
  49.     SUMMER(4),
  50.     AUTUMN(1),
  51.     WINTER(3),
  52.     SPRING(2);
  53.  
  54.     private int multiplyCoefficient;
  55.  
  56.     Season(int multiplyCoefficient) {
  57.         this.multiplyCoefficient = multiplyCoefficient;
  58.     }
  59.  
  60.  
  61.     public int getMultiplyCoefficient() {
  62.         return multiplyCoefficient;
  63.     }
  64.  
  65.     public void setMultiplyCoefficient(int multiplyCoefficient) {
  66.         this.multiplyCoefficient = multiplyCoefficient;
  67.     }
  68. }
  69.  
  70.  
  71. DiscountType.java
  72. package hotel;
  73.  
  74. public enum DiscountType {
  75.     NONE(0),
  76.     SECONDVISIT(10),
  77.     VIP(20);
  78.  
  79.     private double percent;
  80.  
  81.     DiscountType(double percent) {
  82.         this.percent = percent;
  83.     }
  84.  
  85.  
  86.     public double getPercent() {
  87.         return percent;
  88.     }
  89.  
  90.     public void setPercent(double percent) {
  91.         this.percent = percent;
  92.     }
  93. }
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement