···
package com.company;
public class Article {
//把常用的属性 封禁
public String name; //名字
public int amount ; //库存
public double price; //单价
public int number; //售出数量
public void print(int index) {
System.out.println(index + "\t" + name + "\t" +amount + "\t" + price + "\t" + number);
}
//
public void setArticle(String mingzi , int kucun , double danjia , int shouchu){
name = mingzi;
amount = kucun;
price = danjia;
number = shouchu;
}
}
package com.company;
/* 商品集合 便于管理多个商品,ArticleSet 商品集合*/
public class ArticleSet {
// 准备一个数组,里面存储商品,作为仓库
Article [] articles = new Article[30];
}
package com.company;
/* 商品管理员 */
public class ArticleManage {
// 创建一个商品集合(仓库)实例
ArticleSet articleSet = new ArticleSet();
//对新建的仓库进行初始化
public void initial() {
//创建商品
Article xiaomi11 = new Article();
xiaomi11.setArticle("小米11", 50, 2999, 1);
Article xiaomi11pro = new Article();
xiaomi11.setArticle("小米pro", 30, 3999, 2);
//把商品放入仓库中
articleSet.articles[0] = xiaomi11;
articleSet.articles[1] = xiaomi11pro;
}
//起始菜单
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("*********************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要执行的操作");
int gongnengbianhao = scanner.nextInt();
switch (gongnengbianhao) {
case 1:
System.out.println("查看商品信息");
chakan(); //调用查看商品信息的方法
break;
case 2:
System.out.println("新增商品信息");
add();
break;
case 3:
System.out.println("删除商品信息");
delete();
break;
case 4:
System.out.println("卖出商品");
sell();
break;
case 5:
System.out.println("排行榜");
paihangbang();
break;
case 6:
System.out.println("退出系统");
exit();
flag = false;
break;
default:
System.out.println("你输入的有误");
break;
}
}while (flag) ;
}
public void exit(){
System.out.println("退出");
}
public void paihangbang(){
//排序(冒泡排序)
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 tempArticle = articleSet.articles[j];
articleSet.articles[j] = articleSet.articles[j+1];
articleSet.articles[j+1] = tempArticle;
}
}
}
}
System.out.println("***********排行榜**********");
System.out.println("名次 \t 销售数量 \t 商品名称");
for (int i = 0 ; i < articleSet.articles.length ; i ++){
System.out.println((i+1) +"\t"+articleSet.articles[i].number+"\t"+articleSet.articles[i].name);
}
}
//卖出商品
public void sell(){
/*
sout(卖出名称)
scanner = new Scanner
name = sc.next()
for(; int i < length ;)
articles[i] .equals( name ) )
接受卖出的数量
int num = sc.nextInt();
if (num <kucun)
新库存 = 原库存 - num
新卖出 = 就卖出 +num
sout"卖出成功"
*/
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].amount = articleSet.articles[i].amount + shoumai;
}
System.out.println("售卖成功");
break;
}
}
}
//删除商品
public void delete(){
System.out.println("亲输入你要删除的商品编号");
Scanner scanner = new Scanner(System.in);
int delNo = scanner.nextInt();
for(int i = 0; i < articleSet.articles.length;i++) {
if (articleSet.articles[i] != null && (i + 1) == delNo) {//数组中的元素存在&&找到要删除的元素i
int j = i;
while (articleSet.articles[j + i] != null) {// 要移动的元素后一个位置不为空,不执行以下操作
articleSet.articles[j] = articleSet.articles[j + i]; //把后面元素的值覆盖在前面的元素上
j++;
}
articleSet.articles[i] = null; // 最后一个元素手动修改为空
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}
}
//查看商品信息
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+1);
}
}
}
//添加商品
public void add(){
System.out.println("请输入商品名称");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println("请输入价格");
double price = scanner.nextDouble();
System.out.println("请输入库存");
int kucun = scanner.nextInt();
System.out.println("卖出数量");
int maichu = scanner.nextInt();
Article newArticle = new Article();
newArticle.setArticle( name , kucun , price, maichu);
for (int i =0; i<articleSet.articles.length; i++){
if(articleSet.articles[i]==null){
articleSet.articles[i]=newArticle;
break;
}
}
}
}
package com.company;
public class Demo {
public static void main(String[] args) {
ArticleManage articleManage = new ArticleManage();
articleManage.initial();
articleManage.startMenu();
}
}
···