I need some hints please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Luron31
    New Member
    • Jul 2009
    • 1

    I need some hints please

    Hello everyone,
    im doing a project and im really stuck on it...
    i dont want answers. i have a very guilty conscience. i just need some hints as to what im doing wrong or what i could do right...

    "A VendingMachine object represents a vending machine that sells products like candy or soda. It has a fixed number of slots, each of which can hold one kind of product for sale. For example, a machine might have 10 Snickers bars in slot 0 and 7 cans of Pepsi in slot 1, but it cannot have some Snickers and some Pepsi in the same slot. There is a maximum number of items that can fit into any one slot - for example, if the maximum was 10, no more Snickers could fit into slot 0. The same kind of product can be in more than one slot, however. The machine must keep track of the name, price, and current quantity of each product, as well as the amount of cash in the machine.

    Slots of the vending machine are be numbered starting at zero.

    Define appropriate fields, and complete the following methods of VendingMachine:

    VendingMachine( int numslots, int maxperslot, double cash) (constructor): Takes the number of slots in the vending machine, the maximum number of items that can fit into any one slot, and the amount of cash in the machine initially. This is the constructor method.

    void setProduct(int slot, String product, double price): Make the given slot hold items of the specified kind of product, sold for the given price. The initial quantity of the product in this slot should be zero. If the slot already held another kind of product, the old product should be removed from this slot.

    void restockProduct( String product, int quantity): Add the given quantity of the specified product to the vending machine. Put as many of the items as possible into the first slot that has been designated to hold that particular kind of product (using setProduct()). If not all of the items will fit into the first slot, put as many of the rest as possible into the second slot that holds that kind of product, etc. For partial credit, your method should at least be able to find the first slot designated for the specified product and put all of the items there.

    double getCashOnHand() : Return the amount of cash now in the vending machine (this amount should increase whenever an item is purchased).

    int getQuantity(int slot): Return the number of items in the given slot.

    int getQuantity(Str ing product): Return the total number of items of the specified kind of product that are in the vending machine. Remember that this product may be in more than one slot. If the product is not in the vending machine at all, simply return zero.

    boolean buyItem(int slot): Attempt to buy one item from the given slot. Return true if successful. "





    public class VendingMachine
    {
    //fields?
    public int numslots; //???
    public int maxperslot; //???
    public double cash;
    double amountcash;
    string quantity;
    double quant;

    //is this supposed to be a different word than below?

    public VendingMachine( int numslots, int maxperslot, double cash)
    {
    //the constructor
    //whats supposed to go in front of the equals sign?
    int numberOfSlots = numslots;
    int maxItemsInSlot = maxperslot;
    double totalCashInMach ine = cash;
    // complete this method
    }

    public class setProduct(int slot, String product, double price)
    {
    //this.product= price;
    }

    //int[] slot = {String product};
    //String[] product;
    //String[] product = {double price};
    }

    public class restockProduct( String product, int quantity)
    {
    if (quantity <= maxperslot)
    {System.out.pri ntln("This is the " + quantity);}

    }

    public double getCashOnHand()
    {
    return amountcash; // replace this line with your code
    }

    public int getQuantity(int slot)
    {
    return quant; // replace this line with your code
    }

    public int getQuantity(Str ing product)
    {
    return quantity; // replace this line with your code
    }

    public boolean buyItem(int slot)
    {
    return false; // replace this line with your code
    }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    If you read that little story think of the nouns as entities or classes and think of the verbs as the methods of those classes (or objects thereof) can perform. Also think of 'has a' and 'is a' relationships where applicable.

    So a vending machine has slots and it has an amount of cash. It also has a constraint on the number of items it can contain in each slot. So basically a vending machine looks like this:

    Code:
    public class VendingMachine {
       private Slot[] slots;
       private int maxItem;
       private double cash;
    }
    The member variables are the state of the vending machine object; the constuctor sets its initial state:

    Code:
    public VendingMachine(int slots, int maxItem, double cash) {
       this.slots= new Slot[slots];
       this.maxItem= maxItem;
       this.cash= cash;
    }
    Note that the array slots knows how many slots it has in it: slots.length. The parameter 'slot' isn't stored anywhere because it isn't needed anymore after creating the array.

    A Slot contains a product and a price and the number of items stored in it, so:

    Code:
    public class Slot {
       private String productName;
       private int items;
       private double price;
    }
    ... and it has a constructor that initializes its state in a similar way as the constructor for the VendingMachine did. I'm sure you can take it from here.

    kind regards,

    Jos

    Comment

    Working...