guhusf 发表于 2021-10-25 13:20:33

商品系统

package shangpin;

import java.util.InputMismatchException;
import java.util.Scanner;
class OutNumException extends Exception

{

        OutNumException(){

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

        }

}
class OutNumException2 extends Exception

{

        OutNumException2(){

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

        }

}
class ShangPin{

        String name;        //商品名称
        double price;        //商品价格
        int num;                //库存量
        ShangPin(){}
        ShangPin(String name,double price,int num){
                this.name = name;
               
                this.num = num;
               
                this.price=price;

        }
        void print() {

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

        }

}

class Food extends ShangPin{

        String name;

        int num,data;//保质期

        double price;
        Food(){}
        Food(int num,int data,String name,double price) {

                this.num = num;

                this.name = name;

                this.data = data;
               
                this.price=price;

        }
        void print() {

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


        }
}
       
class Clothes extends ShangPin{

                String name,size;

                int num;//保质期

                double price;
                Clothes(){}
                Clothes(int num,String name,String size,double price) {

                        this.num = num;

                        this.name = name;

                        this.size = size;
                       
                        this.price=price;
                       

                }
                void print() {

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


                }
               
}

public class ShangPing {



        public static void main(String[] args) {

                // TODO Auto-generated method stub

                Scanner sc = new Scanner(System.in);

                ShangPin[] e = new ShangPin;
                Clothes[] o = new Clothes;
                Food[] f = new Food;
                double money;
                //服装
                o=new Clothes(10, "T恤","S", 100);
                o=new Clothes(13, "T恤","M", 110);
                o=new Clothes(20, "T恤","L", 120);
//食物
                f=new Food(30,30,"辣条",5.0);
                f=new Food(5,3,"面包",4.5);
                f=new Food(14,90,"泡面",4.4);
         e=f;
         e=f;
         e=f;
         e=o;
         e=o;
         e=o;
         System.out.println("当前库存:\n");
         for(int i=0;i<6;i++)
         {
             e.print();
         }
while(true)
{
try {
        System.out.println("请输入你要购买的商品:\n");
        System.out.println("1.食物\t2.衣服\t3.我全都要\n");
       
        int n=sc.nextInt();
        if(n<1||n>3)
        {
                throw new OutNumException2();
        }
        switch(n) {
        case 1:
                System.out.println("1.辣条\t2.面包\t3.泡面\n");
                System.out.println("请输入购买的数量:\n");
                int a=sc.nextInt();
                int b=sc.nextInt();
                int c=sc.nextInt();
                if(f.num<a||f.num<b||f.num<c)
                {
                        throw new OutNumException();
                }
                f.num-=a;
                f.num-=b;
                f.num-=c;
       
                money=f.price*a+f.price*b+f.price*c;
               
                System.out.println("剩余库存:\n");
                System.out.println("购买成功,应支付"+money+"元\n");
               for(int i=0;i<6;i++)
         {
             e.print();
         }
               break;
        case 2:
                System.out.println("1.S\t2.M\t3.L\n");
                System.out.println("请输入购买的数量:\n");
                int a1=sc.nextInt();
                int b1=sc.nextInt();
                int c1=sc.nextInt();
                if(o.num<a1||o.num<b1||o.num<c1)
                {
                        throw new OutNumException();
                }
                o.num-=a1;
                o.num-=b1;
                o.num-=c1;
                money=o.price*a1+o.price*b1+o.price*c1;
               
                System.out.println("剩余库存:\n");
                System.out.println("购买成功,应支付"+money+"元\n");
               for(int i=0;i<6;i++)
         {
             e.print();
         }
               break;
        case 3:
                System.out.println("1.辣条\t2.面包\t3.泡面\t4.S\t5.M\t6.L\n");
                System.out.println("请输入购买的数量:\n");
                int a2=sc.nextInt();
                int b2=sc.nextInt();
                int c2=sc.nextInt();
                int a3=sc.nextInt();
                int b3=sc.nextInt();
                int c3=sc.nextInt();
                if(f.num<a2||f.num<b2||f.num<c2||o.num<a3||o.num<b3||o.num<c3)
                {
                        throw new OutNumException();
                }
                f.num-=a2;
                f.num-=b2;
                f.num-=c2;
                o.num-=a3;
                o.num-=b3;
                o.num-=c3;
                money=f.price*a2+f.price*b2+f.price*c2+o.price*a3+o.price*b3+o.price*c3;
               
                System.out.println("剩余库存:\n");
                System.out.println("购买成功,应支付"+money+"元\n");
               for(int i=0;i<6;i++)
         {
             e.print();
         }
               break;
               
        }
        break;
        }

catch (InputMismatchException e1) {

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

}
        catch(OutNumException e1)
{
                System.out.println(e1.getMessage());
} catch (OutNumException2 e1) {
                // TODO Auto-generated catch block
        System.out.println(e1.getMessage());
        }
       
        }
}
}

冰点z 发表于 2021-10-25 19:15:47

建议代码分模块重构一下
页: [1]
查看完整版本: 商品系统