I wrote this java program that performs functions of a vending mention. But now I need to write it using methods, how can I do this? Here is my code: (Any help would be greatly appreciated!
import java.util.Scanner;
import java.util.Random;
public class VendingMachine
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); // reader for standard input
Random random = new Random(); // random number generator
int[] items = {65, 95, 50, 110, 75}; // prices for five items in cents
int[] coins = {5, 10, 25, 100}; // nickel, dime, quarter and dollar
boolean exactChange; // whether exact change is required flag
int price; // price of item
int amount; // amount deposited
boolean cancelSelection; // flag to indicate if selection is cancelled
int sales; // total number of sales
int remaining;
int count;
int choice;
int select;
char ch;
exactChange = random.nextBoolean();
sales = 0;
choice = 1;
do
{
if (sales == 10)
{
System.out.println("OUT-OF-SERVICE…
System.out.print("Do you want to make the machine operational? (y/n): ");
ch = keyboard.next().toUpperCase().charAt(0);
if (ch == 'Y')
sales = 0;
}
else
{
System.out.println("--------------");
System.out.println(" Vending Machine ");
System.out.println("--------------");
for (int i=0; i<items.length; i++)
System.out.println((i+1) + ". Item price " + items[i] + " cents");
System.out.println((items.length + 1) + ". Exit");
System.out.println("Exact change to be inserted: " + exactChange);
System.out.print("Enter the choice: ");
choice = keyboard.nextInt();
if (choice >= 1 && choice <= items.length)
{
price = items[choice-1];
amount = 0;
cancelSelection = false;
while (amount < price)
{
System.out.println("-----------");
System.out.println(" Deposit Money ");
System.out.println("-----------");
System.out.println("1. Nickel");
System.out.println("2. Dime");
System.out.println("3. Quarter");
System.out.println("4. Dollar");
System.out.println("5. Cancel selection");
System.out.print("Enter the amount: ");
select = keyboard.nextInt();
if (select >= 1 && select <= 4)
{
amount = amount + coins[select-1];
}
else if (select == 5)
{
cancelSelection = true;
break;
}
else
{
System.out.println("Error: you have entered an invalid selection, try again");
}
}
if (!cancelSelection)
{
if (!exactChange)
{
if (amount == price)
{
System.out.println("No change due");
}
else
{
System.out.println("Changes are:");
remaining = amount - price;
// get the number of one dollars
count = remaining / 100;
if (count == 1)
System.out.println("1 dollar");
else if (count > 1)
System.out.println(count + " dollars");
remaining = remaining % 100;
// get the number of 25 cents
count = remaining / 25;
if (count == 1)
System.out.println("1 quarter");
else if (count > 1)
System.out.println(count + " quarters");
remaining = remaining % 25;
// get the number of 10 cents
count = remaining / 10;
if (count == 1)
System.out.println("1 dime");
else if (count > 1)
System.out.println(count + " dimes");
remaining = remaining % 10;
// get the number of 5 cents
count = remaining / 5;
if (count == 1)
System.out.println("1 nickel");
else if (count > 1)
System.out.println(count + " nickels");
}
}
exactChange = random.nextBoolean();
sales += 1;
}
}
else if (choice != (items.length + 1))
{
System.out.println("Error: you have entered invalid choice");
}
}
} while (choice != (items.length + 1));
}
}