package edu.xcdq.Demo406;
import java.util.Scanner;
/**
-
商品管理员
*/
public class ArticleManade {
//创建一个实体的仓库对象,并初始化
ArticleSet articleSet = new ArticleSet();//初始化仓库,放入初始商店
public void initial(){
Article xiaomi11 = new Article();
xiaomi11.setArticle("小米11" , 30 , 1999 , 0);Article xiaomi11pro = new Article(); xiaomi11pro.setArticle("小米11pro",40 ,2999,0); Article xiaomiUltra = new Article(); xiaomiUltra.setArticle("小米增强版",50,3999,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("******************************");Scanner scanner = new Scanner(System.in); System.out.println("请输入你要执行的功能"); int funNo = scanner.nextInt(); switch (funNo){ case 1 -> {System.out.println("1 查看商品信息"); chakan();} case 2 -> {System.out.println("2 新增商品信息"); add();} case 3 -> {System.out.println("3 删除商品信息"); delete();} case 4 -> {System.out.println("4 卖出商品信息"); sell();} case 5 -> {System.out.println("5 销售排行榜"); paihangbang();} case 6 -> {System.out.println("6 退出"); flag = false; break;} } }while (flag);
}
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].number ){
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++){
if ( articleSet.articles[i] !=null ){
System.out.println( (i+1) + "\t" + articleSet.articles[i].number + "\t" + articleSet.articles[i].name);
}
}
}
public void sell(){
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 ;
}
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 ){
int j = i ;
while ( articleSet.articles[j+1] !=null ){
articleSet.articles[j] = articleSet.articles[j+1];
j++;
}
articleSet.articles[i] = null;
System.out.println("删除成功");
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+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 cukun = scanner.nextInt();
System.out.println("已卖出数量:");
int maichu = scanner.nextInt();Article newArticle = new Article(); newArticle.setArticle(name , (int) price, cukun , maichu); for ( int i = 0 ; i < articleSet.articles.length; i++){ if (articleSet.articles[i] == null ){ articleSet.articles[i] = newArticle; break; } }
}
}