Java实现简单电脑管理系统(继承,尚未使用多态的方法来实现)

1、需求如下:
        
2、新建一个Computer的抽象类
package computer;

public abstract class Computer {
	private String name,brand,cpu,memory,hardDisk,monitor;
	
	public Computer(){
		
	}
	
	public Computer(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;
	}
	
	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;
	}
}
3、noteComp继承于Computer,但是battery为特有属性,另有一个show()方法,用于输出对象信息
package computer;

public class noteComp extends Computer{
	String battery;
	
	public noteComp(){
		
	}
	
	public noteComp(String name,String brand,String cpu,String memory,String hardDisk,String monitor,String battery){
		super(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.getName()+"\t"+this.getBrand()+"\t"+this.getCpu()+"\t"+this.getMemory()+"\t"+this.getHardDisk()+"\t"+this.getMonitor()+"\t"+this.getBattery()+"\t");
	}
}
4、deskTop类继承于Computer类,hostType为特有属性,另有一个show()方法,用于输出对象信息
package computer;

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 name,String brand,String cpu,String memory,String hardDisk,String monitor,String hosttype) {
		super(name,brand,cpu,memory,hardDisk,monitor);
		this.hosttype=hosttype;
	}
	
	public void show() {
		System.out.println(this.getName()+"\t"+this.getBrand()+"\t"+this.getCpu()+"\t"+this.getMemory()+"\t"+this.getHardDisk()+"\t"+this.getMonitor()+"\t\t"+this.getHosttype()+"\t");
	}
}

5、Test类,含有程序入口main()

package computer;
import java.util.Scanner;

public class Test {
	public static void main(String [] args) {
		String name,brand,cpu,memory,hardDisk,monitor,battery,hosttype;
		Scanner input=new Scanner(System.in);
		
		noteComp[] noteArrary= new noteComp[10];//noteCOMP数组
		deskTop[] deskArrary=new deskTop[10];//deskTop数组
		
		boolean isExit=false;
		int choose=-1;
		int noteCount=0;//确定noteComp数量
		int deskCount=0;//确定deskTop数量
		int num=0;//区分是否退出系统
		
		System.out.println("*********欢迎使用电脑管理系统**********");
		while (num==0) {
			System.out.println("1、查看电脑信息");
			System.out.println("2、增加电脑信息");
			System.out.println("3、删除电脑信息");
			System.out.println("******************");
			choose=input.nextInt();
			int i=0;
			switch(choose) {
			case 1://查看电脑信息
				if(noteCount>0||deskCount>0) {//查看是否有电脑
					System.out.println("序号\t型号\t品牌\tcpu\t内存\t外存\t显示器\t芯数\t主机");
					for(i=0;noteArrary[i]!=null;i++){
						System.out.print(i+1+"\t");
						noteArrary[i].show();
					}
					for(int j=0;deskArrary[j]!=null;j++){
						System.out.print((j+i+1)+"\t");
						deskArrary[j].show();
					}
					
					int flag=1;//若输入序号不对则一直循环
					while(flag==1) {
						System.out.println("请输入要查看的序号:");
						int n=input.nextInt();
						if (n<=(noteCount+deskCount)&&n>0) {
							System.out.println("您要查看的信息为:");
							if(n<=noteCount){
								noteArrary[n-1].show();
								flag=0;
								break;
							}
							else{
								deskArrary[n-noteCount-1].show();
								flag=0;
								break;
							}
						}
						else {
							System.out.println("序号不正确");
							continue;
						}
					}
				}
				else {
					System.out.println("无电脑");
				}
				break;
			case 2:
				System.out.println("请输入电脑类型:");
				System.out.println("1、笔记本");
				System.out.println("2、台式机");
				int choose2=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(choose2==1){//若是笔记本
					System.out.println("请选择电池芯数:1、6芯 \t\t\t  2、9芯");
					battery=input.next();
					if(battery.equals("1")){
						battery="6芯";
					}
					else{
						battery="9芯";
					}
					noteArrary[noteCount]=new noteComp(name,brand,cpu,memory,hardDisk,monitor,battery);
					noteCount++;
					System.out.println("添加成功!\n当前电脑信息如下:");
					System.out.println("序号\t型号\t品牌\tcpu\t内存\t外存\t显示器\t芯数\t主机");
					for(i=0;noteArrary[i]!=null;i++){
						System.out.print(i+1+"\t");
						noteArrary[i].show();
					}
					for(int j=0;deskArrary[j]!=null;j++){
						System.out.print((j+i+1)+"\t");
						deskArrary[j].show();
					}
				}
				else {//若是台式机
					System.out.println("请选择主机类型:1、立式 \t\t\t  2、卧式");
					hosttype=input.next();
					if(hosttype.equals("1")){
						hosttype="立式";
					}
					else{
						hosttype="卧式";
					}
					deskArrary[deskCount]=new deskTop(name,brand,cpu,memory,hardDisk,monitor,hosttype);
					System.out.println("添加成功!\n当前电脑信息如下:");
					deskCount++;
					System.out.println("序号\t型号\t品牌\tcpu\t内存\t外存\t显示器\t芯数\t主机");
					for(i=0;noteArrary[i]!=null;i++){
						System.out.print(i+1+"\t");
						noteArrary[i].show();
					}
					for(int j=0;deskArrary[j]!=null;j++){
						System.out.print((j+i+1)+"\t");
						deskArrary[j].show();
					}
				}
				break;
				
			case 3:
				System.out.println("序号\t型号\t品牌\tcpu\t内存\t外存\t显示器\t芯数\t主机");
				//输出
				for(i=0;noteArrary[i]!=null;i++){
					System.out.print(i+1+"\t");
					noteArrary[i].show();
				}
				for(int j=0;deskArrary[j]!=null;j++){
					System.out.print((j+i+1)+"\t");
					deskArrary[j].show();
				}
				
				System.out.println("请输入待删除的序号:");
				int no=input.nextInt();
				if(no<=noteCount){
					for(i=no;i<=noteCount;i++){
						noteArrary[i-1]=noteArrary[i];
					}
					noteCount--;
				}
				else {
					for(i=no-deskCount;i<=deskCount;i++) {
						deskArrary[i-1]=deskArrary[i];
					}
					deskCount--;
				}
				System.out.println("删除成功!\n当前电脑信息如下:");
				System.out.println("序号\t型号\t品牌\tcpu\t内存\t外存\t显示器\t芯数\t主机");
				for(i=0;noteArrary[i]!=null;i++){
					System.out.print(i+1+"\t");
					noteArrary[i].show();
				}
				for(int j=0;deskArrary[j]!=null;j++){
					System.out.print((j+i+1)+"\t");
					deskArrary[j].show();
				}
				break;
				
			case 4:
				isExit=true;
				break;
				
			default :
				isExit=true;
				break;
			}
			
			if (!isExit) {
				System.out.print("输入0返回:");
				num = input.nextInt();
			} 
			else {
				break;
			}
		}
	}
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值