学校作业
参考师兄的
题目:
类的设计应用:
算机组装店有很多品牌的计算机配件,包括处理器(CPU)、主板(mainBoard)、内存(Ram)、……等。 每种配件都多个厂家(company)、型号(type)、价格(price)三个基本信息。
按下图所示的类的关系和要求编写适当的类,实现相关的功能。
1、假设每种配件有两个厂家,每个厂家产品有两个型号,每个型号价格不同
2、程序运行时逐个显示不同配件的型号,提供 配件型号选择菜单(如输入1,代表选择型号1,输入2,代码选择型号2)
3、根据选择的配件型号,创造出一个Computer实例,
4、 创建实例完成时,输出总价格
5、调用Conputer实例的showDetail()方法,输出输出配件的详细信息。
根据类图完成类的设置,并在Test类的main方法中测试你的程序
父类:“产品”
package theme5_2;
public class Product {
private String name;//产品名称
private String company;
private String type;
private double price;
public Product(String company, String name, String type, double price) {
super();
this.name = name;
this.company = company;
this.type = type;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return ":[制造商=" + company + ", 产品名称=" + name + ", 型号=" + type + ", 价格=" + price + "]";
}
}
CPU信息
package theme5_2;
public class CPU extends Product {
private String buffer;//CPU的缓存
public CPU( String company,String name, String type, double price, String buffer) {
super(company, name, type, price);
this.buffer = buffer;
}
public String getBuffer() {
return buffer;
}
public void setBuffer(String buffer) {
this.buffer = buffer;
}
public String toString() {
String superStr = super.toString();//先将前面那一长条穿下来给它,为了后面更好的加上“缓存”这个数据
return "CPU" + superStr.substring(0,superStr.length()-1)+",缓存="+buffer+"]";//填写CPU信息
}
}
屏幕信息
package theme5_2;
public class Screen extends Product {
private String size;//屏幕尺寸
public Screen( String company, String name, String type, double price, String size) {
super( company, name, type, price);
this.size = size;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String toString() {
String superStr = super.toString();//先将前面那一长条穿下来给它,为了后面更好的加上“缓存”这个数据
return "显示器" + superStr.substring(0,superStr.length()-1)+",尺寸="+size+"]";//填写CPU信息
}
}
组装电脑
package theme5_2;
public class Computer extends Product {
private CPU cpu;
private Screen screen;
public Computer( String company, String name, String type, double price, CPU cpu, Screen screen) {
super( company, name, type, price);
this.cpu = cpu;
this.screen = screen;
}
public CPU getCpu() {
return cpu;
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
public Screen getScreen() {
return screen;
}
public void setScreen(Screen screen) {
this.screen = screen;
}
public String toString() {
return "电脑"+super.toString();
}
public void showDetail() {
System.out.println("配件:");
if(cpu!=null)
System.out.println(cpu);
if(screen!=null)
System.out.println(screen);
}
}
购买电脑
package theme5_2;
import java.util.Scanner;
public class Shop {
//商店配件
private CPU[] cpus = new CPU[2];
private Screen[] screens = new Screen[2];
//构造方法,商店有的东西
public Shop() {
super();
cpus[0] = new CPU("Intel", "CPU", "I7-7600", 1500, "2M");
cpus[1] = new CPU("Intel", "CPU", "I5-7600", 1000, "1M");
screens[0] = new Screen("KTC", "显示器", "KTC2400", 1500, "24英寸");
screens[1] = new Screen("联想", "显示器", "21寸高清", 1700, "21英寸");
}
public void Sevice() {
System.out.println("欢迎光临,HL先生!");
System.out.println("请您选择您喜欢的配件,组装您的军工级神机!");
double cost = 0;//总花费
Scanner sc = new Scanner(System.in);
showScreens();
int a = sc.nextInt();
Screen screen = null;
cost+=screens[a-1].getPrice();
screen = screens[a-1];
showCpus();
int b = sc.nextInt();
CPU cpu = null;
cost+=cpus[b-1].getPrice();
cpu = cpus[b-1];
Computer com = new Computer("HL先生", "军工级神机", "华师NB_Plus++", cost, cpu, screen);
System.out.println(com);
com.showDetail();
}
private void showScreens() {
for(int i=0; i<screens.length; i++)
System.out.println((i+1)+":"+screens[i]);
System.out.println("请选择您中意的显示器:");
}
private void showCpus() {
for(int i=0; i<cpus.length; i++)
System.out.println((i+1)+":"+cpus[i]);
System.out.println("请选择您niubility的CPU:");
}
}
测试
package theme5_2;
public class Test {
public static void main(String[] args) {
Shop shop = new Shop();
shop.Sevice();
}
}
最后成品
PS:我的跟师兄比起来,模仿师兄的,简化了一些,只是为了完成作业。
当然,在模仿前任的过程中,慢慢自己也理解了很多,感谢师兄@passerbyYSQ贡献的答案,让后生我从中收获颇丰!