metalni

APS Labs 1 Patuvanje

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