展开全部
// 产品类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
系统退出!
已赞过
已踩过<
你对这个回答的评价是?
评论
收起