2 Replies - 2757 Views - Last Post: 14 November 2011 - 07:14 AM Rate Topic: -----

#1 adrk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-September 11

Vending machine problem

Posted 14 November 2011 - 06:35 AM

Hi people. I have to make a vending machine with sodas. I am using an array to in which the first index is the name of the drink and the second index is the number of drinks left. My question is how do I check if there are drinks left?

public interface Sellable {
	
	public int getPrice();
	public String getName();

}




import java.util.Scanner;
public class Automat implements Sellable {
	Scanner s = new Scanner(System.in);
	public int credit = 0;
	public int price;
	public String name;
	public static Sellable[][] produse = new Sellable[3][1];
	

			
			
			public int getPrice(){
		return price;
	}
			public String getName() {
				return name;
			}
	
public void insertCoins(int quantity){
	System.out.println("Please insert credit");
	credit += s.nextInt();
}
public void sellProductAtIndex(int index){
	System.out.println("Please Select Drink");
	System.out.println();
	Scanner input = new Scanner(System.in);
	while (true) {
		System.out.println(" Press 0 to Select CocaCola");
		System.out.println(" Press 1 to Select Pepsi");
		System.out.println(" Press 2 to Select 7Up");
		System.out.println();
		int choice = input.nextInt();
		while (choice < 1 || choice > 3) {
			 System.out.println("Please pick a valid product");
			 System.out.print("Your Choice    :");
			 choice = input.nextInt();
		}
		if(choice==1 && produse[0][0].equals(0)){
			System.out.println("Out of order please select another product");
		}
		else if(choice==1 && (produse[0][0]>0)	)
	}
}



public static void main(String [] args){
	produse[0][0]= new CocaCola(2, 10);
	produse[1][0]= new Pepsi(1,5);
	produse[2][0]= new Zup(3,3);
}



}



public class CocaCola implements Sellable{
	public int price = 2;
	public String name = "CocaCola";
	public int buc = 10;
	

	public CocaCola(int price, int buc){
		this.price = price;
		this.buc = buc;
	}
	
	public int getPrice(){
		return this.price;
	}
	public String getName(){
		return this.name;
	}
}


and so forth.....

Is This A Good Question/Topic? 0
  • +

Replies To: Vending machine problem

#2 Mylo   User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Vending machine problem

Posted 14 November 2011 - 07:08 AM

It would probably easier to create a drinksLeft variable in the vending machine and subtract one each time a SellProductAtIndex() method is called.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Vending machine problem

Posted 14 November 2011 - 07:14 AM

Question:

View Postadrk, on 14 November 2011 - 07:35 AM, said:

My question is how do I check if there are drinks left?


Answer:

View Postadrk, on 14 November 2011 - 07:35 AM, said:

the second index is the number of drinks left.


Suedo code:

if (second index of this product > 0) then sellTheDrink and decrement the inventory quantity
else Tell the customer sold out and make another selection

To tell if *any* flavor has any quantity:
Loop through all the drinks
Check the quantity
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1