LAB 01 All displayed prompts and results should be well formatted. The assignmen
ID: 3849076 • Letter: L
Question
LAB 01 All displayed prompts and results should be well formatted. The assignment is based on the following Programming Exercise in the book: Programming Projects Chapter 5 Defining Classes II Programming Exercises Page 339 Hot Dog Stands This assignment requires the following enhancements: There must be at least 2 java source code files: HotDogStandMainClass java Contains the main HotDogStandMainClass. HotDogStandClass java Contains the Hot Dog Stand Class. Inventory Control The hotdog stand class will have an inventory of hot dogs that the class must be able to track. The class must have methods to sct, access, add to, and subtract sales from the inventory. A hot dog stand class constructor must set the default initial inventory to 25 hotdogs. Accounting The hot dog class must keep track of the total hotdogs sold since its object instantiation. Buy (int i method Instead of a sold method as in the book) the hotdog class must have a buy methodthat has an parameter that indicates the number of dogs that wants to buy. The hotdog class should be intelligent enough to sell only hotdogs that are in inventory. If the buy is more than the inventory, the class should state the hotdogs in inventory and ask if the remaining (not 0) hotdogs in inventory is sufficient for the buyer. If there is a buy when there are 0 hotdogs in invventory, the class should state that it is out of hotdogs to the buyer. Develop a method called toString0 for the hotdog stands class that displays a String object allthe Hotdog stand object member values and states in a well formatted manner. The main class must include driver code that tests all the logic paths of the hot stand class. Do not use JPanel or interactive prompts, unit test the hot class by coding in test scenarios in the main program which will act as a driver.Explanation / Answer
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
public class Inventory {
ConcurrentHashMap<Product,Integer> Inv=new ConcurrentHashMap<Product, Integer>();
HashMap<Integer, Product> actualProduct=new HashMap<Integer, Product>();
void buildInventory(String filename){
try{
FileInputStream fstream = new FileInputStream("C:\dev\eclipse_workspace\Warehouse\src\"+filename);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null){
String S[]=strLine.split(" ");
int id=Integer.parseInt(S[0]);
int quantity=Integer.parseInt(S[2]);
int lid=Integer.parseInt(S[3]);
Location L=new Location(lid, S[4]);
Product P=new Product(id, S[1], L);
Inv.put(P, quantity);
actualProduct.put(id, P);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
void printInventory(){
System.out.println("Printing Inventory");
for(Product P:Inv.keySet()){
System.out.print(P.id+" ");
System.out.print(P.name+" ");
System.out.print(P.loc.id+" "+P.loc.name+" ");
System.out.print(Inv.get(P));
System.out.println();
}
}
void pickProduct(int id, int quantity){
if(!actualProduct.containsKey(id))
System.out.println("Sorry, the product is not available in inventory");
else if(quantity<=0)
System.out.println("Invalid Quantity");
else if(Inv.get(actualProduct.get(id)).intValue()<quantity)
System.out.println("Not enough stock");
else
Inv.put(actualProduct.get(id), Inv.get(actualProduct.get(id)).intValue()-quantity);
}
void restockProduct(int id, int quantity){
if(!actualProduct.containsKey(id))
System.out.println("Sorry, the product is not available in inventory");
else if(quantity<=0)
System.out.println("Invalid Quantity");
else
Inv.put(actualProduct.get(id), Inv.get(actualProduct.get(id)).intValue()+quantity);
}
public static void main(String args[]){
System.out.println(args[0]);
final Inventory Inv=new Inventory();
Inv.buildInventory(args[0]);
Inv.printInventory();
Thread t1 = new Thread(new Runnable() {
public void run() {
Inv.pickProduct(2, 40);
}
});
t1.start();
Inv.printInventory();
Thread t2 = new Thread(new Runnable() {
public void run() {
Inv.pickProduct(2, 40);
}
});
t2.start();
Inv.printInventory();
Thread t3 = new Thread(new Runnable() {
public void run() {
Inv.restockProduct(2, 50);
}
});
t3.start();
Inv.printInventory();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.