469,360 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

Vending machine program

Please help I need help with this homework.

Its about vending machines, and it should be a piece of cake to you code experts out there.

Its about vending machines. Heres the question:

It should have two classes, one for the vending machine(VM) and the other for drinks. The capacity of the VM is specified on creation. The drinks can be added after this. VM just created should have no drinks. These drinks are stored in slots and are numbered. Only these specified coins can be accepted: 10, 50 cents and 1 dollar. VM should have no coins when created. The VM should let the user know how much the VM currently has and should be displayed as e.g. $1.05. The VM should allow the user to be able to abort and the VM should refund the money if it has not given any drinks. The VM should display what coins will be used for the refund. This program should not allow runtime errors.

Please note that the answer to this should be very simple and basic as we have covered only the very basic stuff, since this is only an introductory course. The answer will be of no use to me if it isn't solved in a basic manner.

Thanks in advance. I really appreciate any assistance available as I'm afraid I'll fail the course. Whats upsetting is that we have not been prepared enough to solve such a question, yet we are being asked.

Regards,

John
Oct 30 '07 #1
7 14220
Ganon11
3,652 Expert 2GB
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

Then when you are ready post a new question in this thread.

MODERATOR
Oct 30 '07 #2
Can you at least help me out a little. So it starts with probably class name:

public class VM
{
public VM()
{
vendingMachnine[];
}

public double[] coins = {};
public final String[] vM= {"Pepsi","Fanta","Diet Coke"};
}

Then what? How can I learn if I don't have similar examples.
Oct 30 '07 #3
r035198x
13,262 8TB
Can you at least help me out a little. So it starts with probably class name:

public class VM
{
public VM()
{
vendingMachnine[];
}

public double[] coins = {};
public final String[] vM= {"Pepsi","Fanta","Diet Coke"};
}

Then what? How can I learn if I don't have similar examples.
1.) Please use code tags when posting code.
2.) Your specs say "The capacity of the VM is specified on creation". Where have you specified this in your class? You should refer to your specs every time you are not sure about what to do next.
Oct 30 '07 #4
JosAH
11,448 Expert 8TB
The following two remarks really amaze me; no kidding.

The answer will be of no use to me if it isn't solved in a basic manner.

Then what? How can I learn if I don't have similar examples.
Here are a few hints: measure the amount of money in pennies, so instead
of thinking in $1.05 think of 105 pennies. You can use ints for monetary amounts
then. The brand of a drink is coupled to a selling price, so the Drink class can
be as simple as this:

Expand|Select|Wrap|Line Numbers
  1. public class Drink {
  2.    private String brand;
  3.    private int money;
  4.    public Drink(String brand, int money) {
  5.       this.brand= brand;
  6.       this.money= money;
  7.    }
  8.    // two getters should be here
  9.    // and may an equals() and hashCode() method
  10. }
  11.  
Note that this class is a complete 'bean' class. The vending machine should
contain some form of container that can store Drinks. The actual container is
up to you. I'd use a Map for it that associates a brand of Drinks with the number
of cans present in the vending machine but feel free to use a simple array and
add utitility functionality around that array (e.g. is Cola Light still available? etc.)

Next, realize that the vending machine is a state machine, i.e. there are only
a couple of states which determine what the machine can and should do given
a certain state.

Examples of states are: reset state, enough money supplied state, non enough
money supplied state, return change of money after dispensing a can. return all
money and back to the reset state etc. etc.

This forum will certainly *not* do your homework for you; I guess the hints I gave
you above will get you started in a more sensible way.

kind regards,

Jos
Oct 30 '07 #5
Hi,

Thanks for helping get started guys. I guess I got a little panicked, but I've been able to make some progress. Now though I'm stuck and was wondering if you could help me out.

I'm using a multiple array for this vending machine program. Though I'm not sure. Should one dimensional array be enough. Also, with this program the user should be able to buy the drink by selecting its slot number. So how do I write the code so that I can specifiy with the index number of the array to be outputted, or simply allow it to be bought. Basically I'm trying to make an input and the output should be the content of the array at the specified index number.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < array.length; i++) {           
  2.  
  3. }               
  4.  
  5.  
Can someone please help me out.

Regards,

John
Nov 1 '07 #6
r035198x
13,262 8TB
Hi,

Thanks for helping get started guys. I guess I got a little panicked, but I've been able to make some progress. Now though I'm stuck and was wondering if you could help me out.

I'm using a multiple array for this vending machine program. Though I'm not sure. Should one dimensional array be enough. Also, with this program the user should be able to buy the drink by selecting its slot number. So how do I write the code so that I can specifiy with the index number of the array to be outputted, or simply allow it to be bought. Basically I'm trying to make an input and the output should be the content of the array at the specified index number.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < array.length; i++) {           
  2.  
  3. }               
  4.  
  5.  
Can someone please help me out.

Regards,

John
Why don't you start by writing the classes required and fulfiling their specifications?
Nov 1 '07 #7
JosAH
11,448 Expert 8TB
Hi,

Thanks for helping get started guys. I guess I got a little panicked, but I've been able to make some progress. Now though I'm stuck and was wondering if you could help me out.

I'm using a multiple array for this vending machine program. Though I'm not sure. Should one dimensional array be enough. Also, with this program the user should be able to buy the drink by selecting its slot number. So how do I write the code so that I can specifiy with the index number of the array to be outputted, or simply allow it to be bought. Basically I'm trying to make an input and the output should be the content of the array at the specified index number.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < array.length; i++) {           
  2.  
  3. }               
  4.  
  5.  
Can someone please help me out.

Regards,

John
You could use the little class I sketched in my previous reply (with a couple of
modifications):

Expand|Select|Wrap|Line Numbers
  1. public class Drink {
  2.    private String brand;
  3.    private int money;
  4.    private int stock;
  5.    public Drink(String brand, int money, int stock) {
  6.       this.brand= brand;
  7.       this.money= money;
  8.       this.stock= stock;
  9.    }
  10.    // three getters and a few setters should be here
  11.    // and may an equals() and hashCode() method
  12. }
  13.  
The vending machine could simply use an array of Drinks then:

Expand|Select|Wrap|Line Numbers
  1. public class VendingMachine {
  2.  
  3.    private Drink[] drinks;
  4.  
  5.    // a machine with 'nofDrinks' slots
  6.    public VendingMachine(int nofDrinks) {
  7.       drinks= new Drink[nofDrinks];
  8.    }
  9.    // setters here to put stock in the vending machine
  10.    // getters here to inspect the stock
  11. }
  12.  
kind regards,

Jos
Nov 2 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by pradeepkumar | last post: by
reply views Thread by joestevens232 | last post: by
3 posts views Thread by ismaeel | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.