package shangpin;
public class Goods {
private String id;//商品编号
private String name;//名称
private double price;//价格
private String miaoshu;//描述
@Override
//重写toString
public String toString() {
return "商品信息:"+id+","+name+","+price+","+miaoshu;
}
public Goods(String id, String name, double price, String miaoshu) {
this.id = id;
this.name = name;
this.price = price;
this.miaoshu = miaoshu;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getMiaoshu() {
return miaoshu;
}
public void setMiaoshu(String miaoshu) {
this.miaoshu = miaoshu;
}
}
package shangpin;
import java.util.List;
public class Zsgc {
//添加,删除,修改,单查询,展示全查
//添加(上架商品)
public static boolean add(Goods go, List<Goods> dd){
boolean f = true;
for (Goods bianhao : dd){
if (go.getId().equals(bianhao.getId())){
f = false;
break;
}
}
if (f){
dd.add(go);
}
return f;
}
//修改(调整价格)
public static boolean modify(Goods go,List<Goods> dd){
boolean f = false;
for (Goods bianhao : dd){
if (go.getId().equals(bianhao.getId())){
f = true;
bianhao.setName(go.getName());
bianhao.setPrice(go.getPrice());
bianhao.setMiaoshu(go.getMiaoshu());
break;
}
}
return f;
}
//删除(下架商品 )
public static boolean delete(String id,List<Goods> dd){
boolean f = false;
for (Goods bianhao : dd){
if (bianhao.getId().equals(id)){
dd.remove(bianhao);
f = true;
break;
}
}
return f;
}
//单查
public static Goods select(String id,List<Goods> dd){
Goods g = null;
for (Goods bianhao : dd){
if (bianhao.getId().equals(id)){
g = bianhao;
break;
}
}
return g;
}
//展示(商品列表)
public static void show(List<Goods> dd){
for (Goods bianhao : dd){
System.out.println(bianhao);
}
}
}
package shangpin;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Spin {
//添加静态字段
static List<Goods> dd = new ArrayList<Goods>();
static Scanner read = new Scanner(System.in);
public static void main(String[] args) {
dd.add(new Goods("11","汽水",3,"气泡水"));
dd.add(new Goods("12","饼干",8,"饿了就吃"));
dd.add(new Goods("13","辣条",5,"卫龙"));
//定义数据类型
int n = 0;
String id,name,miaoshu;
double price;
Goods g;
boolean f;
do {
System.out.println("欢迎使用999商品管理系统");
System.out.println("---商品列表请输入:1---");
System.out.println("---上架商品请输入:2---");
System.out.println("---下架商品请输入:3---");
System.out.println("---调整商品请输入:4---");
System.out.println("---单查某个商品请输入:5---");
System.out.println("---退出:0----");
System.out.print("请选择:");
n = read.nextInt();
switch (n) {
case 1:
System.out.println("所有商品列表为:");
System.out.println("-------编号 名称 价格 描述");
Zsgc.show(dd);
break;
case 2:
System.out.println("请输入新商品编号:");
id = read.next();
System.out.println("请输入新商品名称:");
name = read.next();
System.out.println("请输入新商品价格:");
price = read.nextDouble();
System.out.println("请输入新商品描述:");
miaoshu = read.next();
g = new Goods(id,name,price,miaoshu);
f = Zsgc.add(g,dd);
if (f) {
System.out.println("上架成功");
}else {
System.out.println("上架失败");
}
break;
case 3:
System.out.println("请输入要删除的商品编号:");
id = read.next();
f = Zsgc.delete(id,dd);
if (f){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
break;
case 4:
System.out.println("请输入要修改的商品编号");
id = read.next();
System.out.println("请输入要修改的商品名称");
name = read.next();
System.out.println("请输入要修改的商品价格");
price = read.nextDouble();
System.out.println("请输入要修改的商品描述");
miaoshu = read.next();
g = new Goods(id,name,price,miaoshu);
f = Zsgc.modify(g,dd);
if (f) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
break;
case 5:
System.out.println("请输入要查询的商品编号");
id = read.next();
g = Zsgc.select(id,dd);
if (g != null){
System.out.println(g);
}else {
System.out.println("没有此商品");
}
break;
}
}while (n != 0);
}
}
运行结果:
文章来源地址https://www.toymoban.com/news/detail-512434.html
文章来源:https://www.toymoban.com/news/detail-512434.html
到了这里,关于java商品管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!