SlideShare a Scribd company logo
1 of 13
Download to read offline
Why won't my code build and run? and what is the correct code so it builds successfully?
getting errors in the Product.cpp file error:(Expected unqualified-id)
code below
Main.cpp
#include <iostream>
#include <time.h>
#include "InventorySystem.hpp"
#include <iostream>
#include "Product.hpp"
#include <time.h>
#include <stdlib.h>
using namespace std;
int main() {
srand(time( NULL ));
Inventory_System *h1 = nullptr ;
h1 = new Inventory_System("Radioshack", 117);
h1->BuildInventory();
h1->ShowInventory();
h1->ShowDefectInventory();
h1->Terminate();
delete h1;
return 0;
};
InventorySystem.cpp
#include "InventorySystem.hpp"
#include "InventoryItem.hpp"
#include "Product.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
Inventory_System::Inventory_System() {
}
Inventory_System::Inventory_System(std::string name, int id) :
store_name(name),
store_id(id) {
}
Inventory_System::~Inventory_System() {
for ( int i = 0; i < item_count; i++) {
delete ptr_inv_item[i];
}
}
int Inventory_System::GetStoreId(){
return store_id;
}
int Inventory_System::GetItemCount(){
return item_count;
}
std::string Inventory_System::GetStoreName(){
return store_name;
}
void Inventory_System::BuildInventory() {
std::ifstream file("products_test.txt", std::ios::in);
std::string name, quantity, price, condition;
Inventory_Item *item;
if (!file) {
std::cerr << "Error: Failed to open filen";
exit(-1);
}
else {
while (!file.eof()) {
item = new Product();
Product *product = static_cast <Product*> (item);
std::getline(file, name, ';');
product->SetItemName(name);
std::getline(file, quantity, ';');
product->SetItemQuantity(stoi(quantity));
std::getline(file, price, ';');
product->SetProductPrice(stold(price));
std::getline(file, condition, 'n');
switch (condition[0]) {
case 'D':
product->SetProductCondition(PC_DEFECTIVE);
break ;
case 'N':
product->SetProductCondition(PC_NEW);
break ;
case 'R':
product->SetProductCondition(PC_REFURBISHED);
break ;
case 'U':
product->SetProductCondition(PC_USED);
break ;
default :
product->SetProductCondition(PC_NEW);
break ;
}
product->SetProductId(rand() % 10000);
ptr_inv_item[item_count] = product;
item_count++;
}
}
}
void Inventory_System::ShowInventory() {
for ( int i = 0; i < item_count; i++) {
Product *temp = static_cast <Product*> (ptr_inv_item[i]);
temp->Display();
}
}
void Inventory_System::ShowDefectInventory() {
for ( int i = 0; i < item_count; i++) {
Product *temp = static_cast <Product*> (ptr_inv_item[i]);
if (temp->GetProductCondition() == PC_DEFECTIVE) {
temp->Display();
}
}
}
void Inventory_System::Terminate() {
std::ofstream file;
std::stringstream ss;
file.open("example.txt");
for ( int i = 0; i < item_count; i++) {
Product *temp = static_cast <Product*> (ptr_inv_item[i]);
file << temp->GetItemName() << ";"
<< temp->GetItemQuantity() << ";"
<< temp->GetProductPrice() << ";";
switch (temp->GetProductCondition()) {
case PC_DEFECTIVE:
file << "Dn";
break ;
case PC_NEW:
file << "Nn";
break ;
case PC_REFURBISHED:
file << "Rn";
break ;
case PC_USED:
file << "Un";
break ;
default :
file << "Dn";
break ;
}
}
}
InventorySystem.h
#ifndef InventorySystem_hpp
#define InventorySystem_hpp
#include "InventoryItem.hpp"
#include "Product.hpp"
#include <stdio.h>
#include <string>
class Inventory_System {
public :
Inventory_System();
Inventory_System(std::string, int );
~Inventory_System();
int GetStoreId();
int GetItemCount();
std::string GetStoreName();
void BuildInventory();
void ShowInventory();
void ShowDefectInventory();
void Terminate();
private :
std::string store_name;
int store_id, item_count = 0;
Inventory_Item **ptr_inv_item = new Inventory_Item*[512];
};
#endif /* InventorySystem_hpp */
InventoryItem.cpp
#include "InventoryItem.hpp"
#include <iostream>
Inventory_Item::Inventory_Item(){
}
Inventory_Item::Inventory_Item(std::string name, int quantity) :
item_name(name),
item_quantity(quantity) {
}
Inventory_Item::~Inventory_Item() {
std::cout << "Inventory Item " << item_name << " with " << item_quantity << " item(s)
destroyed.n";
}
std::string Inventory_Item::GetItemName() const {
return item_name;
}
int Inventory_Item::GetItemQuantity() const {
return item_quantity;
}
void Inventory_Item::SetItemName(std::string name) {
item_name = name;
}
void Inventory_Item::SetItemQuantity( int quantity) {
item_quantity = quantity;
}
void Inventory_Item::Display() const {
std::cout << item_name << " (" << item_quantity << ") ";
}
InventoryItem.h
#ifndef InventoryItem_hpp
#define InventoryItem_hpp
#include <stdio.h>
#include <string>
class Inventory_Item {
public :
Inventory_Item();
Inventory_Item(std::string name, int quantity);
~Inventory_Item();
std::string GetItemName() const ;
int GetItemQuantity() const ;
void SetItemName(std::string);
void SetItemQuantity( int );
virtual void Display() const ;
protected :
std::string item_name;
int item_quantity;
private :
};
#endif /* InventoryItem_hpp */
Product.cpp
#include "Product.hpp"
#include <iostream>
#include <iomanip>
Product::Product() :
Inventory_Item(),
product_id(0),
product_price(0.0) {
}
Inventory_Item(),
product_id(0),
product_price(0.0){
}
Product::Product(std::string name, int quantity) :
Inventory_Item(name, quantity),
product_id(rand()% 1000),
product_price(100.0 * ((rand() + 1) / double (RAND_MAX + 2))){
}
Product::~Product() {
std::cout << "Product " << product_id << " priced $" << product_price << " has been
destroyed.";
}
int Product::GetProductId() const {
return product_id;
}
double Product::GetProductPrice() const {
return product_price;
}
T_Condition Product::GetProductCondition() const {
return condition_;
}
void Product::SetProductId( int id){
product_id = id;
}
void Product::SetProductPrice( double price) {
product_price = price;
}
void Product::SetProductCondition(T_Condition condition) {
condition_ = condition;
}
void Product::Display() const {
Inventory_Item::Display();
std::cout << product_id << " $" << std::setprecision(4) << product_price << 'n';
}
Product.h
#ifndef Product_hpp
#define Product_hpp
#include "InventoryItem.hpp"
#include <stdio.h>
#include "Product.hpp"
#include <string>
typedef enum { PC_NEW, PC_USED, PC_REFURBISHED, PC_DEFECTIVE } T_Condition;
class Product : public Inventory_Item {
public :
Product();
Product(std::string, int );
~Product();
int GetProductId() const ;
double GetProductPrice() const ;
T_Condition GetProductCondition() const ;
void SetProductId( int );
void SetProductPrice( double );
void SetProductCondition(T_Condition);
virtual void Display() const ;
private :
T_Condition condition_;
int product_id;
double product_price;
};
#endif /* Product_hpp */
Why won't my code build and run- and what is the correct code so it bu (1).pdf

More Related Content

Similar to Why won't my code build and run- and what is the correct code so it bu (1).pdf

In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
bradburgess22840
 
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfC++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
aassecuritysystem
 
#include customer.h#include heap.h#include iostream.docx
#include customer.h#include heap.h#include iostream.docx#include customer.h#include heap.h#include iostream.docx
#include customer.h#include heap.h#include iostream.docx
AASTHA76
 
#include stdio.h#include stdlib.h#include string.hstruct.pdf
#include stdio.h#include stdlib.h#include string.hstruct.pdf#include stdio.h#include stdlib.h#include string.hstruct.pdf
#include stdio.h#include stdlib.h#include string.hstruct.pdf
anubhavnigam2608
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
ssuser454af01
 
maincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdfmaincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdf
abiwarmaa
 
Develop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdfDevelop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdf
flashfashioncasualwe
 

Similar to Why won't my code build and run- and what is the correct code so it bu (1).pdf (20)

Writing native bindings to node.js in C++
Writing native bindings to node.js in C++Writing native bindings to node.js in C++
Writing native bindings to node.js in C++
 
How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js Module
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded Python
 
Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#
 
Android ndk
Android ndkAndroid ndk
Android ndk
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
OpenCMIS Part 1
OpenCMIS Part 1OpenCMIS Part 1
OpenCMIS Part 1
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfC++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
#include customer.h#include heap.h#include iostream.docx
#include customer.h#include heap.h#include iostream.docx#include customer.h#include heap.h#include iostream.docx
#include customer.h#include heap.h#include iostream.docx
 
Introduction to Spring Boot.pdf
Introduction to Spring Boot.pdfIntroduction to Spring Boot.pdf
Introduction to Spring Boot.pdf
 
