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:

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;
        }
    }

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

  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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.