Advertisement
Laitofai

Untitled

May 13th, 2020
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.         Transport t = new Transport("Car", 1500);
  5.         System.out.println(t.GetInfo(5));
  6.         Transport d = new Transport("Car", -1000);
  7.         System.out.println(d.GetInfo(2));
  8.     Car c1 = new Car("Audi", 500, "premium", 50);
  9.     System.out.println(c1.GetInfo(1));
  10.     }
  11. }
  12.  
  13. class Transport {
  14.     public double rentPrice;
  15.     public String name;
  16.  
  17.     public String GetInfo(int hours){
  18.         return name + "............" + (rentPrice * hours);
  19.     }
  20.  
  21.     //setters
  22.     public void SetRentPrice(double rentPrice){
  23.         if(rentPrice >= 0){
  24.             this.rentPrice = rentPrice;
  25.         }else{
  26.             this.rentPrice = 0;
  27.             System.out.println("Error: negative rent price");
  28.         }
  29.     }
  30.  
  31.     //constructors
  32.     public Transport(String name, double rentPrice){
  33.         SetRentPrice(rentPrice);
  34.         this.name = name;
  35.     }
  36. }
  37.  
  38. class Car extends Transport {
  39.     /*
  40.         "eco" - 100%
  41.         "business" - 120%
  42.         "premium" - 200%
  43.     */
  44.     public String complectation;
  45.     public int extraPrice;
  46.  
  47.     public Car(String name, double rentPrice, String complectation, int extraPrice){
  48.         super(name, rentPrice);
  49.         SetPrice(extraPrice);
  50.         this.complectation = complectation;
  51.     }
  52.  
  53.     public void SetPrice(int extraPrice){
  54.         if(extraPrice >= 0){
  55.             this.extraPrice = extraPrice;
  56.         }else{
  57.             this.extraPrice = 0;
  58.             System.out.println("Error");
  59.         }
  60.     }
  61.  
  62.     @Override
  63.     public String GetInfo(int hours){
  64.         double finalRentPrice;
  65.         switch(complectation){
  66.             case ("eco"):
  67.                 finalRentPrice = rentPrice;
  68.                 break;
  69.             case ("business"):
  70.                 finalRentPrice = rentPrice * 1.2;
  71.                 break;
  72.             case ("premium"):
  73.                 finalRentPrice = rentPrice * 2;
  74.                 break;
  75.             default:
  76.                 System.out.println("Error: incorrect complectation!");
  77.                 finalRentPrice = rentPrice;
  78.         }
  79.         return name + "............" + (finalRentPrice * hours + extraPrice);
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement