Advertisement
StefanTodorovski

[ADS/АПС] Intro 2: Trip / Патување

Oct 23rd, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.80 KB | None | 0 0
  1. /*
  2. Define Java class Trip. The attributes for this class are: name of the agency that organize the trip (String) and price of the arrangement (int).
  3.  
  4. For this class implement:
  5. - appropriate constructor set and get methods
  6. - A duration of each trip can be calculated by the method: int vratiVremeVoDenovi();.
  7.  
  8. A trip can be either a festive trip or a vacation. Model classes FestiveTrip and Vacation. Moreover, besides the name and price for every vacation, store data for the duration of the trip in days (int), while for every festive trip store data for start day (int) and month (int) and end day (int) and month (int) of the trip. On the bases of these data the number of days (taken that all months have 30 days) can be determined.
  9.  
  10. For the classes FestiveTrip and Vacation implement:
  11. - appropriate constructors set and get methods appropriate methods
  12.  
  13. In addition, take into account the real price for the vacation is 1,000 denars cheaper. These (1,000 denars) are paid by the state.
  14.  
  15. Exceptions: Festive trips are conducted in the same year, and the start day should precede the calendar end day of the trip. If you attempt to instantiate an object of the FestiveTrip class where it is not fulfilled, it is necessary to throw a general exception Exception. It should be caught in the constructor where it is thrown. Deal with it so that you will replace the values for the start day and month with the end day and month of the trip. Thus if the exception is caught to be printed in a new line message on the screen: Exeption.
  16.  
  17. In the main method of the Test class created is an array of n variables of type Trip. Since from the standard input is entered information on the elements of the array, your task is to implement the following requirements:
  18. (Requirement 1 ) On a standard output print the names of all the agencies that offer festive trips that start in June, (in a row and separated by a space).
  19. (Requirement 2 ) On a standard output print the average duration of all trips expressed in days.
  20. (Requirement 3 ) From a standard input read information about a vacation (name, price, duration). Create a variable holiday of type Vacation which referenced to an object of type (Vacation) created with information from the input.
  21. (Requirement 4 ) On a standard output print minimum price of the trip from those trips which duration is longer than vacation that is loaded from the input (holiday). While for it, use the method int minPrice(Trip[] niza, int n, Trip holiday);This method is a static into the Trip class.
  22. */
  23.  
  24. import java.util.Scanner;
  25.  
  26. class MyException extends Exception {
  27.     private String msg;
  28.    
  29.     public MyException()  { }
  30.    
  31.     public MyException(String msg) {
  32.         this.msg = msg;
  33.     }
  34.    
  35.     @Override
  36.     public String toString() {
  37.         return "Exception\n";
  38.     }
  39.  
  40. }
  41.  
  42.  
  43. class Trip {
  44.     String agency;
  45.     int price;
  46.    
  47.     public Trip() { }
  48.    
  49.     public Trip(String agency, int price) {
  50.         this.agency = agency;
  51.         this.price = price;
  52.     }
  53.  
  54.     public String getAgency() {
  55.         return agency;
  56.     }
  57.  
  58.     public void setAgency(String agency) {
  59.         this.agency = agency;
  60.     }
  61.  
  62.     public int getPrice() {
  63.         return price;
  64.     }
  65.  
  66.     public void setPrice(int price) {
  67.         this.price = price;
  68.     }
  69.    
  70.     public int vratiVremeVoDenovi() { return 1; }
  71.    
  72.     public static int minPrice(Trip[] niza, int n, Trip holiday) {
  73.         int min = niza[0].getPrice();
  74.         for(int i=1; i<n; i++) {
  75.             if(niza[i].getPrice() < min) {
  76.                 min = niza[i].getPrice();
  77.             }
  78.         }
  79.         return min;
  80.     }
  81.        
  82. }
  83.  
  84. class FestiveTrip extends Trip {
  85.     private int startDay, startMonth, endDay, endMonth;
  86.    
  87.     public FestiveTrip() { super(); }
  88.  
  89.     public FestiveTrip(String agency, int price, int startDay, int startMonth, int endDay, int endMonth) {
  90.         super(agency, price);
  91.  
  92.         this.startDay = startDay;
  93.         this.startMonth = startMonth;
  94.         this.endDay = endDay;
  95.         this.endMonth = endMonth;
  96.        
  97.         try {
  98.             if(endMonth < startMonth || endMonth == startMonth && endDay < startDay) {
  99.                 throw new MyException("Exception");
  100.             }
  101.         } catch(MyException e) {
  102.             System.out.print(e.toString());
  103.             this.startDay = endDay;
  104.             this.startMonth = endMonth;
  105.             this.endDay = startDay;
  106.             this.endMonth = startMonth;
  107.         }
  108.     }
  109.  
  110.     public int getStartDay() {
  111.         return startDay;
  112.     }
  113.  
  114.     public void setStartDay(int startDay) {
  115.         this.startDay = startDay;
  116.     }
  117.  
  118.     public int getStartMonth() {
  119.         return startMonth;
  120.     }
  121.  
  122.     public void setStartMonth(int startMonth) {
  123.         this.startMonth = startMonth;
  124.     }
  125.  
  126.     public int getEndDay() {
  127.         return endDay;
  128.     }
  129.  
  130.     public void setEndDay(int endDay) {
  131.         this.endDay = endDay;
  132.     }
  133.  
  134.     public int getEndMonth() {
  135.         return endMonth;
  136.     }
  137.  
  138.     public void setEndMonth(int endMonth) {
  139.         this.endMonth = endMonth;
  140.     }
  141.  
  142.     public int vratiVremeVoDenovi() {
  143.         return ((this.endMonth * 30) + this.endDay) - ((this.startMonth * 30) + this.startDay);
  144.     }
  145. }
  146.  
  147.  
  148. class Vacation extends Trip {
  149.     private int days;
  150.    
  151.     public Vacation() { super(); }
  152.  
  153.     public Vacation(String agency, int price, int days) {
  154.         super(agency, (price-1000));
  155.         this.days = days;
  156.     }
  157.  
  158.     public int vratiVremeVoDenovi() {
  159.         return days;
  160.     }
  161.  
  162.     public void setDays(int days) {
  163.         this.days = days;
  164.     }
  165.  
  166. }
  167.  
  168.  
  169.  
  170.  
  171. public class Test {
  172.  
  173.    
  174.     public static void main(String[] args) {
  175.         int n;
  176.         Scanner in=new Scanner(System.in);
  177.         n=in.nextInt();
  178.        
  179.         Trip nizaPatuvanje[]=new Trip[n];
  180.        
  181.         for (int i=0;i<n;i++){
  182.             int tip=in.nextInt();
  183.             if (tip==0){
  184.                 String ime=in.next();
  185.                 int cena =in.nextInt();
  186.                 int vreme=in.nextInt();
  187.                 nizaPatuvanje[i]=new Vacation(ime,cena,vreme);
  188.             }
  189.             else {
  190.                 String ime=in.next();
  191.                 int cena =in.nextInt();
  192.                 int pocD=in.nextInt();
  193.                 int pocM=in.nextInt();
  194.                 int krajD=in.nextInt();
  195.                 int krajM=in.nextInt();
  196.                 nizaPatuvanje[i]=new FestiveTrip(ime,cena,pocD,pocM, krajD,krajM);
  197.                
  198.             }
  199.         }
  200.        
  201.         //решение на барање 1
  202.         for(Trip t : nizaPatuvanje) {
  203.             if(t instanceof FestiveTrip) {
  204.                 if(((FestiveTrip) t).getStartMonth() == 6) {
  205.                     System.out.print(t.agency + " ");
  206.                 }
  207.             }
  208.         }
  209.         System.out.println("");
  210.        
  211.         //решение на барање 2
  212.         int denovi = 0;
  213.         for(Trip t : nizaPatuvanje) {
  214.             denovi += t.vratiVremeVoDenovi();
  215.         }
  216.         System.out.println((double)denovi/nizaPatuvanje.length);
  217.        
  218.         //решение на барање 3  
  219.         String agency = in.next();
  220.         int price = in.nextInt();
  221.         int days = in.nextInt();
  222.         Vacation holiday = new Vacation(agency, price, days);
  223.        
  224.         //решение на барање 4
  225.         Trip arrTrips[] = new Trip[n];
  226.         int counter=0;
  227.         for(Trip t : nizaPatuvanje) {
  228.             if(t.vratiVremeVoDenovi() > holiday.vratiVremeVoDenovi()) {
  229.                 arrTrips[counter++] = t;
  230.             }
  231.         }
  232.        
  233.         System.out.println(Trip.minPrice(arrTrips, counter, holiday));
  234.     }
  235.  
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement