|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- 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[6];
- Clothes[] o = new Clothes[3];
- Food[] f = new Food[3];
- double money;
- //服装
- o[0]=new Clothes(10, "T恤","S", 100);
- o[1]=new Clothes(13, "T恤","M", 110);
- o[2]=new Clothes(20, "T恤","L", 120);
- //食物
- f[0]=new Food(30,30,"辣条",5.0);
- f[1]=new Food(5,3,"面包",4.5);
- f[2]=new Food(14,90,"泡面",4.4);
- e[0]=f[0];
- e[1]=f[1];
- e[2]=f[2];
- e[3]=o[0];
- e[4]=o[1];
- e[5]=o[2];
- System.out.println("当前库存:\n");
- for(int i=0;i<6;i++)
- {
- e[i].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[0].num<a||f[1].num<b||f[2].num<c)
- {
- throw new OutNumException();
- }
- f[0].num-=a;
- f[1].num-=b;
- f[2].num-=c;
-
- money=f[0].price*a+f[1].price*b+f[2].price*c;
-
- System.out.println("剩余库存:\n");
- System.out.println("购买成功,应支付"+money+"元\n");
- for(int i=0;i<6;i++)
- {
- e[i].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[0].num<a1||o[1].num<b1||o[2].num<c1)
- {
- throw new OutNumException();
- }
- o[0].num-=a1;
- o[1].num-=b1;
- o[2].num-=c1;
- money=o[0].price*a1+o[1].price*b1+o[2].price*c1;
-
- System.out.println("剩余库存:\n");
- System.out.println("购买成功,应支付"+money+"元\n");
- for(int i=0;i<6;i++)
- {
- e[i].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[0].num<a2||f[1].num<b2||f[2].num<c2||o[0].num<a3||o[1].num<b3||o[2].num<c3)
- {
- throw new OutNumException();
- }
- f[0].num-=a2;
- f[1].num-=b2;
- f[2].num-=c2;
- o[0].num-=a3;
- o[1].num-=b3;
- o[2].num-=c3;
- 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;
-
- System.out.println("剩余库存:\n");
- System.out.println("购买成功,应支付"+money+"元\n");
- for(int i=0;i<6;i++)
- {
- e[i].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());
- }
-
- }
- }
- }
复制代码 |
|