In: Computer Science
Java
This is background information First, code this project. Write a program that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in 5-cent increments (25, 30, 35, ..., 90, 95, or 100) and the machine only accepts a single dollar bill to pay for the item. For example, a possible dialogue with the user might beEnter price of item(from 25 cents to a dollar, in 5-cent increments): 45You bought an item for 45 cents and gave me a dollar,so your change is 2 quarters,0 dimes, and 1 nickel.
You can write this program based on the program or with if statements. After getting it to work, include input checking. Display the change only if a valid price is entered (no less than 25 cents, no more than 100 cents, and an integer multiple of 5 cents). Otherwise, display separate error messages for any of the following invalid inputs: a cost under 25 cents, a cost that is not an integer multiple of 5, and a cost that is more than a dollar.
Write comments
CODE
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
//variables
int price;
int quarters, dimes, nickel;
Scanner sc = new Scanner(System.in);
//prompt for price
System.out.print("Enter price of item(from 25 cents to a dollar, in 5-cent increments): ");
price = sc.nextInt();
//if price less than 25 cents
if(price < 25)
{
System.out.println("Invalid price, Price can't be less than 25 cents.");
}
//if price greater than 100 cents
else if(price > 100)
{
System.out.println("Invalid price, Price can't be greater than 100 cents.");
}
//if price is not multiple of 5
else if(price % 5 != 0)
{
System.out.println("Invalid price, Price must be multiple of 5.");
}
//if a valid price
else
{
//calculating total change
int cents = 100 - price;
//calculating number of quarters
quarters = cents / 25;
//calculating remaining change
cents = cents % 25;
//calculating number of dimes
dimes = cents / 10;
//calculating remaining change
cents = cents % 10;
//calculating number of nickel
nickel = cents / 5;
//printing output
System.out.println("You bought an item for " + price + " cents and gave me a dollar,so your change is " + quarters + " quarters," + dimes + " dimes, and " + nickel + " nickel.");
}
}
}
OUTPUT

CODE SCREEN SHOT

