详细步骤概述
步骤一:创建类Main
package edu.xcdq;
/**
* 商品类
*/
public class Main {
// 名字 单价 库存 已卖数量
public String name;
public double price;
public int amount;
public int number;
/*
1. 商品展示
*/
public void print(int index) {
System.out.println(index + "\t" + name + "\t" + price + "\t" + amount + "\t" + number);
}
public void setArticle(String mingzi , double danjia, int kucun,int yishou){
name=mingzi;
price=danjia;
amount = kucun;
number = yishou;
}
}
步骤二:创建类Main1
package edu.xcdq;
/**
* @author WYP
* @date 2021/4/6 14:30
*/
/*
2.商品集合类
*/
public class Main1 {
Main[] articles= new Main[38];
}
步骤三:创建类Main2
Main内包含 initial、startMenu
package edu.xcdq;
import java.util.Scanner;
/**
* @author WYP
* @date 2021/4/6 14:30
*/
public class Main2 {
Main1 main = new Main1();
Scanner input = new Scanner(System.in);
/**
* 3. 初始化商品
*/
public void initial(){
Main xiaomi12 = new Main();
/*
xiaomi9.name = "小米12";
xiaomi9.price = 4999;
xiaomi9.amount = 30;
xiaomi9.number = 8;
*/
xiaomi12.setArticle("小米12",4999,30,8);
Main xiaomi11 = new Main();
xiaomi11.setArticle("小米11",3999,30,10);
Main redmi10pro = new Main();
redmi10pro.setArticle("Redmi10por",2999,30,0);
Main redmi9 = new Main();
redmi9.setArticle("Redmi9",1999,30,0);
main.articles[0] = xiaomi12;
main.articles[1] = xiaomi11;
main.articles[2] = redmi10pro;
main.articles[3] = redmi9;
}
public void startMenu() {
boolean falg = 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("查看商品信息");
chakan();
break;
case 2:
System.out.println("新增商品信息");
add();
break;
case 3:
System.out.println("删除商品信息");
delete();
break;
case 4:
System.out.println("卖出商品");
saleOut();
break;
case 5:
System.out.println("商品销售排行榜");
leaderBand();
break;
case 6:
System.out.println("谢 谢 使 用 !");
falg = false;
break;
default:
System.out.println("输入不符合要求请重新选择!");
break;
}
}while (falg);
}
public void chakan(){
System.out.println("编码 \t 名称\t单价\t库存\t售出数量");
for (int i = 0; i < main.articles.length; i++) {
if (main.articles[i] !=null){
main.articles[i].print(i+1);
}
}
}
public void add(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入商品名称:");
String name = scanner.next();
System.out.println("请输入单价:");
int price = input.nextInt();
System.out.println("请输入库存:");
int amount = input.nextInt();
System.out.println("请输入已卖数量:");
int number = scanner.nextInt();
Main mainn = new Main();
mainn.setArticle(name,price,amount,number);
for (int i = 0; i < main.articles.length; i++) {
if (main.articles[i] == null){
main.articles[i] = mainn;
break;
}
}
}
private void delete() {
System.out.println("请输入要删除的商品编码:");
Scanner input = new Scanner(System.in);
int delNo = input.nextInt();
boolean flag = true;
for (int i = 0; i < main.articles.length; i++) {
if (main.articles[i] != null && (i+1) ==delNo){
int j = i;
while (main.articles[j+1] !=null){
main.articles[j] = main.articles[j+1];
j++;
}
main.articles[j] = null;
flag = true;
break;//后续的空数组元素没有必要执行
}else {
flag = false;
}
}
if (flag){
System.out.println("删除成功");
}else {
System.out.println("删除失败!");
}
}
private void saleOut() {
System.out.println("请输入您要买出的商品的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
boolean flag = true;
for (int i = 0; i < main.articles.length; i++) {
if (main.articles[i] != null && main.articles[i].name.equals(name) ){
System.out.println("请输入要卖出的数量:");
int maichu = scanner.nextInt();
if (maichu<main.articles[i].amount){
//新库存 = 旧库存 - 卖出数量
main.articles[i].amount = main.articles[i].amount -maichu;
//新售出 = 旧售出 + 卖出数量
main.articles[i].number = main.articles[i].number+ maichu;
flag = true;
}else {
flag = false;
System.out.println("库存不够了,抓紧时间进货");
}
break;
}else {
flag = false;
// System.out.println("你要卖出的商品没有找到");
}
}
if (flag){
System.out.println("卖出成功");
}else {
System.out.println("卖出失败");
}
}
private void leaderBand() {
/*
Main[] articles= new Main[38];
for (int i = 0; i < articles.length; i++) {
if (main.articles[i]!=null){
articles[i]=main.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){
Main paihangbang = articles[j];
articles[j] = articles[j+1];
articles[j+1] = paihangbang;
}
}
}
}
}*/
int count = 0;
for (int i = 0; i < main.articles.length; i++) {
if (main.articles[i] !=null){
count ++ ;
}
}
Main[] newTemp = new Main[count];
for (int i = 0; i < count; i++) {
newTemp[i] = main.articles[i];
}
for (int i = 0; i < newTemp.length-1; i++) {
for (int j = 0; j < newTemp.length -i-1; j++) {
if (newTemp[j+1] != null){
if (newTemp[j].number < newTemp[j+1].number){
Main temp = newTemp[j];
newTemp[j] = newTemp[j+1];
newTemp[j+1] = temp;
}
}
}
}
//显示名称
System.out.println("名称:\t销售数量:\t商品名称:");
for (int i = 0; i < newTemp.length; i++) {
System.out.println((i+1)+"\t"+ newTemp[i].number + "\t" +newTemp[i].name);
}
}
}
步骤四:创建类main3
package edu.xcdq;
/**
* @author WYP
* @date 2021/4/6 14:30
*/
/*
4. 菜单切换
*/
public class Main3 {
public static void main(String[] args) {
Main2 main2 = new Main2();
main2.initial();
main2.startMenu();
}
}
运行结果: