Advertisement
Filip_Markoski

1.Intro.2 Trip (Solved)

Oct 16th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class MyException extends Exception {
  4.     private String msg;
  5.  
  6.     public MyException(String msg) {
  7.         this.msg = msg;
  8.     }
  9.  
  10.     public MyException() {
  11.     }
  12.  
  13.     @Override
  14.     public String toString() {
  15.         return "Exception\n";
  16.     }
  17. }
  18.  
  19. class Trip {
  20.     protected String name;
  21.     protected int price;
  22.  
  23.     public Trip(String name, int price) {
  24.         this.name = name;
  25.         this.price = price;
  26.     }
  27.  
  28.     public String getName() {
  29.         return name;
  30.     }
  31.  
  32.     public void setName(String name) {
  33.         this.name = name;
  34.     }
  35.  
  36.     public int getPrice() {
  37.         return price;
  38.     }
  39.  
  40.     public void setPrice(int price) {
  41.         this.price = price;
  42.     }
  43.  
  44.     public int returnTimeInDays() {
  45.         return 0;
  46.     }
  47.  
  48.     @Override
  49.     public String toString() {
  50.         return name;
  51.     };
  52.  
  53.     static public int minPrice(Trip[] trips, Trip holiday) {
  54.         int min = 1000000;
  55.         for (Trip trip : trips) {
  56.             if (holiday.returnTimeInDays() < trip.returnTimeInDays()) {
  57.                 if (trip.getPrice() < min) {
  58.                     min = trip.getPrice();
  59.                 }
  60.             }
  61.             // System.out.println(holiday.getPrice() + " " + trip.getPrice() + " " + min);
  62.         }
  63.         return min;
  64.     }
  65.  
  66. }
  67.  
  68. class Vacation extends Trip {
  69.     private int days;
  70.  
  71.     public Vacation(String name, int price, int days) {
  72.         super(name, price - 1000);
  73.         this.days = days;
  74.     }
  75.  
  76.     public int getDays() {
  77.         return days;
  78.     }
  79.  
  80.     public void setDays(int days) {
  81.         this.days = days;
  82.     }
  83.  
  84.     public int returnTimeInDays() {
  85.         return days;
  86.     }
  87.  
  88. }
  89.  
  90. class FestiveTrip extends Trip {
  91.     private int startDay, startMonth, endDay, endMonth;
  92.  
  93.     public FestiveTrip(String name, int price, int startDay, int startMonth, int endDay, int endMonth) {
  94.         super(name, price);
  95.         try {
  96.             if (startDay + startMonth * 30 > endDay + endMonth * 30) {
  97.                 throw new Exception("Exception");
  98.             }
  99.         } catch (Exception e) {
  100.             String msg = e.getMessage();
  101.             System.out.println(msg);
  102.             int temp = startDay;
  103.             startDay = endDay;
  104.             endDay = temp;
  105.  
  106.             temp = startMonth;
  107.             startMonth = endMonth;
  108.             endMonth = temp;
  109.         }
  110.         this.startDay = startDay;
  111.         this.startMonth = startMonth;
  112.         this.endDay = endDay;
  113.         this.endMonth = endMonth;
  114.     }
  115.  
  116.     public int returnTimeInDays() {
  117.         return (endDay + endMonth * 30) - (startDay + startMonth * 30);
  118.     }
  119.  
  120.     public int getStartDay() {
  121.         return startDay;
  122.     }
  123.  
  124.     public void setStartDay(int startDay) {
  125.         this.startDay = startDay;
  126.     }
  127.  
  128.     public int getStartMonth() {
  129.         return startMonth;
  130.     }
  131.  
  132.     public void setStartMonth(int startMonth) {
  133.         this.startMonth = startMonth;
  134.     }
  135.  
  136.     public int getEndDay() {
  137.         return endDay;
  138.     }
  139.  
  140.     public void setEndDay(int endDay) {
  141.         this.endDay = endDay;
  142.     }
  143.  
  144.     public int getEndMonth() {
  145.         return endMonth;
  146.     }
  147.  
  148.     public void setEndMonth(int endMonth) {
  149.         this.endMonth = endMonth;
  150.     };
  151.  
  152. }
  153.  
  154. public class Test {
  155.     public static void main(String[] args) {
  156.         int n;
  157.         Scanner in = new Scanner(System.in);
  158.         n = in.nextInt();
  159.  
  160.         Trip trips[] = new Trip[n];
  161.  
  162.         for (int i = 0; i < n; i++) {
  163.             int tip = in.nextInt();
  164.             if (tip == 0) {
  165.                 String ime = in.next();
  166.                 int cena = in.nextInt();
  167.                 int vreme = in.nextInt();
  168.                 trips[i] = new Vacation(ime, cena, vreme);
  169.             } else {
  170.                 String ime = in.next();
  171.                 int cena = in.nextInt();
  172.                 int pocD = in.nextInt();
  173.                 int pocM = in.nextInt();
  174.                 int krajD = in.nextInt();
  175.                 int krajM = in.nextInt();
  176.                 trips[i] = new FestiveTrip(ime, cena, pocD, pocM, krajD, krajM);
  177.  
  178.             }
  179.         }
  180.  
  181.         // решение на барање 1
  182.         StringBuffer sb = new StringBuffer();
  183.         for (Trip trip : trips) {
  184.             if (trip instanceof FestiveTrip) {
  185.                 //sb.append(" instanceof " + ((FestiveTrip) trip).getStartMonth());
  186.                 if (((FestiveTrip) trip).getStartMonth() == 6) {
  187.                     sb.append(trip.toString());
  188.                     sb.append(" ");
  189.                 }
  190.             }
  191.         }
  192.         System.out.println(sb.toString());
  193.  
  194.         sb.delete(0, sb.length());
  195.         // решение на барање 2
  196.         double sum = 0;
  197.         for (Trip trip : trips) {
  198.             /*
  199.             System.out.println(trip.toString() +" "+ trip.returnTimeInDays());
  200.             if (trip instanceof FestiveTrip) {
  201.                 System.out.println(((FestiveTrip) trip).getStartMonth() +" | "+((FestiveTrip) trip).getEndMonth());
  202.             }
  203.             */
  204.             sum += trip.returnTimeInDays();
  205.         }
  206.         double average = sum / trips.length;
  207.         System.out.println(average);
  208.  
  209.         // решение на барање 3
  210.         String nameVacation = in.next();
  211.         int priceVacation = in.nextInt();
  212.         int daysVacation = in.nextInt();
  213.         Vacation holiday = new Vacation(nameVacation, priceVacation, daysVacation);
  214.  
  215.         // решение на барање 4
  216.         System.out.println(Trip.minPrice(trips, holiday));
  217.  
  218.     }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement