编写一个Application程序 程序中包括计算机Computer类 生成几个Computer类对象并输出相关信息

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/06 17:32:16

编写一个Application程序 程序中包括计算机Computer类 生成几个Computer类对象并输出相关信息
编写一个Application程序 程序中包括计算机Computer类 生成几个Computer类对象并输出相关信息

编写一个Application程序 程序中包括计算机Computer类 生成几个Computer类对象并输出相关信息
写一个封装类,再另外写一个实现功能类(用的是java)
封装类:
package com.shxt.lesson9.homework;
/**
* 创建一个Computer类
要求成员变量包括如下:
(1)编号(即计算机的唯一标识)
(2)CPU型号,
(3)主板型号,
(4)硬盘大小,
(5)内存大小,
(6)显卡型号,
(7)价格,
方法包括如下:
(1)打印当前计算机的所有属性
(2)为计算机添加CPU型号
(3)为计算机添加主板型号
(4)为计算机添加硬盘大小
(5)为计算机添加内存大小
(6)为计算机添加显卡型号
(7)设置计算机价格
(8)获取计算机价格
* */
public class Computer {
private int code;
private String cpu;
private String mb;
private String ide;
private String mm;
private String gpu;
private int price;

public Computer(int code, String[] params) {
this.code = code;
cpu = params[0];
mb = params[1];
ide = params[2];
mm = params[3];
gpu = params[4];
price = Integer.parseInt(params[5]);
}

public void setMb(String mb) {
this.mb = mb;
}

public int getCode() {
return code;
}

public String toString() {
return "code:" + code
+ "| cpu: " + cpu
+ "| mb:" + mb
+ "| ide:" + ide
+ "| mm :" + mm
+ "| gpu:" + gpu
+ "| price:" + price;
}

}
实现功能类:
package com.shxt.lesson9.homework;
import java.util.ArrayList;
import java.util.Scanner;
/**
* (1)要求使用ArrayList存储计算机对象,
形如:ArrayList cList = new ArrayList();
(2)为ArrayList对象(例如:上面的cList)添加3个元素.
(3)模拟用户输入(Scanner对象)获取对应的Computer信息,并且输出具体的参数.
(4)遍历输出ArrayList中的Computer,同时输出Computer的详细信息.
* */
public class ComputerManager {
ArrayList cList = new ArrayList();

private int code = 1;

/**
* 添加一个Computer到cList
* i52350-p67-st500-kingston8G-GTX590-10000
* i72370-p67-st1000-kingston8G-GTX590-12000
* */
public boolean add() {
boolean bAdd = false;
System.out.println("请输入:格式为:cpu-mb-ide-mm-gpu-price");
Scanner scan = new Scanner(System.in);

String userPrint = scan.next();

String[] info = userPrint.split("-");

//实例化Computer的对象,第一个参数是computer的唯一标识,第二个参数是computer的属性
Computer c = new Computer(code, info);

cList.add(c);

code++;

System.out.println("添加成功!");
return bAdd;
}

/**
* 根据用户输入的code获取Computer信息
* */
public void getComputer() {
printAll();
System.out.println("请输入计算机的Code(编号)");
Scanner scan = new Scanner(System.in);
int userCode = scan.nextInt();
for (Computer temp : cList) {
if (temp.getCode() == userCode) {
System.out.println(temp);
System.out.println("根据提示输入:1.修改mb 2.修改ide");
int userCheck = scan.nextInt();
System.out.println("请输入商品型号");
String mCode = scan.next();
switch (userCheck) {
case 1:
temp.setMb(mCode);
break;
}
System.out.println(temp);
break;
}
}
}

/**
*
* */
public void printAll() {
for (Computer temp : cList) {
System.out.println(temp);
}
System.out.println("----------------------------------");
}

public void workflow() {
boolean bRun = true;
while (bRun) {
System.out.println("按提示输入:1.添加计算机 2.查询计算机 3.打印全部计算机 4.退出");
Scanner scan = new Scanner(System.in);
int userCheck = scan.nextInt();
switch (userCheck) {
case 1:
add();
break;
case 2:
getComputer();
break;
case 3:
printAll();
break;
case 4:
bRun = false;
System.out.println("退出成功!");
break;
}
}
}

public static void main(String[] args) {
ComputerManager cm = new ComputerManager();
cm.workflow();
}
}