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:

Unit Price : $10.00
    Quantity: 5
Subtotal: $50.00
Sales Tax: $3.00
Total: $53.00

Here is my code:

public class CashRegister
        private RetailItem retailItem;
        private int quantityItems;
        private final double SALES_TAX = .06;
        private int subTotal;
        private double price;

        public CashRegister ()
            quantityItems = 0;
            subTotal = 0;

       public CashRegister(RetailItem retailObject, int quantity)
          price = retailObject.getPrice();

          quantityItems = quantity;

        public RetailItem getRetailItem()

            return new RetailItem();

        public double getSubTotal()
            return quantityItems * retailItem.getPrice();

        public double getTax()
            return SALES_TAX;

        public double getTotal()
            return subTotal + SALES_TAX;
4 Years
Discussion Span
Last Post by stultuske

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

  1. remove the first constructor
  2. complete the second constructor to set all the values (since you can all deduct them from the two you pass on)
  3. add a toString method
  4. in your main method:
    a. instantiate a RetailItem
    b. call the constructor of CashRegister with the RetailItem and the number of items
    c. print the CashRegister
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.