java 用数组存储书籍信息_JAVA简单编程:使用数组保存书,CD,磁带等信息,并能实现插入,删除,查找功能...

展开全部

// 产品类public class Product {

private String name;

private String type;

private float price;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public float getPrice() {

return price;

}

public void setPrice(float price) {

this.price = price;

}}

// 测试类import java.io.IOException;

import java.util.Scanner;public class TestMain {

public Product getProductByKey(Product []productdb,Product p) {

for (Product product : productdb) {

if (product == null) {

continue;

}

if (product.getName().equals(p.getName())) {

return product;

}

if (product.getType().equals(p.getType())) {

return product;

}

if (product.getPrice() == p.getPrice()) {

return product;

}

}

return null;

}

public boolean add(Product []productdb, Product addP) {

for (int i = 0; i < productdb.length; i++) {

if (productdb[i] == null) {

productdb[i] = addP;

return true;

}

}

return false;

}

public boolean delete(Product []productdb, String name) {

for (int i = 0; i < productdb.length; i++) {

if (productdb[i].getName().equals(name)) {

productdb[i] = null;

return true;

}

}

return false;

}

public static void main(String args[]) throws IOException {

TestMain test = new TestMain();

Product []productdb = new Product[100];

int op = -1;

try {

do {

System.out.println("1.添加 2.查找 3.删除 0.退出");

System.out.println("请选择:");

Scanner cin=new Scanner(System.in);

op = cin.nextInt();

switch( op ) {

case 0:

System.out.println("系统退出!");

return;

case 1:

Product p = new Product();

System.out.println("请输入e5a48de588b662616964757a686964616f31333332636365产品名称:");

p.setName(cin.next());

System.out.println("请输入产品类型:");

p.setType(cin.next());

System.out.println("请输入产品价格:");

p.setPrice(cin.nextFloat());

boolean flagadd = test.add(productdb,p);

if (flagadd)

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

else

System.out.println("添加失败!");

break;

case 2:

Product searchP = new Product();

int search = -1;

String key = "";

System.out.println("请输入查询内容:1.按名称 2.按类型 3.按价格 4.列出所有产品");

search = cin.nextInt();

if (search == 1) {

System.out.println("请输入产品名称:");

searchP.setName(cin.next());

Product resultP = test.getProductByKey(productdb,searchP);

if (resultP != null) {

System.out.println("名称:" + resultP.getName()

+ ",类型:" + resultP.getType()

+ ",价格:" + resultP.getPrice());

} else {

System.out.println("没有匹配产品!");

}

} else if (search == 2) {

System.out.println("请输入产品类型:");

searchP.setType(cin.next());

Product resultP = test.getProductByKey(productdb,searchP);

if (resultP != null) {

System.out.println("名称:" + resultP.getName()

+ ",类型:" + resultP.getType()

+ ",价格:" + resultP.getPrice());

} else {

System.out.println("没有匹配产品!");

}

} else if (search == 3) {

System.out.println("请输入产品价格:");

searchP.setPrice(cin.nextFloat());

Product resultP = test.getProductByKey(productdb,searchP);

if (resultP != null) {

System.out.println("名称:" + resultP.getName()

+ ",类型:" + resultP.getType()

+ ",价格:" + resultP.getPrice());

} else {

System.out.println("没有匹配产品!");

}

} else if (search == 4) {

for (Product product : productdb) {

if (product != null) {

System.out.println("名称:" + product.getName()

+ ",类型:" + product.getType()

+ ",价格:" + product.getPrice());

}

}

}

break;

case 3:

System.out.println("请输入要删除的产品名称:");

String name = cin.next();

boolean flagdel = test.delete(productdb, name);

if (flagdel)

System.out.println("删除成功!");

else

System.out.println("删除失败!");

break;

}

} while (op != 0);

} catch (Exception e) {

System.out.println("输入了无效值!系统退出!");

e.printStackTrace();

}

}}

运行结果:1.添加 2.查找 3.删除 0.退出

请选择:

1

请输入产品名称:

三国演义

请输入产品类型:

请输入产品价格:

58.9

添加成功!

1.添加 2.查找 3.删除 0.退出

请选择:

1

请输入产品名称:

水浒传

请输入产品类型:

请输入产品价格:

66.7

添加成功!

1.添加 2.查找 3.删除 0.退出

请选择:

2

请输入查询内容:1.按名称 2.按类型 3.按价格 4.列出所有产品

1

请输入产品名称:

三国演义

名称:三国演义,类型:书,价格:58.9

1.添加 2.查找 3.删除 0.退出

请选择:

2

请输入查询内容:1.按名称 2.按类型 3.按价格 4.列出所有产品

4

名称:三国演义,类型:书,价格:58.9

名称:水浒传,类型:书,价格:66.7

1.添加 2.查找 3.删除 0.退出

请选择:

3

请输入要删除的产品名称:

水浒传

删除成功!

1.添加 2.查找 3.删除 0.退出

请选择:

0

系统退出!

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值