JAVA. I have a hard bonus question I need help with. Should be able to accomplis
ID: 3716677 • Letter: J
Question
JAVA. I have a hard bonus question I need help with. Should be able to accomplish it without any advanced coding. Its my 1st semester in Java.
-------------------------------------------------------------------------------------------------------------
Write a class named RetaiI_Item that holds data about an item in a retail store. The class should store the following data in attributes:
•Item Number
•tem Description
•Units in Inventory
•Price
Create another class named Cash_Register that can be used with the Retail_Item class. The Cash_Register class should be able to internally keep a list of
Retail_Item objects. The class should have the following methods:
•A method named display_menu that displays the snapshot of data in the store.
•A method named purchase_item that accepts a Retail_Item object as an argument. Each time the purchase_item method is called, the Retail_Item that is
passed as an argument should be added to the list.
• A method named get_total that returns the total price of all the Retail_Items objects stored in the Cash_Register’s internal list.
• A method named show_items that displays data about the Retail_Item objects stored in the Cash_Register object’s intern al list.
• A method named clear that should clear the Cash_Register object’s internal list. Write a TestBonus class that uses
ArrayList to store Retail_Item objects. Each object in the ArrayList should hold data about an item in the retail store (Item Number, Item Description, Units in Inventory, and Price). The test class should also use the Cash_Register objects to allow the user to select several items for purchase , using a menu.
See the program menu on the second page. These objects must be stored in an ArrayList. When the user is ready to check out, the program should display a list of
all the items he or she has selected for purchase, as well as the total price, taxes (8.025%), and the final price.
The data for the retail store is available in the file Bonus.txt (its content is whown below)
.(Note: This program might be tested with a different file so allow theuser to enter the name of the file.)
Allow the user to run the program as many times as possible until a sentinel value, less than zero (0), has been entered for the selected item.
No input, processing, or output should happen in the main method. All work should be delegated to other non-static methods.
You will not get credit if the program is not written or does not work as expected. Test the program with your own data.
Program Menu
1. Pants
2. Jeans
3. Shirt
4. Dress
5. Socks
6.Sweater
7. Jacket
8. Suit
9. Swimsuit
10. Skirt
11. Clear Cash Register
12. Show Inventory
13. Check Out
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Bonus file content "Bonus.txt"
_______________________________________
____________________________________________
Explanation / Answer
Retail_Item - is any purchased item or item in store inventory
Cash_Register - represents single customer
TestBonus - represents a store
ItemType and _constant - are helper classes
RETAILSTORE.JAVA
import java.io.*;
import java.util.*;
import java.lang.*;
/*
Write a class named RetaiI_Item that holds data about an item in a retail store. The class should store the following data in attributes:
•Item Number
•Item Description
•Units in Inventory
•Price*/
class Retail_Item
{
int Item_Number=-1;
String Item_Description="";
int UnitsinInventory=0;
double Price=0;
public Retail_Item(int _Item_Number, String _Item_Description, int _UnitsinInventory, double _Price ) {
Item_Number = _Item_Number ;
Item_Description = _Item_Description ;
UnitsinInventory = UnitsinInventory + _UnitsinInventory ;
Price = _UnitsinInventory * _Price;
}
}
/*
Create another class named Cash_Register that can be used with the Retail_Item class. The Cash_Register class should be able to internally keep a list of
Retail_Item objects.
The class should have the following methods:
•A method named display_menu that displays the snapshot of data in the store.
•A method named purchase_item that accepts a Retail_Item object as an argument.
Each time the purchase_item method is called, the Retail_Item that is
passed as an argument should be added to the list.
• A method named get_total that returns the total price of all the Retail_Items objects stored in the Cash_Register’s internal list.
• A method named show_items that displays data about the Retail_Item objects stored in the Cash_Register object’s intern al list.
• A method named clear that should clear the Cash_Register object’s internal list. Write a TestBonus class that uses
ArrayList to store Retail_Item objects. Each object in the ArrayList should hold data about an item in the retail store (Item Number, Item Description, Units in Inventory, and Price). The test class should also use the Cash_Register objects to allow the user to select several items for purchase , using a menu.
*/
/*
ASSUMPTION: I am assuming Cash_register is unique for each purchase done by the customer.
It will contain only the items that are purchased.
*/
class Cash_Register
{
public ArrayList<Retail_Item> items;
public double tax;
public double FinalPrice;
public TestBonus store;
/* display program menu
Program Menu
1. Pants
2. Jeans
3. Shirt
4. Dress
5. Socks
6. Sweater
7. Jacket
8. Suit
9. Swimsuit
10. Skirt
11. Clear Cash Register
12. Show Inventory
13. Check Out
Allow the user to run the program as many times as possible until a sentinel value,
less than zero (0), has been entered for the selected item.
*/
public int display_menu(){
int mnuoption =-1;
while(true)
{
System.out.println(" ________________________________________________________________________");
System.out.println(" Program Menu");
System.out.println(" ________________________________________________________________________");
System.out.println(" 1. Pants 6. Sweater 11. Clear Cash Register");
System.out.println(" 2. Jeans 7. Jacket ");
System.out.println(" 3. Shirt 8. Suit 12. Show Inventory ");
System.out.println(" 4. Dress 9. Swimsuit ");
System.out.println(" 5. Socks 10. Skirt 13. Check Out ");
//System.out.println(" 6. Sweater");
//System.out.println(" 7. Jacket");
//System.out.println(" 8. Suit");
//System.out.println(" 9. Swimsuit");
//System.out.println(" 10. Skirt");
//System.out.println(" 11. Clear Cash Register");
//System.out.println(" 12. Show Inventory");
//System.out.println(" 13. Check Out");
System.out.print(" Please select option (-1 or lesser to exit): ");
Scanner in = new Scanner(System.in);
mnuoption = in.nextInt();
if (mnuoption <= -1)
{
System.out.println(" ");
System.out.println("...........................S A Y O N A A R A A....................... ");
System.out.println(" LLEGAR DE NUEVO");
System.out.println(" <|>");
break;
}
else if (mnuoption == _constant.mnuoption_CHECKOUT)
{
checkout();
break;
}
else if (mnuoption == _constant.mnuoption_SHOW_INVENTORY )
{
store.show_inventory();
}
else
{
processMenuOption(mnuoption);
}
}
return mnuoption;
}
public void purchase_item(Retail_Item item)
{
Retail_Item cashregisteritem;
Retail_Item storeitem = store.findItemInStore(item.Item_Description);
cashregisteritem = finditemfromregister(item.Item_Description);
if (cashregisteritem != null)
{
//check if item is available in store
if ((storeitem.UnitsinInventory - cashregisteritem.UnitsinInventory - item.UnitsinInventory) < 0)
{
System.out.printf(" NO MORE %s AVAILABLE ... ", item.Item_Description);
return;
}
//increasing or decreasing the quantity of items ordered
cashregisteritem.UnitsinInventory= cashregisteritem.UnitsinInventory+ item.UnitsinInventory;
//make sure the item price is provided via the input parameter
cashregisteritem.Price= cashregisteritem.UnitsinInventory * item.Price;
}
else
{
//check if item is available in store
if ((storeitem.UnitsinInventory - item.UnitsinInventory) < 0)
{
System.out.printf("No more %s available", item.Item_Description);
return;
}
items.add(item);
}
}
public double get_total(){
int totalitems = items.size();
double totalprice = 0;
Retail_Item item = null;
for(int i=0;i<totalitems;i++)
{
item = items.get(i);
totalprice = totalprice + item.Price;
}
return totalprice;
}
public void show_items(){
System.out.println(" -------------------- CURRENT CUSTOMER ORDER -------------------- ");
_constant.show_items(items);
double totalprice = 0;
totalprice = get_total();
tax = ((totalprice*_constant.TAXRATE)/100);
FinalPrice = totalprice + tax;
System.out.printf(" TOTAL PRICE = %.2f", totalprice);
System.out.printf(" TOTAL TAX (%.2f)%% = %.2f", _constant.TAXRATE,tax);
System.out.printf(" FINAL PRICE = %.2f", FinalPrice);
System.out.println(" ------------------------------------------------------------------ ");
_constant.pressanykey();
}
public void clear(){
items.clear();
}
public Retail_Item finditemfromregister(String itemdescription)
{
int totalitems = items.size();
Retail_Item itemfound = null;
Retail_Item item = null;
for (int i=0;i<totalitems;i++)
{
item = items.get(i);
if (item.Item_Description == itemdescription)
{
//item found
itemfound = item;
}
}
return itemfound;
}
public void processMenuOption(int mnuoption)
{
try
{
switch(mnuoption)
{
case _constant.mnuoption_PANTS:
case _constant.mnuoption_JEANS:
case _constant.mnuoption_SHIRT :
case _constant.mnuoption_DRESS :
case _constant.mnuoption_SOCKS :
case _constant.mnuoption_SWEATER:
case _constant.mnuoption_JACKET :
case _constant.mnuoption_SUIT:
case _constant.mnuoption_SWIMSUIT:
case _constant.mnuoption_SKIRT :
double itemprice=0;
Retail_Item storeitem = store.findItemInStore(_constant.getItemTypeDescription(mnuoption));
purchase_item(new Retail_Item(storeitem.Item_Number,storeitem.Item_Description,1,storeitem.Price));
break;
case _constant.mnuoption_CLEAR_CASH_REGISTER :
clear();
break;
case _constant.mnuoption_SHOW_INVENTORY :
break;
case _constant.mnuoption_CHECKOUT :
checkout();
break;
} //end switch
}//end try
catch(Exception e)
{System.out.println(e);}
}//endfunction
public void checkout()
{
show_items();
}
public Cash_Register(TestBonus _store)
{
items = new ArrayList<Retail_Item>();
store = _store; //the store to which this cash_register belongs
}
}
/*
Write a TestBonus class that uses
ArrayList to store Retail_Item objects.
Write a TestBonus class that uses
ArrayList to store Retail_Item objects.
Each object in the ArrayList should hold data about an item in the retail store
(Item Number, Item Description, Units in Inventory, and Price).
The test class should also use the Cash_Register objects
to allow the user to select several items for purchase , using a menu.
*/
/*
ASSUMPTION: I am assuming TestBonus represents the Store itself.
It will have all the items in the store and its latest inventory status
It will have an array for cash_register depicting every purchase made by
different customers
I am assuming storeItem is meant to add to the inventory of the store
*/
class TestBonus
{
//store items in the inventory.
//these items have to be deducted at every checkout
public ArrayList<Retail_Item> items;
//array of all checkout customers. Each customer is a cashregister in this array
public ArrayList<Cash_Register> customers;
//currentcustomer added to array only on checkout.
//It represents current customer that is processed on counter
public Cash_Register currentcustomer;
public String storename ;
public TestBonus(String _storename)
{
storename = _storename;
currentcustomer = new Cash_Register(this); // current customer in progress
items = new ArrayList<Retail_Item>(); //store items
customers = new ArrayList<Cash_Register>(); // all checked out customers
}
//add the new item to the store
public void storeItem(Retail_Item item)
{
items.add(item);
}
/*
The data for the retail store is available in the file Bonus.txt (its content is whown below)
.(Note: This program might be tested with a different file so allow theuser to enter the name of the file.)
*/
public boolean build(String args[])
{
/*
test code: to see what arguments were provided
for(int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}*/
if (args.length >=1)
{
String storeFilename = args[0];
Scanner infile = null;
try
{
infile = new Scanner(new File(storeFilename));
//System.out.printf(" creating the store with items from the file named %s ", storeFilename);
int itemid = -1;
String itemdescription ="";
int totalitems = 0;
double itemprice = 0;
while (infile.hasNextLine())
{
String reading = infile.nextLine(); //read string from infile
//Test code:uncomment the line below if you wish to see what is read from the file
//System.out.printf(" %s ",reading);
String[] itemdetails ;
Retail_Item item = null;
try
{
itemdetails = reading.split("\s+");
itemid = Integer.parseInt(itemdetails[0]);
itemdescription = itemdetails[1].toUpperCase();
totalitems = Integer.parseInt(itemdetails[2]);
itemprice = Double.parseDouble(itemdetails[3]);
item= new Retail_Item(itemid, itemdescription, totalitems, itemprice);
storeItem(item); //add the new item to the store
}
catch(Exception e){
//continue reading the file for next line.
//maybe the text was not in correct format and hence the error
;
}
}
}catch(Exception e) //catches any error
{
System.out.println(e);
}
return true;
}
else
{
System.out.println("/n/nThe store could not be built. /n/nPlease provide the filename where all the store item information is provided");
return false;
}
}//end build
public void open()
{
boolean bStoreIsOpen = true;
int mnuoption = -1;
while(bStoreIsOpen)
{
System.out.printf(" %s",storename.toUpperCase());
mnuoption = currentcustomer.display_menu();
/*If exit selected then exit close store */
if (mnuoption <=-1)
bStoreIsOpen = false;
else if (mnuoption == _constant.mnuoption_CHECKOUT )
{
customers.add(currentcustomer);
updateinventory(currentcustomer.items);
currentcustomer = new Cash_Register(this);
}
}
}
public void show_inventory()
{
show_items();
_constant.pressanykey();
}
public Retail_Item findItemInStore(String itemdescription)
{
int totalitemsinstore = items.size();
Retail_Item item = null;
Retail_Item itemfound = null;
for(int i=0;i<totalitemsinstore;i++)
{
item = items.get(i);
//System.out.printf(" searching %s in store for %s",item.Item_Description ,itemdescription);
if (item.Item_Description.equals(itemdescription) == true)
{
itemfound = item;
break;
}
}
//_constant.pressanykey();
return itemfound;
}
public void updateinventory(ArrayList<Retail_Item> checkedoutitems)
{
Retail_Item currentcheckedoutitem = null;
Retail_Item iteminStore = null;
int checkedoutitem_number ;
String checkedoutitem_description;
int checkedoutquantity;
int totalcheckedoutitems = checkedoutitems.size();
for (int i=0;i<totalcheckedoutitems;i++)
{
currentcheckedoutitem = checkedoutitems.get(i);
checkedoutitem_number = currentcheckedoutitem.Item_Number;
checkedoutitem_description = currentcheckedoutitem.Item_Description;
checkedoutquantity = currentcheckedoutitem.UnitsinInventory;
iteminStore = findItemInStore(checkedoutitem_description);
if (iteminStore != null)
{
iteminStore.UnitsinInventory = iteminStore.UnitsinInventory - checkedoutquantity;
}
else
System.out.println("The item checked out does not actually exists in store");
}
}
public void show_items()
{
System.out.println(" -------------------------STORE INVENTORY-----------------------");
_constant.show_items(items);
}
}//end TestBonus class
//class to help match up items that are ordered versus whats in store
class ItemType
{
int itemtypeid;
String itemdescription;
public ItemType(int _itemtypeid, String _itemdescription)
{
itemtypeid = _itemtypeid;
itemdescription = _itemdescription;
}
}
/* class _constant is used for defining constants and common functions across all classes */
class _constant{
private _constant() { } // Prevents instantiation
//menu option constants
public static final int mnuoption_PANTS = 1;
public static final int mnuoption_JEANS = 2;
public static final int mnuoption_SHIRT = 3;
public static final int mnuoption_DRESS = 4;
public static final int mnuoption_SOCKS = 5;
public static final int mnuoption_SWEATER = 6;
public static final int mnuoption_JACKET = 7;
public static final int mnuoption_SUIT = 8;
public static final int mnuoption_SWIMSUIT = 9;
public static final int mnuoption_SKIRT = 10;
public static final int mnuoption_CLEAR_CASH_REGISTER = 11;
public static final int mnuoption_SHOW_INVENTORY = 12;
public static final int mnuoption_CHECKOUT = 13;
public static final ItemType [] itemtypes = { new ItemType(mnuoption_PANTS, "PANTS"),
new ItemType(mnuoption_JEANS, "JEANS"),
new ItemType(mnuoption_SHIRT, "SHIRT"),
new ItemType(mnuoption_DRESS, "DRESS"),
new ItemType(mnuoption_SOCKS, "SOCKS"),
new ItemType(mnuoption_SWEATER, "SWEATER"),
new ItemType(mnuoption_SUIT, "SUIT"),
new ItemType(mnuoption_JACKET, "JACKET"),
new ItemType(mnuoption_SWIMSUIT, "SWIMSUIT"),
new ItemType(mnuoption_SKIRT, "SKIRT")
};
/* constant to define the current tax rate*/
public static final double TAXRATE = 8.025;
public static final String getItemTypeDescription(int mnuoption)
{
String itemdescription = "";
for(int i=0;i< itemtypes.length;i++)
{
if (itemtypes[i].itemtypeid == mnuoption)
itemdescription = itemtypes[i].itemdescription;
}
return itemdescription;
}
public static final void pressanykey()
{
try
{
Scanner sc=new Scanner(System.in);
System.out.printf(" Press enter key to continue .....");
sc.nextLine();
}
catch(Exception e)
{
System.out.println(e);
}
}
public static final void display_item(Retail_Item item)
{
if (item.Item_Description.length()>=8)
System.out.printf(" %d %s %d $%.2f",
item.Item_Number,
item.Item_Description ,
item.UnitsinInventory,
item.Price);
else
System.out.printf(" %d %s %d $%.2f",
item.Item_Number,
item.Item_Description ,
item.UnitsinInventory,
item.Price);
}
public static final void show_items(ArrayList<Retail_Item> items)
{
System.out.printf(" ITEMNO DESCP QTY PRICE");
int totalstoreitems =items.size();
Retail_Item item = null;
for(int j=0;j<totalstoreitems ;j++)
{
item = items.get(j);
_constant.display_item(item);
}
//_constant.pressanykey();
;
}
}
// MAIN CLASS that runs the stores and customers
public class RetailStore
{
/*
No input, processing, or output should happen in the main method.
All work should be delegated to other non-static methods.
*/
public static void main(String args[])
{
try
{
TestBonus mynewstore = new TestBonus("dsouza retail store");
if(mynewstore.build(args)==true)
{
mynewstore.open();
}
}//end try
catch(Exception e)
{
System.out.print("Exception caught: ");
e.printStackTrace();
}
}//end main
} //end RetailStore
INPUT : Bonus.txt
1000 Pants 10 19.99
2000 Jeans 2 25.95
3000 Shirt 15 12.50
4000 Dress 3 79.00
5000 Socks 50 1.98
6000 Sweater 5 49.99
7000 Jacket 1 85.95
8000 Suit 2 150.98
9000 Swimsuit 6 44.50
9500 Skirt 4 65.99
OUTPUT : compiling, main screen and how to exit
D:Program FilesJavajdk1.8.0_91in>javac RetailStore.java
D:Program FilesJavajdk1.8.0_91in>java RetailStore Bonus.txt
DSOUZA RETAIL STORE
________________________________________________________________________
Program Menu
________________________________________________________________________
1. Pants 6. Sweater 11. Clear Cash Register
2. Jeans 7. Jacket
3. Shirt 8. Suit 12. Show Inventory
4. Dress 9. Swimsuit
5. Socks 10. Skirt 13. Check Out
Please select option (-1 or lesser to exit): -2
...........................S A Y O N A A R A A.......................
LLEGAR DE NUEVO
<|>
D:Program FilesJavajdk1.8.0_91in>
OUTPUT : SHOW INVENTORY MENU OPTION - shows items available in store at present
D:Program FilesJavajdk1.8.0_91in>java RetailStore Bonus.txt
DSOUZA RETAIL STORE
________________________________________________________________________
Program Menu
________________________________________________________________________
1. Pants 6. Sweater 11. Clear Cash Register
2. Jeans 7. Jacket
3. Shirt 8. Suit 12. Show Inventory
4. Dress 9. Swimsuit
5. Socks 10. Skirt 13. Check Out
Please select option (-1 or lesser to exit): 12
-------------------------STORE INVENTORY-----------------------
ITEMNO DESCP QTY PRICE
1000 PANTS 10 $199.90
2000 JEANS 2 $51.90
3000 SHIRT 15 $187.50
4000 DRESS 3 $237.00
5000 SOCKS 50 $99.00
6000 SWEATER 5 $249.95
7000 JACKET 1 $85.95
8000 SUIT 2 $301.96
9000 SWIMSUIT 6 $267.00
9500 SKIRT 4 $263.96
Press enter key to continue .....
OUTPUT : Buy one jean, one shirt, one shirt and checkout
D:Program FilesJavajdk1.8.0_91in>java RetailStore Bonus.txt
DSOUZA RETAIL STORE
________________________________________________________________________
Program Menu
________________________________________________________________________
1. Pants 6. Sweater 11. Clear Cash Register
2. Jeans 7. Jacket
3. Shirt 8. Suit 12. Show Inventory
4. Dress 9. Swimsuit
5. Socks 10. Skirt 13. Check Out
Please select option (-1 or lesser to exit): 1
________________________________________________________________________
Program Menu
________________________________________________________________________
1. Pants 6. Sweater 11. Clear Cash Register
2. Jeans 7. Jacket
3. Shirt 8. Suit 12. Show Inventory
4. Dress 9. Swimsuit
5. Socks 10. Skirt 13. Check Out
Please select option (-1 or lesser to exit): 2
________________________________________________________________________
Program Menu
________________________________________________________________________
1. Pants 6. Sweater 11. Clear Cash Register
2. Jeans 7. Jacket
3. Shirt 8. Suit 12. Show Inventory
4. Dress 9. Swimsuit
5. Socks 10. Skirt 13. Check Out
Please select option (-1 or lesser to exit): 3
________________________________________________________________________
Program Menu
________________________________________________________________________
1. Pants 6. Sweater 11. Clear Cash Register
2. Jeans 7. Jacket
3. Shirt 8. Suit 12. Show Inventory
4. Dress 9. Swimsuit
5. Socks 10. Skirt 13. Check Out
Please select option (-1 or lesser to exit): 13
-------------------- CURRENT CUSTOMER ORDER --------------------
ITEMNO DESCP QTY PRICE
1000 PANTS 1 $199.90
2000 JEANS 1 $51.90
3000 SHIRT 1 $187.50
TOTAL PRICE = 439.30
TOTAL TAX (8.03)% = 35.25
FINAL PRICE = 474.55
------------------------------------------------------------------
Press enter key to continue .....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.