简单饮料自动售货机

简单饮料售货机

实现功能:

设计一个处理单价为5角钱的饮料自动售货机:规格说明
若投入一角或一元的硬币,选择饮料,输出饮料。若无零钱则显示没有零钱,退回硬币,若有零钱,找回零钱并送出饮料,

设计分析:

使用面对对象的方法对自动售货机处理的逻辑进行具体实现,首先使用抽象的思维规划出有硬币Coin,饮料Drink,自动售货机的类,硬币存在不同价值的硬币对象,饮料类有类型属性,可以生成不同类型的饮料对象,自动售货机类,有零钱个数属性,有出售饮料的功能,和内部的找钱逻辑,故有公有的sellDrink()方法出售饮料,私有的returnCharge方法用与找钱。

package com.company;

import java.util.Scanner;
import java.util.concurrent.Callable;

public class AutoMain {
    public static void main(String[] args) {
        com.company.AutoBooth autoBooth = new com.company.AutoBooth(80);
        System.out.println("请选择饮料:");
        Scanner scanner = new Scanner(System.in);
        String drinks=scanner.nextLine();
       // Drink drink=new Drink("drinks");
        Drink drink = new Drink();
        drink.setDrinkName(drinks);
        System.out.println(drink.getDrinkName());
        Coin coin = new Coin(10);
        autoBooth.sellDrink(drink,coin);
      //  int num=autoBooth.getNum();
        System.out.println( "您要的饮料为:"+autoBooth.getDrink().getDrinkName());
        System.out.println("投币金额为:"+coin.getCoinValue()+"饮料名:"+autoBooth.getDrink().getDrinkName()+" 找零"+autoBooth.getReturnCoin().getCoinValue());



    }
}

package com.company;

public class Coin {

    private int coinValue;

    public Coin() {
    }

    public Coin(int coinValue) {

        this.coinValue = coinValue;
    }

    public int getCoinValue() {

        return coinValue;
    }

    public void setCoinValue(int coinValue) {

        this.coinValue = coinValue;
    }
}

package com.company;

public class Drink {

    private String drinkName;
    private double drinkPrice;

    public Drink() {
    }

    public Drink(String drinkName) {

        this.drinkName = drinkName;
    }

    public Drink(String drinkName, double drinkPrice) {
        this.drinkName = drinkName;
        this.drinkPrice = drinkPrice;
    }

    public String getDrinkName() {
        return drinkName;
    }

    public void setDrinkName(String drinkName) {

        this.drinkName = drinkName;
    }

    public double getDrinkPrice() {

        return drinkPrice;
    }

    public void setDrinkPrice(double drinkPrice) {
        this.drinkPrice = drinkPrice;
    }
}

package com.company;

public class AutoBooth {

    private Drink drink;
    private Coin ReturnCoin;
    private int num;

    public AutoBooth() {
    }

    public AutoBooth(int num) {
        this.num = num;
    }

    public AutoBooth(Drink drink, Coin coin, int num) {
        this.drink = drink;
        this.ReturnCoin = coin;
        this.num = num;
    }

    public Drink getDrink() {
        return drink;
    }

    public void setDrink(Drink drink) {
        this.drink = drink;
    }

    public Coin getReturnCoin() {
        return ReturnCoin;
    }

    public void setReturnCoin(Coin coin) {
        this.ReturnCoin = coin;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }


    public void sellDrink(Drink drink,Coin coin)
    {

        if(coin.getCoinValue()==5)
        {
            this.drink=drink;
            this.ReturnCoin=new Coin(0);
            this.num++;
        }
        if(coin.getCoinValue()==10)
        {
            if(this.num>0)
            {
                this.ReturnCoin=new Coin(5);
                this.num--;
                this.drink=drink;

            }
            else
            {
                this.ReturnCoin=new Coin(10);
                this.drink=new Drink("橙汁");
            }


        }


    }


}

实现效果:

简单饮料自动售货机

新手小白,欢迎各位大神指导建议

上一篇:最小硬币数


下一篇:PHP递归题目