鱼C论坛

 找回密码
 立即注册
查看: 4415|回复: 1

[学习笔记] 商品系统

[复制链接]
发表于 2021-10-25 13:20:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. package shangpin;

  2. import java.util.InputMismatchException;
  3. import java.util.Scanner;
  4. class OutNumException extends Exception

  5. {

  6.         OutNumException(){

  7.         super("购买数量超出库存!");

  8.         }

  9. }
  10. class OutNumException2 extends Exception

  11. {

  12.         OutNumException2(){

  13.         super("请输入正确的序号!");

  14.         }

  15. }
  16. class ShangPin{

  17.         String name;        //商品名称
  18.         double price;        //商品价格
  19.         int num;                //库存量
  20.         ShangPin(){}
  21.         ShangPin(String name,double price,int num){
  22.                 this.name = name;
  23.                
  24.                 this.num = num;
  25.                
  26.                 this.price=price;

  27.         }
  28.         void print() {

  29.                 System.out.println("库存为:" + num +"名为:" + name+"价格为"+price);

  30.         }

  31. }

  32. class Food extends ShangPin{

  33.         String name;

  34.         int num,data;//保质期

  35.         double price;
  36.         Food(){}
  37.         Food(int num,int data,String name,double price) {

  38.                 this.num = num;

  39.                 this.name = name;

  40.                 this.data = data;
  41.                
  42.                 this.price=price;

  43.         }
  44.         void print() {

  45.                 System.out.println("库存为:" + num +" 商品名为:" + name+" 价格为"+price+" 保质期"+data+"天");


  46.         }
  47. }
  48.        
  49. class Clothes extends ShangPin{

  50.                 String name,size;

  51.                 int num;//保质期

  52.                 double price;
  53.                 Clothes(){}
  54.                 Clothes(int num,String name,String size,double price) {

  55.                         this.num = num;

  56.                         this.name = name;

  57.                         this.size = size;
  58.                        
  59.                         this.price=price;
  60.                        

  61.                 }
  62.                 void print() {

  63.                         System.out.println("库存为:" + num +" 商品名为:" + name+" 价格为"+price+" 型号"+size);


  64.                 }
  65.                
  66. }

  67. public class ShangPing {



  68.         public static void main(String[] args) {

  69.                 // TODO Auto-generated method stub

  70.                 Scanner sc = new Scanner(System.in);

  71.                 ShangPin[] e = new ShangPin[6];
  72.                 Clothes[] o = new Clothes[3];
  73.                 Food[] f = new Food[3];
  74.                 double money;
  75.                 //服装
  76.                 o[0]=new Clothes(10, "T恤","S", 100);
  77.                 o[1]=new Clothes(13, "T恤","M", 110);
  78.                 o[2]=new Clothes(20, "T恤","L", 120);
  79. //食物
  80.                 f[0]=new Food(30,30,"辣条",5.0);
  81.                 f[1]=new Food(5,3,"面包",4.5);
  82.                 f[2]=new Food(14,90,"泡面",4.4);
  83.          e[0]=f[0];
  84.          e[1]=f[1];
  85.          e[2]=f[2];
  86.          e[3]=o[0];
  87.          e[4]=o[1];
  88.          e[5]=o[2];
  89.          System.out.println("当前库存:\n");
  90.          for(int i=0;i<6;i++)
  91.          {
  92.                  e[i].print();
  93.          }
  94. while(true)
  95. {
  96. try {
  97.         System.out.println("请输入你要购买的商品:\n");
  98.         System.out.println("1.食物\t2.衣服\t3.我全都要\n");
  99.        
  100.         int n=sc.nextInt();
  101.         if(n<1||n>3)
  102.         {
  103.                 throw new OutNumException2();
  104.         }
  105.         switch(n) {
  106.         case 1:
  107.                 System.out.println("1.辣条\t2.面包\t3.泡面\n");
  108.                 System.out.println("请输入购买的数量:\n");
  109.                 int a=sc.nextInt();
  110.                 int b=sc.nextInt();
  111.                 int c=sc.nextInt();
  112.                 if(f[0].num<a||f[1].num<b||f[2].num<c)
  113.                 {
  114.                         throw new OutNumException();
  115.                 }
  116.                 f[0].num-=a;
  117.                 f[1].num-=b;
  118.                 f[2].num-=c;
  119.        
  120.                 money=f[0].price*a+f[1].price*b+f[2].price*c;
  121.                
  122.                 System.out.println("剩余库存:\n");
  123.                 System.out.println("购买成功,应支付"+money+"元\n");
  124.                  for(int i=0;i<6;i++)
  125.          {
  126.                  e[i].print();
  127.          }
  128.                  break;
  129.         case 2:
  130.                 System.out.println("1.S\t2.M\t3.L\n");
  131.                 System.out.println("请输入购买的数量:\n");
  132.                 int a1=sc.nextInt();
  133.                 int b1=sc.nextInt();
  134.                 int c1=sc.nextInt();
  135.                 if(o[0].num<a1||o[1].num<b1||o[2].num<c1)
  136.                 {
  137.                         throw new OutNumException();
  138.                 }
  139.                 o[0].num-=a1;
  140.                 o[1].num-=b1;
  141.                 o[2].num-=c1;
  142.                 money=o[0].price*a1+o[1].price*b1+o[2].price*c1;
  143.                
  144.                 System.out.println("剩余库存:\n");
  145.                 System.out.println("购买成功,应支付"+money+"元\n");
  146.                  for(int i=0;i<6;i++)
  147.          {
  148.                  e[i].print();
  149.          }
  150.                  break;
  151.         case 3:
  152.                 System.out.println("1.辣条\t2.面包\t3.泡面\t4.S\t5.M\t6.L\n");
  153.                 System.out.println("请输入购买的数量:\n");
  154.                 int a2=sc.nextInt();
  155.                 int b2=sc.nextInt();
  156.                 int c2=sc.nextInt();
  157.                 int a3=sc.nextInt();
  158.                 int b3=sc.nextInt();
  159.                 int c3=sc.nextInt();
  160.                 if(f[0].num<a2||f[1].num<b2||f[2].num<c2||o[0].num<a3||o[1].num<b3||o[2].num<c3)
  161.                 {
  162.                         throw new OutNumException();
  163.                 }
  164.                 f[0].num-=a2;
  165.                 f[1].num-=b2;
  166.                 f[2].num-=c2;
  167.                 o[0].num-=a3;
  168.                 o[1].num-=b3;
  169.                 o[2].num-=c3;
  170.                 money=f[0].price*a2+f[1].price*b2+f[2].price*c2+o[0].price*a3+o[1].price*b3+o[2].price*c3;
  171.                
  172.                 System.out.println("剩余库存:\n");
  173.                 System.out.println("购买成功,应支付"+money+"元\n");
  174.                  for(int i=0;i<6;i++)
  175.          {
  176.                  e[i].print();
  177.          }
  178.                  break;
  179.                
  180.         }
  181.         break;
  182.         }

  183. catch (InputMismatchException e1) {

  184.         System.out.println("输入类型错误");
  185.         sc.next();

  186. }
  187.         catch(OutNumException e1)
  188. {
  189.                 System.out.println(e1.getMessage());
  190. } catch (OutNumException2 e1) {
  191.                 // TODO Auto-generated catch block
  192.         System.out.println(e1.getMessage());
  193.         }
  194.        
  195.         }
  196. }
  197. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-25 19:15:47 | 显示全部楼层
建议代码分模块重构一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 09:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表