I am currently working on writing a simple vending machine that dispenses just one drink, I am just a beginner at this so I would appreciate some help from this community.

package assignment1;

public class VendingMachine
extends java.lang.Object{
	// fields
	java.lang.String givenDrinkName;
	private int givenDrinkPrice;
	private static final int INITIAL_BALANCE = 0;
	private int balance;
	
	// constructors
	public VendingMachine() {
		givenDrinkName = "juicy";
		balance = INITIAL_BALANCE;
	}
	
	public void insertCoin(int amount) {
		balance = amount;
	}
	
	public int getBalance () {
		return balance;
	}
	
	public int makeChange() {
		int change = balance;
		balance = 0;
		return balance;
	}
	
	public java.lang.String dispense() {
		return givenDrinkName;
	}
}

Now I know I need a main method (possibly public static void main(String[] args)) but where does this go? and will my script work? The vending machine is supposed to dispense a drink, tell you the balance, make change, and be able to insert the amount to purchase the drink. I don't believe this script has to be that specific but I am wondering if this script works

Member Avatar for hfx642

It is customary to put the "main" method after your constructor "VendingMachine",
but before your other methods.
At least... That's what I do.
Look at programming examples to see what you should put in a "main" method.
It should be fairly sparse.

What does makeChange() method do??? Currently, what it does is to reset the "balance" variable to 0 and return 0. Nothing else?

public int makeChange() {
  int change = balance;  // copy balance value to "change" local variable
  balance = 0;           // reset balance to 0
  return balance;        // return balance which is 0 ???
}

The "main" method can be anywhere inside your class scope (inside class ClassName{}). For me, I put it at the end of the class definition because the method is likely to be a quick test. Once you do packaging, you will see that you cannot leave this method inside your class.

// this is my format when I code in Java
class ClassName {
  //list of class variables here
  //...

  // list of constructors
  public ClassName() {
  }
  //...

  // list of public methods
  public void publicMethod() {
  }
  //...
  //...

  // list of protected methods
  protected void publicMethod() {
  }
  //...
  //...

  // list of private methods
  private void privateMethod() {
  }
  //...
  //...

  // main
  public static void main(String[] args) {
  }
}

Now, I would want to talk about how do you want to test your class? There are 2 ways, one is to put a "main" method inside your class and test it. The other is to create a new class and use the class in it. If I were you, I prefer the second for assignment, but I would do the first while I am developing the code.

PS: Why do you call the code as "script"? I don't think Java language code should be called "script" because it is not really a script but compilable language.

I would suggest asking your teacher or lecturer what they are looking for, as they will end up marking your "Assignment 1".

The main method can be placed anywhere within the class definition for VendingMachine:

public class VendingMachine extends java.lang.Object
{
    // Main method somewhere in here
}

You will probably want to fix your makeChange() method as well, as Taywin suggested. You want to return change , not balance .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.