Stacks and Queues You read about time complexities this week and practiced in th
ID: 3808403 • Letter: S
Question
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.
Explanation / Answer
main.cpp
====
/*
Inventory program in c++.
*/
#include <iostream>
#include "inventoryDB.cpp"
int main()
{
std::cout << "Hi!" << std::endl;
InventoryDB *db1 = new InventoryDB();
std::cout << "Adding new Inventory.." << std::endl;
db1->add_inventory(12345, 25,
9, 1993,
1000);
db1->add_inventory(12346, 26,
9, 2013,
10277);
std::cout << db1->show_stack() << std::endl;
std::cout << "Removing the last inventory!" << std::endl;
db1->pop_stack();
std::cout << db1->show_stack() << std::endl;
return 0;
}
// inventoryDB.cpp
// ====
#include <vector>
#include <string>
#include "inventory.cpp"
class InventoryDB {
std::vector<Inventory*> db;
public:
// Add a new Inventory
void add_inventory(int serial, int date,
int month, int year,
int lot);
std::string show_stack();
void pop_stack();
};
/** Adds something to stack **/
void InventoryDB::add_inventory(int serial, int date,
int month, int year,
int lot)
{
Inventory *in1 = new Inventory();
in1->set_serial_num(serial);
in1->set_manufact_date(date, month, year);
in1->set_lot_num(lot);
db.push_back(in1);
};
/** show stack **/
std::string InventoryDB::show_stack()
{
std::string out;
for (Inventory* item : db)
{
out += "Serial Number: " + std::to_string(item->get_serial_num())
+ ", Manufacturing Date: " + item->get_manufact_date()
+ ", Lot No: " + std::to_string(item->get_lot_num())
+ " ";
}
return out;
};
/** Removes last element **/
void InventoryDB::pop_stack() { db.pop_back(); };
// inventory.cpp
// ====
#include <string>
struct Date {
int day;
int month;
int year;
};
class Inventory {
int serial_num;
Date manufact_date;
int lot_num;
public:
// Setters
void set_serial_num(int serial);
void set_manufact_date(
int date, int month, int year);
void set_lot_num(int lot);
// Getters
int get_serial_num();
std::string get_manufact_date();
int get_lot_num();
};
// Setters
void Inventory::set_serial_num(int serial) { serial_num = serial; };
void Inventory::set_manufact_date(
int date, int month, int year)
{
manufact_date = {date, month, year};
};
void Inventory::set_lot_num(int lot) { lot_num = lot; };
// Getters
int Inventory::get_serial_num() { return serial_num; };
std::string Inventory::get_manufact_date()
{
return std::to_string(manufact_date.month) + "/"
+ std::to_string(manufact_date.day) + "/"
+ std::to_string(manufact_date.year);
};
int Inventory::get_lot_num() { return lot_num; };
all files must must be in the same directory.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.