Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA PLEASE Continue for the Cash Register and Retail Item question I had pos

ID: 3754839 • Letter: I

Question

IN JAVA PLEASE Continue for the Cash Register and Retail Item question I had posted prior to this. Please conitnue the code. request : This is for a beginner so please much it as simple as possible to understand. Modifty the Cash Register Class to create a file containing a sales receipt. The program should ask the user for the quantity of the items being purchased and then generate a file with contents to similar to the following: SALES RECEIPT UNIT PRICE: 10.00 QUANTITY: 5 SUBTOTAL: 50 SALES TAX: 3 TOTAL: 53

Explanation / Answer

Driver.java

import java.util.*; //Import which is needed for the scanner object
import java.io.*; //IO import for file managment
public class Driver {
    public static void main(String[] args) throws IOException { //Main method, IOException for file managment exceptions
        //Scanner
        Scanner scanner = new Scanner(System.in);
        //Defining the retail item that is going to be purchased
        RetailItem item = new RetailItem("Kershaw Blur", 20, 119.99);

        //Promting the user to enter the amount they want to buy
        System.out.println("How many of the item would you like to buy?");
        CashRegister register = new CashRegister(item, scanner.nextInt()); //The last part uses the users input

        //Try to catch file managment exceptions
        try {
            //PrintWriter creates and writes to a file calles SalesReceipt.txt
            PrintWriter writer = new PrintWriter("SalesReceipt.txt");
            //Printing out the header
            writer.printf("SALES RECEIPT ");
            //Printing out all the results
            writer.printf("Unit Prices: %.2f$ Quantity: %d Subtotal: %.2f$ Sales Tax: %.2f$ Total: %.2f$ "
                          ,item.getPrice(), register.getQuantity(), register.getSubtotal()
                          , register.getTax(), register.getTotal());
            writer.close(); //Closing the PrintWriter
            }

        catch (FileNotFoundException e) { //Used to catch exceptions manually
            e.printStackTrace();
        }
    }
}


CashRegister.java

public class CashRegister {
    private RetailItem item; //Variable declaration/ Reference
    private int quantity;

    //Constructor which sets the value of the local field to the ones passed in the argumetns
    public CashRegister(RetailItem item, int quantity) {
        this.item = item;
        this.quantity = quantity;
    }

    //Returns the quantity
    public int getQuantity() {
        return quantity;
    }

    //Returns the subtotal which is the price of the item times the quatity bought
    public double getSubtotal() {
        return item.getPrice()*quantity;
    }

    //Returns the amount of sales tax which is 6% of the subtotal
    public double getTax() {
        return getSubtotal()*0.06;
    }

    //Returns the total which is the tax plus the subtotal
    public double getTotal() {
        return getSubtotal()+getTax();
    }
}


RetailItem.java

public class RetailItem {
    private String description; //Variable declaration
    private int unitsOnHand;
    private double price;

    //Constructor which sets all the values passed as arguments
    public RetailItem(String description, int unitsOnHand, double price) {
        this.description = description;
        this.unitsOnHand = unitsOnHand;
        this.price = price;
    }

    //Sets the value of description
    public void setDescription(String description) {
        this.description = description;
    }

    //Sets the value of unitsOnHand
    public void setUnitsOnHand(int untisOnHand) {
        this.unitsOnHand = untisOnHand;
    }

    //Sets the value of the price field
    public void setPrice(double price) {
        this.price = price;
    }

    //Returns the value of description
    public String getDescription() {
        return description;
    }

    //Returns the value of unitsOnHand
    public int getUnitsOnHand() {
        return unitsOnHand;
    }

    //Returns the value of price
    public double getPrice() {
        return price;
    }
}