Java

Vending Machine

Page 1 of 1

4 Replies - 4379 Views - Last Post: 23 December 2007 - 06:22 PM Rate Topic: -----

#1 livingstone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-November 07

Java

Post icon  Posted 23 December 2007 - 05:02 AM

Students will write an application to implement a soda vending machine. The vending machine accepts dollar bills, quarters, dimes, and nickels. It dispenses cans of soda, which cost 60 cents, and bottles, which cost a dollar. It should store the number of cans and bottles of soda that the machine contains. It should also store the amount of money deposited. All information about the content of the vending machine, and its sales must be implemented in a class called VendingMachine, with the appropriate instance variables.

Here is a typical session:

Vending Machine:
Currently containing 10 cans, and 5 bottles.
Current sales: $0
What do you want to buy? (1 for can, 2 for bottle, 0 for exit): 1

Vending Machine:
Currently containing 9 cans, and 5 bottles.
Current sales: $.60
What do you want to buy? (1 for can, 2 for bottle, 0 for exit): 2

Vending Machine:
Currently containing 9 cans, and 4 bottles.
Current sales: $1.60
What do you want to buy? (1 for can, 2 for bottle, 0 for exit): 2

Vending Machine:
Currently containing 9 cans, and 3 bottles.
Current sales: $2.60
What do you want to buy? (1 for can, 2 for bottle

public class VendingMachine1Main {
  VendingMachine cansdrinks = new VendingMachineCansDrink(nofDrinks);
  VendingMachine bottlesdrinks = new VendingMachinebottlesDrinks(nofDrinks);
 
	int dollarbill;
	int quarters;
	int dimes;
	int nickels;
}
		
	public class void main VendingMachine1main{ 
	VendingMachine machine= new VendingMachine();
	
	Machine. fillup(10)cans; // fill up with ten cans
	Machine. InsertToken();
	Machine.InsertToken();
	System.out.print("Token Count=");
	System.out.println(Machine .getTokenCount());
	System.out.print("can Count= ");
	System.out.println(machine.getcanCount());
	Machine. fillup(5)bottles; //fill up with five bottle
	Machine. InsertToken();
	Machine.InsertToken();
	System.out.print("Token Count=");
	System.out.println(Machine .getTokenCount());
	System.out.print("bottle Count= ");
	System.out.println(machine.getbottleCount());
	}
	
	
	
	public void{
	  Machine. fillup(9)cans; // fill up with ten cans
	Machine. InsertToken();
	Machine.InsertToken();
	System.out.print("Token Count=");
	System.out.println(Machine .getTokenCount());
	System.out.print("can Count= ");
	System.out.println(machine.getcanCount());
	Machine. fillup(5)bottles; //fill up with five bottle
	Machine. InsertToken();
	Machine.InsertToken();
	System.out.print("Token Count=");
	System.out.println(Machine .getTokenCount());
	System.out.print("bottle Count= ");
	System.out.println(machine.getbottleCount());
	}
	
	public  void {
		Machine. fillup(9)cans; // fill up with ten cans
	Machine. InsertToken();
	Machine.InsertToken();
	System.out.print("Token Count=");
	System.out.println(Machine .getTokenCount());
	System.out.print("can Count= ");
	System.out.println(machine.getcanCount());
	Machine. fillup(5)bottles; //fill up with five bottle
	Machine. InsertToken();
	Machine.InsertToken();
	System.out.print("Token Count=");
	System.out.println(Machine .getTokenCount());
	System.out.print("bottle Count= ");
	System.out.println(machine.getbottleCount());
	}
	
	public static void {
		Machine. fillup(9)cans; // fill up with ten cans
	Machine. InsertToken();
	Machine.InsertToken();
	System.out.print("Token Count=");
	System.out.println(Machine .getTokenCount());
	System.out.print("can Count= ");
	System.out.println(machine.getcanCount());
	Machine. fillup(3)bottles; //fill up with five bottle
	Machine. InsertToken();
	Machine.InsertToken();
	System.out.print("Token Count=");
	System.out.println(Machine .getTokenCount());
	System.out.print("bottle Count= ");
	System.out.println(machine.getbottleCount());
	
	
	}
	}
	}


:) :) :) , 0 for exit): 0

Good bye.
this is the question ,and this is what I did or attempt to solve it please help me out

MOD EDIT : :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Java

#2 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Java

Posted 23 December 2007 - 07:58 AM

Moved to Java Forums.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Java

Posted 23 December 2007 - 08:42 AM

Your session doesn't show user putting in money, so why keep track of the change? All the other code doesn't relate to the typical session, either. Given the specs, it only looks like you have to handle three options. This could be done with very simple code, e.g.

public class VendingMachine {
	private double sales;
	private int cans;
	private int bottles;
	
	public VendingMachine() {
		fillMachine();
	}
	
	public void fillMachine() {
		sales = 0;
		cans = 10;
		bottles = 5;
	}
	
	public int getCanCount() { }
	public int getBottleCount() { }
	public double getSales() { }
	
	public void vendCan() {
		if (this.cans==0) {
			System.out.println("Sorry, out of cans.");
		} else {
			this.cans -= 1;
			this.sales += 0.6;
		}
	}
	
	public boolean getUserSelection() {
	}
	
	public static void main(String[] argv) {
		VendingMachine machine = new VendingMachine();
		while(machine.getUserSelection());
	}
	
}



Hope this helps.

This post has been edited by baavgai: 23 December 2007 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 livingstone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-November 07

Re: Java

Posted 23 December 2007 - 04:08 PM

View Postbaavgai, on 23 Dec, 2007 - 08:42 AM, said:

Your session doesn't show user putting in money, so why keep track of the change? All the other code doesn't relate to the typical session, either. Given the specs, it only looks like you have to handle three options. This could be done with very simple code, e.g.

public class VendingMachine {
	private double sales;
	private int cans;
	private int bottles;
	
	public VendingMachine() {
		fillMachine();
	}
	
	public void fillMachine() {
		sales = 0;
		cans = 10;
		bottles = 5;
	}
	
	public int getCanCount() { }
	public int getBottleCount() { }
	public double getSales() { }
	
	public void vendCan() {
		if (this.cans==0) {
			System.out.println("Sorry, out of cans.");
		} else {
			this.cans -= 1;
			this.sales += 0.6;
		}
	}
	
	public boolean getUserSelection() {
	}
	
	public static void main(String[] argv) {
		VendingMachine machine = new VendingMachine();
		while(machine.getUserSelection());
	}
	
}



Hope this helps.

Thank alot I really think these help me on the right track ,but the issues is that there is a missing returning statement ,I did not compile , and I having been trying to figure out the returning statement , please help me out !! :)
Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Java

Posted 23 December 2007 - 06:22 PM

It wouldn't compile. It's a framework. There's a lot of stuff missing. Here's an example.

This:
public int getCanCount() { }


Needs to be this:
public int getCanCount() { return this.cans; }


It's up to you to fill in the rest. Your biggest method will be getUserSelection, where status will be printed, user data will be asked for, a three will return false, a one or two will call other methods.

An outline like this will help you figure out what's really important to your program. Now, it's time to write some code. Have fun.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1