商品信息
public class Article {
public String name; // 商品名称
public int amount; // 商品库存数量
public double price; // 商品价格
public int number; // 商品售出数量
public void print(int index) {
System.out.print(index+"\t"+name+"\t"+price+"\t"+amount+"\t"+number);
}
public void price(int i) {
}
public class ArticleSet {
Article[] articles = new Article[50];
}
/*
* 商品管理类
* */
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 = "小米8";
xiaoMiMIX3.price = 2049 ;
xiaoMiMIX3.amount = 40;
xiaoMiMIX3.number = 0;
Article redMiNote7Pro = new Article();
redMiNote7Pro.name = "Redmi 7";
redMiNote7Pro.price = 699;
redMiNote7Pro.amount = 80;
redMiNote7Pro.number = 0;
Article xiaoMiPlay = new Article();
xiaoMiPlay.name = "小米6X";
xiaoMiPlay.price = 749;
xiaoMiPlay.amount = 100 ;
xiaoMiPlay.number = 0;
articleSet.articles[0] = xiaoMi9;
articleSet.articles[0] = xiaoMiMIX3;
articleSet.articles[0] = redMiNote7Pro;
articleSet.articles[0] = xiaoMiPlay;
}
/*
* 菜单切换
* */
public void startMenu() {
boolean flag = true ; // 是否操作
do {
System.out.print("欢迎使用前程商城后台管理系统");
System.out.print("------------------------------------------------------");
System.out.print("1. 查看商品信息");
System.out.print("2. 新增商品信息");
System.out.print("3. 删除商品信息");
System.out.print("4. 卖出商品");
System.out.print("5. 商品销售排行榜");
System.out.print("6. 退出");
System.out.print("------------------------------------------------------");
System.out.print("请选择要执行的操作: ");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.print("查看商品信息");
break;
case 2:
System.out.print("新增商品信息");
break;
case 3:
System.out.print("删除商品信息");
break;
case 4:
System.out.print("卖出商品");
break;
case 5:
System.out.print("商品销售排行榜");
break;
case 6:
System.out.print("谢谢使用");
flag = false;
break;
default:
System.out.print("输入不符合要求请重新选择");
break;
}
}while (flag);
}
/*
* 测试类
* */
public class Demo {
public static void main(String[] args) {
ArticleManage articleManage = new ArticleManage();
articleManage.initial();
articleManage.startMenu();
}
}
/*
* 查看商品信息
* */
public void search() {
System.out.print("编号\t名称\t价格\t库存\t售出");
for (int i = 0; i < articleSet.articles.length; i++) {
if (articleSet.articles[i] != null ){
articleSet.articles[i].price(i+1);
}
}
}
/*
* 新增商品
* */
public void add() {
System.out.print("请输入商品名称: ");
String name = input.next();
System.out.print("请输入价格: ");
int price = input.nextInt();;
System.out.print("请输入库存: ");
int amount = input.nextInt();
Article article = new Article();
article.name = name;
article.price = price;
article.amount = amount;
article.number = 0;
}
/*
* 删除商品
* */
public void delete(){
System.out.print("请输入商品编号: ");
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+1] !=null) {
articleSet.articles[j] = articleSet.articles[j+1];
j++ ;
}
articleSet.articles[j] = null;
flag = true;
break;
}else {
flag = false;
}
}
if (flag) {
System.out.print("删除成功! ");
}else {
System.out.print("删除失败,请重新操作! ");
}
}
/*
* 销售业务
* */
public void sell(){
System.out.print("请输入你要卖出的商品名称: ");
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.print("请输入您要卖出的数量");
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.print("商品数量不够,请抓紧进货! ");
flag = false;
}
break;
}else {
flag = false;
}
}
if (flag){
System.out.print("卖出商品成功!");
}else {
System.out.print("卖出商品失败!");
}
}
/*
* 商品销售排行榜
* */
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.print("*********************************");
System.out.print("名次\t销售数量\t商品名称");
for (int i = 0; i < articles.length; i++){
if (articles[i] != null){
System.out.print(i+1+"\t"+articles[i].number+"\t"+articles[i].name);
}
}
}