需求
1)菜单显示
2)商品信息展示
3)商品信息查询
4)退出系统
5)尽量提升系统的交互性
实现思路
1)将商品抽象为一个内部类,实现其基本方法
2)在商品系统类中,创建Scanner类的实例对象,用于交互,创建Goods类的集合,用来存储商品。
3)根据需求实现对应的方法
4)在构造方法中调用这些方法,并实现交互
实现代码
public class SystemOfGoods {
Scanner scanner = new Scanner(System.in);
List<Goods> goods = new ArrayList<>();
SystemOfGoods(){
menu();
}
public void menu(){
String command;
//给出提示信息
tips();
while(true){
//用户输入
System.out.print("input:");
command = scanner.next();
if(command.equals("quit")){//退出
break;
}else if(command.equals("help")){//提示
tips();
}else if(command.equals("show")){//商品信息显示
showGoodsArray();
}else if(command.equals("add")){//商品信息录入
addGoods();
}else if(command.equals("search")){//商品信息查询
searchGoods();
}else{
System.out.println("没有这个命令!");
}
}
}
public void init(){
}
//展示提示信息
public void tips(){
System.out.println("<-------提示------>");
System.out.println("输入quit:退出");
System.out.println("输入help:操作提示");
System.out.println("输入show:商品信息展示");
System.out.println("输入add:商品信息录入");
System.out.println("输入search:商品信息查询");
System.out.println("<---------------->");
}
//展示所有的商品信息
public void showGoodsArray(){
System.out.println("<---------------->");
System.out.println("所有商品信息展示如下:");
int i = 1;
for(Goods g:goods){
System.out.print(i + ",");
i++;
g.showGoods();
}
System.out.println("<---------------->");
}
//商品信息录入
public void addGoods(){
//定义变量
String name;
String price;
String quantity;
double p = 0;
int q = 0;
//提示输入信息
System.out.println("<-----商品录入----->");
System.out.print("请输入商品名:");
name = scanner.next();
System.out.print("请输入价格:");
price = scanner.next();
try{
p = Double.valueOf(price);
}catch (NumberFormatException e){
System.out.println("价格输入错误!请重新输入");
addGoods();
return;
}
System.out.print("请输入数量:");
quantity = scanner.next();
try{
q = Integer.valueOf(quantity);
}catch (NumberFormatException e){
System.out.println("数量输入错误!请重新输入");
addGoods();
return;
}
System.out.println("<---------------->");
//创建该商品
Goods g = new Goods(name,p,q);
//将商品添加入商品集合
goods.add(g);
}
//查找对应商品
public void searchGoods(){
String name;
//用于判断是否有该商品
boolean flag = false;
System.out.println("<---------------->");
System.out.print("请输入你要查找的商品名:");
name = scanner.next();
for(Goods g:goods){
if(g.name.equals(name)){
g.showGoods();
flag = true;
}
}
if(!flag){
System.out.println("没有你要找的商品");
}
System.out.println("<---------------->");
}
//内部商品类
static class Goods {
//商品名
private String name;
//商品价格
private double price;
//商品数量
private int quantity;
public Goods(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
//展示商品信息
public void showGoods(){
System.out.println("商品名:"+name+",价格:"+price+",数量:"+quantity);
// System.out.println(name + "\t" + String.valueOf(price) + "\t" + String.valueOf(quantity));
}
//修改商品价格
public void modifyPrice(double price){
if(price > 0 ){
this.price = price;
}
}
//增加商品数量
public void addQuantity(double addNumber){
this.quantity += addNumber;
}
//减少商品数量
public void reduceQuantity(double reduceNumber){
this.quantity -= reduceNumber;
}
}
public static void main(String[] args) {
SystemOfGoods system = new SystemOfGoods();
}
}
总结
1)实现该系统从代码上来说并不是很难,最重要的还是思路。
2)要先根据需求决定你要实现哪些主要的方法,再考虑这些主要的方法代码是否重复,若有重复可实现一些工具方法。
3)对于系统来说用户的交互性是比较重要的,一定要给出足够多的提示。
4)要仔细考虑一些运行时可能会出现的错误,如:int a = scanner.nextInt() 使用这个代码,即使你给出了提示,但是用户也有可能输入错误导致程序中断,因此这就需要换种思路.
5)在实现一些需求时,可能其中包含着另一个需求,可将其抽象出来,为器创建一个方法,到时候调用即可。