NOTE BASIC CIS 102 CODES RetailItem Class Write a class nam
NOTE ; BASIC CIS 102 CODES
RetailItem Class
Write a class named RetailItem that holds data about an item in a retail store. The
class should have the following fields:
• name. The name field references a String object that holds the name of the
item.
• quantity. The quantity field is an int variable that holds the number of items
represented by the instance.
• price. The price field is a double that holds a single item’s retail price.
Write a constructor that accepts arguments for each field, accessor methods that
return the values in these fields, a totalCost() that returns the cost of all quantity
items, and a toString() method that returns a formatted string for the Sales
Receipte below.
CashRegister Class. Write a CashRegister class that can be used with the RetailItem
class above. The CashRegister class should simulate the sale of a series of
RetailItems. It should have one field, items, an ArrayList of Retail
Items and the following methods:
• addItem method accepts a RetailItem object.
• printItems method takes no arguments, but prints all the items.
• getSubtotal method should return the subtotal of the sale, which is the sum of
the retail items added so far. This method must get the price from each
RetailItem object that was passed as an argument to the addItem method.
• getTax method should return the amount of sales tax on the purchase. The sales
tax rate is 6 percent of a retail sale.
• getTotal method should return the total of the sale, which is the subtotal plus
the sales tax.
Demonstrate the class in a program that asks the user for the name, quantity and
price of items being purchased repeatedly until an empty line is entered, and then
displays a sales receipt .
MY CODE SO FAR......
/*Write a class named RetailItem that holds data about an item in a retail store.
*The class should have the following fields:
*name,quantity,price.
*/
import java.util.*;
public class RetailItem
{
private String nameOfItem;
private int NumOfItems;
private double retailPriceofItem;
/*Write a constructor that accepts arguments for each field, accessor methods that
*return the values in these fields.
*/
public RetailItem( String name,int quantity, double retailPrice)
{
nameOfItem = name;
NumOfItems = quantity;
retailPriceofItem = retailPrice;
}
//The name field references a String object that holds the name of the item.
public String getNameOfItem(String name)
{
return name;
}
//The quantity field is an int variable that holds the number of items represented by the instance.
public int getNumOfItems(int quantity)
{
return quantity;
} `
//The price field is a double that holds a single item’s retail price.
public double getRetailPriceofItem(double retailPrice)
{
return retailPrice;
}
// a totalCost() that returns the cost of all quantity items
public double getTotalCost(int quantity, double price, int total)
{
total = quantity * price;
return total;
}
//a toString() method that returns a formatted string for the Sales Receipte
}
Solution
import java.util.*;
public class RetailItem
{
private String description;
private int unitsOnHand;
private double retailPrice;
RetailItem(String descrpt, int numUnits, double price)
{
description = descrpt;
unitsOnHand = numUnits;
retailPrice = price;
}
public String getDescription()
{
return description;
}
public int getUnits()
{
return unitsOnHand;
}
public double getPrice()
{
return retailPrice;
}
public void setDescription(String d)
{
description = d;
}
public void setUnitsOnHand(int units)
{
unitsOnHand = units;
}
public void setPrice(double p)
{
retailPrice = p;
}
public String toString()
{
return \" The description is \" + description + \"\ Units are \" + unitsOnHand
+ \"\ Retail Price is \" + retailPrice;
}
}
public class testRetail
{
public static void main(String[] args)
{
RetailItem item1 = new RetailItem (String \"Jacket\", int 12, double 59.95);
RetailItem item2 = new RetailItem (String \"Designer Jeans\", int 40, double 34.95);
RetailItem item3 = new RetailItem (String \"Shirt\", int 20, double 24.95);
System.out.println(item1.toString())...
System.out.println(item2.toString())...
System.out.println(item3.toString())...
}
}



