步驟一:創(chuàng)建類Main
package edu.xcdq;
/**
* 商品類
*/
public class Main {
// 名字 單價 庫存 已賣數(shù)量
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;
}
}
步驟三:創(chuàng)建類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("歡迎使用前程商城后臺管理系統(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("查看商品信息");
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售出數(shù)量");
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("請輸入已賣數(shù)量:");
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;//后續(xù)的空數(shù)組元素沒有必要執(zhí)行
}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("請輸入要賣出的數(shù)量:");
int maichu = scanner.nextInt();
if (maichu<main.articles[i].amount){
//新庫存 = 舊庫存 - 賣出數(shù)量
main.articles[i].amount = main.articles[i].amount -maichu;
//新售出 = 舊售出 + 賣出數(shù)量
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銷售數(shù)量:\t商品名稱:");
for (int i = 0; i < newTemp.length; i++) {
System.out.println((i+1)+"\t"+ newTemp[i].number + "\t" +newTemp[i].name);
}
}
}
步驟四:創(chuàng)建類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();
}
}