By GokiSoft.com| 19:56 13/02/2023|
Java Basic

Exam - Kiểm tra 60 phút lập trình OOP - Quản lý thiết bị máy tính - Lập trình OOP

Thiết kế lớp class Device gồm các thuộc tính sau:

- id: int

- name: String

- manufacturer: String

- importedDate: Date

+ void input: void -> method

void display: void -> method

Thiết kế lớp Computer kế thừa từ lớp Device gồm các thuộc tính

- cpu: String

- ram: String

Thiết kế lớp Monitor kế thừa từ lớp Device gồm các thuộc tính

- size: String (đơn vị inch, cm, m, ..)

Yêu cầu: Tạo getter/setter, hàm tạo tương ứng, và toString() cho từng class object & thiết kế ham nhập và hàm hiển thị.

Tạo 1 mảng Device[] list = new Device[5] trong class Main (test chương trình)

Thực hiện các chức năng sau:

1. Nhập thông tin thiết bị. (Hỏi người dùng thiết bị cần nhập Computer hay Monitor) -> Nhập đủ 5 thiết bị là kết thúc nhập

2. Hiển thị thông tin thiết bị

3. Hiển thị số Computer được nhập vào

4. Hiển thị số Monitor được nhập vào.

5. Thoát.

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Do Trung Duc [T2008A]
Do Trung Duc

2021-03-09 02:02:25



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package caculatormanager;

import java.util.Date;

/**
 *
 * @author TrungDuc
 */
public abstract class Device {

    int id;
    String name;
    Date importedDate;
    
    
    public Device() {
       
    }

    public Device(int id, String name, Date importedDate) {
        this.id = id;
        this.name = name;
        this.importedDate = importedDate;
    }
    
    public abstract void input();

    public abstract void display();

    @Override
    public String toString() {
        return "Device{" + "id=" + id + ", name=" + name + ", importedDate=" + importedDate + '}';
    }
    

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getImportedDate() {
        return importedDate;
    }

    public void setImportedDate(Date importedDate) {
        this.importedDate = importedDate;
    }
    
    
    
}



Do Trung Duc [T2008A]
Do Trung Duc

2021-03-09 02:02:14



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package caculatormanager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TrungDuc
 */
public class Monitor extends Device {

    String size;

    public Monitor() {

    }

    public Monitor(String size, int id, String name, Date importedDate) {
        super(id, name, importedDate);
        this.size = size;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    @Override
    public void display() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return super.toString() + "Monitor{" + "size=" + size + '}';
    }

    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap id");
        id = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap ten");
        name = scan.nextLine();
        System.out.println("Nhap ngay imported");
        try {
            importedDate = new SimpleDateFormat("dd-mm-yyyy").parse(scan.nextLine());
        } catch (ParseException ex) {
            Logger.getLogger(Computer.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Nhap kich thuoc man hinh");
        size = scan.nextLine();
    }

}



Do Trung Duc [T2008A]
Do Trung Duc

2021-03-09 02:01:59



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package caculatormanager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TrungDuc
 */
public class Computer extends Device {

    String cpu, ram;

    public Computer() {
    }

    public Computer(String cpu, String ram, int id, String name, Date importedDate) {
        super(id, name, importedDate);
        this.cpu = cpu;
        this.ram = ram;
    }

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getRam() {
        return ram;
    }

    public void setRam(String ram) {
        this.ram = ram;
    }

    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap id");
        id = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap ten");
        name = scan.nextLine();
        System.out.println("Nhap ngay imported");
        try {
            importedDate = new SimpleDateFormat("dd-mm-yyyy").parse(scan.nextLine());
        } catch (ParseException ex) {
            Logger.getLogger(Computer.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Nhap so lieu ram");
        ram = scan.nextLine();
        System.out.println("Nhap so lieu cpu");
        cpu = scan.nextLine();

    }

    @Override
    public void display() {
       System.out.println(this);
    }

    @Override
    public String toString() {
        return super.toString() + "Computer{" + "cpu=" + cpu + ", ram=" + ram + '}';
    }

 

}



Do Trung Duc [T2008A]
Do Trung Duc

2021-03-09 02:01:47



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package caculatormanager;

import java.util.Scanner;

/**
 *
 * @author TrungDuc
 */
public class CaculatorManager {

    static Device[] list = new Device[5];

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int option;
        do {
            Menu();
            System.out.println("Nhập lua chon: ");
             option = Integer.parseInt(scan.nextLine());

            switch (option) {
                case 1:
                    nhapThietBi();
                    break;

                case 2:
                    hienThiThietBi();

                    break;

                case 3:
                    hienThiComputer();
                    break;

                case 4:
                    hienThiMonitor();
                    break;

                case 5:
                    System.out.println("Thoat chuong trinh...");
                    break;

                default:
                    System.out.println("Nhap sai...");
                    break;
            }

        } while (option != 2);

    }

    public static void Menu() {
        System.out.println("1.Nhập thông tin thiết bị.....");
        System.out.println("2.Hiển thị thông tin thiết bị....");
        System.out.println("3.Hiển thị số Computer được nhập vào....");
        System.out.println("4.Hiển thị số Monitor được nhập vào.....");
        System.out.println("5.Thoat....");
    }

    public static void menuCreateDevice() {
        System.out.println("1. Nhap Computer moi");
        System.out.println("2. Nhap Monitor moi");
        System.out.println("Chon: ");

    }

    public static void nhapThietBi() {
        for (int i = 0; i < 5; i++) {
            Scanner scan = new Scanner(System.in);
            menuCreateDevice();
            int optionDevice = Integer.parseInt(scan.nextLine());

            switch (optionDevice) {
                case 1:
                    Device computer = new Computer();
                    computer.input();
                    list[i] = computer;
                    break;

                case 2:
                    Device monitor = new Monitor();
                    monitor.input();
                    list[i] = monitor;
                    break;
            }

        }
    }

    public static void hienThiThietBi() {
        for (Device device : list) {
            device.display();
        }
    }

    public static void hienThiComputer() {
        for (Device device : list) {
            if (device instanceof Computer) {
                device.display();
            }
        }
    }

    public static void hienThiMonitor() {
        for (Device device : list) {
            if (device instanceof Monitor) {
                device.display();
            }
        }
    }
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-03-08 09:13:50


#Computer.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Exam1_QuanLyMayTinh;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MyPC
 */
public class Computer extends Device{
    String cpu;
    String ram;

    public Computer() {
    }

    public Computer(String cpu, String ram) {
        this.cpu = cpu;
        this.ram = ram;
    }

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getRam() {
        return ram;
    }

    public void setRam(String ram) {
        this.ram = ram;
    }
    
    
    
    

    @Override
    void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ID:");
        id = Integer.parseInt(scan.nextLine());
        System.out.println("Ten thiet bi:");
        name = scan.nextLine();
        System.out.println("Ten hang:");
        manu = scan.nextLine();
        System.out.println("Ngay nhap:");
        try {
            importedDate = new SimpleDateFormat("dd/MM/yyyy").parse(scan.nextLine());
        } catch (ParseException ex) {
            Logger.getLogger(Computer.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Nhap CPU:");
        cpu = scan.nextLine();
        System.out.println("Nhap RAM:");
        ram = scan.nextLine();
    }

    @Override
    void display() {
        System.out.println("ID: " + id);
        System.out.println("Ten thiet bi: " + name);
        System.out.println("Nha san xuat: " + manu);
        System.out.println("Ngay nhap: " + importedDate);
        System.out.println("CPU: " + cpu);
        System.out.println("RAM: " + ram);
    }

    private Date SimpleDateFormat(String a) {
        return SimpleDateFormat(a);
    }
}


#Device.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Exam1_QuanLyMayTinh;

import java.util.Date;

/**
 *
 * @author MyPC
 */
public abstract class Device {
    int id;
    String name;
    String manu;
    Date importedDate;

    public Device() {
    }

    public Device(int id, String name, String manu, Date importedDate) {
        this.id = id;
        this.name = name;
        this.manu = manu;
        this.importedDate = importedDate;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getManu() {
        return manu;
    }

    public void setManu(String manu) {
        this.manu = manu;
    }

    public Date getImportedDate() {
        return importedDate;
    }

    public void setImportedDate(Date importedDate) {
        this.importedDate = importedDate;
    }

    @Override
    public String toString() {
        return "Device{" + "id=" + id + ", name=" + name + ", manu=" + manu + ", importedDate=" + importedDate + '}';
    }
    
    
    
    abstract void input();
    abstract void display();
}


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Exam1_QuanLyMayTinh;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Main {
    public static void main(String[] args) {
        Device[] list = new Device[5];
        Scanner scan = new Scanner(System.in);
        int choose;
        do{
            showMenu();
            System.out.println("Lua chon chuong trinh");
            choose = Integer.parseInt(scan.nextLine());
            switch(choose){
                case 1:
                    int index = 0;
                    while(index < 5){
                        System.out.println("Nhap Computer hay Monitor:");
                        System.out.println("1.Computer");
                        System.out.println("2.Monitor");
                        int select = Integer.parseInt(scan.nextLine());
                        if(select == 1){
                            Computer computer = new Computer();
                            computer.input();
                            list[index] = computer;
                        }else{
                            Monitor monitor = new Monitor();
                            monitor.input();
                            list[index] = monitor;
                        }
                        index++;
                        if(index == 5) System.out.println("Ket thuc nhap!!");
                    }
                    break;
                case 2:
                    for (Device object : list) {
                        object.display();
                    }
                    break;
                case 3:
                    int countComputer = 0;
                    for (Device object : list) {
                        if(object instanceof Computer){
                            countComputer++;
                        }
                    }
                    System.out.println("So luong Computer: " + countComputer);
                    break;
                case 4:
                    int countMonitor = 0;
                    for (Device object : list) {
                        if(object instanceof Monitor) {
                            countMonitor++;
                        }
                    }
                    System.out.println("So luong Monitor: " + countMonitor);
                    break;
                case 5:
                    System.out.println("Tam biet!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!");
                    break;
            }
        }while(choose != 5);
        
    }
    
    static void showMenu(){
        System.out.println("1.Nhap thong tin thiet bi");
        System.out.println("2.Hien thi thong tin thiet bi");
        System.out.println("3.Hien thi so Computer duoc nhap vao");
        System.out.println("4.Hien thi so Monitor duoc nhap vao");
        System.out.println("5.Thoat");
    }
}


#Monitor.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Exam1_QuanLyMayTinh;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MyPC
 */
public class Monitor extends Device{
    String size;

    public Monitor() {
    }

    public Monitor(String size) {
        this.size = size;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }
    
    
    
    @Override
    void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ID:");
        id = Integer.parseInt(scan.nextLine());
        System.out.println("Ten thiet bi:");
        name = scan.nextLine();
        System.out.println("Ten hang:");
        manu = scan.nextLine();
        System.out.println("Ngay nhap:");
        try {
            importedDate = new SimpleDateFormat("dd/MM/yyyy").parse(scan.nextLine());
        } catch (ParseException ex) {
            Logger.getLogger(Computer.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Nhap kich co:");
        size = scan.nextLine();
    }

    @Override
    public String toString() {
        return "Monitor{" + "size=" + size + '}';
    }
    
    

    @Override
    void display() {
        System.out.println("ID: " + id);
        System.out.println("Ten thiet bi: " + name);
        System.out.println("Nha san xuat: " + manu);
        System.out.println("Ngay nhap: " + importedDate);
        System.out.println("Kich co: " + size);
    }

    private Date SimpleDateFormat(String nextLine) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    
}