Write a program to make change. the initialization program will start with how m
ID: 3557456 • Letter: W
Question
Write a program to make change. the initialization program will start with how much you have in the cash register and in what denominations the money is in. You will enter a purchase amount amount and the currency that was used to make the purchase. The cash register holds currency in the following denominations: (3) 20 dollar bills (2) 10 dollar bills (5) 5 dollar bills (11) dollar bills (10) quarters (29) dimes (15) nickels (300) pennies Your program should: 1. print out the change including the denominations. 2. update the amount of money in the cash register. So the dialog will look something like this: "Enter purchase amount:" "Enter currency taken in:" How many 20 dollar bills: How many 10 dollar bills: How many 5 dollar bills: How many 1 dollar bills: How many quarters: How many dimes: How many nickels: How many pennies: Its possible you won't be able to make change. Then tell the user you are sorry. You can use the scanner and system.out to do i/o or you can use the swing windowing to do the i/o.
Explanation / Answer
Program Code:
import java.util.Scanner;
public class BiilsDesk
{
static int SUM = 200;
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double initial,purchase_amt,total;
int addQ = 1,leftQ;
int twenty, ten, five,one, dimes, quarter, nickels, pennies;
String currency;
System.out.println("Initial cash available="+SUM);
System.out.println("Enter the purchase amount:");
purchase_amt=sc.nextDouble();
System.out.println("Enter currency taken in: ");
currency=sc.next();
System.out.println("How many 20 dollar bills: ");
twenty=sc.nextInt();
System.out.println("How many 10 dollar bills: ");
ten=sc.nextInt();
System.out.println("How many 5 dollar bills: ");
five=sc.nextInt();
System.out.println("How many 1 dollar bills: ");
> System.out.println("How many quarters: ");
quarter=sc.nextInt();
System.out.println("How many dimes: ");
dimes=sc.nextInt();
System.out.println("How many nickels: ");
nickels=sc.nextInt();
System.out.println("How many pennies: ");
pennies=sc.nextInt();
total=(twenty*20)+(ten*10)+(five*5)+(1)+(quarter*0.25)+(dimes*0.10)+(nickels*0.05)+(pennies*0.01);
if(total==purchase_amt)
System.out.println("Succesful.!");
else if(total>purchase_amt)
System.out.println("Please collect change: "+(total-purchase_amt));
else
System.out.println("Sorry! Need "+(purchase_amt-total)+" amount to complete transaction!");
}
}
Sample Output:
Initial cash available=200
Enter the purchase amount:
150.345
Enter currency taken in:
dollars
How many 20 dollar bills:
5
How many 10 dollar bills:
1
How many 5 dollar bills:
1
How many 1 dollar bills:
5
How many quarters:
3
How many dimes:
9
How many nickels:
4
How many pennies:
10
Sorry! Need 32.394999999999996 amount to complete transaction!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.