COMSC-210 Lab 5: Stacks and Queues You read about time complexities this week an
ID: 3811591 • Letter: C
Question
COMSC-210 Lab 5: Stacks and Queues
You read about time complexities this week and practiced in the exercises within your readings, this lab is focusing on last week's readings: Stacks and Queues and the time complexity of each.
Inventory Bin Stack
You have been hired by Company X, Inc to develop an inventory control system. The following are some guidelines and specifications. You may expand upon these (make sure that the specifications are covered) and be as creative as you like!
Design an inventory class that stores the following members:
serialNum:An integer that holds a part's serial number.
manufactDate:A member that holds the date the part was manufactured.
lotNum:An integer that holds the part's lot number.
The class should have appropriate member functions for storing data into, and retrieving data from, these members.
Next, design a stack class that can hold objects of the class described above.
Last, design a program that uses the stack class described above. The program should have a loop that asks the user if he or she wishes to add a part to inventory, or take a part from inventory. The loop should repeat until the user is finished.
If the user wishes to add a part to inventory, the program should ask for the serial number, date of manufacture, and lot number. The data should be stored in an inventory object, and pushed onto the stack.
If the user wishes to take a part from inventory, the program should pop the top-most part from the stack and display the contents of its member variables.
When the user finishes the program, it should display the contents of the member values of all the objects that remain on the stack.
Finally, once you have completed your lab, analyze your algorithms. Provide a brief review of your stack and explain the time complexity of the individual functions (pop & push). Include this summary as a .txt file with your lab submission.
Please run it to make sure that it works.
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.Date;
import java.util.GregorianCalendar;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Sam
*/
public class InventoryStackDriver {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
InventoryStack stack = new InventoryStack();
int slNo, lotNo;
String[] mfgDate;
int ch;
while (true) {
System.out.println("1. Add item 2. Remove Item 3. Quit");
ch = Integer.parseInt(br.readLine());
if (ch == 1) {
System.out.println("Enter sl no: ");
slNo = Integer.parseInt(br.readLine());
System.out.println("Enter mfg date in format yyyy/mm/dd: ");
mfgDate = br.readLine().split("/"); //split into year month and day
System.out.println("Enter lot no: ");
lotNo = Integer.parseInt(br.readLine());
GregorianCalendar date = new GregorianCalendar(Integer.parseInt(mfgDate[0]), Integer.parseInt(mfgDate[1])-1, Integer.parseInt(mfgDate[2])); //create calender object
stack.push(new Inventory(slNo, date , lotNo));
}
else if (ch == 2) {
System.out.println(stack.pop()); //print poped item
}
else if (ch == 3)
break;
else
System.out.println("Invalid entry.");
}
Inventory temp = stack.pop();
System.out.println("Items in stack are:");
while (temp != null) { //print remaining stack items, untill stack is empty
System.out.println(temp);
temp = stack.pop();
}
}
}
class Inventory {
private final int slNo;
private final GregorianCalendar mfgDate;
private final int lotNo;
public Inventory(int slNo, GregorianCalendar mfgDate, int lotNo) {
this.slNo = slNo;
this.mfgDate = mfgDate;
this.lotNo = lotNo;
}
public int getSlNo() {
return slNo;
}
public GregorianCalendar getMfgDate() {
return mfgDate;
}
public int getLotNo() {
return lotNo;
}
@Override
public String toString() {
return "Inventory{" + " slNo=" + slNo + " mfgDate=" + mfgDate.getTime() + " lotNo=" + lotNo + " }";
}
}
class InventoryStack {
private final LinkedList<Inventory> stack;
public InventoryStack() {
stack = new LinkedList<>();
}
void push(Inventory a){
stack.addFirst(a);
}
Inventory pop(){
return stack.pollFirst();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.