java____電商管理
1查看商品:根據(jù)商品數(shù)組中的下表戴差,顯示商品的序號(hào)、名稱铛嘱、價(jià)格暖释、庫(kù)存信息和銷售信息等信息
2:新增商品:根據(jù)用戶輸入的商品名稱、價(jià)格庫(kù)存數(shù)量等信息墨吓,創(chuàng)建商品球匕,并將其保存為商品數(shù)組中,
3刪除商品:根據(jù)用戶輸入的商品序號(hào)帖烘,從數(shù)組中移除對(duì)應(yīng)下標(biāo)的商品亮曹,
4商品銷售:根據(jù)用戶輸入的商品名稱和購(gòu)買數(shù)量,修改數(shù)組中商品的庫(kù)存數(shù)量和銷售數(shù)量蚓让。
5商品銷售排行:根據(jù)銷售數(shù)量對(duì)數(shù)組中的商品進(jìn)行排序乾忱,并展示。
6 推出系統(tǒng):提示“謝謝使用”
需要?jiǎng)?chuàng)建四個(gè)類:Article商品屬性類历极,ArticleSet商品倉(cāng)庫(kù)類,ArticleManage商品管理類和調(diào)用他們的Mian類
創(chuàng)建Article類衷佃,確定商品屬性和初始化商品
public class Article {
? ? public String name;
? ? public double price;
? ? public int amount;
? ? public int number;
? ? public void print(int index ){
? ? ? ? System.out.println(index +"\t\t "+ name +"\t" + price + "\t" + amount +"\t" + number );
? ? }
? ? public void setArticle(String mingzi , double danjia , int kucun , int shouchu) {
? ? ? ? name = mingzi;
? ? ? ? price = danjia;
? ? ? ? amount = kucun;
? ? ? ? number = shouchu;
? ? }
}
創(chuàng)建ArticleSet類趟卸,開(kāi)辟商品倉(cāng)庫(kù),用于儲(chǔ)存商品元素
public class ArticleSet {
? ? Article [] articles = new Article[30];
}
創(chuàng)建ArticleManage類氏义,調(diào)用Article類和ArticleSet類锄列,
并實(shí)現(xiàn)增刪改查
public class ArticleManage {
? ? ArticleSet articleSet = new ArticleSet();
? ? public void initial () {
? ? ? ? Article xiaomi11 = new Article();
? ? ? ? xiaomi11.setArticle("小米11" , 1999,100,1);
? ? ? ? Article xiaomi11Pro = new Article();
? ? ? ? xiaomi11Pro.setArticle("小米11pro" ,2999 , 200, 0);
? ? ? ? Article xiaomiUltra = new Article();
? ? ? ? xiaomiUltra.setArticle("小米Ultra" ,3999 , 300,2);
? ? ? ? articleSet.articles[0] = xiaomi11;
? ? ? ? articleSet.articles[1] = xiaomi11Pro;
? ? ? ? articleSet.articles[2] = xiaomiUltra;
? ? }
? ? public void startMenu(){
? ? ? ? boolean flag = true;
? ? ? ? do {
? ? ? ? ? ? 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 退出");
? ? ? ? ? ? Scanner scanner = new Scanner(System.in);
? ? ? ? ? ? System.out.println("請(qǐng)輸入你要執(zhí)行的功能編號(hào):");
? ? ? ? ? ? int funNO? = scanner.nextInt();
? ? ? ? ? ? switch (funNO) {
? ? ? ? ? ? ? ? case 1 : { System.out.println("1 查看商品信息"); chakan();break; }
? ? ? ? ? ? ? ? case 2 : { System.out.println("2 新增商品信息"); add();break; }
? ? ? ? ? ? ? ? case 3 : { System.out.println("3 刪除商品信息"); delete(); break;}
? ? ? ? ? ? ? ? case 4 : { System.out.println("4 賣出商品信息"); sell();break;}
? ? ? ? ? ? ? ? case 5 : { System.out.println("5 銷售排行榜");? paihangbang();break; }
? ? ? ? ? ? ? ? case 6 : { System.out.println("6 退出"); flag = false; exit();break;}
? ? ? ? ? ? }
? ? ? ? }while ( flag );
? ? }
? ? public void exit() {
? ? ? ? System.out.println("謝謝,感謝使用! 已經(jīng)退出");
? ? }
? ? public void paihangbang() { //排行榜
? ? ? ? // 排序 (冒泡排序)
? ? ? ? for ( int i = 0 ; i < articleSet.articles.length - 1? ; i ++ ) {
? ? ? ? ? ? for ( int j = 0 ; j < articleSet.articles.length - i -1 ; j ++ ) {
? ? ? ? ? ? ? ? // 如果當(dāng)前元素的值比后面的元素值小 那么和后面的元素交換位置
? ? ? ? ? ? ? ? if (articleSet.articles[j] != null && articleSet.articles[j+1] != null ){
? ? ? ? ? ? ? ? ? ? if (? articleSet.articles[j].number < articleSet.articles[j+1].number ) {
? ? ? ? ? ? ? ? ? ? ? ? // 交換位置
? ? ? ? ? ? ? ? ? ? ? ? Article tempArticle = articleSet.articles[j];
? ? ? ? ? ? ? ? ? ? ? ? articleSet.articles[j] = articleSet.articles[j+1] ;
? ? ? ? ? ? ? ? ? ? ? ? articleSet.articles[j+1] = tempArticle;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 打印排行榜
? ? ? ? System.out.println("**********排行榜***********");
? ? ? ? System.out.println("名次 \t 銷售數(shù)量 \t 商品名稱");
? ? ? ? for (int i = 0; i < articleSet.articles.length; i++) {
? ? ? ? ? ? if ( articleSet.articles[i] != null ) {
? ? ? ? ? ? ? ? System.out.println( (i+1)? + "\t\t\t" + articleSet.articles[i].number +"\t\t"+ articleSet.articles[i].name );
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public void sell() { //出售商品
? ? ? ? System.out.println("請(qǐng)輸入你要售賣的商品名稱:");
? ? ? ? Scanner scanner = new Scanner(System.in);
? ? ? ? String name = scanner.next();
? ? ? ? for (int i = 0; i < articleSet.articles.length; i++) {
? ? ? ? ? ? if ( (articleSet.articles[i].name).equals(name) ) {
? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入你要售賣的數(shù)量:");
? ? ? ? ? ? ? ? int shoumai = scanner.nextInt();
? ? ? ? ? ? ? ? if ( shoumai < articleSet.articles[i].amount ) {
? ? ? ? ? ? ? ? ? ? articleSet.articles[i].amount = articleSet.articles[i].amount - shoumai ;
? ? ? ? ? ? ? ? ? ? articleSet.articles[i].number = articleSet.articles[i].number + shoumai ;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? System.out.println("售賣成功");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? ? public void chakan () {
? ? ? ? ? System.out.println("編號(hào) \t 名稱 \t 單價(jià) \t 庫(kù)存 \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("請(qǐng)輸入商品名稱:");
? ? ? ? ? Scanner scanner = new Scanner(System.in);
? ? ? ? ? String name = scanner.next();
? ? ? ? ? System.out.println("輸入價(jià)格:");
? ? ? ? ? double price = scanner.nextDouble();
? ? ? ? ? System.out.println("輸入庫(kù)存:");
? ? ? ? ? int cukun = scanner.nextInt();
? ? ? ? ? System.out.println("已賣出數(shù)量:");
? ? ? ? ? int maichu = scanner.nextInt();
? ? ? ? ? Article newArticle = new Article();
? ? ? ? ? newArticle.setArticle(name, price, cukun, maichu);
? ? ? ? ? for (int i = 0; i < articleSet.articles.length; i++) {
? ? ? ? ? ? ? if (articleSet.articles[i] == null) {
? ? ? ? ? ? ? ? ? articleSet.articles[i] = newArticle;
? ? ? ? ? ? ? ? ? break; // 只添加數(shù)組中的第一個(gè)空位置惯悠,后續(xù)的空位直接跳過(guò)邻邮,中斷循環(huán)
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? }
? ? ? ? public void delete() { //刪除商品
? ? ? ? ? ? System.out.println("請(qǐng)輸入你要?jiǎng)h除的商品編號(hào):");
? ? ? ? ? ? Scanner scanner = new Scanner(System.in);
? ? ? ? ? ? int delNo? = scanner.nextInt();
? ? ? ? ? ? for (int i = 0; i < articleSet.articles.length; i++) {
? ? ? ? ? ? ? ? if ( articleSet.articles[i] != null && (i+1) == delNo ) {? //數(shù)組中的元素存在 && 找到要?jiǎng)h除的元素i
? ? ? ? ? ? ? ? ? ? int j = i ;// 備份要?jiǎng)h除元素的下標(biāo)
? ? ? ? ? ? ? ? ? ? while ( articleSet.articles[j+1] != null ) { // 要移動(dòng)的元素后一個(gè)位置不為空 ,執(zhí)行以下操作
? ? ? ? ? ? ? ? ? ? ? ? articleSet.articles[j] = articleSet.articles[j+1] ; // 把后面元素的值覆蓋在前面的元素上
? ? ? ? ? ? ? ? ? ? ? ? j ++ ;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? articleSet.articles[j] = null; // 最后一個(gè)元素手動(dòng)修改為空
? ? ? ? ? ? ? ? ? ? System.out.println("刪除成功克婶!");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? System.out.println("刪除失敗");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? }
}
最后在創(chuàng)建Mian類調(diào)用ArticleManage類筒严,實(shí)現(xiàn)代碼運(yùn)行
public class Demo {
? ? public static void main(String[] args) {
? ? ? ? ArticleManage articleManage = new ArticleManage();
? ? ? ? articleManage.initial();
? ? ? ? articleManage.startMenu();
? ? }
}
隨筆
贊
小禮物走一走,來(lái)簡(jiǎn)書(shū)關(guān)注我
贊賞支持
相關(guān)推薦
一情萤、Linux基礎(chǔ)
閱讀 264
六鸭蛙、R語(yǔ)言進(jìn)階
閱讀 276
第九階段 python郵件自動(dòng)發(fā)送報(bào)表
閱讀 53
vue--封裝后臺(tái)管理項(xiàng)目通用組件
閱讀 1329
MySQL 表復(fù)制
閱讀 82