Java Program Grocery Receipt

I want to modify the Cash Resigter Code below to create a file containing a sales receipt. The program should ask the user for the quantity of items being purchased, and then generate a file with contents below:

I'm having trouble getting my code to compile and not sure why. This is my task: I have to create a program to print out a receipt of the following items bought in a grocery store called XYZ. The items are: Milk $1.89,quantity: 5, no tax Sugar $0.60, quantity: 10, 8% tax Candy $0.89, quantity: 20, 8% tax. I want to modify the Cash Resigter Code below to create a file containing a sales receipt. The program should ask the user for the quantity of items being purchased, and then generate a file with contents below: SALE RECEIPT Unit Price: $10.00 Quantity: 5 Subtotal: $50.00 Sales Tax: $3.00 Total: $53.00. Here is my code. After downloading it, you will need a program like Winzip to decompress it. Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. FOR YOUR OWN SAFETY, PLEASE: 1. Re-scan downloaded files using your personal virus checker before using.

Here is my code:

  • 3 Contributors
  • forum2 Replies
  • 2,082 Views
  • 16 Minutes Discussion Span
  • commentLatest PostLatest Postby stultuske

Recommended Answers

What are you having problems with converting the posted code?
Do you have any specific questions?
Two things I see that you need:
Ask user for data. Scanner class can help
write data to file. See the PrintWriter class

Jump to Post

All 2 Replies

What are you having problems with converting the posted code?
Do you have any specific questions?
Two things I see that you need:
Ask user for data. Scanner class can help
write data to file. See the PrintWriter class

Java Program Grocery Receipt Examples

(Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.

Example

Answer:

class Invoice{
// Declare instance variables
String number;
String description;
int quantity;
double price;

// Constructor for variables’ initialization
Invoice(String n, String d, int q, double p){
number = n;
description = d;
quantity = q;
price = p;
}

Java program grocery receiptsJava

// Set method
void setNumber(String n){
number = n;
}
void setDescription(String d){
description = d;
}
void setQuantity(int q){
quantity = q;
if(q < 0)
quantity = 0;
}
void setPrice(double p){
price = p;
if(p < 0)
price = 0.0;
}

// Get method
String getNumber(){
return number;
}
String getDescription(){
return description;
}
int getQuantity(){
return quantity;
}
double getPrice(){
return price;
}

// getInvoiceAmount method
double getInvoiceAmount(){
return quantity*price;
}
}

class InvoiceTest{
public static void main(String[] args){
// Create Object(s)
Invoice object1 = new Invoice(“27”, “Blueberry parfait”, 3, 9.9);
Invoice object2 = new Invoice(“12”, “Rainbow Fraffucino”, 5, 6.6);
Invoice object3 = new Invoice(“94”, “Strawberry shortcake”, 7, 29.8);

Receipt

// Print the Invoice
System.out.println(“This is an Invoice for the Item(s) Sold:” + “n”);
System.out.println(object1.getNumber() + “t” + object1.getDescription() + “t” + object1.getQuantity() + “t” + object1.getPrice() + “t” + object1.getInvoiceAmount());

Java Program Grocery Receipt Example

System.out.println(object2.getNumber() + “t” + object2.getDescription() + “t” + object2.getQuantity() + “t” + object2.getPrice() + “t” + object2.getInvoiceAmount());

Program

System.out.println(object3.getNumber() + “t” + object3.getDescription() + “t” + object3.getQuantity() + “t” + object3.getPrice() + “t” + object3.getInvoiceAmount());

Java Program Grocery Receipts

System.out.println(“n”);

Java Program Grocery Receipt Template

double total = object1.getInvoiceAmount() + object2.getInvoiceAmount() + object3.getInvoiceAmount();
System.out.println(“TOTAL: “+ total + “nn”);
}
}

Comments are closed.