Static code analysis: what? how? why?
Static code analysis: what? how? why?Static code analysis: what? how? why?
Static code analysis: what? how? why?
 
V8
V8V8
V8
 
#include stdio.h#include stdlib.h#include string.hstruct.pdf
#include stdio.h#include stdlib.h#include string.hstruct.pdf#include stdio.h#include stdlib.h#include string.hstruct.pdf
#include stdio.h#include stdlib.h#include string.hstruct.pdf
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
maincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdfmaincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdf
 
Develop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdfDevelop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdf
 

More from umeshagarwal39

More from umeshagarwal39 (20)

Write a c++ program that will input TWO characters then print if it is.pdf
Write a c++ program that will input TWO characters then print if it is.pdfWrite a c++ program that will input TWO characters then print if it is.pdf
Write a c++ program that will input TWO characters then print if it is.pdf
 
Wredk 10-5 Ps- Rob- Fes- me ton- Would youi like to haye a cotfer Thre.pdf
Wredk 10-5 Ps- Rob- Fes- me ton- Would youi like to haye a cotfer Thre.pdfWredk 10-5 Ps- Rob- Fes- me ton- Would youi like to haye a cotfer Thre.pdf
Wredk 10-5 Ps- Rob- Fes- me ton- Would youi like to haye a cotfer Thre.pdf
 
Write 200-250 words Q-1 Identify specific examples of different stake.pdf
Write 200-250 words  Q-1 Identify specific examples of different stake.pdfWrite 200-250 words  Q-1 Identify specific examples of different stake.pdf
Write 200-250 words Q-1 Identify specific examples of different stake.pdf
 
Working with the Concepts 5- Cable subscriptions decline- The number o.pdf
Working with the Concepts 5- Cable subscriptions decline- The number o.pdfWorking with the Concepts 5- Cable subscriptions decline- The number o.pdf
Working with the Concepts 5- Cable subscriptions decline- The number o.pdf
 
Within the organizational structure of Philips- the national organizat.pdf
Within the organizational structure of Philips- the national organizat.pdfWithin the organizational structure of Philips- the national organizat.pdf
Within the organizational structure of Philips- the national organizat.pdf
 
With appropriate referencing and elaborate explanation- write a minimu.pdf
With appropriate referencing and elaborate explanation- write a minimu.pdfWith appropriate referencing and elaborate explanation- write a minimu.pdf
With appropriate referencing and elaborate explanation- write a minimu.pdf
 
Winger corporation was organized in March- It is authorized to issue 5.pdf
Winger corporation was organized in March- It is authorized to issue 5.pdfWinger corporation was organized in March- It is authorized to issue 5.pdf
Winger corporation was organized in March- It is authorized to issue 5.pdf
 
William Ouchi's Theory Z recognized Multiple Choice the combined benef.pdf
William Ouchi's Theory Z recognized Multiple Choice the combined benef.pdfWilliam Ouchi's Theory Z recognized Multiple Choice the combined benef.pdf
William Ouchi's Theory Z recognized Multiple Choice the combined benef.pdf
 
Wildhorse Company has a balancet in its Accouats Receivable control ac.pdf
Wildhorse Company has a balancet in its Accouats Receivable control ac.pdfWildhorse Company has a balancet in its Accouats Receivable control ac.pdf
Wildhorse Company has a balancet in its Accouats Receivable control ac.pdf
 
Wildhorse Company has a balance in its Accounts Recevvable control acc.pdf
Wildhorse Company has a balance in its Accounts Recevvable control acc.pdfWildhorse Company has a balance in its Accounts Recevvable control acc.pdf
Wildhorse Company has a balance in its Accounts Recevvable control acc.pdf
 
why does this code keep generating this error- even though the URL is.pdf
why does this code keep generating this error- even though the URL is.pdfwhy does this code keep generating this error- even though the URL is.pdf
why does this code keep generating this error- even though the URL is.pdf
 
Why do you think the SEC (should or should not) to go after celebritie.pdf
Why do you think the SEC (should or should not) to go after celebritie.pdfWhy do you think the SEC (should or should not) to go after celebritie.pdf
Why do you think the SEC (should or should not) to go after celebritie.pdf
 
Who would be at increased risk for vitamin B12 deficiency without fort.pdf
Who would be at increased risk for vitamin B12 deficiency without fort.pdfWho would be at increased risk for vitamin B12 deficiency without fort.pdf
Who would be at increased risk for vitamin B12 deficiency without fort.pdf
 
