инд 3

← разместить еще код

Расшаренный код:

public abstract class Device {

    private String model;
    private String manufacturer;
    private int year;
    private String processor;

    public Device(String model, String manufacturer, int year, String processor) {
        this.model = model;
        this.manufacturer = manufacturer;
        this.year = year;
        this.processor = processor;
    }

    boolean search(String val, int сharacteristic) {
        switch (сharacteristic) {
            case 1: {
                if (getManufacturer().equals(val)) {
                    return true;
                }
                break;
            }
            case 2: {
                if (getYear() == Integer.parseInt(val)) {
                    return true;
                }
                break;
            }
            case 3: {
                if (getProcessor().equals(val)) {
                    return true;
                }
                break;
            }

        }
        return false;
    }

    public String getProcessor() {
        return processor;
    }

    public void setProcessor(String processor) {
        this.processor = processor;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    void print() {
        System.out.println(this.getModel());
        System.out.println(this.getManufacturer());
        System.out.println(this.getYear());
        System.out.println(this.getProcessor());
    }

    abstract void set();

}
class Vector {

    private int N;
    private Device[] arr;
    private int size;

    Vector() {
        this.N = 10;
        this.arr = new Device[N];
        this.size = 0;
    }

    Vector(int capacity) {
        this.N = capacity;
        this.arr = new Device[N];
        this.size = 0;
    }

    Vector(Device tmparr[]) {
        this.N = tmparr.length + 5;
        this.arr = new Device[N];
        for (int i = 0; i < tmparr.length; i++) {
            arr[i] = tmparr[i];
        }
        this.size = tmparr.length;

    }

    public void insert(Device val, int pos) {
        if (pos > size + 1) {
            System.out.println("Установка на данную позицию невозможна");
            return;
        }

        if (size == N) {
            N = N * 2;
            Device[] arrTmp = new Device[N];
            for (int i = 0; i < size; i++) {
                arrTmp[i] = arr[i];
            }
            arr = arrTmp;
        }

        for (int i = size; i >= pos; i--) {
            arr[i] = arr[i - 1];
        }
        arr[pos - 1] = val;
        size++;
    }

    public void print() {
        if (size == 0) {
            System.out.println("Empty");
            return;
        }
        for (int i = 0; i < size; i++) {
            arr[i].print();
        }
        System.out.println();
        return;
    }

    public Device get(int pos) {
        return arr[pos - 1];
    }

    public void getSize() {
        System.out.println("Size: " + size);
    }

    public void set(int pos) {
        if (pos > size) {
            System.out.println("Изменение несуществующего элемента невозможна!");
            return;
        }
        arr[pos - 1].set();
    }

    public void del(int pos) {

        for (int i = pos; i < size; i++) {
            arr[i - 1] = arr[i];
        }
        size--;


    }

    public void search(String val, int сharacteristic) {

        for (int i = 0; i < size; i++) {
            if(сharacteristic == 4 && (arr[i] instanceof Notebook)){
                continue;
            }
            if(сharacteristic == 5 && (arr[i] instanceof Desktop)) {
                continue;
            }
            if(arr[i].search(val , сharacteristic)){
                arr[i].print();
            }
        }
    }

    public void clear() {
        size = 0;
    }

    public Vector clone(Vector vect) {
        Vector clonVect = new Vector(vect.arr.length);
        Device val;
        for (int i = 0; i < size; i++) {
            val = get(i + 1);
            clonVect.insert(val, i + 1);
        }
        return clonVect;
    }

}

import java.util.Scanner;

public class Notebook extends Device {


    private String Battery_life;
    private int diagonal;

    Scanner in = new Scanner(System.in);


    Notebook(String model, String manufacturer, int year, String battery_life, String processor, int diagonal) {
        super(model, manufacturer, year, processor);

        this.Battery_life = battery_life;
        this.diagonal = diagonal;

    }

    boolean search(String val , int сharacteristic) {
        super.search(val , сharacteristic);
        switch (сharacteristic) {

            case 5: {
                if (getBattery_life().equals(val)) {
                    return true;
                }
                break;
            }
            case 6:{
                if(getDiagonal()== Integer.parseInt(val)){
                    return true;
                }
            }
            default: {
                return false;
            }
        }

        return false;
    }


    void set(){
        System.out.println("Enter 1 to edit the model\nEnter 2 to edit the manufacturer\nEnter 3 to edit the year\nEnter 4 to edit the battery life");
        int ans = in.nextInt();
        System.out.println("Enter new value");
        String tmp = new String();
        tmp = in.nextLine();
        switch(ans){
            case 1:{
                setModel(tmp);
                break;
            }
            case 2:{
                setManufacturer(tmp);
                break;
            }
            case 3:{
                setYear(Integer.parseInt(tmp));
                break;
            }
            case 4:{
                setBattery_life(tmp);
            }
            default:{
                System.out.println("Do not correct answer");
            }
        }
    }


    public int getDiagonal() {
        return diagonal;
    }

    public void setDiagonal(int diagonal) {
        this.diagonal = diagonal;
    }

