PDA

View Full Version : Vending Machine Simulator



Luron31
07-08-2009, 10:18 PM
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(String 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 totalCashInMachine = 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.println("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(String product)
{
return quantity; // replace this line with your code
}

public boolean buyItem(int slot)
{
return false; // replace this line with your code
}
}

Jesdisciple
07-10-2009, 04:31 AM
First of all, don't think I'm coming down on you for anything. This is intended as constructive criticism.

JavaScript is not Java (http://www.dynamicdrive.com/forums/forumdisplay.php?f=22). It shouldn't have even been named "JavaScript"; it's a misnomer. In fact, the official name is ECMAScript but no one uses that name because it's not very memorable or easy to pronounce.

As to your question... You need to actually ask a question. :p You didn't even say how the given code doesn't work. So make sure to be a good bit more specific when you ask in the other forum (http://www.dynamicdrive.com/forums/forumdisplay.php?f=22).

thetestingsite
07-10-2009, 04:40 AM
I just moved this thread to the correct Forum. Please let us know how the program is failing (such as errors given, where it stops working, etc).

jjog1
08-07-2009, 03:28 PM
//fields?
public int numslots;
public int maxperslot;
public double cash;
double amountcash;
string quantity;


I think this will cause problems. Those variable names are being used as parameters in your methods, so you don't need to put them there. You be using the ones in your constructor instead
eg.

public int numberOfSlots;

public VendingMachine(int numslots, int maxperslot, double cash)
{

numberOfSlots = numslots;

}

This allows all your methods to access the values in the variables.


//whats supposed to go in front of the equals sign?

When you're assigning a value to a variable no other symbol goes before the =
eg.

//If you've declared the variable already in your class
numberOfSlots = numslots;

//If you are declaring and assigning the variable at the same time
int numberOfSlots = numslots;


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


setProduct and restockProduct shouldn't be "class" they are methods in the VendingMachine class.


My advice would be to read up a bit more about using Arrays, you're on the right path by using them.

The post was a little vague but I hope this helped a bit without making you feel guilty.

EstZer
06-14-2010, 08:58 AM
I know this thread is quite old but I have the same problem now. And the answers or hints found on this thread are helpful, but still I need more. Is there anyone here knows more about this?

Jesdisciple
06-14-2010, 06:39 PM
Use a Vector<Slot> as your VendingMachine's internal representation, and give each Slot name, price, and quantity properties. When finding a Slot, iterate through the Vector and examine each item's name to determine whether it is the correct one. You could use a modified version of a for-loop to restock, with the number of remaining items as your sentry variable, but a while-loop would work too.

Jesdisciple
06-16-2010, 04:51 PM
Note: As the same item in a different slot should have the same price, the price should probably be kept track of separately such as in a Map<String, int>.

wodeln
07-21-2011, 09:21 AM
lalaaaaaaaaaaaaaaaaaaaa

wodeln
07-21-2011, 09:23 AM
http://asktoday.info/2.jpg

wodeln
07-21-2011, 09:23 AM
qqqhttp://asktoday.info/1.jpg