Java实现简单的电脑管理系统(多态)

1、需求如下

Java实现简单的电脑管理系统(多态)_第1张图片

2、问题解决

    (1)构建computer的抽象类,包含一个show()的方法

package CompMgrPoly;

public abstract class Computer {
	private String type,name,brand,cpu,memory,hardDisk,monitor;

	public Computer(){
		
	}
	
	public Computer(String type,String name,String brand,String cpu,String memory,String hardDisk,String monitor){
		this.name=name;
		this.brand=brand;
		this.cpu=cpu;
		this.hardDisk=hardDisk;
		this.memory=memory;
		this.monitor=monitor;
		this.type=type;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getBrand() {
		return brand;
	}
	
	public void setBrand(String brand) {
		this.brand = brand;
	}
	
	public String getCpu() {
		return cpu;
	}
	
	public void setCpu(String cpu) {
		this.cpu = cpu;
	}
	
	public String getMemory() {
		return memory;
	}
	
	public void setMemory(String memory) {
		this.memory = memory;
	}
	
	public String getHardDisk() {
		return hardDisk;
	}
	
	public void setHardDisk(String hardDisk) {
		this.hardDisk = hardDisk;
	}
	
	public String getMonitor() {
		return monitor;
	}
	
	public void setMonitor(String monitor) {
		this.monitor = monitor;
	}
	
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	public void show() {
		System.out.println(this.getName()+"\t"+this.getBrand()+"\t"+this.getCpu()+"\t"+this.getMemory()+"\t"+this.getHardDisk()+"\t"+this.getMonitor()+"\t");
	}

}

    (2)构建noteComp和deskTop两个类,均继承自Computer类,另外,对Computer类内的show方法进行重写(overwrite),使其可以分别打印出独有属性。

noteComp类

package CompMgrPoly;


public class noteComp extends Computer{
	String battery;
	
	public noteComp(){
		
	}
	
	public noteComp(String type,String name,String brand,String cpu,String memory,String hardDisk,String monitor,String battery){
		super(type,name,brand,cpu,memory,hardDisk,monitor);
		this.battery=battery;
	}
	
	public String getBattery() {
		return battery;
	}
	
	public void setBattery(String battery) {
		this.battery = battery;
	}
	
	public void show() {
		System.out.println(this.getType()+"\t"+this.getName()+"\t"+this.getBrand()+"\t"+this.getCpu()+"\t"+this.getMemory()+"\t"+this.getHardDisk()+"\t"+this.getMonitor()+"\t"+this.getBattery()+"\t");
	}
}
deskTop类

package CompMgrPoly;

public class deskTop extends Computer{
	String hosttype;
	
	public deskTop(){
		
	}
	
	public String getHosttype() {
		return hosttype;
	}
	
	public void setHosttype(String hosttype) {
		this.hosttype = hosttype;
	}
	
	public deskTop (String type,String name,String brand,String cpu,String memory,String hardDisk,String monitor,String hosttype) {
		super(type,name,brand,cpu,memory,hardDisk,monitor);
		this.hosttype=hosttype;
	}
	
	public void show() {
		System.out.println(this.getType()+"\t"+this.getName()+"\t"+this.getBrand()+"\t"+this.getCpu()+"\t"+this.getMemory()+"\t"+this.getHardDisk()+"\t"+this.getMonitor()+"\t\t"+this.getHosttype()+"\t");
	}
}

    (3)在test类中加入main()函数,作为程序入口,test类中新建一个Show()函数,可以展示所有电脑信息

package CompMgrPoly;
import java.util.Scanner;

public class Test {
	public void Show(Computer[] c) {
		System.out.println("序号\t类型\t型号\t品牌\tcpu\t内存\t外存\t显示器\t芯数\t主机");
			for(int i=0;i0) {
					test.Show(compArrary);
					int flag=1;
					while(flag==1) {
						System.out.println("请输入要查看的序号:");
						int n=input.nextInt();
						if (n<=count&&n>0) {
							System.out.println("您要查看的信息为:");
							System.out.println("类型\t型号\t品牌\tcpu\t内存\t外存\t显示器\t芯数\t主机");
							compArrary[n-1].show();
							flag=0;
						}
						else {
							System.out.println("序号不正确");
							continue;
						}
					}
				}
				else {
					System.out.println("No computer!");
				}
				break;
			case 2:
				System.out.println("请输入电脑类型:");
				System.out.println("1、笔记本");
				System.out.println("2、台式机");
				int chtype=input.nextInt();
				System.out.println("请输入型号名称:");
				name=input.next();
				System.out.println("请输入品牌名称:");
				brand=input.next();
				System.out.println("请输入CPU名称:");
				cpu=input.next();
				System.out.println("请输入内存:");
				memory=input.next();
				System.out.println("请输入硬盘:");
				hardDisk=input.next();
				System.out.println("请输入显示器尺寸:");
				monitor=input.next();
				
				if(chtype==1) {
					System.out.println("请选择电池芯数:1、6芯 \t\t\t  2、9芯");
					battery=input.next();
					if(battery.equals("1")){
						battery="6芯";
					}
					else{
						battery="9芯";
					}
					compArrary[count]=new noteComp("笔电",name,brand,cpu,memory,hardDisk,monitor,battery);
					count++;
					System.out.println("添加成功!当前电脑信息如下:");
					test.Show(compArrary);
				}
				else {
					System.out.println("请选择主机类型:1、立式 \t\t\t  2、卧式");
					hosttype=input.next();
					if(hosttype.equals("1")){
						hosttype="立式";
					}
					else{
						hosttype="卧式";
					}
					compArrary[count]=new deskTop("台式",name,brand,cpu,memory,hardDisk,monitor,hosttype);
					count++;
					System.out.println("添加成功!当前电脑信息如下:");
					test.Show(compArrary);
				}
				
				break;
				
			case 3:
				test.Show(compArrary);
				int flag=1;
				
				while(flag==1) {
					System.out.println("请输入要删除的序号:");
					int n=input.nextInt();
					if (n<=count&&n>0) {
						for(int i=n;i
3、程序运行结果

Java实现简单的电脑管理系统(多态)_第2张图片

Java实现简单的电脑管理系统(多态)_第3张图片

Java实现简单的电脑管理系统(多态)_第4张图片


你可能感兴趣的:(JAVA基础)