Part 3 And now for the fun part, the challenge. This challenge is asking you to
ID: 3756082 • Letter: P
Question
Part 3 And now for the fun part, the challenge. This challenge is asking you to write a program that will simulate a cash register.Ask for the price of an item, the payment amount from the customer, and then print out the change due to the customer but print it out as dollars, the number of quarters, the number of dimes, the number of nickels, and the number of pennies. Hint, this is a good opportunity to work with modules or remainder division. Also allow the user to enter multiple transactions. You have 1 hour and a half to complete this task, so take your time and have fun. After you write the program and test it try to write an explanation to tell us how did you solve your problem, using your own words.Explanation / Answer
public class Item {
private double price;
private String name;
public void setPrice(double price) {
this.price=price;
}
public void setName(String name) {
this.name=name;
}
public double getPrice(){
return price;
}
public String getName(){
return name;
}
public Item() {
}
public Item(String name,double price){
this.price=price;
this.name=name;
}
}
import java.util.*;
public class CacheRegister {
public static void main (String[] args)
{
ArrayList<Item> a= new ArrayList<>();
a.add(new Item("Biscuit", 1.5));
a.add(new Item("Lays", .4));
a.add(new Item("Thumpsup",3.1));
a.add(new Item("Bhujiya",5.2));
a.add(new Item("ParleG",1.8));
System.out.println("Availabe items are");
int count=0;
for (Item i : a){
System.out.println("Press " +(++count) +" for "+i.getName()+" "+i.getPrice());
}
System.out.println("Please select an item");
Scanner sc= new Scanner(System.in);
int s= sc.nextInt();
System.out.println("Please pay amount");
double amount= sc.nextDouble();
Item item= a.get(--s);
double price= item.getPrice();
if(amount> price) {
double remainingAmount=amount-price;
int dollar, quarter, dime,nickel, penny,temp;
dollar=(int)remainingAmount;
remainingAmount=remainingAmount-dollar;
temp=(int)(remainingAmount*100);
quarter=temp/25;
temp=temp%25;
dime=temp/10;
temp=temp%10;
nickel=temp/5;
penny=temp%5;
System.out.println("Please collect remaining amount");
if(dollar>0) {
System.out.println("Dollar "+dollar);
}
if(quarter>0) {
System.out.println("Quarter "+quarter);
}
if(dime>0) {
System.out.println("Dime "+dime);
}
if(nickel>0) {
System.out.println("Nickel "+nickel);
}
if(penny>0) {
System.out.println("Penny "+penny);
}
}
else if(price==amount) {
System.out.println("No amount remaining");
}
else {
System.out.println("You gave less price");
}
}
}
We have created one class Item which will hold item name and price. And have created Arraylist of items.
Customer can choose any item from that list. and pay money. remaining amount will be returned to the customer.