veronikaaa86

SoftUni Coffee Orders

Mar 1st, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. package exams.june2016;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.math.BigDecimal;
  7. import java.util.Arrays;
  8. import java.util.Calendar;
  9. import java.util.GregorianCalendar;
  10.  
  11. public class P02_SoftUniCoffeeOrders {
  12.     public static void main(String[] args) throws IOException {
  13.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  14.  
  15.         int n = Integer.parseInt(reader.readLine());
  16.  
  17.         //double allPrices = 0;
  18.         BigDecimal allPrices = new BigDecimal("0.0");
  19.         for (int i = 0; i < n; i++) {
  20.             BigDecimal priceCoffee = new BigDecimal(reader.readLine());
  21.             int[] date = Arrays.stream(reader.readLine().split("/"))
  22.                     .mapToInt(Integer::parseInt)
  23.                     .toArray();
  24.             long countCoffee = Long.parseLong(reader.readLine());
  25.  
  26.             int day = date[0];
  27. //            int month = date[1] - 1;
  28.             int month = date[1];
  29.             int year = date[2];
  30.  
  31. //            Calendar myCal = new GregorianCalendar(year, month, day);
  32. //            int daysInMonth = myCal.getActualMaximum(Calendar.DAY_OF_MONTH);
  33.             int daysInMonth = getDays(month, year);
  34.             //double priceMonthCoffee = (daysInMonth * countCoffee) * priceCoffee;
  35.             long coffeeForMonth = daysInMonth * countCoffee;
  36.             BigDecimal coffeeForMonthBig = new BigDecimal(coffeeForMonth);
  37.             BigDecimal priceMonthCoffee = coffeeForMonthBig.multiply(priceCoffee);
  38.             System.out.printf("The price for the coffee is: $%.2f%n", priceMonthCoffee);
  39.             //allPrices = allPrices + priceMonthCoffee;
  40.             allPrices = allPrices.add(priceMonthCoffee);
  41.  
  42.         }
  43.         System.out.printf("Total: $%.2f", allPrices);
  44.     }
  45.     public static int getDays (int month, int year) {
  46.  
  47.         int numberOfDays = 0;
  48.         switch (month) {
  49.             case 1:
  50.                 numberOfDays = 31;
  51.                 break;
  52.             case 2:
  53.                 if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
  54.                     numberOfDays = 29;
  55.                 } else {
  56.                     numberOfDays = 28;
  57.                 }
  58.                 break;
  59.             case 3:
  60.                 numberOfDays = 31;
  61.                 break;
  62.             case 4:
  63.                 numberOfDays = 30;
  64.                 break;
  65.             case 5:
  66.                 numberOfDays = 31;
  67.                 break;
  68.             case 6:
  69.                 numberOfDays = 30;
  70.                 break;
  71.             case 7:
  72.                 numberOfDays = 31;
  73.                 break;
  74.             case 8:
  75.                 numberOfDays = 31;
  76.                 break;
  77.             case 9:
  78.                 numberOfDays = 30;
  79.                 break;
  80.             case 10:
  81.                 numberOfDays = 31;
  82.                 break;
  83.             case 11:
  84.                 numberOfDays = 30;
  85.                 break;
  86.             case 12:
  87.                 numberOfDays = 31;
  88.                 break;
  89.         }
  90.         return numberOfDays;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment