This code is in Java. This week you are going to write a client program for the RetailItem Class you created in Week 10.

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

This code is in Java. This week you are going to write a client program for the RetailItem Class you created in Week 10.

Post by answerhappygod »

This code is in Java. This week you are going to write a client
program for the RetailItem Class you created in Week
10. (Code is provided down below, please use it as the
base for the rest of the instructions. DO NOT JUST COPY AND PASTE
THE CODE O PROVIDED, IT IS NOT THE ANSWER!!!!) Note:
As part of this exercise, you may have to expand your RetailItem
class by adding some additional methods.
FOR THE PERSON WHO KEEPS ANSWERING THIS QUESTION: the
code you keep providing does indeed have trouble, when I try to run
it it says: "Cannot read the array length because
"RetailItem.RetailItem.data" is null" also, the one time I was able
to get it to work, when I pressed one it did not copy what was on
the file it just reprinted the output... Please do not reply to
this question unless you fixed that.
Follow every single instruction please, and put the
meaning for what you are doing using // so I know how it works.
Please also make sure that everything works properly before posting
the answer, and that with the file it directs
correctly.
Read through thoroughly!!!!
The driver should include the following menu items:
1. Display All Retail Items.
2. Display a Specific Retail Item.
3. Remove Retail Item Units
4. Add Retail Item Units
5. Change retail items price.
6. Change retail items description.
7. Exit
All retail item data is expected to be read from and written to a
file. It is important that you
modularize you code, it minimize the amount of code you will need
to write.
When a user selects one of these menu items here is what should
happen. Upon completion of
the selected item the menu above is redisplayed.
Display All Retail Items:
Displays a list of all the items in the Retail Item database along
with there description, units on
Hand, and their price.
Display a specific Retail Item:
User is prompted for the description of a retail item. The user
will then enter the description of
an item and press enter. The item is then looked up in the database
and if found it will display
the description, units on hand and the price for the item. If the
item is not found, an appropriate
error message is displayed.
Remove Retail Item Units:
User is prompted for the description of a retail item and the
number of units to remove. The
user will then enter the description of an item and press enter. If
the item is not found, an
appropriate error message will be displayed and the user will be
given the chance to enter a new
description. Upon entering a valid description, the user will be
prompted to enter the number of
units to remove. Reduce the inventory on hand based on the number
entered. If a user enters a
negative number, an appropriate error message should be displayed
and allow the user to re-enter
the number. Also, the number of units on hand can never be
negative, prevent this from
happening as well.
Add Retail Item Units:
User is prompted for the description of a retail item and the
number of units to add. The user
will then enter the description of an item and press enter. If the
item is not found, an appropriate
error message will be displayed and the user will be given the
chance to enter a new description.
Upon entering a valid description, the user will be prompted to
enter the number of units to add.
Increase the inventory on hand based on the number entered. If a
user enters a negative number,
an appropriate error message should be displayed and allow the user
to re-enter the number.
Change Retail Items price:
User is prompted for the description of a retail item and the new
price. The user will then enter
the description of an item and press enter. If the item is not
found, an appropriate error message
will be displayed and the user will be given the chance to enter a
new description. Upon
entering a valid description, the user will be prompted to enter a
new price. The new price
cannot be negative or 0. Prevent this from happening. Upon entering
a valid price, update the
price of the retail item.
Change Retail Items Description:
User is prompted for the description of a retail item and the new
description. The user will then
enter the description of an item and press enter. If the item is
not found, an appropriate error
message will be displayed and the user will be given the chance to
enter a new description.
Upon entering a valid description, the user will be prompted to
enter a new description. Update
the description of the retail item.
Exit:
Saves the current state of all the retail items to a file and exits
the program.
Note: Description cannot contain blanks/spaces. If user enters one
or more spaces in the
description, this is an error and should be flagged as an
error.
Bonus (5 points)
If you would like to earn some bonus points add the following items
to the menu:
1. Add New Retail Item
2. Delete Retail item
Add Retail item:
User is prompted to enter a description, units on hand, and price
of the new item. The new
item added to the Retail Item list. To add a new Retail Item you
will need to add one more
item to the array of Retail Items. This means you need to create an
array of Retail Items with
1 more item in it. To do this you will need to copy over all the
items from the original array
to the new array and then add in your new Retail Item.
Delete Retail item:
User is prompted for the description of the retail item to be
removed. The user will then
enter the description of an item and press enter. The item is then
looked up. If the item
exists, prompt the user to confirm to they wish to delete this
item. Upon confirmation delete
the item. To delete the item you will need to create a new array
that is one element smaller
and then copy all of the remaining retail items from the original
array to the new array. If
the item is not found in the list of retail items, an appropriate
error message should be
displayed.
Again, below is just a base for the instructions above,
it is not the answer.
import java.util.Scanner;
import java.io.*;
public class RetailItem {
private String description;
private int unitsOnHand;
private double price;
RetailItem()
{
description = "Unknown";
unitsOnHand = 0;
price = 0.0;
}
RetailItem(Scanner r)
{
readData(r);
}
RetailItem(String newDescription, int newUnitsOnHand, double
newPrice)
{
setDescription(newDescription);
setUnitsOnHand(newUnitsOnHand);
setPrice(newPrice);
}
public void setDescription(String newDescription)
{
if( newDescription.equals("") )
{
description = "Unknown";
} else {
description = newDescription;
}
}
public void setUnitsOnHand(int newUnitsOnHand)
{
unitsOnHand = 0;
if( unitsOnHand >= 0 )
{
unitsOnHand = newUnitsOnHand;
}
}
public void setPrice(double newPrice)
{
price = 0.0;
if( newPrice > 0.0 )
{
price = newPrice;
}
}
public String getDescription()
{
return description;
}
public int getUnitsOnHand()
{
return unitsOnHand;
}
public double getPrice()
{
return price;
}
public boolean readData(Scanner r)
{
boolean failed = true; // Indicates that we have failed reading
the
data from the file.
if( r.hasNext() )
{
String newDescription = r.next();
if( r.hasNextInt() )
{
int newUnitsOnHand = r.nextInt();
if( r.hasNextDouble() )
{
double newPrice = r.nextDouble();
setDescription(newDescription);
setUnitsOnHand(newUnitsOnHand);
setPrice(newPrice);
failed = false;
}
}
}
return failed;
}
public void writeData(PrintWriter w)
{
w.println(description + " " + unitsOnHand + " " + price);
}
}
I can't attach a file but here is the content of
RetailItemDatabase.txt, you have to make your own .txt file with
this in it:
DesignerJeans 40 34.95
Shirt 20 24.95
Jacket
Jacket 12.0 59.95
Jacket 12
Jacket 12 $59.95
Jacket -12 59.95
Jacket 12 -59.95
Jacket 12 59.95 xyz
Jacket 12 59.95
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply