Advertisement
atanasovetr

Phones

Jan 27th, 2021
1,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. Device class:
  2.  
  3. public class Device {
  4.     protected String brand;
  5.     protected String model;
  6.     protected double price;
  7.  
  8.     public Device(){
  9.  
  10.     }
  11.  
  12.     public Device(String brand, String model, double price) {
  13.         this.brand = brand;
  14.         this.model = model;
  15.         this.price = price;
  16.     }
  17.  
  18.     public String getBrand() {
  19.         return brand;
  20.     }
  21.  
  22.     public void setBrand(String brand) {
  23.         this.brand = brand;
  24.     }
  25.  
  26.     public String getModel() {
  27.         return model;
  28.     }
  29.  
  30.     public void setModel(String model) {
  31.         this.model = model;
  32.     }
  33.  
  34.     public double getPrice() {
  35.         return price;
  36.     }
  37.  
  38.     public void setPrice(double price) {
  39.         this.price = price;
  40.     }
  41.  
  42.     @Override
  43.     public String toString() {
  44.         return "Brand: " + brand + ", " + "Model: " + model + ", " + "Price: " + price;
  45.     }
  46. }
  47.  
  48.  
  49. BrokenDevice class:
  50.  
  51. public class BrokenDevice extends Device{
  52.     private String symptoms;
  53.     private int repairDays;
  54.  
  55.     public BrokenDevice(String brand, String model, double price, String symptoms, int repairDays) {
  56.         super(brand, model, price);
  57.         this.symptoms = symptoms;
  58.         this.repairDays = repairDays;
  59.     }
  60.  
  61.     public String getSymptoms() {
  62.         return symptoms;
  63.     }
  64.  
  65.     public void setSymptoms(String symptoms) {
  66.         this.symptoms = symptoms;
  67.     }
  68.  
  69.     public int getRepairDays() {
  70.         return repairDays;
  71.     }
  72.  
  73.     public void setRepairDays(int repairDays) {
  74.         this.repairDays = repairDays;
  75.     }
  76.  
  77.     @Override
  78.     public String toString() {
  79.         return super.toString() + ", " + "Symptoms: " + symptoms + ", " + "Days needed for repair: " + repairDays;
  80.     }
  81. }
  82.  
  83.  
  84. Service class:
  85.  
  86. import java.util.ArrayList;
  87.  
  88. public class Service {
  89.     ArrayList<Device> goodCondDevices;
  90.     ArrayList<BrokenDevice> badCondDevices;
  91.  
  92.     public Service(ArrayList<Device> goodCondDevices, ArrayList<BrokenDevice> badCondDevices) {
  93.         this.goodCondDevices = goodCondDevices;
  94.         this.badCondDevices = badCondDevices;
  95.     }
  96.  
  97.  
  98.     public void addDevice(Device deviceToAdd){
  99.         this.goodCondDevices.add(deviceToAdd);
  100.     }
  101.  
  102.     public void addBrokenDevice(BrokenDevice brokenDeviceToAdd){
  103.         this.badCondDevices.add(brokenDeviceToAdd);
  104.     }
  105.  
  106.     public void brokenToGood(BrokenDevice brokenDeviceRepaired){
  107.         this.goodCondDevices.add(brokenDeviceRepaired);
  108.         this.badCondDevices.remove(brokenDeviceRepaired);
  109.     }
  110.  
  111.     public void printBySymptom(String symptom){
  112.         for(BrokenDevice device: this.badCondDevices){
  113.             if(device.getSymptoms().equals(symptom)){
  114.                 System.out.println(device);
  115.             }
  116.         }
  117.     }
  118.  
  119.     public double wholePrice(){
  120.         double sum = 0;
  121.         for(Device device: this.goodCondDevices){
  122.             sum+= device.getPrice();
  123.         }
  124.         for(BrokenDevice device: this.badCondDevices){
  125.             sum+= device.getPrice();
  126.         }
  127.         return sum;
  128.  
  129.     }
  130.  
  131.     @Override
  132.     public String toString() {
  133.         StringBuilder str = new StringBuilder();
  134.         str.append("All Devices in good quality:" + '\n') ;
  135.         for (Device device: goodCondDevices){
  136.             str.append(device.toString() + '\n');
  137.         }
  138.         str.append("All Devices in bad quality:" + '\n') ;
  139.         for (BrokenDevice device: badCondDevices){
  140.             str.append(device.toString() + '\n');
  141.         }
  142.         return str.toString();
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement