Question

in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "Ret...

in java

PART ONE =======================================

Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "RetailItem" class which simulates the sale of a retail item. It should have a constructor that accepts a "RetailItem" object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased. Your class should have these methods:

  • getSubtotal: returns the subtotal of the sale, which is quantity times price. This method needs to get the price from the RetailItem object that was passed as an argument to the constructor.
  • getTax: returns the tax on the purchase. Assume 6% as the tax rate.
  • getTotal: returns the total of the sale, which is subtotal plus tax.

Demonstrate the class in a program that asks the user for the quantity of items being purchased, and then displays the sale's subtotal, tax amount, and total.

public class RetailItem {

   private String description;   // Item description
   private int unitsOnHand;      // Number of units on hand
   private double price;         // Unit price

   public RetailItem(){
      description = "";
      unitsOnHand = 0;
      price = 0.0;
   }

   public RetailItem(String d, int u, double p) {
      description = d;
      unitsOnHand = u;
      price = p;
   }

   public void setDescription(String d) {
      description = d;
   }

   public void setUnitsOnHand(int u) {
      unitsOnHand = u;
   }

   public void setPrice(double p) {
      price = p;
   }

   public String getDescription() {
      return description;
   }

   public int getUnitsOnHand() {
      return unitsOnHand;
   }

   public double getPrice() {
      return price;
   }
}

PART TWO ===================================================

Modify your program in Part One to create a file containing a sales receipt. The program should ask the user for the quantity of items being purchased, and generate a file with a receipt that displays something like this:

SALES RECEIPT
Unit price: $10.00
Quantity: 5
Subtotal: $50.00
Tax: $3.00
Total: $53.00
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// CashRegister.java

public class CashRegister {

      // attributes

      private RetailItem item;

      private int quantity;

      // constructor taking RetailItem object and quantity

      public CashRegister(RetailItem item, int quantity) {

            this.item = item;

            this.quantity = quantity;

      }

      // returns the subtotal

      public double getSubtotal() {

            return item.getPrice() * quantity;

      }

      // returns the tax amount

      public double getTax() {

            //applying 6% tax

            return getSubtotal() * 0.06;

      }

      // returns the total

      public double getTotal() {

            return getSubtotal() + getTax();

      }

}

// Test.java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class Test {

      public static void main(String[] args) throws FileNotFoundException {

            // creating a RetailItem object with unit price 25

            RetailItem item = new RetailItem();

            item.setPrice(25);

            // defining a scanner to read input

            Scanner scanner = new Scanner(System.in);

            // asking for the quantity

            System.out.print("Enter the quantity of items being purchased: ");

            int qty = scanner.nextInt();

            // creating a CashRegister object with retail item and quantity

            CashRegister register = new CashRegister(item, qty);

            // displaying unit price, subtotal, tax and total to screen

            System.out.printf("Unit price: $%.2f\n", item.getPrice());

            System.out.printf("Subtotal: $%.2f\n", register.getSubtotal());

            System.out.printf("Tax: $%.2f\n", register.getTax());

            System.out.printf("Total: $%.2f\n", register.getTotal());

            // creating a file named receipt.txt and writing unit price, subtotal,

            // tax and total to file

            PrintWriter writer = new PrintWriter(new File("receipt.txt"));

            writer.println("SALES RECEIPT");

            writer.printf("Unit price: $%.2f\n", item.getPrice());

            writer.printf("Subtotal: $%.2f\n", register.getSubtotal());

            writer.printf("Tax: $%.2f\n", register.getTax());

            writer.printf("Total: $%.2f\n", register.getTotal());

            //closing file, saving results

            writer.close();

      }

}

/*OUTPUT*/

Enter the quantity of items being purchased: 40

Unit price: $25.00

Subtotal: $1000.00

Tax: $60.00

Total: $1060.00

Add a comment
Answer #2
public class RetailItem {

   private String description;   // Item description
   private int unitsOnHand;      // Number of units on hand
   private double price;         // Unit price

   public RetailItem(){
      description = "";
      unitsOnHand = 0;
      price = 0.0;
   }

   public RetailItem(String d, int u, double p) {
      description = d;
      unitsOnHand = u;
      price = p;
   }

   public void setDescription(String d) {
      description = d;
   }

   public void setUnitsOnHand(int u) {
      unitsOnHand = u;
   }

   public void setPrice(double p) {
      price = p;
   }

   public String getDescription() {
      return description;
   }

   public int getUnitsOnHand() {
      return unitsOnHand;
   }

   public double getPrice() {
      return price;
   }
}


source: mm
answered by: mmm
Add a comment
Know the answer?
Add Answer to:
in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "Ret...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds...

    USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds data about an item in a retail store. The class should have the following member variables: description: A string that holds a brief description of the item. unitsOnHand: An int that holds thw number of units currently in inventory. price: A double that holds that item's retail price. Write the following functions: -appropriate mutator functions -appropriate accessor functions - a default constructor that sets:...

  • Write a CashRegiste class that can be used with the Retntailtem class that you wrote in...

    Write a CashRegiste class that can be used with the Retntailtem class that you wrote in the module4. The cashRegister class should simulate the sale of a retail item. It should have a constructor that accepts a Retilltem objectject The constructor should also accept an integer that represents the quantity of item being purchased. In addition, the class should have the following properties and methods: as an argument. private final double TAX RATE private double retail; private int quantity; 0.06;...

  • Write the following program in Java using Eclipse. A cash register is used in retail stores...

    Write the following program in Java using Eclipse. A cash register is used in retail stores to help clerks enter a number of items and calculate their subtotal and total. It usually prints a receipt with all the items in some format. Design a Receipt class and Item class. In general, one receipt can contain multiple items. Here is what you should have in the Receipt class: numberOfItems: this variable will keep track of the number of items added to...

  • Use java and continue stage 2 and 3 stage 1 code public abstract class BabyItem { protected String name;...

    Use java and continue stage 2 and 3 stage 1 code public abstract class BabyItem { protected String name; public BabyItem() { name=""; } public BabyItem(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { } public abstract double getCost(); } ======================================================================================== public class BabyFood extends BabyItem { private int numberOfJars; private double pricePerDozen; public BabyFood() { super(); numberOfJars = 0; pricePerDozen = 0; } public BabyFood(int numberOfJars, double pricePerDozen) {...

  • JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes...

    JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes four instance variables; partNumber (type String), partDescription (type String), quantity of the item being purchased (type int0, and pricePerItem (type double). Perform the following queries on the array of Invoice objects and display the results: Use streams to sort the Invoice objects by partDescription, then display the results. Use streams to sort the Invoice objects by pricePerItem, then display the results. Use streams to...

  • Programming Assignment 6: Object Oriented Programming Due date: Check Syllabus and Canvas Objectives: After successfully completing...

    Programming Assignment 6: Object Oriented Programming Due date: Check Syllabus and Canvas Objectives: After successfully completing this assignment, students will practice Object Oriented design and programming by creating a Java program that will implement Object Oriented basic concepts. RetailItem Class: Part 1: Write a class named RetailItem that holds data about an item in a retail store. The class should have the following fields: • description. The description field references a String object that holds a brief description of the...

  • JAVA Objective : • Learning how to write a simple class. • Learning how to use...

    JAVA Objective : • Learning how to write a simple class. • Learning how to use a prewritten class. • Understanding how object oriented design can aid program design by making it easier to incorporate independently designed pieces of code together into a single application. Instructions: • Create a project called Lab13 that contains three Java file called Book.java , Bookstore.java and TempleBookstore.java • I’ve provided a screenshot of how your project should look like. • Be sure to document...

  • Requirements:  Your Java class names must follow the names specified above. Note that they are...

    Requirements:  Your Java class names must follow the names specified above. Note that they are case sensitive. See our template below.  BankAccount: an abstract class.  SavingAccount: a concrete class that extends BankAccount. o The constructor takes client's firstname, lastname and social security number. o The constructor should generate a random 6-digit number as the user account number. o The initial balance is 0 and the annual interest rate is fixed at 1.0% (0.01).o The withdraw method signature...

  • Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in....

    Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in. The main method prompts for a salary, then uses a TaxTableTools method to get the tax rate. The program then calculates the tax to pay and displays the results to the user. Run the program with annual salaries of 10000, 50000, 50001, 100001 and -1 (to end the program) and note the output tax rate and tax to pay. a. Modify the TaxTableTools class...

  • Write a Gui programming by using JavaFx menus, stage and screen concepts to the RetailItem class,...

    Write a Gui programming by using JavaFx menus, stage and screen concepts to the RetailItem class, Write a class named Retailltem that holds data about an item in a retail store. The class should have the following fields description: The description field is a String object that holds a brief description of the item . unitsOnHand: The unitsOnHand field is an int variable that holds the number of units currently in inventory Price: The price field is a double that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT