實(shí)現(xiàn)步驟
步驟1:數(shù)據(jù)初始化
(1)創(chuàng)建項目ArticleManage栋荸,創(chuàng)建類Article,添加4個屬性,一個方法展示商品信息
public class Article {
public String name; // 商品名稱
public int amount; // 商品庫存數(shù)量
public double price; // 商品價格
public int number; // 商品售出數(shù)量
/**
*商品信息展示
*/
public void print(int index) {
System.out.print(index+"\t"+name+"\t"+price+"\t"+amount+"\t"+number);
}
(2)創(chuàng)建ArticleSet類煤墙,并在類中定義1個長度為50的數(shù)組保存商品
public class ArticleSet {
Article[] articles = new Article[50];
}
(3)創(chuàng)建ArticleManage,創(chuàng)建ArticleSet對象articleSet宪拥,實(shí)現(xiàn)初始化數(shù)據(jù)的方法initial(),初始化4件商品的信息
/*
* 商品管理類
* */
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;
}
步驟2:實(shí)現(xiàn)菜單切換
在ArticleManage類中編寫菜單顯示和菜單切換的startMenu方法仿野,代碼如下:
/**
*菜單切換
*/
public void startMenu() {
boolean flag = true ; // 是否操作
do {
System.out.print("歡迎使用前程商城后臺管理系統(tǒng)");
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("請選擇要執(zhí)行的操作: ");
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);
}
在測試類中編寫程序入口,實(shí)例化ArticleManage并實(shí)現(xiàn)initial方法和startMenu方法
測試類代碼
/**
* 測試類
*/
public class Demo {
public static void main(String[] args) {
ArticleManage articleManage = new ArticleManage();
articleManage.initial();
articleManage.startMenu();
}
}
步驟3:實(shí)現(xiàn)查看商品信息
在ArticleManage類中編寫查看商品信息的search方法她君,并在startMenu方法中輸入1時調(diào)用search方法
/**
*查看商品信息
*/
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);
}
}
}
步驟4:實(shí)現(xiàn)新增商品信息
在ArticleManage類中編寫添加商品信息的add方法脚作,并在startMenu方法中輸入2時調(diào)用search方法
/**
* 新增商品
*/
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;
for (int i<0; i< articleSet.articles.length; i++){
if (articleSet.articles[i]==nell){
articleSet.articles[i]=article;
break;
}
}
}
步驟5:實(shí)現(xiàn)刪除商品信息
在ArticleManage類中編寫刪除商品信息的delete方法,并在startMenu方法中輸入3時調(diào)用delete方法
/**
* 刪除商品
*/
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("刪除失敗球涛,請重新操作劣针! ");
}
}
步驟6:實(shí)現(xiàn)商品銷售的業(yè)務(wù)處理
在ArticleManage類中編寫賣出商品的sell方法,并在startMenu方法中輸入4時調(diào)用sell方法
/**
*銷售業(yè)務(wù)
*/
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("請輸入您要賣出的數(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.print("商品數(shù)量不夠亿扁,請抓緊進(jìn)貨捺典! ");
flag = false;
}
break;
}else {
flag = false;
}
}
if (flag){
System.out.print("賣出商品成功!");
}else {
System.out.print("賣出商品失敗从祝!");
}
}
步驟7:實(shí)現(xiàn)商品銷售排行榜
在ArticleManage類中編寫商品銷售排行榜的leaderboard方法襟己,并在startMenu方法中輸入5時調(diào)用leaderboard方法
/**
* 商品銷售排行榜
*/
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銷售數(shù)量\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);
}
}
}