1.package shop;
/** 商品物件类
* 自定义类 shop.Article*/
public class Article {
//名字 单价 库存 已卖数量 public String name;
public double price;
public int amount;
public int number;
public void print(int index){ System.out.println(index+"\t"+name+"\t"+price+"\t"+amount+"\t"+number);
}
public void setArticle(String mingzi,double danjia, int kucun, int shouchu){ name=mingzi;
price=danjia;
amount=kucun;
number=shouchu;
}
}
2.package shop;
import java.util.Scanner;
public class ArticleMange {
ArticleSet articleSet = new ArticleSet();
//仓库初始化 放入一些商品 public void initial(){ Article xiaomi11 = new Article();
xiaomi11.setArticle("小米11",1999,30,0); Article xiaomi11Pro = new Article(); xiaomi11Pro.setArticle("小米11Pro",2999,10,0); Article xiaomiUltra = new Article();
xiaomiUltra.setArticle("小米至尊版",3999,20,0);
articleSet.articles[0]=xiaomi11;
articleSet.articles[1]=xiaomi11Pro;
articleSet.articles[2]=xiaomiUltra;
} //启动菜单
public void startMenu(){ boolean flag= true;
do { 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("*******************************");
System.out.println("请输入功能编号:"); Scanner scanner= new Scanner(System.in); int funNo=scanner.nextInt();
switch (funNo){ case 1:
System.out.println("1.查看商品信息"); chakan(); break;
case 2:
System.out.println("2.新增商品信息"); add();
break; case 3: System.out.println("3.删除商品信息"); delete();
break;
case 4:
System.out.println("售出商品信息"); saleOut();
break;
case 5:
System.out.println("排行榜"); leadeBand();
break;
case 6:
System.out.println("谢谢"); flag = false;
break;
default:
System.out.println("请输入正确的功能编号"); break;
}
}while (flag);
}
private void leadeBand() {
/*
* 降序排序
* for() 所有元素全部参与排序
* for() 当前元素和后面的元素比较
* if (当前元素<后续元素小)*/
//排序 for(int i=0; i<articleSet.articles.length-1;i++){ for (int j=0;j<articleSet.articles.length-i-1;j++){ if (articleSet.articles[j] !=null&&articleSet.articles[j+1] !=null){ if (articleSet.articles[j].number<articleSet.articles[j+1].number){ Article newTamp=articleSet.articles[j]; articleSet.articles[j]=articleSet.articles[j+1]; articleSet.articles[j+1]=newTamp; }
}
}
}
//打印结果 System.out.println("******************");
System.out.println("名次\t销售数量\t商品名称"); for (int i=0;i<articleSet.articles.length;i++){ if (articleSet.articles[i]!=null){ System.out.println(i+1+"\t"+articleSet.articles[i].number+"\t"+articleSet.articles[i].name); }
}
}
private void saleOut() { System.out.println("请输入你要售卖的商品名称:"); Scanner scanner=new Scanner(System.in); String name=scanner.next(); for (int i=0;i<articleSet.articles.length;i++){ if((articleSet.articles[i].name).equals(name)){ System.out.println("请选择你要售卖的数量:");
int shoumai= scanner.nextInt(); if (shoumai<articleSet.articles[i].amount){ articleSet.articles[i].amount=articleSet.articles[i].amount-shoumai; articleSet.articles[i].number=articleSet.articles[i].number+shoumai;
}
}
}
}
private void delete() {
System.out.println("请输入要删除的商品编号:"); Scanner scanner=new Scanner(System.in); int bianhao = scanner.nextInt(); boolean flag=true; for (int i=0;i<articleSet.articles.length;i++){ if (articleSet.articles[i] !=null && (i+1)==bianhao){ int j=1; while (articleSet.articles[j+1] !=null){ //后面的元素向前移动 articleSet.articles[j]=articleSet.articles[j+1]; j++;
}
articleSet.articles[j]=null;
flag=true;
break;
//后续的空数组元素没有必要执行
}else {
flag = false;
}
}
if (flag){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
private void add() { Scanner scanner = new Scanner(System.in); System.out.println("请输入商品的名称:"); String name = scanner.next(); System.out.println("请输入单价:"); double price= scanner.nextDouble(); System.out.println("请输入库存:"); int count= scanner.nextInt(); System.out.println("请输入已卖的数量"); int number= scanner.nextInt(); Article newArticle = new Article(); newArticle.setArticle(name,price,count,number); for (int i=0;i<articleSet.articles.length;i++){ if (articleSet.articles[i]==null){ articleSet.articles[i]=newArticle;
//把新建的对象放在数组中的第一个空位置 break;
//后续的空位置直接跳过
}
}
}
public void chakan(){ System.out.println("编号 \t 名字 \t 单价 \t 库存 \t 已售"); for (int i=0;i<articleSet.articles.length;i++){ if (articleSet.articles[i] !=null){ articleSet.articles[i].print(i);
}
}
}
}
3.package shop;
/*
* 商品集合
* 仓库进行管理商品
*/
public class ArticleSet {
Article[] articles = new Article[30];
}
4.ackage shop;
public class Demo {
public static void main(String[] args) {
ArticleMange articleMange = new ArticleMange();
articleMange.initial(); articleMange.startMenu();
}
}