RetailItemjava Write a class named RetailItem that reads da

RetailItem.java

- Write a class named RetailItem that reads data from an input file about an item in a retail store.
- The class should have the following fields: description (String), unitsOnHand (int), price (double).
- Write a constructor that accepts arguments for each field
- Write mutators & accessors that return thte values in these fields.
- Add a static constant attribute called DISCOUNT and set it to 20% or (0.20) to the RetailItem class.
- Include a applyDiscount() method - Computes the new price of an item using the constant as above
- Include a toString() to the RetailItem class - Displays the description, unitsOnHand, and Price.

RetailItemDriver.java

- Declare 3 retail items as follows: (Item#1: Jacket, 12 on hand, price $59.95), (Item#2: Jeans, 40 on hand, price $34.95), (Item#3: Shirt, 20 on hand, price $24.95).
- Create a method to read data for the 3 objects and create the objects (from a data file), create objects one at a time and return the object reference.
- Write a method named display() - displays one object at a time & displays the current state of the object. Include the display of the new price of the item after the discount is applied using method applyDiscount() for this.
- Write a method named displayAll() - displays all RetailItem objects, one at a time.

Solution

Hi FRiend, Please find my implementation.

You have not mentioned the structure of data in file. So i implemented to take input from console.

######### RetailItem.java ###########

public class RetailItem {

  

   // instance variable

   private String description;

   private int unitsOnHand;

   private double price;

  

   private static double DISCOUNT = 0.20;

  

   // constructor

   public RetailItem(String description, int unitsOnHand, double price) {

       this.description = description;

       this.unitsOnHand = unitsOnHand;

       this.price = price;

   }

       // GETTERS AND SETTERS

  

   public String getDescription() {

       return description;

   }

   public int getUnitsOnHand() {

       return unitsOnHand;

   }

   public double getPrice() {

       return price;

   }

   public void setDescription(String description) {

       this.description = description;

   }

   public void setUnitsOnHand(int unitsOnHand) {

       this.unitsOnHand = unitsOnHand;

   }

   public void setPrice(double price) {

       this.price = price;

   }

  

   public void applyDiscount(){

       price = price - price*DISCOUNT;

   }

  

   @Override

   public String toString() {

       return description+\", \"+unitsOnHand+\" on hand, price $\"+price;

   }

  

  

}

########## RetailItemDriver.java ############

import java.util.Scanner;

public class RetailItemDriver {

  

   private static Scanner sc = new Scanner(System.in);

  

   public static void display(RetailItem item){

       System.out.println(item);

       System.out.println(\"After applying discount: \");

       item.applyDiscount();

       System.out.println(item);

   }

  

   public static void displayAll(RetailItem[] items){

       for(RetailItem item: items)

           System.out.println(item);

   }

  

   public static RetailItem getRetailObject(){

      

       System.out.print(\"Enter description: \");

       String des = sc.next();

       System.out.print(\"Enter number of units: \");

       int unit = sc.nextInt();

       System.out.print(\"Enter price:\");

       double price = sc.nextDouble();

      

       //returning RetailItem object

       return new RetailItem(des, unit, price);

   }

   public static void main(String[] args) {

      

       // Declaring 3 RetailItem Object

       RetailItem retail1 = new RetailItem(\"Item#1: Jacket\", 12, 59.9);

       RetailItem retail2 = new RetailItem(\"Item#2: Jeans\", 40, 34.95);

       RetailItem retail3 = new RetailItem(\"Item#3: Shirt\", 20, 24.95);

      

       RetailItem[] retails = new RetailItem[3]; // creating an array of RetailItem Object

      

       for(int i=0; i<3; i++){

           System.out.println(\"Enter Item #\"+(i+1)+\" details\");

           retails[i] = getRetailObject();

       }

      

       display(retails[2]);

      

       displayAll(retails);

      

      

   }

}

/*

Sample run:

Enter Item #1 details

Enter description: Jacket

Enter number of units: 12

Enter price:59.9

Enter Item #2 details

Enter description: Jeans

Enter number of units: 40

Enter price:34.95

Enter Item #3 details

Enter description: Shirt

Enter number of units: 20

Enter price:24.95

Shirt, 20 on hand, price $24.95

After applying discount:

Shirt, 20 on hand, price $19.96

Jacket, 12 on hand, price $59.9

Jeans, 40 on hand, price $34.95

Shirt, 20 on hand, price $19.96

*/

RetailItem.java - Write a class named RetailItem that reads data from an input file about an item in a retail store. - The class should have the following field
RetailItem.java - Write a class named RetailItem that reads data from an input file about an item in a retail store. - The class should have the following field
RetailItem.java - Write a class named RetailItem that reads data from an input file about an item in a retail store. - The class should have the following field
RetailItem.java - Write a class named RetailItem that reads data from an input file about an item in a retail store. - The class should have the following field
RetailItem.java - Write a class named RetailItem that reads data from an input file about an item in a retail store. - The class should have the following field

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site