从整体上来说,这个简易系统分为以下四个部分:店主、顾客、关于、退出。各部分应该实现的功能如下图:
整体框架代码
public static Scanner reader = new Scanner(System.in);
public static void main(String[] args) {
Boolean condi = true;
GoodsCenter.initGoodsArray();
while (condi) {
beginMune();
System.out.println("请选择您的操作:");
String id = reader.nextLine();
switch (id) {
case "S": //店主操作
case "s": {
owner();
continue;
}
case "C": //顾客操作
case "c": {
customer();
break;
}
case "A": //关于
case "a": {
abort();
break;
}
case "Q": //退出系统
case "q": {
quitMenu();
condi = false;
break;
}
default: {
System.out.println("输入有误,请重新输入:");
break;
}
}
}
reader.close();
}
框架很简单,有了框架之后,接着实现各个接口的工作就可以了。
开始、退出、关于菜单
//开始菜单
public static void beginMune() {
System.out.println("*********************************");
System.out.println("******welcome to the market******");
System.out.println("*********************************");
System.out.println("*********************************");
System.out.println("*****[S]店主 [C]顾客*****");
System.out.println("*****[A]关于 [Q]退出*****");
System.out.printf("*********************************\n\n");
}
//退出菜单
public static void quitMenu() {
System.out.println("*********************************");
System.out.println("***********欢迎下次光临**********");
System.out.printf("*********************************\n\n");
}
//关于
public static void abort(){
System.out.println("*********************************");
System.out.println("**** author:cottonrose ****");
System.out.println("****e-mail:1320210577@qq.com****");
System.out.println("**** version:1.1.0 ****");
System.out.println("**** date:2018-12-01 ****");
System.out.printf("*********************************\n\n");
}
读取输入流信息
在java中,读取输入流的信息需要通过Scanner来实现,通过Scanner读取控制台用户输入的一行信息,将该信息转换为商品类型,并存放入对象中,来实现对商品的控制。代码如下:
public static Goods readGoods(){
while(true){
String goods = reader.nextLine(); //读取一行信息存入到goods中
goods = goods.trim(); //将信息中的前后空格去掉
String[] goodInfo = goods.split(" "); //以信息中空格为界限,将信息存入一个字符串数组中
if(goodInfo!=null && goodInfo.length==6 || goodInfo.length==2){ //当字符数组中有6个或2个元素时,执行以下代码
if(goodInfo.length==6) { //创建一个商品类对象,内容是用户输入的商品信息
Goods good = new Goods(
Integer.parseInt(goodInfo[0]),
goodInfo[1],
Float.parseFloat(goodInfo[2]),
Integer.parseInt(goodInfo[3]),
LocalDate.parse(goodInfo[4].subSequence(0,goodInfo[4].length())),
LocalDate.parse(goodInfo[5].subSequence(0,goodInfo[5].length())),
LocalDate.now()
);
return good; //返回该商品
}
if(goodInfo.length==2){
Goods good = new Goods(Integer.parseInt(goodInfo[0]),goodInfo[1]);
return good;
}
}else{
System.out.println("格式有误,请重新输入");
}
}
}
店主操作
public static void owner() {
boolean con = true;
while (con){
ownerMenu(); //打印店主操作菜单
int num = GoodsCenter.isToTime(); //判断商品是否到期
if(num!=0){
System.out.printf("上架商品中出现过期商品共"+num+"件,请及时下架!\n");
}
System.out.println("请选择您的操作:");
String op = reader.nextLine();
switch (op) {
case "P": //商品上架
case "p": {
GoodsCenter.printGoods();
System.out.println("请输入上架商品信息:格式如下:(不用填写上架日期)");
System.out.println("1 面包 5 20 2018-02-03 2018-11-15");
Goods goods = readGoods();
GoodsCenter.addGood(goods);
GoodsCenter.printGoods();
break;
}
case "D": //商品下架
case "d": {
GoodsCenter.printGoods();
System.out.println("请输入下架商品编号及商品名称:格式如下:");
System.out.println("1 面包");
Goods goods = readGoods();
GoodsCenter.deleteGood(goods);
GoodsCenter.printGoods();
break;
}
case "M": //修改商品信息
case "m": {
GoodsCenter.printGoods();
System.out.println("请输入要修改的商品编号及商品名称:格式如下:");
System.out.println("1 面包");
Goods goods = readGoods();
GoodsCenter.modifyGood(goods);
GoodsCenter.printGoods();
break;
}
case "Q": //退出店主模块
case "q": {
con = false;
break;
}
default: {
System.out.println("输入有误,请重新输入:");
break;
}
}
}
}
//店主菜单
public static void ownerMenu() {
System.out.println("*********************************");
System.out.println("***** 店主 *****");
System.out.println("*********************************");
System.out.println("*****[P]上架 [D]下架*****");
System.out.println("*****[M]修改 [Q]退出*****");
System.out.printf("*********************************\n\n");
}
顾客操作
public static void customer(){
boolean con = true;
Order order = new Order(); //创建一个订单对象,将顾客选购的商品放入该对象中
while(con){
Scanner input = new Scanner(System.in);
customerMenu(); //打印顾客操作菜单
switch (input.nextLine()){
case"A": //顾客选购商品
case"a":{
GoodsCenter.printGoods();
System.out.println("请选择要购买的商品:格式如下:");
System.out.println("1 2 (商品编号 数量)");
String[] info = input.nextLine().split(" ");
if(info!=null && info.length==2){
Goods goods = GoodsCenter.getGood(Integer.parseInt(info[0]));
if(goods!=null){
order.add(Integer.parseInt(info[0]),Integer.parseInt(info[1]));
order.printOrder();
}else{
System.out.println("商品不存在,请重新选择");
continue;
}
}else{
System.out.println("输入格式有误,请重新输入");
continue;
}
break;
}
case"S": //打印订单
case"s":{
order.printOrder();
break;
}
case"M": //修改商品订单
case"m":{
System.out.println("请选择需要修改的商品:格式如下:");
System.out.println("1 2 (商品编号 要减少的数量)");
String[] info = input.nextLine().split(" ");
if(info!=null && info.length==2){
if(order.isInOrder(Integer.parseInt(info[0]))){
order.modify(Integer.parseInt(info[0]),Integer.parseInt(info[1]));
order.printOrder();
}else{
System.out.println("该商品目前不在商品订单中,请重新输入");
continue;
}
}else{
System.out.println("输入格式有误,请重新输入");
continue;
}
break;
}
case"Q": //退出顾客操作模块
case"q":{
con = false;
break;
}
default:{
System.out.println("输入有误,请重新输入");
}
}
input.close();
}
}
//顾客菜单
public static void customerMenu(){
System.out.println("*********************************");
System.out.println("***** 顾客 *****");
System.out.println("*********************************");
System.out.println("*****[A]选购 [S]结算*****");
System.out.println("*****[M]修改 [Q]退出*****");
System.out.printf("*********************************\n\n");
}
商品信息类
通过创建一个商品信息类,来实现对商品信息的控制。代码如下:
class Goods{
private int no; //商品货号
private String name; //商品名称
private float price; //商品价格
private int num; //商品数量
private LocalDate toTime; //截止日期
private LocalDate proTime; //生产日期
private LocalDate upTime; //上架日期
//构造方法
public Goods(int no, String name, float price, int num, LocalDate proTime, LocalDate toTime, LocalDate upTime) {
this.no = no;
this.name = name;
this.price = price;
this.num = num;
this.proTime = proTime;
this.toTime = toTime;
this.upTime = upTime;
}
public Goods(int no,String name){
this.no = no;
this.name = name;
}
public int getNo() {
return this.no;
}
public String getName() {
return this.name;
}
public float getPrice() {
return this.price;
}
public int getNum() {
return this.num;
}
public LocalDate getUpTime() {
return this.upTime;
}
public LocalDate getProTime() {
return this.proTime;
}
public LocalDate getToTime() {
return this.toTime;
}
public void setNo(int no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(float price) {
this.price = price;
}
public void setNum(int num) {
this.num = num;
}
public void setUpTime(LocalDate upTime) {
this.upTime = upTime;
}
public void setProTime(LocalDate proTime) {
this.proTime = proTime;
}
public void setToTime(LocalDate toTime) {
this.toTime = toTime;
}
}
商品位类(货架)
创建一个可以存放商品的类,相当于货架,控制商品的上架、下架、修改等操作。代码如下:
class GoodsCenter{
private static int maxGoods = 10; //最大容量
private static Goods[] goodsArray; //商品信息数组
//初始化商品存储容器
public static void initGoodsArray(){
goodsArray = new Goods[maxGoods];
int i = 0;
while(i<goodsArray.length){
goodsArray[i] = new Goods(i+1,"——",0.0F,0,
LocalDate.of(1999,1,1),
LocalDate.of(1999,1,1),
LocalDate.of(1999,1,1));
i++;
}
}
//添加商品
public static void addGood(Goods goods){
if(isFull()){
System.out.println("商品架已满,不能上架");
return;
}
if(isExit(goods)){
System.out.println("该商品已存在,不能上架");
return;
}
Goods tmp = goodsArray[goods.getNo()-1];
if(tmp.getNo()==goods.getNo()){
tmp.setName(goods.getName());
tmp.setPrice(goods.getPrice());
tmp.setNum(goods.getNum());
tmp.setUpTime(goods.getUpTime());
tmp.setProTime(goods.getProTime());
tmp.setToTime(goods.getToTime());
System.out.println("添加成功");
}else{
return;
}
}
//删除商品
public static void deleteGood(Goods goods){
if(isEmpty()){
System.out.println("商品位全为空,删除失败");
return;
}
if(isExit(goods)){
Goods tmp = goodsArray[goods.getNo()-1];
if(tmp.getNo()==goods.getNo()){
tmp.setName("——");
tmp.setPrice(0.0F);
tmp.setNum(0);
tmp.setUpTime(LocalDate.of(1999,1,1));
tmp.setProTime(LocalDate.of(1999,1,1));
tmp.setToTime(LocalDate.of(1999,1,1));
System.out.println("删除成功");
}
}else{
System.out.println("该商品不存在,删除失败");
}
}
//修改商品信息
public static void modifyGood(Goods goods){
if(isEmpty()){
System.out.println("商品位全为空,修改失败");
return;
}
if(isExit(goods)){
boolean con = true;
while (con){
System.out.println("请选择要修改的商品属性:");
System.out.println("a.单价 b.库存 q.退出修改");
Scanner sc= new Scanner(System.in);
switch (sc.nextLine()){
case"a":
case"A":{
System.out.println("请输入要修改的单价:如:10.0");
float price = sc.nextFloat();
goodsArray[goods.getNo()-1].setPrice(price);
break;
}
case"b":
case"B": {
System.out.println("请输入要修改的库存数量:如:10");
int num = sc.nextInt();
goodsArray[goods.getNo() - 1].setNum(num);
break;
}
case"q":
case"Q":{
con = false;
break;
}
default:{
System.out.println("输入有误,请重新输入");
}
}
}
System.out.println("修改成功");
}else{
System.out.println("该商品不存在,删除失败");
}
}
//商品是否已满
public static boolean isFull(){
for(int i=0; i<goodsArray.length;i++){
if(goodsArray[i].getName().equals("——")){
return false;
}
}
return true;
}
//商品位是否全为空
public static boolean isEmpty(){
for(int i=0; i<goodsArray.length;i++){
if(goodsArray[i].getName().equals("——")){
continue;
}else{
return false;
}
}
return true;
}
//商品是否存在
public static boolean isExit(Goods goods){
for(int i=0;i<goodsArray.length;i++){
Goods tmp = goodsArray[i];
if(tmp.getNo()==goods.getNo() && tmp.getName().equals(goods.getName())){
return true;
}
}
return false;
}
//打印商品信息
public static void printGoods(){
System.out.println("========================================================");
System.out.println("No 商品 单价 库存 上架时间 生产日期 到期日期");
System.out.println("========================================================");
for(int i=0; i<goodsArray.length; i++){
if(i+1<10){
System.out.print(" "+goodsArray[i].getNo()+" ");
}else{
System.out.print(goodsArray[i].getNo()+" ");
}
System.out.printf("%-8s",goodsArray[i].getName());
System.out.printf("%-7.2f ",goodsArray[i].getPrice());
System.out.printf("%5d ",goodsArray[i].getNum());
System.out.print(" "+goodsArray[i].getUpTime().toString()+" ");
System.out.print(goodsArray[i].getProTime().toString()+" ");
System.out.println(goodsArray[i].getToTime().toString()+" ");
}
System.out.printf("========================================================\n");
}
//判断有几件商品到期了
public static int isToTime(){
int count = 0;
for(int i=0; i<goodsArray.length; i++){
if(!goodsArray[i].getName().equals("——")){
LocalDate date = LocalDate.now();
if(!date.isBefore(goodsArray[i].getToTime())){
count++;
}
}
}
return count;
}
//返回商品
public static Goods getGood(int id){
for(int i=0; i<goodsArray.length; i++){
Goods tmp = goodsArray[i];
if(tmp.getNo()==id && !tmp.getName().equals("——")){
return goodsArray[i];
}
}
return null;
}
public static int getMaxGoods(){
return maxGoods;
}
订单类
创建一个订单类,用于对顾客选购商品的订单进行管理。代码如下:
//订单类
class Order{
private static int orderId = 0;
private int id;
private Goods[] cargo;
private int[] cargoNumber;
public Order(){
this.id = ++orderId;
this.cargo = new Goods[GoodsCenter.getMaxGoods()];
this.cargoNumber = new int[GoodsCenter.getMaxGoods()];
}
//添加商品
public void add(int id, int num){
int index = id-1;
this.cargo[index] = GoodsCenter.getGood(id);
this.cargoNumber[index] += num;
}
//修改商品订单
public void modify(int id, int num){
int index = id-1;
int number = this.cargoNumber[index]-num;
if(number>0){
this.cargoNumber[index] = number;
}else{
this.cargo[index] = null;
this.cargoNumber[index] = 0;
}
System.out.println("修改成功");
}
//商品是否在订单里
public boolean isInOrder(int id){
if(this.cargo[id-1]!=null){
return true;
}
return false;
}
//计算商品价钱
public double getPrice(){
double price = 0;
for(int i=0;i<this.cargo.length; i++){
if(this.cargo[i]!=null){
price += this.cargoNumber[i]*this.cargo[i].getPrice();
}
}
return price;
}
//打印商品订单
public void printOrder(){
System.out.println("==============================");
System.out.println("单号:"+id);
System.out.println("打印日期:"+LocalDate.now().toString());
System.out.println("==============================");
System.out.println("No 商品 单价 数量 合计");
for(int i=0; i<this.cargo.length; i++){
Goods tmp = this.cargo[i];
if(tmp!=null){
int count = this.cargoNumber[i];
if(count>0){
System.out.printf("%d %-5s %-6.2f %-5d\n",
tmp.getNo(),tmp.getName(),tmp.getPrice(),this.cargoNumber[i]);
}else{
continue;
}
}
}
System.out.printf("总计:%7.2f\n",this.getPrice());
System.out.printf("==============================\n\n");
}
}
运行结果
店主操作部分
1、程序运行–>菜单–>选择店主(s)–>选择上架(p)–>输入商品信息,结果如下:
2、上架商品中如果出现了过期商品,系统会提示存在几件过期商品。如下图:
3、店主在上架商品的过程中若有需要修改的商品信息,可以选择 “ M ” 进行更改,将1号面包的单价修改为10,库存修改为10,如下图:
4、修改成功后的商品信息如下图所示:
5、商品中出现过期商品应下架,选择 “ D ” 进行下架,如下图所示:
顾客操作部分
6、菜单–>顾客(C)–>选购(A),购买2号商品apple,数量为5,如下图所示:
7、继续选购,3号商品小米,数量为2;4号商品水杯,数量为4,结果如图所示:
8、选购完成后,打印订单(S),如图所示:
9、修改选购商品的数量,若选购商品不在订单中,则无法修改,如下图:
10、修改2号商品,数量减少3,结果如图所示:
关于、退出操作模块
11、菜单–>关于(A)、退出(Q),结果如图所示:
上述代码还需要扩展,如有不足之处,请指正。
(整体代码已上传至GitHub:https://github.com/cottonrose/checkstand)