需求:实现一商城,主要有店铺和商品栏,商品栏中有商品。商品的信息包括商品名、商品的描述、价格、库存、上架时间。
tips:
1.商品使用自定义类,在设置具体属性值时进行判断抛出自定义异常,减少测试类中代码逻辑判断。
2.商城使用HashMap,键储存店铺,值储存商品栏,商品栏为单列集合,使用ArraList存储商品信息。
3.利用正则表达式、捕获异常、等对输入进行校验,使用while+continue循环进行重复判断。
4.遇到复杂的逻辑不要慌,层层递进,在实现整体逻辑的基础,逐步加深局部逻辑。
详细代码如下:
1.测试类
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class ShoppingSystemTest {
// 创建商品
static HashMap<String, ArrayList<Goods>> hm = new HashMap<>();
static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");
static Scanner sc = new Scanner(System.in);
static {
Goods s1 = new Goods("洗发水", "防脱发", 10.5, 100, sdf.format(new Date()));
Goods s2 = new Goods("香皂", "洗澡", 5, 50, sdf.format(new Date()));
Goods s3 = new Goods("牙膏", "漱口", 15, 100, sdf.format(new Date()));
ArrayList<Goods> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
hm.put("生活区", list);
}
public static void main(String[] args) {
while (true) {
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("请输入:");
// 获取用户输入
String choose;
while (true){
choose = sc.nextLine();
if(choose.matches("[1-6]")){
break;
}else{
System.out.println("请输入1-6数字");
continue;
}
}
int ch = Integer.parseInt(choose);
switch (ch) {
case 1 -> findAllGoods();
case 2 -> addGoods();
case 3 -> deleteGoods();
case 4 -> findGoods();
case 5 -> modifiedGoods();
case 6 -> System.exit(0);
}
}
}
/*功能5 修改某个商品*/
private static void modifiedGoods() {
while (true) {
String name;
while (true) {
try {
System.out.println("请输入店铺名称");
name = sc.nextLine();
if(!name.matches(".+")){
throw new EmptyNameException("店铺名称为空");
}
break;
} catch (EmptyNameException e) {
System.out.println("店铺名称不能为空");
continue;
}
}
// 在HashMap中查询店铺名
if (hm.containsKey(name)) {
//存在 获取店铺的商品栏
ArrayList<Goods> goodsList = hm.get(name);
System.out.println("输入商品名");
String nameGoods=sc.nextLine();
int count=0;
for (int i = 0; i < goodsList.size(); i++) {
count++;
Goods goods =goodsList.get(i);
if( goods.getName().equals(nameGoods)){//找到该商品
String input;
while (true) {
System.out.println("按1修改商品名称");
System.out.println("按2修改商品描述");
System.out.println("按3修改商品价格");
System.out.println("按4修改商品库存");
System.out.println("按5修改商品上架日期");
input=sc.nextLine();
if(input.matches("[1-5]")){
break;
}
else {
System.out.println("请输入1-5");
continue;}
}
switch (input){
case "1":
while (true) {
try {
System.out.println("输入商品新名称");
String newName = sc.nextLine();
goods.setName(newName);
break;
} catch (EmptyNameException e) {
e.printStackTrace();
continue;
}
}
break;
case "2":
System.out.println("请输入商品新的描述");
String desGoods = sc.nextLine();
if (desGoods.matches(".+")) {
goods.setDes(desGoods);
} else {
goods.setDes("无");
}
break;
case "3":
while (true) {
try {
System.out.println("请输入新价格(元)");
String inputPrice = sc.nextLine();
if (inputPrice.matches("[0-9]+")) {
Double priceGoods = Double.parseDouble(inputPrice);
goods.setPrice(priceGoods);
break;
} else {
System.out.println("输入不合法");
continue;
}
} catch (ErrorPriceException e) {
e.printStackTrace();
continue;
}
}
break;
case "4":
while (true) {
try {
System.out.println("请输入商品新库存数");
//判断是否输入的数字
String inputNumber = sc.nextLine();
if (inputNumber.matches("[0-9]+")) {
int countGoods = Integer.parseInt(inputNumber);
goods.setCount(countGoods);
break;
} else {
System.out.println("请输入阿拉伯数字");
continue;
}
} catch (CountException e) {
e.printStackTrace();
continue;
} }
break;
case "5":
System.out.println("输入上新时间,(yyyy年MM月dd日 HH:mm:ss E)");
String newTimeStr=sc.nextLine();
// 字符串转日期格式
try {
Date newTime = sdf.parse(newTimeStr);
goods.setTime(sdf.format(newTime));
} catch (ParseException e) {
e.printStackTrace();
}
break;
}
}
}
if(count==goodsList.size()-1){ System.out.println("没有该商品");}
break;
}
else{
System.out.println("店铺不存在,重新输入店铺y,退出n");
String input=sc.nextLine();
if("y".equalsIgnoreCase(input)){
continue;
}else if("n".equalsIgnoreCase(input)){
System.out.println("退出");
System.exit(0);
}
else {
System.out.println("没有这种选项");
continue;
}
}
}
}
/*功能4 查询某个商品*/
private static void findGoods() {
System.out.println("请输入你想要的商品名");
String input=sc.nextLine();
// 循环所有店铺,再循环商品栏,查看有无该商品名是
Set<String> keySet = hm.keySet();
int count=1;
for (String name : keySet) {
// name为店铺名,goodsList为该店铺对应的商品栏
ArrayList<Goods> goodsList=hm.get(name);
for (Goods goods : goodsList) {//遍历商品栏查找对应商品
if(goods.getName().equals(input)) {//找到对应商品,输出
System.out.println("商品所在店铺为" + name);
System.out.println( goods);
count=0;
}
}
}
if(count==1){System.out.println("没有该商品");}
}
/*功能3 删除商品*/
private static void deleteGoods() {
// 首先获取店铺名
String name;
while (true) {
try {
System.out.println("请输入店铺名称");
name = sc.nextLine();
if(!name.matches(".+")){
throw new EmptyNameException("店铺名称为空");
}
break;
} catch (EmptyNameException e) {
System.out.println("店铺名称不能为空");
continue;
}
}
// 在HashMap中查询店铺名
if (hm.containsKey(name)) {
//存在,获取商品集合
ArrayList<Goods> goodsList = hm.get(name);
System.out.println("需要删除的商品名");
String nameGoods=sc.nextLine();
// 在商品ArrayList集合中找该商品
int count=0;
for (int i = 0; i < goodsList.size(); i++) {
count++;
Goods goods = goodsList.get(i);//循环获取单个商品对象
if((goods.getName()).equals(nameGoods)){//判断商品名称是否需要删除
goodsList.remove(i);
System.out.println("删除" + nameGoods);
break;
}
}
if(count==goodsList.size()-1){System.out.println("没找到商品");}
}else{
System.out.println("没有该店铺");
System.exit(0);
}
}
/*功能2 上架商品*/
private static void addGoods() {
// 首先获取店铺名
String name;
while (true) {
try {
System.out.println("请输入你要上新商品的店铺名称");
name = sc.nextLine();
if(!name.matches(".+")){
throw new EmptyNameException("店铺名称为空");
}
break;
} catch (EmptyNameException e) {
System.out.println("店铺名称不能为空");
continue;
}
}
// 在HashMap中查询店铺名
if (hm.containsKey(name)) {
//存在
ArrayList<Goods> goodsList = hm.get(name);
Goods goods = new Goods();
while (true) {
try {
System.out.println("请输入新商品的名称");
String nameGoods = sc.nextLine();
goods.setName(nameGoods);//设置try...catch
break;
} catch (EmptyNameException e) {
e.printStackTrace();
continue;
}
}
System.out.println("请输入商品描述");
String desGoods = sc.nextLine();
if (desGoods.matches(".+")) {
goods.setDes(desGoods);
} else {
goods.setDes("无");
}
while (true) {
try {
System.out.println("请输入价格(元)");
String input = sc.nextLine();
if (input.matches("[0-9]+")) {
Double priceGoods = Double.parseDouble(input);
goods.setPrice(priceGoods);
break;
} else {
System.out.println("请输入阿拉伯数字");
continue;
}
} catch (ErrorPriceException e) {
e.printStackTrace();
continue;
}
}
while (true) {
try {
System.out.println("请输入个数");
//判断是否输入的数字
String input = sc.nextLine();
if (input.matches("[0-9]+")) {
int countGoods = Integer.parseInt(input);
goods.setCount(countGoods);
break;
} else {
System.out.println("请输入阿拉伯数字");
continue;
}
} catch (CountException e) {
e.printStackTrace();
continue;
} }
// 自动生成上新时间
Date time = new Date();
goods.setTime(sdf.format(time));
// 添加商品信息无误,添加至商品
goodsList.add(goods);
System.out.println("添加成功");
} else {
while (true) {
System.out.println("不存在,是否添加新店铺(Y/N)");
String input=sc.nextLine();
if("y".equalsIgnoreCase(input)){
hm.put(name,new ArrayList<>());//设置店铺名称为HashMap的键,同时创建Arraylist容器用于存储商品
System.out.println("添加成功");
break;
}
else if("n".equalsIgnoreCase(input)){
System.out.println("退出");
System.exit(0);
}else{
System.out.println("没有该选项");
continue;}
}
}
}
/*功能1 查看全部商品*/
private static void findAllGoods() {
hm.forEach((String s, ArrayList<Goods> goodslist) -> {
// s为店铺名,goodslist为商品架,商品在商品架里
System.out.println("+++++++" + s + "+++++++");
for (Goods goods : goodslist) {
System.out.println("商品名称:" + goods.getName());
System.out.println("描述:" + goods.getDes());
System.out.println("价格:" + goods.getPrice()+"(元)");
System.out.println("库存:" + goods.getCount());
System.out.println("上架时间:" + goods.getTime());
System.out.println("--------------------------------------");
}
});
}
}
2.自定义商品类
public class Goods {
// 商品类
private String name;
private String des;
private double price;
private int count;
private String time;
// 构造方法
public Goods() {
}
public Goods(String name, String des, double price, int count, String time) {
this.name = name;
this.des = des;
this.price = price;
this.count = count;
this.time = time;
}
// set/get
// 商品名
public String getName() {
return name;
}
public void setName(String name) {
if(name.matches(".+")){
this.name = name;}
else{
throw new EmptyNameException("商品名不能为空");
}
}
// 商品描述
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;}
// 商品价格
public double getPrice() {
return price;
}
public void setPrice(double price) {
if(price<0){
throw new ErrorPriceException("价格不能为负");
}
else{
this.price = price;}
}
// 商品个数
public int getCount() {
return count;
}
public void setCount(int count) {
if(count<=0){
throw new CountException("个数不能小于0");
}
else{
this.count = count;
}
}
// 商品时间
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
// 重新tostring
@Override
public String toString() {
return "{" +
"'" + name + '\'' +
", 描述:'" + des + '\'' +
", 价格:" + price +
", 库存:" + count +
",上架时间:" + time +
'}';
}
}
3.自定义异常
public class ErrorPriceException extends RuntimeException{
public ErrorPriceException() {
}
public ErrorPriceException(String message) {
super(message);
}
}
public class EmptyNameException extends RuntimeException {
public EmptyNameException() {
}
public EmptyNameException(String message) {
super(message);
}
}
public class CountException extends RuntimeException {
public CountException() {
}
public CountException(String message) {
super(message);
}
}