1 Replies - 1848 Views - Last Post: 08 October 2013 - 06:55 AM Rate Topic: -----

#1 kolibri   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-October 13

Vending machine problem

Posted 08 October 2013 - 04:56 AM

I have for a week now tried to read and understand how methods and classes work, but i can't implement those in my vending machine problem. I can not understand how to write the code for having choice of drink in one class, payment in another class and the calculation in a third.
Second i need the program to return to the choice menu if select is typed and the amount paid is below the price for the drink choosen. I have only been able to make a line that calculates what remains but the program then keeps on going until you got the right price or above. I really hope for some help as this is the first time i have ever tried to write a program.

Here is what i have made.



import java.util.*;
 public class Automaton {

public static void main (String[] args) {
 boolean exit;
 exit = false;
 String choice;
 String coin;
 String first ="You have choosen ";
 String second =", please pay ";
 String third = " pence and type select when done; ";
 String coinsacc = "Coins accepted: 1,2,5,10 and 20: ";
 String in = "Insert coin: ";
 String water1 = "water";
 String coca = "cola";
 String juicy ="juice";
 String thanks = "Thank you. No change.";
 String thank = "Thank you. Your change is: ";
 String back = " pence remaining.";
 String not = "Not enough paid ";
 String pence = " pence.";
 String select = "select";
 final int juice = 15;
 final int water = 10;
 final int cola = 12;
 Scanner console = new Scanner(System.in);
 while (!exit) {
 System.out.println("Choose your drink ");
 System.out.println("Water press w ");
 System.out.println("Cola press c ");
 System.out.println("Juice press j ");
 System.out.print("Your choice: ");
 choice = console.next();
 if (choice.equalsIgnoreCase("w")) {
 System.out.println(first + water1 + second + water + third);
 System.out.println(coinsacc);
 int amount = 0;
 while (amount < 100000000) {
 System.out.println(in);
 coin = console.next();
 if (coin.equals("1")) {
 amount += 1;
 } else if (coin.equals("2")) {
 amount += 2;
 } else if (coin.equals("5")) {
 amount += 5;
 } else if (coin.equals("10")) {
 amount += 10;
 } else if (coin.equals("20")) {
 amount += 20;
 } else if (!coin.equals("1") && !coin.equals("2") && !coin.equals("5")
 && !coin.equals("10") && !coin.equals("20") &&
 !coin.equalsIgnoreCase(select)) {
 System.exit(0);
 } else if (coin.equalsIgnoreCase(select) && amount == 10 ) {
 System.out.println(thanks);
 } else if (coin.equalsIgnoreCase(select) && amount > 10) {
 System.out.println(thank + (amount - water) + pence);
 } else if (coin.equalsIgnoreCase(select) && amount < 10) {
 System.out.println(not + (water - amount) + back);
 break ;
 }
 }
 }
 if (choice.equalsIgnoreCase("c")) {
 System.out.println(first + coca + second + cola + third);
 System.out.print(coinsacc);
 int amount = 0;
 while (amount < 100000000) {
 System.out.println(in);
 coin = console.next();
 if (coin.equals("1")) {
 amount += 1;
 } else if (coin.equals("2")) {
 amount += 2;
 } else if (coin.equals("5")) {
 amount += 5;
 } else if (coin.equals("10")) {
 amount += 10;
 } else if (coin.equals("20")) {
 amount += 20;
 } else if (!coin.equals("1") && !coin.equals("2")
 && !coin.equals("5") && !coin.equals("10")
 && !coin.equals("20") && !coin.equalsIgnoreCase(select)) {
 System.exit(0);
 } else if (coin.equalsIgnoreCase(select) && amount < 12) {
 System.out.println(not + (cola - amount) + back);
 } else if (coin.equalsIgnoreCase(select) && amount == 12 ) {
 System.out.println(thanks);
 } else if (coin.equalsIgnoreCase(select) && amount > 12) {
 System.out.println(thank + (amount - cola) + pence);
 break ;
 }
 }
 }
 if (choice.equalsIgnoreCase("j")) {
 System.out.println(first + juicy + second + juice + third);
 System.out.print(coinsacc);
 int amount = 0;
 while (amount < 100000000) {
 System.out.println(in);
 coin = console.next();
 if (coin.equals("1")) {
 amount += 1;
 } else if (coin.equals("2")) {
 amount += 2;
 } else if (coin.equals("5")) {
 amount += 5;
 } else if (coin.equals("10")) {
 amount += 10;
 } else if (coin.equals("20")) {
 amount += 20;
 } else if (!coin.equals("1") && !coin.equals("2")
 && !coin.equals("5") && !coin.equals("10")
 && !coin.equals("20") && !coin.equalsIgnoreCase(select)) {
 System.exit(0);
 } else if (coin.equalsIgnoreCase(select) && amount < 15) {
 System.out.println(not + (juice - amount) + back);
 } else if (coin.equalsIgnoreCase(select) && amount == 15 ) {
 System.out.println(thanks);
 } else if (coin.equalsIgnoreCase(select) && amount > 15) {
 System.out.println(thank + (amount - juice) + pence);
 break ;
 }
 }
 }
 else if (choice.equalsIgnoreCase("terminate")) {
 System.out.print("Done");
 exit = true;
 }
 }
 }
 }



Is This A Good Question/Topic? 0
  • +

Replies To: Vending machine problem

#2 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: Vending machine problem

Posted 08 October 2013 - 06:55 AM

Quote

I can not understand how to write the code for having choice of drink in one class, payment in another class and the calculation in a third.


Whoa there! Just start with one class and modify your code around that class (because much will change).

So, for instance a product may look something like this:

public class Product {
    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() { return name; }
    public double getPrice() { return price }
}



Then, take code like this: String coca = "cola"; final int cola = 12;

And make it something like this: Product soda = new Product("Coca-cola", 12.0);

So, now you construct the product and give it its properties. Once constructed, it is read-only since it does not have a way to set those properties.

Now, when you want to use it, you call the get methods and there you have it.

So, try refitting your current project with this new class first. Worry about the next class later.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1