Batoul Slaiman 72030543 Final

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Batoul slaiman 72030543 final

import java.util.*;

/**
 *
 * @author batoul
 */
abstract class device {

    protected String Brand;


    protected double price;

    public device(String Brand, double price) {


        this.Brand = Brand;
        this.price = price;
  }

    public String getBrand() {


        return Brand;
  }

    public void setBrand(String Brand) {


        this.Brand = Brand;
  }

    public double getPrice() {


        return price;
  }

    public void setPrice(double price) {


        this.price = price;
  }

    @Override
    public String toString() {
        return "Brand=" +this.getBrand + ", this.getprice=" + price;
  }

    public abstract boolean isAlternative(Object o);


}
Public class laptop extends device {

    private String cpu;


    private String type;

    public laptop(String cpu, String type, String Brand, double price) {


        super(Brand, price);
        this.cpu = cpu;
        this.type = type;
  }

    public String getCpu() {


        return cpu;
  }

    public void setCpu(String cpu) {


        this.cpu = cpu;
  }

    public String getType() {


        return type;
  }

    public void setType(String type) {


        this.type = type;
  }

    @Override
    public String toString() {
        return "laptop{" + super.toString() + “\n+"cpu=" + this,getcpu + ", type=" +
this.gettype + '}';
  }

    @Override
    public boolean isAlternative(Object o) {
        if (o instanceof laptop) {
            if (((laptop) o).getCpu().equalsIgnoreCase(this.cpu) && ((laptop)
o).getType().equalsIgnoreCase(this.type)) {
                return true;
      }
    }
        return false;
  }
}

class Usb extends device {

    private int storage;

    public Usb(String brand, double price, int s) {


        super(brand, price);
        this.storage = s;
  }

    @Override
    public String toString() {
        return "Usb " + super.toString() + "\nStorage is" + storage + " GB ";
  }

    @Override
    public boolean isAlternative(Object o) {
        return true;
  }
}

public class TechShop {

    private String name;


    private ArrayList<device> devices;

    public TechShop(String name) {


        this.name = name;
        this.devices = new ArrayList<device>();
  }

    public void addDevice(device d) {


        devices.add(d);
  }

    public String getName() {


        return name;
  }

    public void setName(String name) {


        this.name = name;
  }

    public ArrayList<device> getDevices() {


        return devices;
  }

    public void setDevices(ArrayList<device> devices) {


        this.devices = devices;
  }

    @Override
    public String toString() {
        String s = "TechShop name=" + name + '\n' + "Devices: \n";
        for (int i = 0; i < devices.size(); i++) {
            if (devices.get(i) instanceof laptop) {
                s += ((laptop) devices.get(i)).toString();
      }
            if (devices.get(i) instanceof Usb) {
                s += ((Usb) devices.get(i)).toString();
      }
    }
        return s;
  }

main
public class exammm {

    public static void main(String[] args) {


        TechShop t = new TechShop("Modern Technology");
        t.addDevice(new laptop(“corex”, "type2", "lenova", 200));
        t.addDevice(new laptop("corei7", "type9", "mac", 1200));
        t.addDevice(new Usb("br5", 1000, 42));

        for (int i = 1; i < t.getDevices().size(); i++) {


            if (t.getDevices().get(i) instanceof laptop)
            if (t.getDevices().get(i).isAlternative(t.getDevices().get(0))) {
                System.out.println(t.getDevices().get(i).toString());
      }
    }
        for (int i = 1; i < t.getDevices().size(); i++) {
            if (t.getDevices().get(i) instanceof laptop)
            if (((laptop)t.getDevices().get(i)).getCpu().equalsIgnoreCase("corei7")
&&((laptop)t.getDevices().get(i)).getType().equalsIgnoreCase("Ultrabook") ) {
            
                System.out.println(t.getDevices().get(i).toString());
      }
    }
  }

You might also like