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

本文介绍了一个电脑管理系统的实现过程,包括构建抽象基类Computer及其派生类noteComp和deskTop,通过重写show方法来展示不同类型的电脑信息。此外,还提供了一个测试类,用于展示所有电脑的信息,并实现了增加、删除和查看电脑信息的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;i<count;i++) {
				System.out.print(i+1+"\t");
				c[i].show();
			}
	}
	static int count=0;//全局变量
	public static void main(String [] args) {
		String name,brand,cpu,memory,hardDisk,monitor,battery,hosttype;
		Scanner input=new Scanner(System.in);
		Computer[] compArrary=new Computer[10];
		boolean isExit=false;
		int choose=-1;
		int num=0;//区分是否退出系统
		Test test=new Test();
		System.out.println("*********欢迎使用电脑管理系统**********");
		while (num==0) {
			System.out.println("1、查看电脑信息");
			System.out.println("2、增加电脑信息");
			System.out.println("3、删除电脑信息");
			System.out.println("4、退出系统");
			System.out.println("******************");
			choose=input.nextInt();
			switch(choose) {
			case 1://查看电脑信息
				if(count>0) {
					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<compArrary.length;i++) {
							compArrary[i-1]=compArrary[i];
						}
						count--;
						System.out.println("删除成功!当前电脑信息如下:");
						test.Show(compArrary);
						flag=0;
					}
					else {
						System.out.println("序号不正确");
						continue;
					}
				}
				
				break;
				
			case 4:
				isExit=true;
				break;
				
			default :
				isExit=true;
				break;
			}
			
			if (!isExit) {
				System.out.print("输入0返回:");
				num = input.nextInt();
			} 
			else {
				break;
			}
		}
		input.close();
	}
}
3、程序运行结果





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值