实施自动售货机,我做对了吗?

时间:2018-10-24 22:14:44

标签: java algorithm data-structures hashmap

我试图通过实施自动售货机来提高我在OOP和数据结构方面的技能,因为这是一个常见的采访项目。

自动售货机的基本流程如下:

-此用例始于客户要购买小吃的情况。

-客户通过按键盘上的数字来选择一个数字。

-VM会显示一条消息,显示小吃适用于所选号码,并显示其价格。

-客户插入钱。

-VM验证资金。

-VM接受付款。

-每次输入新钱时,VM都会显示累计金额。

-VM监视接受的钱数,如果钱足够,则VM将选择的小吃分发给客户。

-VM确定是否应将任何更改发送回给客户。

-VM在面板上显示更改。

-然后,VM分发更改。

到目前为止我所做的:

物品类

public class Item {

private String name;
private int number;
private int quntity;
private double price;

public Item(String name, int number, int quntity, double price)
{
    this.name = name;
    this.number = number;
    this.quntity = quntity;
    this.price = price;
}

//setters and getters

public void setName(String name)
{
    this.name = name;
}


public String getName()
{
    return this.name;
}

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

  public int getNum()
{
    return this.number;
}

public void setQuntity(int quntity)
{
    this.quntity = quntity;
}

public int getQuntity()
{
    return this.quntity;
}

public void setPrice(double price)
{
    this.price = price;
}

public double getPrice()
{
    return this.price;
}

}

自动售货机类

public class VendingMachine {

private HashMap<String,Item> itemInventory;

public VendingMachine()
{
    initializeVM();
}

private void initializeVM() {

    Item item1 = new Item("Snickers", 1, 5, 2.5);
    itemInventory.put(item1.getName(), item1);

    Item item2 = new Item("KitKat", 2, 5, 2.5);
    itemInventory.put(item2.getName(), item2);

    Item item3 = new Item("Skittles", 3, 5, 3.0);
    itemInventory.put(item3.getName(), item3);

    Item item4 = new Item("Starbust", 4, 5, 3.0);
    itemInventory.put(item4.getName(), item4);

    Item item5 = new Item("Muffin", 5, 5, 4.0);
    itemInventory.put(item5.getName(), item5);

    Item item6 = new Item("Ice tea", 6, 5, 2.5);
    itemInventory.put(item6.getName(), item6);

    Item item7 = new Item("Ice coffe", 7, 5, 2.5);
    itemInventory.put(item7.getName(), item7);

    Item item8 = new Item("Pringles", 8, 5, 4.0);
    itemInventory.put(item8.getName(), item8);

    Item item9 = new Item("Chetos", 9, 5, 2.0);
    itemInventory.put(item9.getName(), item9);

    Item item10 = new Item("Lez", 10, 5, 2.5);
    itemInventory.put(item10.getName(), item10);

    Item item11 = new Item("Cola", 11, 5, 2.5);
    itemInventory.put(item11.getName(), item11);

    Item item12 = new Item("XL", 12, 5, 5.0);
    itemInventory.put(item12.getName(), item12);

    Item item13 = new Item("Watter", 13, 5, 2.0);
    itemInventory.put(item13.getName(), item13);

    Item item14 = new Item("Marshmelo", 14, 5, 2.5);
    itemInventory.put(item14.getName(), item14);

    Item item15 = new Item("nugget", 15, 5, 6.0);
    itemInventory.put(item15.getName(), item1);

    Item item16 = new Item("hot dog", 16, 5, 5.0);
    itemInventory.put(item16.getName(), item16);

    Item item17 = new Item("orange juce", 17, 5, 2.5);
    itemInventory.put(item17.getName(), item17);

    Item item18 = new Item("Lemon juce", 18, 5, 2.5);
    itemInventory.put(item18.getName(), item18);

    Item item19 = new Item("Gum", 19, 5, 2.0);
    itemInventory.put(item19.getName(), item19);

    Item item20 = new Item("Twix", 20, 5, 3.5);
    itemInventory.put(item20.getName(), item20);

    Item item21 = new Item("Kinder", 21, 5, 3.5);
    itemInventory.put(item21.getName(), item21);

    Item item22 = new Item("Redbull", 22, 5, 4.0);
    itemInventory.put(item22.getName(), item22);

    Item item23 = new Item("Sandwitch", 23, 5, 4.5);
    itemInventory.put(item23.getName(), item23);

    Item item24 = new Item("Bavaria", 24, 5, 3.5);
    itemInventory.put(item24.getName(), item24);

    Item item25 = new Item("Mars", 25, 5, 3.5);
    itemInventory.put(item25.getName(), item25);

}

}

这是实施VM的正确方法吗?我应该考虑些什么?

0 个答案:

没有答案
相关问题