Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.50 KB | None | 0 0
  1. Zadanie 1
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. class Product {
  7.     private double price;
  8.  
  9.     public Product(double price) {
  10.         this.price = price;
  11.     }
  12.  
  13.     double calculateBargainPrice() {
  14.         return price;
  15.     }
  16.  
  17.     public double getPrice() {
  18.         return price;
  19.     }
  20.  
  21.     public void setPrice(double price) {
  22.         this.price = price;
  23.     }
  24. }
  25.  
  26. class Vehicle extends Product {
  27.     private String brand;
  28.     private String model;
  29.  
  30.  
  31.     public Vehicle(String brand, String model, double price) {
  32.         super(price);
  33.         this.brand = brand;
  34.         this.model = model;
  35.     }
  36.  
  37.     @Override
  38.     public double calculateBargainPrice() {
  39.         return super.calculateBargainPrice() * .95f;
  40.     }
  41.  
  42.     @Override
  43.     public String toString() {
  44.         return "Vehicle{" +
  45.                 "brand='" + brand + '\'' +
  46.                 ", model='" + model + '\'' +
  47.                 '}';
  48.     }
  49. }
  50.  
  51. class Car extends Vehicle {
  52.     private String carBody;
  53.  
  54.     public Car(String brand, String model, double price, String carBody) {
  55.         super(brand, model, price);
  56.         this.carBody = carBody;
  57.     }
  58.  
  59.     @Override
  60.     public double calculateBargainPrice() {
  61.         return super.calculateBargainPrice() * 0.85f;
  62.     }
  63.  
  64.     public String getCarBody() {
  65.         return carBody;
  66.     }
  67.  
  68.     public void setCarBody(String carBody) {
  69.         this.carBody = carBody;
  70.     }
  71. }
  72.  
  73. class Motorcyce extends Vehicle {
  74.     private String typeOfDriver;
  75.  
  76.     public Motorcyce(String brand, String model, double price, String typeOfDriver) {
  77.         super(brand, model, price);
  78.         this.typeOfDriver = typeOfDriver;
  79.     }
  80.  
  81.     @Override
  82.     public double calculateBargainPrice() {
  83.         return super.calculateBargainPrice() * .9f;
  84.     }
  85.  
  86.     public String getTypeOfDriver() {
  87.         return typeOfDriver;
  88.     }
  89.  
  90.     public void setTypeOfDriver(String typeOfDriver) {
  91.         this.typeOfDriver = typeOfDriver;
  92.     }
  93. }
  94.  
  95. public class Main {
  96.     public static void main(String[] args) {
  97.         List<Product> list = new ArrayList<>();
  98.         list.add(new Vehicle("Brand", "Model", 10333));
  99.         list.add(new Car("Brand", "Model", 10333, "CarBody"));
  100.         list.add(new Motorcyce("Brand", "Model", 10333, "TypeOfDriver"));
  101.         list.forEach(System.out::print);
  102.     }
  103. }
  104.  
  105. Zadanie 2
  106.  
  107. import java.util.ArrayList;
  108. import java.util.List;
  109. import java.util.function.Supplier;
  110.  
  111. abstract class Product {
  112.     private double price;
  113.  
  114.     public Product(double price) {
  115.         this.price = price;
  116.     }
  117.  
  118.     abstract double calculateBargainPrice();
  119.  
  120.     public double getPrice() {
  121.         return price;
  122.     }
  123.  
  124.     public void setPrice(double price) {
  125.         this.price = price;
  126.     }
  127. }
  128.  
  129. abstract class Vehicle extends Product {
  130.     private String brand;
  131.     private String model;
  132.  
  133.  
  134.     public Vehicle(String brand, String model, double price) {
  135.         super(price);
  136.         this.brand = brand;
  137.         this.model = model;
  138.     }
  139.    
  140.     @Override
  141.     public String toString() {
  142.         return "Vehicle{" +
  143.                 "brand='" + brand + '\'' +
  144.                 ", model='" + model + '\'' +
  145.                 '}';
  146.     }
  147. }
  148.  
  149. class Car extends Vehicle {
  150.     private String carBody;
  151.  
  152.     public Car(String brand, String model, double price, String carBody) {
  153.         super(brand, model, price);
  154.         this.carBody = carBody;
  155.     }
  156.  
  157.     @Override
  158.     double calculateBargainPrice() {
  159.         return calculateBargainPrice() * 0.85f;
  160.     }
  161.  
  162.     public String getCarBody() {
  163.         return carBody;
  164.     }
  165.  
  166.     public void setCarBody(String carBody) {
  167.         this.carBody = carBody;
  168.     }
  169. }
  170.  
  171. class Motorcyce extends Vehicle {
  172.     private String typeOfDriver;
  173.  
  174.     public Motorcyce(String brand, String model, double price, String typeOfDriver) {
  175.         super(brand, model, price);
  176.         this.typeOfDriver = typeOfDriver;
  177.     }
  178.  
  179.     @Override
  180.     double calculateBargainPrice() {
  181.         return calculateBargainPrice() * 0.9f;
  182.     }
  183.  
  184.     public String getTypeOfDriver() {
  185.         return typeOfDriver;
  186.     }
  187.  
  188.     public void setTypeOfDriver(String typeOfDriver) {
  189.         this.typeOfDriver = typeOfDriver;
  190.     }
  191. }
  192.  
  193. public class Main {
  194.     public static void main(String[] args) {
  195.         List<Product> list = new ArrayList<>();
  196.         list.add(new Vehicle("Brand", "Model", 10333) {
  197.             @Override
  198.             double calculateBargainPrice() {
  199.                 return 1000;
  200.             }
  201.         });
  202.         list.add(new Car("Brand", "Model", 10333, "CarBody"));
  203.         list.add(new Motorcyce("Brand", "Model", 10333, "TypeOfDriver"));
  204.         list.forEach(System.out::print);
  205.     }
  206. }
  207.  
  208. Zadanie 3
  209.  
  210. import java.util.ArrayList;
  211. import java.util.List;
  212.  
  213. class Product implements ProductInterface {
  214.     private double price;
  215.  
  216.     public Product(double price) {
  217.         this.price = price;
  218.     }
  219.  
  220.     @Override
  221.     public double calculateBargainPrice() {
  222.         return price;
  223.     }
  224.  
  225.     public double getPrice() {
  226.         return price;
  227.     }
  228.  
  229.     public void setPrice(double price) {
  230.         this.price = price;
  231.     }
  232. }
  233.  
  234. class Vehicle extends Product implements VehicleInterface {
  235.     private String brand;
  236.     private String model;
  237.  
  238.  
  239.     public Vehicle(String brand, String model, double price) {
  240.         super(price);
  241.         this.brand = brand;
  242.         this.model = model;
  243.     }
  244.  
  245.     @Override
  246.     public double calculateBargainPrice() {
  247.         return super.calculateBargainPrice() * .95f;
  248.     }
  249.  
  250.     @Override
  251.     public String toString() {
  252.         return "Vehicle{" +
  253.                 "brand='" + brand + '\'' +
  254.                 ", model='" + model + '\'' +
  255.                 '}';
  256.     }
  257.  
  258.     @Override
  259.     public String getBrand() {
  260.         return brand;
  261.     }
  262.  
  263.     @Override
  264.     public String getModel() {
  265.         return model;
  266.     }
  267. }
  268.  
  269. class Car extends Vehicle {
  270.     private String carBody;
  271.  
  272.     public Car(String brand, String model, double price, String carBody) {
  273.         super(brand, model, price);
  274.         this.carBody = carBody;
  275.     }
  276.  
  277.     @Override
  278.     public double calculateBargainPrice() {
  279.         return super.calculateBargainPrice() * 0.85f;
  280.     }
  281.  
  282.     public String getCarBody() {
  283.         return carBody;
  284.     }
  285.  
  286.     public void setCarBody(String carBody) {
  287.         this.carBody = carBody;
  288.     }
  289. }
  290.  
  291. class Motorcyce extends Vehicle {
  292.     private String typeOfDriver;
  293.  
  294.     public Motorcyce(String brand, String model, double price, String typeOfDriver) {
  295.         super(brand, model, price);
  296.         this.typeOfDriver = typeOfDriver;
  297.     }
  298.  
  299.     @Override
  300.     public double calculateBargainPrice() {
  301.         return super.calculateBargainPrice() * .9f;
  302.     }
  303.  
  304.     public String getTypeOfDriver() {
  305.         return typeOfDriver;
  306.     }
  307.  
  308.     public void setTypeOfDriver(String typeOfDriver) {
  309.         this.typeOfDriver = typeOfDriver;
  310.     }
  311. }
  312.  
  313. interface ProductInterface {
  314.     double calculateBargainPrice();
  315.     double getPrice();
  316.     void setPrice(double price);
  317. }
  318.  
  319. interface VehicleInterface extends ProductInterface {
  320.     String getBrand();
  321.     String getModel();
  322. }
  323.  
  324. public class Main {
  325.     public static void main(String[] args) {
  326.         List<ProductInterface> list = new ArrayList<>();
  327.         list.add(new Vehicle("Brand", "Model", 10333));
  328.         list.add(new Car("Brand", "Model", 10333, "CarBody"));
  329.         list.add(new Motorcyce("Brand", "Model", 10333, "TypeOfDriver"));
  330.         list.forEach(System.out::print);
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement