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; // 是否繼續(xù)操作
do {
System.out.println("歡迎使用超市管理系統(tǒng)");
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("請選擇要執(zhí)行的操作: ");
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("編號(hào)\t 名稱\t 價(jià)格\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("請輸入價(jià)格:");
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("請輸入商品編號(hào):");
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("刪除失敗,請重新操作!");
}
}
/**
* 銷售業(yè)務(wù)
*/
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("請輸入你要賣出的數(shù)量");
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("商品數(shù)量不夠,請抓緊進(jìn)貨");
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 銷售數(shù)量\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();
}
}