Why are funny channels -funny- (different from other channels)- Becaus.pdf
Why are funny channels -funny- (different from other channels)- Becaus.pdfWhy are funny channels -funny- (different from other channels)- Becaus.pdf
Why are funny channels -funny- (different from other channels)- Becaus.pdf
 
Why do we try to remove air bubbles in the electroblot set up- Protein.pdf
Why do we try to remove air bubbles in the electroblot set up- Protein.pdfWhy do we try to remove air bubbles in the electroblot set up- Protein.pdf
Why do we try to remove air bubbles in the electroblot set up- Protein.pdf
 
Why do we need Confusion matrix-What is recall- accuracy- precision an.pdf
Why do we need Confusion matrix-What is recall- accuracy- precision an.pdfWhy do we need Confusion matrix-What is recall- accuracy- precision an.pdf
Why do we need Confusion matrix-What is recall- accuracy- precision an.pdf
 
Which of these is the best example of mechanical digestion- Group of a.pdf
Which of these is the best example of mechanical digestion- Group of a.pdfWhich of these is the best example of mechanical digestion- Group of a.pdf
Which of these is the best example of mechanical digestion- Group of a.pdf
 
Whispering Winds Corp- reported these income statement data for a 2-ye.pdf
Whispering Winds Corp- reported these income statement data for a 2-ye.pdfWhispering Winds Corp- reported these income statement data for a 2-ye.pdf
Whispering Winds Corp- reported these income statement data for a 2-ye.pdf
 
While in her hepatocyte- you notice that a molecule of carbon dioxide.pdf
While in her hepatocyte- you notice that a molecule of carbon dioxide.pdfWhile in her hepatocyte- you notice that a molecule of carbon dioxide.pdf
While in her hepatocyte- you notice that a molecule of carbon dioxide.pdf
 
Which was not a reason it took 100 years for the Copernican Revolution.pdf
Which was not a reason it took 100 years for the Copernican Revolution.pdfWhich was not a reason it took 100 years for the Copernican Revolution.pdf
Which was not a reason it took 100 years for the Copernican Revolution.pdf
 

Recently uploaded

