Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
133
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.  
  211. import java.util.ArrayList;
  212. import java.util.List;
  213.  
  214. class Product implements ProductInterface {
  215.     private double price;
  216.  
  217.     public Product(double price) {
  218.         this.price = price;
  219.     }
  220.  
  221.     @Override
  222.     public double calculateBargainPrice() {
  223.         return price;
  224.     }
  225.  
  226.     public double getPrice() {
  227.         return price;
  228.     }
  229.  
  230.     public void setPrice(double price) {
  231.         this.price = price;
  232.     }
  233. }
  234.  
  235. class Vehicle extends Product implements VehicleInterface {
  236.     private String brand;
  237.     private String model;
  238.  
  239.  
  240.     public Vehicle(String brand, String model, double price) {
  241.         super(price);
  242.         this.brand = brand;
  243.         this.model = model;
  244.     }
  245.  
  246.     @Override
  247.     public double calculateBargainPrice() {
  248.         return super.calculateBargainPrice() * .95f;
  249.     }
  250.  
  251.     @Override
  252.     public String toString() {
  253.         return "Vehicle{" +
  254.                 "brand='" + brand + '\'' +
  255.                 ", model='" + model + '\'' +
  256.                 '}';
  257.     }
  258.  
  259.     @Override
  260.     public String getBrand() {
  261.         return brand;
  262.     }
  263.  
  264.     @Override
  265.     public String getModel() {
  266.         return model;
  267.     }
  268. }
  269.  
  270. class Car extends Vehicle {
  271.     private String carBody;
  272.  
  273.     public Car(String brand, String model, double price, String carBody) {
  274.         super(brand, model, price);
  275.         this.carBody = carBody;
  276.     }
  277.  
  278.     @Override
  279.     public double calculateBargainPrice() {
  280.         return super.calculateBargainPrice() * 0.85f;
  281.     }
  282.  
  283.     public String getCarBody() {
  284.         return carBody;
  285.     }
  286.  
  287.     public void setCarBody(String carBody) {
  288.         this.carBody = carBody;
  289.     }
  290. }
  291.  
  292. class Motorcyce extends Vehicle {
  293.     private String typeOfDriver;
  294.  
  295.     public Motorcyce(String brand, String model, double price, String typeOfDriver) {
  296.         super(brand, model, price);
  297.         this.typeOfDriver = typeOfDriver;
  298.     }
  299.  
  300.     @Override
  301.     public double calculateBargainPrice() {
  302.         return super.calculateBargainPrice() * .9f;
  303.     }
  304.  
  305.     public String getTypeOfDriver() {
  306.         return typeOfDriver;
  307.     }
  308.  
  309.     public void setTypeOfDriver(String typeOfDriver) {
  310.         this.typeOfDriver = typeOfDriver;
  311.     }
  312. }
  313.  
  314. interface ProductInterface {
  315.     double calculateBargainPrice();
  316.     double getPrice();
  317.     void setPrice(double price);
  318. }
  319.  
  320. interface VehicleInterface extends ProductInterface {
  321.     String getBrand();
  322.     String getModel();
  323. }
  324.  
  325. public class Main {
  326.     public static void main(String[] args) {
  327.         List<ProductInterface> list = new ArrayList<>();
  328.         list.add(new Vehicle("Brand", "Model", 10333));
  329.         list.add(new Car("Brand", "Model", 10333, "CarBody"));
  330.         list.add(new Motorcyce("Brand", "Model", 10333, "TypeOfDriver"));
  331.         list.forEach(System.out::print);
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement