HappyDaQingZi

升级商品信息类,提供商品类的数据模型

利用构造方法

 1 package com.heqing.model;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * 商品信息类,提供商品这种数据的模型。
 7  * @author HeQing
 8  */
 9 public class Goods {
10     public Goods(Scanner input) {
11         System.out.print("请输入商品名称:");
12         this.name = input.next();
13         System.out.print("请输入商品价格:");
14         this.price = input.nextDouble();
15         System.out.print("请输入库存:");
16         this.stock = input.next();
17     }
18 
19     /** 商品名称 */
20     public String name;
21     /** 商品价格 */
22     public double price;
23     /** 商品库存 */
24     public String stock;
25     public String getInfo(int index){
26         return index + "\t商品名称:"+this.name+"\t商品价格:"+this.price+"\t商品库存:"+this.stock;
27     }
28 }

增加删除商品的编号错误信息判断

 1 package com.heqing.main;
 2 
 3 import com.heqing.model.Goods;
 4 
 5 import java.util.Scanner;
 6 
 7 /**
 8  * 主类,程序直接从这里进行启动。
 9  *
10  * @author HeQing
11  */
12 public class Main {
13     public static void main(String[] args) {
14         Scanner input = new Scanner(System.in);
15         Goods[] goodsArray = new Goods[200];
16         int arrayIndex = 0;
17         loop:
18         while (true) {
19             System.out.println("===================================================================================");
20             System.out.println("                           欢迎使用畅畅商品管理系统");
21             System.out.println("===================================================================================");
22             System.out.println("[1.添加新商品 2.展示已有信息 3.删除商品信息 5.退出]");
23             System.out.print("请输入-->");
24             switch (input.nextInt()) {
25                 case 1:
26                     Goods newGoods = new Goods(input);
27                     goodsArray[arrayIndex ++] = newGoods;
28                     break;
29                 case 2:
30                     for (int i = 0; i < arrayIndex; i++) {
31                         Goods temp = goodsArray[i];
32                         System.out.println(temp.getInfo(i+1));
33                     }
34                     break;
35                 case 3:
36                     System.out.print("请输入你需要删除的商品的编号:");
37                     int inputNumber = input.nextInt();
38                     if (inputNumber < 1 || inputNumber > arrayIndex){
39                         System.out.println("删除失败");
40                         break ;
41                     }
42                     for (int i = inputNumber - 1; i < arrayIndex; i++) {
43                         goodsArray[i] = goodsArray[i + 1];
44                     }
45                     arrayIndex --;
46                     System.out.println("删除成功!");
47                     break;
48                 case 5:
49                     System.out.println("感谢使用,么么哒!!!");
50                     break loop;
51             }
52         }
53     }
54 }

 

分类:

技术点:

相关文章: