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;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、程序运行结果





  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己写的,望多指教。import java.io.*; import java.net.*; public class link implements Runnable { public ServerSocket serversocket=null;//服务器套接字对象 public Socket socket=null;//客户端套接字 public PrintWriter out=null;//输出流对象 public BufferedReader in=null;//输入流对象 public DataOutputStream tsf=null;//输出流对象 public DataInputStream rcv=null;//输入流对象 public FileOutputStream fos=null;//文件输入流 public FileInputStream fis=null;//文件输出流 byte[] buf; public int patch,patch1; link(){} //创立连接 public void Screateconnect() { try { serversocket=new ServerSocket(9999);//ServerSocket对象监听端口 socket=serversocket.accept();//ServerSocket对象监听到端口并创建套接字 serversocket.close(); } catch(IOException e) { System.out.print("create fail!"); } } public void Ccreateconnect(String ip) { try { socket=new Socket(ip,9999);//创建套接字 } catch(Exception e) { System.out.print("创立连接失败!"); } } //管道通信 //通信内容 //1.文件接发请求以及相关数据 //2.登录、退出信息 //3.视屏聊天请求 public void Cduct() { } public void Sduct() { output("ftn"+String.valueOf(patch)); } //进行聊天 public void output(String str) { try { out=new PrintWriter(socket.getOutputStream(),true);//打开输出流 out.println(str);//发送信息 } catch(Exception e) { System.out.print("输出流创建失败!"); } } public void run() { while(true) { try { in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//打开接受输入流 String str = in.readLine();//输出接受信息 if("ftn".equals(substring(0,3))) { patch1=Integer.parseInt(str.substring(4)); recevie(); } else { System.out.println(in.readLine()); } } catch(Exception e) { System.out.print("输入流创建失败!"); } } } //接受各类文件 public void transmission() { try { tsf=new DataOutputStream(socket.getOutputStream());//打开输出流 fis = new FileInputStream("E:\\程序\\java\\1.AVI"); buf = new byte[1024]; long streamnum=fis.available(); int c=(int) Math.floor(streamnum/1024); patch=(int)streamnum24; Sduct();//发出尾数 if(c>0) { for(int i=0;i<c;i++) { fis.read(buf); tsf.write(buf);//发送信息 tsf.flush(); } } byte[] buff=new byte[c]; System.out.print(buff.length+"\n"); fis.read(buff); tsf.write(buff);//发送信息 tsf.flush(); tsf.close(); fis.close(); } catch(Exception e) { e.printStackTrace(); } } public void recevie() { try { rcv=new DataInputStream(socket.getInputStream());//打开接受输入流 fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1.avi",true);System.out.print("12\n"); buf = new byte[1024]; int c=0; int j=0; if(rcv.available()>1024) { while((c=rcv.read(buf))!=-1) { j++; fos.write(buf); fos.flush(); if(rcv.available()<1024)break; } } buf = new byte[patch1]; while((c=rcv.read(buf))!=-1) { fos.write(buf); fos.flush(); } rcv.close(); fos.close(); } catch(Exception e) { System.out.print("输入流创建失败!2"); } } public void close() { try { in.close(); socket.close(); }catch(Exception ex) { System.out.print("关闭失败"); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值