MUST ALL BE CODED IN JAVA You’ve been hired by Half Foods to write a Java consol
ID: 3856211 • Letter: M
Question
MUST ALL BE CODED IN JAVA
You’ve been hired by Half Foods to write a Java console application that manages product inventory. The application has the following two classes:
Product.java
This class represents one product in their inventory and includes:
Fields
- (static) productCount – count of all distinct products; initialize to 0 in declaration.
- (static) inventoryValue – total inventory value of all products; initialize to 0 in declaration.
- (static) inventoryCount – total count of all products in inventory; initialize to 0 in declaration.
* code
* name
* cost
* count
A constructor with no parameters that sets the fields, respectively, to these values:
productCount = productCount + 1
code = -1
name = "(not set)"
cost = -1
count = -1
A constructor with four parameters that sets the fields, respectively, to these values:
productCount = productCount + 1
inventoryValue = inventoryValue + (cost * count)
inventoryCount = inventoryCount + count
code – set from parameter
name – set from parameter
cost – set from parameter
count – set from parameter
Getter methods for each field (declare the getters for productCount, inventoryValue, and inventoryCount static).
Setter methods for each field (declare the setters for productCount, inventoryValue, and inventoryCount static).
equals method that compares codes from two objects for equality.
toString method for returning instance variable values only.
HW5.java
This class contains the main method and uses the other class to manage Product data. Create text file ProductInventoryIn.txt, paste the following data into it, and place the file in your project folder. It has the following columns:
1) Product code
2) Product name
3) Product cost
4) Product quantity
ProductInventoryIn.txt
70 Gourmet Popcorn 2.80 50
71 Veggie Burgers 5.00 60
72 Italian Bread 4.00 70
73 Electrolyte Water 2.20 80
74 Shaved Parmesan 8.00 90
75 Lemonade 1.50 40
76 High Protein Bars 1.30 30
77 Ginger Shampoo 7.50 20
78 Blueberry Toothpaste 3.00 10
79 Goodnight Candles 2.50 100
Read the data from file ProductInventoryIn.txt into an array of Product objects called products. Present the following menu to the user:
Half Foods Menu
1 – Sell product
2 – Order product
3 – List product inventory
4 – Exit
Enter an option:
Here are what the options do:
Sell product – use a validation loop to prompt for and get from the user the code of the product to be sold (it has to be a valid code). Then use a validation loop to prompt for and get from the user the quantity of the product to be sold. Insure that the quantity is not greater than the current inventory for that product. Update the following fields:
- (static) inventoryValue
- (static) inventoryCount
* count for the product
Print a message in formatted columns including:
* Code
* Quantity
* Revenue from sale
Order Product – use a validation loop to prompt for and get from the user the code of the product to be ordered (it has to be a valid code). Then use a validation loop to prompt for and get from the user the quantity of the product to be ordered. Insure that the quantity is greater than zero. Update the following fields:
* (static) inventoryValue
* (static) inventoryCount
* count for the product
Print a message in formatted columns including:
* Code
* Quantity ordered
* Cost of order
List product inventory shows all product data in formatted columns. It then lists the product count, inventory value, and inventory count.
Exit closes the menu.
Continue to process menu options until the user enter 4. Then write the data to file ProductInventoryOut.txt*** in the same layout as the input file. Use these menu options and inputs for your last run:
Option Code Quantity
3
1 70 10
3
2 71 40
3
1 72 20
3
2 73 25
3
1 10,74 100,10
3
4
Hint: declare a keyboard object as a field (global) and close it at the end of method main.
Explanation / Answer
class ThreadTest1
{
public static void main(String args[])
{
MyThread thread1 = new MyThread("thread1: ");
MyThread thread2 = new MyThread("thread2: ");
thread1.start();
thread2.start();
boolean thread1IsAlive = true;
boolean thread2IsAlive = true;
do {
if (thread1IsAlive && !thread1.isAlive()) {
thread1IsAlive = false;
System.out.println("Thread 1 is dead.");
}
if (thread2IsAlive && !thread2.isAlive()) {
thread2IsAlive = false;
System.out.println("Thread 2 is dead.");
}
} while(thread1IsAlive || thread2IsAlive);
}
}
class MyThread extends Thread
{
static String message[] =
{ "Java", "is", "hot,", "aromatic,", "and", "invigorating."};
public MyThread(String id)
{
super(id);
}
public void run()
{
String name = getName();
for (int i=0;i<message.length;++i) {
randomWait();
System.out.println(name + message[i]);
}
}
void randomWait()
{
try {
sleep((long)(3000*Math.random()));
} catch (InterruptedException x) {
System.out.println("Interrupted!");
}
}
}
This program creates two threads of execution, thread1 and thread2, from the MyThread class. It then starts both threads and executes a do statement that waits for the threads to die. The threads display the Java is hot, aromatic, and invigorating. message word by word, while waiting a short, random amount of time between each word. Because both threads share the console window, the program's output identifies which threads were able to write to the console at various times during the program's execution.
Run ThreadTest1 to get an idea of the output that it produces. Each time you run the program you might get a different program display. This is because the program uses a random number generator to determine how long each thread should wait before displaying its output. Look at the following output:
C:javajdgch08>java ThreadTest1
thread1: Java
thread2: Java
thread2: is
thread2: hot,
thread2: aromatic,
thread1: is
thread1: hot,
thread2: and
thread1: aromatic,
thread1: and
thread2: invigorating.
Thread 2 is dead.
thread1: invigorating.
Thread 1 is dead.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.