package edu.xcdq;
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"+price+"\t"+amount+"\t"+number);
}
}
package edu.xcdq;
import java.util.Scanner;
/**
* @author ChengChaoYue
* @date 2021/4/17 8:13
*/
public class ArticleManage {
ArticleSet articleSet = new ArticleSet();
Scanner input = new Scanner(System.in);
/**
* 初始化商品
*/
public void initial() {
Article xiaoMi9 = new Article();
xiaoMi9.name = "小米9";
xiaoMi9.price = 2799;
xiaoMi9.amount = 60;
xiaoMi9.number = 0;
Article xiaoMiMIX3 = new Article();
xiaoMiMIX3.name = "小米MIX3";
xiaoMiMIX3.price = 2049;
xiaoMiMIX3.amount = 40;
xiaoMiMIX3.number = 0;
Article redMiNote7Pro = new Article();
redMiNote7Pro.name = "红米note7Pro";
redMiNote7Pro.price = 699;
redMiNote7Pro.amount = 80;
redMiNote7Pro.number = 0;
Article xiaoMiPlay = new Article();
xiaoMiPlay.name = "小米Play";
xiaoMiPlay.price = 749;
xiaoMiPlay.amount = 100;
xiaoMiPlay.number = 0;
articleSet.articles[0] = xiaoMi9;
articleSet.articles[1] = xiaoMiMIX3;
articleSet.articles[2] = redMiNote7Pro;
articleSet.articles[3] = xiaoMiPlay;
}
/**
* 菜单切换
*/
public void startMenu() {
boolean flag = true; // 是否继续操作
do {
System.out.println("欢迎使用超市管理系统");
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("请选择要执行的操作: ");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("查看商品信息");
break;
case 2:
System.out.println("新增商品信息");
break;
case 3:
System.out.println("删除商品信息");
break;
case 4:
System.out.println("卖出商品");
break;
case 5:
System.out.println("商品销售排行榜");
break;
case 6:
System.out.println("谢 谢 使 用!");
break;
default:
System.out.println("输入不符合要求请重新选择!");
break;
}
}while (flag);
}
/**
* 查看商品信息
*/
public void search() {
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("请输入商品名称:");
String name = input.next();
System.out.println("请输入价格:");
int price = input.nextInt();
System.out.println("请输入库存");
int amount = input.nextInt();
Article article = new Article();
article.name = name;
article.price = price;
article.amount = amount;
article.number = 0;
for (int i = 0; i < articleSet.articles.length; i++) {
if (articleSet.articles[i] == null) {
articleSet.articles[i] = article;
}
}
}
/**
* 删除商品
*/
public void delete() {
System.out.println("请输入商品编号:");
boolean flag = true; // 是否删除成功
int card = input.nextInt();
for (int i = 0; i < articleSet.articles.length; i++) {
if (articleSet.articles[i]!= null&&(i+1) == card) {
int j=i;
while (articleSet.articles[j+i]!=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("删除失败,请重新操作!");
}
}
/**
* 销售业务
*/
public void sell() {
System.out.println("请输入你要卖出的商品名称:");
String name = input.next();
boolean flag = true; // 是否卖出成功
for (int i = 0; i < articleSet.articles.length; i++) {
if (articleSet.articles[i]!=null&&articleSet.articles[i].name.equals(name)) {
System.out.println("请输入你要卖出的数量");
int number = input.nextInt();
if (number <= articleSet.articles[i].amount) {
articleSet.articles[i].number = articleSet.articles[i].number+number;
articleSet.articles[i].amount = articleSet.articles[i].amount-number;
flag = true;
}else {
System.out.println("商品数量不够,请抓紧进货");
flag = true;
}
break;
}else {
flag = false;
}
}
if (flag) {
System.out.println("卖出商品成功!");
}else {
System.out.println("卖出商品失败");
}
}
/**
* 商品销售排行榜
*/
public void leaderboard() {
Article[] articles = new Article[50];
for (int i = 0; i < articles.length; i++) {
if (articleSet.articles[i]!=null) {
articles[i]=articleSet.articles[i];
}
}
for (int i = 0; i < articles.length-1; i++) {
for (int j = 0; j < articles.length-i-1; j++) {
if (articles[j+1]!=null) {
if (articles[j].number<articles[j+1].number) {
Article tempArticle=articles[j];
articles[j]=articles[j+1];
articles[j+1]=tempArticle;
}
}
}
}
System.out.println("***************************");
System.out.println("名次\t 销售数量\t 商品名称");
for (int i = 0; i < articles.length; i++) {
if (articles[i]!=null) {
System.out.println(i+1+"\t"+articles[i].number+"\t"+articles[i].name);
}
}
}
}
package edu.xcdq;
/**
* @author ChengChaoYue
* @date 2021/4/17 8:12
*/
public class ArticleSet {
Article[] articles=new Article[50];
}
package edu.xcdq;
/**
* @author ChengChaoYue
* @date 2021/4/17 8:44
*/
public class Demo {
public static void main(String[] args) {
ArticleManage articleManage = new ArticleManage();
articleManage.initial();
articleManage.startMenu();
}
}