自动贩卖机Java类和对象

food.class

package OOP.Sale;
import java.util.Scanner;
public class food {
    //属性
    String foodname;
    int price;
    int number;//剩余量
    //方法
public
    void show(){
        System.out.println(this.foodname+"的价格是"+this.price+",剩余数量为"+this.number);
        //this指向当前类
    }
    void getmoney(){
        System.out.println("付款金额:");
        Scanner m= new Scanner(System.in);
        int money=m.nextInt();
        if(money>price&&number>0)
        {
            System.out.println("给你"+this.foodname);
            System.out.println("剩余数量为:"+(this.number-1));
            System.out.println("找零"+(money-this.price)+"元");
        }
        else
            System.out.println("支付失败");
    }
}

Application.class

package OOP.Sale;
import java.util.Scanner;
public class Application {
    public static void main(String[] args) {
        System.out.println("选择商品标号(4为取消支付):");
        Scanner c = new Scanner(System.in);
        int choose = c.nextInt();
        switch (choose) {
            case 1:
                food Coca = new food();
                Coca.foodname = "可乐";
                Coca.number = 10;
                Coca.price = 3;
                Coca.show();
                Coca.getmoney();
                break;
            case 2:
                food mian = new food();
                mian.foodname = "方便面";
                mian.number = 60;
                mian.price = 2;
                mian.show();
                mian.getmoney();
                break;
            case 3:
                food bread = new food();
                bread.foodname = "面包";
                bread.number = 50;
                bread.price = 5;
                bread.show();
                bread.getmoney();
                break;
            case 4:
                System.out.println("取消支付");
                break;
        }
    }
}

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值