Java实现简单商品管理系统
需求如下
-
查看全部商品:
- 展示出所有的商品信息
-
添加商品:
-
键盘录入商品信息,封装为商品对象,并存入集合
-
需要检查商品号码是否存在,保证商品号唯一
-
-
删除商品:
-
根据商品号码进行删除
-
如果商品号不存在要给出提示
-
-
修改商品
-
根据商品号码做修改
-
如果商品号不存在要给出提示
-
-
查询单个商品
- 根据商品查询
-
输入错误
- 输入的命令不是 1 ~ 6 ,给出错误提示并继续输入
代码实现

Goods.java
package com.project.goodsManager;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Goods {
private int id;
private String name;
private double price;
private String number;
private GoodsType type;
private String introduction;
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", number='" + number + '\'' +
", type=" + type +
", introduction='" + introduction + '\'' +
'}' + "\n";
}
}
GoodsOperator.java
package com.project.goodsManager;
import java.util.ArrayList;
import java.util.Scanner;
public class GoodsOperator{
private static final ArrayList<Goods> list = new ArrayList<>();
static {
list.add(new Goods( 1, "phone", 500, "10", GoodsType.ELECTRIC, "phone"));
list.add(new Goods( 2, "knife", 15, "10", GoodsType.TOOL, "ipad"));
list.add(new Goods( 3, "juice", 8.5, "100", GoodsType.FOOD, "food"));
list.add(new Goods( 4, "sweater", 10, "100", GoodsType.CLOTH, "cloth"));
list.add(new Goods( 5, "paper", 20, "100", GoodsType.OTHER, "other"));
}
public static void main(String[] args){
boolean flag = false;
Scanner sc = new Scanner(System.in);
while (!flag){
menu();
System.out.println("请输入你的选择:");
try {
int choice = sc.nextInt();
flag = true;
judgment(choice);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally {
main(null);
}
}
}
public static void menu(){
System.out.println("-----------------------------");
System.out.println("---------1.查看全部商品---------");
System.out.println("---------2.添加商品------------");
System.out.println("---------3.删除商品------------");
System.out.println("---------4.修改商品------------");
System.out.println("---------5.查询商品------------");
System.out.println("---------6.退出---------------");
System.out.println("-----------------------------");
}
private static void judgment(int choice){
try {
if (choice <= 0 || choice >=7){
throw new CustomException("输入异常!");
}else {
switch (choice){
case 1 :
showAll();
break;
case 2 :
addGoods();
break;
case 3 :
deleteGoods();
break;
case 4 :
updateGoods();
break;
case 5 :
queryGoods();
break;
case 6 :
System.out.println("退出");
System.exit(0);
}
}
} finally {
main( null);
}
}
private static void queryGoods() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查询的商品编号:");
int id = 0;
try {
id = sc.nextInt();
for (Goods goods : list) {
if (goods.getId() == id){
System.out.println(goods);
System.out.println("查询成功!");
return;
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("没有此商品!");
}
private static void updateGoods() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的商品号:");
final int len = list.size();
int id;
try {
id = sc.nextInt();
for (int i = 0;i<idMax();i++){
if (list.get(i).getId() == id){
list.remove(i);
add();
System.out.println("修改成功!");
return;
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
System.out.println("没有此商品!");
updateGoods();
}
private static void deleteGoods() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的商品号:");
int id;
try {
id = sc.nextInt();
for (int i = 0;i <= idMax();i++){
if (list.get(i).getId() == id){
list.remove(i);
System.out.println("删除成功!");
main(null);
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally {
System.out.println("没有此商品!");
deleteGoods();
}
}
private static void addGoods() {
add();
System.out.println("添加成功!");
}
private static void showAll() {
System.out.println(list);
}
private static void add(){
Scanner sc = new Scanner(System.in);
int id;
while (true) {
try {
id = idExist();
if (id <=0){
throw new CustomException("输入有误!");
}else {
break;
}
} catch (Exception e){
e.printStackTrace();
}
}
System.out.println("请输入商品名称:");
String name = sc.next();
System.out.println("请输入商品价格:");
double price = sc.nextDouble();
System.out.println("请输入商品数量:");
String number = sc.next();
System.out.println("请输入商品类型,从以下类型中选一输入:");
System.out.println("FOOD,ELECTRIC,CLOTH,TOOL,OTHER");
String type = sc.next();
System.out.println("请输入商品介绍:");
String introduction = sc.next();
Goods goods = new Goods(id, name, price, number, GoodsType.valueOf(type), introduction);
list.add(goods);
}
public static int idMax(){
int max = list.getFirst().getId();
for (Goods goods : list) {
if (goods.getId() > max) {
max = goods.getId();
break;
}
}
return Math.max(max, list.size());
}
public static int idExist(){
Scanner sc = new Scanner(System.in);
int num;
try {
System.out.println("请输入商品编号:");
num = sc.nextInt();
for (Goods goods : list){
if (goods.getId() == num){
System.out.println("该商品编号已存在,请重新输入!");
return -1;
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return num;
}
}
GoodsType.java
package com.project.goodsManager;
public enum GoodsType {
FOOD,
ELECTRIC,
CLOTH,
TOOL,
OTHER
}
CustomException.java
package com.project.goodsManager;
public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
}
GoodsText.java
package com.project.goodsManager;
public class GoodsText {
public static void main(String[] args) {
GoodsOperator.main(null);
}
}
总结
1.欢迎交流
2.在Goods中选用了lombok
3.输入校验写了很多重复的代码,是可以单独抽出来作为代理
4.在判断商品编号id不能重复上,本人用了简单的自定义异常+循环来实现,这个可以用HashMap来实现
5.已将需求变为根据商品编号id来查询商品信息,这样增删改查都要对商品编号id来进行一个校验,就可以抽出一个GoodsId接口,然后让GoodsOperator来实现该接口(未实现)
6.欢迎指教

浙公网安备 33010602011771号