Recently uploaded (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

Why won't my code build and run- and what is the correct code so it bu (1).pdf

  • 1. Why won't my code build and run? and what is the correct code so it builds successfully? getting errors in the Product.cpp file error:(Expected unqualified-id) code below Main.cpp #include <iostream> #include <time.h> #include "InventorySystem.hpp" #include <iostream> #include "Product.hpp" #include <time.h> #include <stdlib.h> using namespace std; int main() { srand(time( NULL )); Inventory_System *h1 = nullptr ; h1 = new Inventory_System("Radioshack", 117); h1->BuildInventory(); h1->ShowInventory(); h1->ShowDefectInventory(); h1->Terminate(); delete h1; return 0; };
  • 2. InventorySystem.cpp #include "InventorySystem.hpp" #include "InventoryItem.hpp" #include "Product.hpp" #include <iostream> #include <fstream> #include <sstream> Inventory_System::Inventory_System() { } Inventory_System::Inventory_System(std::string name, int id) : store_name(name), store_id(id) { } Inventory_System::~Inventory_System() { for ( int i = 0; i < item_count; i++) { delete ptr_inv_item[i]; } } int Inventory_System::GetStoreId(){ return store_id; } int Inventory_System::GetItemCount(){ return item_count;
  • 3. } std::string Inventory_System::GetStoreName(){ return store_name; } void Inventory_System::BuildInventory() { std::ifstream file("products_test.txt", std::ios::in); std::string name, quantity, price, condition; Inventory_Item *item; if (!file) { std::cerr << "Error: Failed to open filen"; exit(-1); } else { while (!file.eof()) { item = new Product(); Product *product = static_cast <Product*> (item); std::getline(file, name, ';'); product->SetItemName(name); std::getline(file, quantity, ';'); product->SetItemQuantity(stoi(quantity)); std::getline(file, price, ';'); product->SetProductPrice(stold(price)); std::getline(file, condition, 'n');
  • 4. switch (condition[0]) { case 'D': product->SetProductCondition(PC_DEFECTIVE); break ; case 'N': product->SetProductCondition(PC_NEW); break ; case 'R': product->SetProductCondition(PC_REFURBISHED); break ; case 'U': product->SetProductCondition(PC_USED); break ; default : product->SetProductCondition(PC_NEW); break ; } product->SetProductId(rand() % 10000); ptr_inv_item[item_count] = product; item_count++; } } }
  • 5. void Inventory_System::ShowInventory() { for ( int i = 0; i < item_count; i++) { Product *temp = static_cast <Product*> (ptr_inv_item[i]); temp->Display(); } } void Inventory_System::ShowDefectInventory() { for ( int i = 0; i < item_count; i++) { Product *temp = static_cast <Product*> (ptr_inv_item[i]); if (temp->GetProductCondition() == PC_DEFECTIVE) { temp->Display(); } } } void Inventory_System::Terminate() { std::ofstream file; std::stringstream ss; file.open("example.txt"); for ( int i = 0; i < item_count; i++) { Product *temp = static_cast <Product*> (ptr_inv_item[i]); file << temp->GetItemName() << ";" << temp->GetItemQuantity() << ";" << temp->GetProductPrice() << ";";
  • 6. switch (temp->GetProductCondition()) { case PC_DEFECTIVE: file << "Dn"; break ; case PC_NEW: file << "Nn"; break ; case PC_REFURBISHED: file << "Rn"; break ; case PC_USED: file << "Un"; break ; default : file << "Dn"; break ; } } } InventorySystem.h #ifndef InventorySystem_hpp #define InventorySystem_hpp #include "InventoryItem.hpp"
  • 7. #include "Product.hpp" #include <stdio.h> #include <string> class Inventory_System { public : Inventory_System(); Inventory_System(std::string, int ); ~Inventory_System(); int GetStoreId(); int GetItemCount(); std::string GetStoreName(); void BuildInventory(); void ShowInventory(); void ShowDefectInventory(); void Terminate(); private : std::string store_name; int store_id, item_count = 0; Inventory_Item **ptr_inv_item = new Inventory_Item*[512]; }; #endif /* InventorySystem_hpp */ InventoryItem.cpp #include "InventoryItem.hpp"
  • 8. #include <iostream> Inventory_Item::Inventory_Item(){ } Inventory_Item::Inventory_Item(std::string name, int quantity) : item_name(name), item_quantity(quantity) { } Inventory_Item::~Inventory_Item() { std::cout << "Inventory Item " << item_name << " with " << item_quantity << " item(s) destroyed.n"; } std::string Inventory_Item::GetItemName() const { return item_name; } int Inventory_Item::GetItemQuantity() const { return item_quantity; } void Inventory_Item::SetItemName(std::string name) { item_name = name; } void Inventory_Item::SetItemQuantity( int quantity) { item_quantity = quantity; } void Inventory_Item::Display() const {
  • 9. std::cout << item_name << " (" << item_quantity << ") "; } InventoryItem.h #ifndef InventoryItem_hpp #define InventoryItem_hpp #include <stdio.h> #include <string> class Inventory_Item { public : Inventory_Item(); Inventory_Item(std::string name, int quantity); ~Inventory_Item(); std::string GetItemName() const ; int GetItemQuantity() const ; void SetItemName(std::string); void SetItemQuantity( int ); virtual void Display() const ; protected : std::string item_name; int item_quantity; private : }; #endif /* InventoryItem_hpp */
  • 10. Product.cpp #include "Product.hpp" #include <iostream> #include <iomanip> Product::Product() : Inventory_Item(), product_id(0), product_price(0.0) { } Inventory_Item(), product_id(0), product_price(0.0){ } Product::Product(std::string name, int quantity) : Inventory_Item(name, quantity), product_id(rand()% 1000), product_price(100.0 * ((rand() + 1) / double (RAND_MAX + 2))){ } Product::~Product() { std::cout << "Product " << product_id << " priced $" << product_price << " has been destroyed."; } int Product::GetProductId() const { return product_id;
  • 11. } double Product::GetProductPrice() const { return product_price; } T_Condition Product::GetProductCondition() const { return condition_; } void Product::SetProductId( int id){ product_id = id; } void Product::SetProductPrice( double price) { product_price = price; } void Product::SetProductCondition(T_Condition condition) { condition_ = condition; } void Product::Display() const { Inventory_Item::Display(); std::cout << product_id << " $" << std::setprecision(4) << product_price << 'n'; } Product.h #ifndef Product_hpp #define Product_hpp
  • 12. #include "InventoryItem.hpp" #include <stdio.h> #include "Product.hpp" #include <string> typedef enum { PC_NEW, PC_USED, PC_REFURBISHED, PC_DEFECTIVE } T_Condition; class Product : public Inventory_Item { public : Product(); Product(std::string, int ); ~Product(); int GetProductId() const ; double GetProductPrice() const ; T_Condition GetProductCondition() const ; void SetProductId( int ); void SetProductPrice( double ); void SetProductCondition(T_Condition); virtual void Display() const ; private : T_Condition condition_; int product_id; double product_price; }; #endif /* Product_hpp */