    public void setBattery_life(String battery_life) {
        Battery_life = battery_life;
    }

    public String getBattery_life() {
        return Battery_life;
    }

    void print() {
        super.print();
        System.out.println(Battery_life);
        System.out.println(diagonal);
    }


}
import java.util.Scanner;

public class Desktop extends Device {

    private String motherboard;
    private String videocard;

    Scanner in = new Scanner(System.in);

    Desktop(String model, String manufacturer, int year, String motherboard, String processor, String videocard) {
        super(model, manufacturer, year, processor);
        this.motherboard = motherboard;
        this.videocard = videocard;
    }

    void set(){
        System.out.println("Enter 1 to edit the model\nEnter 2 to edit the manufacturer\nEnter 3 to edit the year\nEnter 4 to edit the motherbroad");
        int ans = in.nextInt();
        in.nextLine();
        System.out.println("Enter new value");
        String tmp = new String();
        tmp = in.nextLine();
        switch(ans){
            case 1:{
                setModel(tmp);
                break;
            }
            case 2:{
                setManufacturer(tmp);
                break;
            }
            case 3:{
                setYear(Integer.parseInt(tmp));
                break;
            }
            case 4:{
                setMotherboard(tmp);
            }
            default:{
                System.out.println("Do not correct answer");
            }
        }
    }

    boolean search(String val , int сharacteristic) {
        super.search(val, сharacteristic);
        switch (сharacteristic) {
            case 4: {
                if (getMotherboard().equals(val)) {
                    return true;
                }
                break;
            }
            case 7:{
                if(getVideocard().equals(val)){
                    return true;
                }
            }
            default: {
                return false;
            }
        }

        return false;
    }

    public String getVideocard() {
        return videocard;
    }

    public void setVideocard(String videocard) {
        this.videocard = videocard;
    }

    public void setMotherboard(String motherboard) {
        motherboard = motherboard;
    }

    public String getMotherboard() {
        return motherboard;
    }

    void print() {
        super.print();
        System.out.println(this.getMotherboard());
        System.out.println(this.getVideocard());
    }


}

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int ans;
        int pos;
        Vector vector = new Vector();
        String model;
        String manufacturer;
        int year;
        String motherboard;
        String battery_life;
        String processor;
        String videocard;
        int diagonal;
        int сharacteristic;
        String val;
        boolean b=true;

        Device catalog[];

        while (true) {
            System.out.println("Menu:\nEnter 1 to print catalog\nEnter 2 to add desktop\nEnter 3 to add notebook\nEnter 4 to delete element\nEnter 5 to edit element\nEnter 6 to search element\n ");
            if(b) {
                ans = Integer.parseInt(in.next());
                b= false;
            }
            else{
                ans = in.nextInt();
            }
            switch (ans){
                case 1:{
                        vector.print();
                        break;
                }
                case 2:{
                    System.out.println("Enter the  model");
                    model = in.next();
                    System.out.println("Enter the  manufacturer");
                    manufacturer = in.next();
                    System.out.println("Enter the  year");
                    year = Integer.parseInt(in.next());
                    System.out.println("Enter the procossor");
                    processor = in.next();
                    System.out.println("Enter the  motherboard");
                    motherboard = in.next();
                    System.out.println("Enter the videocard");
                    videocard = in.next();

                    Desktop tmp = new Desktop(model , manufacturer , year , motherboard, processor , videocard);
                    System.out.println("Enter the position");
                    pos = in.nextInt();
                    if(pos < 1) {
                        break;
                    }
                    vector.insert(tmp,pos);
                    break;
                }
                case 3:{
                    System.out.println("Enter the  model");
                    model = in.next();
                    System.out.println("Enter the  manufacturer");
                    manufacturer = in.next();
                    System.out.println("Enter the  year");
                    year = Integer.parseInt(in.next());
                    System.out.println("Enter the procossor");
                    processor = in.next();
                    System.out.println("Enter the battery life");
                    battery_life = in.next();
                    System.out.println("Enter the diagonal");
                    diagonal = Integer.parseInt(in.next());

                    Notebook tmp = new Notebook(model , manufacturer , year, battery_life, processor, diagonal);
                    System.out.println("Enter the position");
                    pos = in.nextInt();
                    if(pos < 1){
                        break;
                    }
                    vector.insert(tmp,pos);
                    break;
                }
                case 4:{
                    System.out.println("Enter deleting position");
                    pos = in.nextInt();
                    vector.del(pos);
                    break;
                }
                case 5:{
                    System.out.println("Enter editing position");
                    pos = in.nextInt();
                    vector.set(pos);
                }
                case 6:{
                    System.out.println("Enter 1 to search about manufacturer\nEnter 2 to search about year\nEnter 3 to search about processor\nEnter 4 to search about motherboard\nEnter 5 to search about battery life\nEnter 6 to search about diagonal\nEnter 7 to search about videocard");

                    сharacteristic =Integer.parseInt( in.next());

                    System.out.println("Enter a seacher value");
                    val = in.next();

                    vector.search(val , сharacteristic);
                }
            }

        }
    }
}