Java版本-----商店购物系统

buy.java

public class Buy {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Shops shops = new Shops();

      
    }

}

 

shops.java

import java.util.Scanner;

public class Shops {
    public static int m;
    public static  int numberCup = 0 , numberRubber = 0, numberPen = 0, allPrice = 0;
    /**  构 造 函 数  */
    public Shops(){
    
        showAllGoods();
    }
    
    /**    显 示 所 有 的 商 品   */
    public void showAllGoods(){
        System.out.println("欢迎来到自助超市, 我们这里有以下商品\n1.笔\n2.水杯\n3.橡皮\n4.退出\n请输入相应的编号购买:");
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        switch (i) {
        case 1:
            System.out.println("请输入数量:");
           inputCode();
           numberPen += m;
           showAllBuyGoods();
           
            break;
        case 2:
            System.out.println("请输入数量:");
            inputCode();
            numberCup += m;
            showAllBuyGoods();
            
            break;
            
        case 3:
            System.out.println("请输入数量:");
            inputCode();
            numberRubber += m;
            showAllBuyGoods();
            break;
        case 4:
            System.exit(1);
            break;
        default:
            break;
        }

    }
    /**   用 户 输 入 商 品 编 码 函 数    */
    public void     inputCode(){
        Scanner scann = new Scanner(System.in);
        m = scann.nextInt();
    }
    /**    显 示 已 经 购 买 的 商 品   */
    public void     showAllBuyGoods(){
         allPrice = numberCup * 5 + numberPen * 3 + numberRubber * 2 ;
        System.out.println("你选择了"+numberPen+"只笔,"+numberCup+"个水杯,"+numberRubber+"个橡皮擦, 一共"+allPrice+"元.还需要其他的吗, 需要请输入1, 不需要请输入2.");
        isGoOn();
        
    }
    /**    看 用 户 是 否 继 续   */
    public void isGoOn(){
        Scanner scann = new Scanner(System.in);
        m = scann.nextInt();
        if (m == 1) {
            showAllGoods();
        }else if (m == 2) {
            //TODO
            allPrice = numberCup * 5 + numberPen * 3 + numberRubber * 2 ;
            buy(allPrice);
        }else{
            System.out.println("输入有误, 请重新输入.");
            isGoOn();
        }

    }
    /**   购 买 函  数    */
    public void buy(int a){
        if (a  > 100) {
            System.out.println("选中的物品大于100元,请删除:");
            deleteGoods();
        }else{
            System.out.println("付款成功.");
        }

    }
    /**  删 除 商 品    */
    public void deleteGoods(){
        System.out.println("1.笔  2.水杯  3.橡皮");
        Scanner scanner = new Scanner(System.in);
        int j = scanner.nextInt();
        switch (j) {
        case 1:
            System.out.println("请输入数量:");
           inputCode();
           numberPen -= m;
           showAllBuyGoods();
           
            break;
        case 2:
            System.out.println("请输入数量:");
            inputCode();
            numberCup -= m;
            showAllBuyGoods();
            break;
            
        case 3:
            System.out.println("请输入数量:");
            inputCode();
            numberRubber -= m;
            showAllBuyGoods();
            break;
        case 4:
            System.exit(1);
            break;
        default:
            break;
        
    }
}
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值