创建一个Computer类,使用集合存储计算机对象,添加元素并查询

创建一个Computer类,属性为编号(即计算机的唯一标识)CPU型号,主板型号,硬盘大小,内存大小,显卡型号,价格;
使用集合存储计算机对象
为集合添加3个元素。
用户输入“编号”获取对应的Computer信息,并且输出具体的参数。

package wanzixi;
/**
*@author ShanHai:
*@version 创建时间:2021年8月16日 下午7:22:32
*/

public class Computer {
	public String CPU;
	public String ztype;//主板型号
	public int ysize;
	public int nsize;
	public String xtype;//显卡型号
	public double price;
	public Computer(String CPU, String ztype, int ysize, int nsize, String xtype, double price) {
		this.CPU = CPU;
		this.ztype = ztype;
		this.ysize = ysize;
		this.nsize = nsize;
		this.xtype = xtype;
		this.price = price;
	}
	@Override
	public String toString() {
		return "Computer [CPU=" + CPU + ", ztype=" + ztype + ", ysize=" + ysize + ", nsize=" + nsize + ", xtype="
				+ xtype + ", price=" + price + "]";
	}
	
}

测试类

package wanzixi;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
*@author ShanHai:
*@version 创建时间:2021年8月16日 下午7:26:05
*/

public class TestComputer {
	public static void main(String[] args) {
		Computer com1=new Computer("1001", "intel", 256, 128, "英特尔", 7500);
		Computer com2=new Computer("1002", "legion", 256, 128, "联想", 7500);
		Computer com3=new Computer("1003", "deer", 256, 128, "戴尔", 7500);
		List<Computer> c =new ArrayList<Computer>();
		c.add(com1);
		c.add(com2);
		c.add(com3);
		for(int i=0;i<c.size();i++){
			Computer com = c.get(i);
			System.out.println(com);
		}
		Scanner sc =new Scanner(System.in);
		System.out.print("请输入您要查找的Computer编号:");
		String a=sc.next();
		if(com1.CPU.contains(a)){
			System.out.println(com1);
		}else if(com2.CPU.contains(a)){
			System.out.println(com2);
		}else if(com3.CPU.contains(a)){
			System.out.println(com3);
		}else{
			System.out.println("您输入的编号不存在");
		}
	}
}

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShanHai山海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值