Question
Please correct this code (the exception class) and please submit screenshot as well as text of each seperate file . Note: Each file is seperated.
Please correct this code (the exception class) and please submit screenshot as well as text of each seperate file. Note: Each file is seperated. Data.csv file is in bold at the bottom. This is a notepad file that the program pulls from - showing the item is a book, the quantity the price, the title, author, and publisher. The exception class needs to be able to:
to use try and catch to handle exceptions in at least the following cases: Prevent price from ever being negative Prevent quantity on hand from being negative For each case, create an exception class in the InventoryItem class that can be used to signal the exception type. When NegativePriceException is caught by a client code, the client should generate an error message to that effect and continue. When a NegativeQuantityException is caught by a client code, the client should generate an error message to that effect and set the QuantityOnHand data member to 0.
//Main.cpp
#include
using namespace std;
int main() {
ifstream file("data.csv"); //input file is data.csv from notepad if (!file) { cerr << "Can't open file " << "data.csv" << endl; return(EXIT_FAILURE); }
vector
system("pause"); return 0; }
//end of code for file
//Inventory.cpp code
#include "Inventory.h" #include
InventoryItem::InventoryItem() { description = " "; quantityOnHand = 0; } InventoryItem::InventoryItem(int a, string d) { if (quantityOnHand >= 0) quantityOnHand = a; description = d; } void InventoryItem::setDescription(string d) // description data member is set to string d; { description = d; } void InventoryItem::setPrice(double a) { price = a; }
void InventoryItem::setQuantityOnHand(int q) //set quantityOnHand to to q if q >= 0; unchanged otherwise; { if (q >= 0) quantityOnHand = q;
} void InventoryItem::display() {
cout << "Quantity :" << quantityOnHand << endl; cout << " Description : " << description << endl; cout << "Price :" << price << endl; } void InventoryItem::readInventoryItem(ifstream& inFile) { string des; int quant; double p; if (inFile.is_open()) {
string line; getline(inFile, line, ','); setDescription(line); int q; double p; char comma; inFile >> q; setQuantityOnHand(q); inFile >> comma; inFile >> p; InventoryItem::setPrice(p); inFile.ignore(100, ' '); }
} InventoryItem::InventoryItem(double z) { if (z >= 0) { price = z; } }
//end of file
//Inventory.h file
#pragma once
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include
#include
#include
//InventoryItem.cpp
using namespace std;
class InventoryItem
{
private:
string description; // The item description
int quantityOnHand; // Number of units on hand
double price;
public:
InventoryItem();
InventoryItem(int, string);
InventoryItem(double);
// Accessor functions
string getDescription() {
return description;
}
int getQuantityOnHand() {
/******************** prevent quantity on hand from being negative ********************************************************/
try {
if (price < 0)
{
void message();
}
}
catch (int price ) {
cout << "QuantityOnHand can not be negative";
}
return quantityOnHand;
}
double getPrice() {
/********************* prevent price from ever being negative **********************************************************/
try {
if (price < 0)
{
void message();
}
}
catch (int price ) {
cout << "Price can not be negative";
}
return price;
}
// Mutator functions
void setDescription(string); // sets description member to argument
void setQuantityOnHand(int); // sets QuantityOnHand to argument
void display();
void readInventoryItem(ifstream& inFile);
void setPrice(double);
};
class InventoryItem:: Exception // definition of nested class
{
public :
public:
Exception(const string& msg) : msg_(msg) {}
~Exception() {}
string getMessage() const {return(msg_);}
private:
string msg_;
void message()
{
throw(Exception("Value should not be negative "));
}
};
#endif
//end of file
//book.h
#define BOOK_H #include "Inventory.h" #include
} void read(ifstream& inFile) { string line; int p; double q; char comma; getline(inFile, line, ','); inFile >> p; inFile >> comma; inFile >> q; inFile >> comma; string line1, line2, line3; getline(inFile, line1, ','); getline(inFile, line2, ','); getline(inFile, line3, ','); setTitle(line1); setAuthor(line2); setPublisher(line3);
}
}
//end of file
data.csv
Book,10,10.00,The Old Man and the Sea,Ernest Hemingway,Benediction Books Book,15,12.00,The Great Gatsby,F. Scott Fitzgerald,Scribner Book,12,8.50,Lord of the Flies,William Golding,Faber and Faber Book,-1,10.50,Lord of the Rings,JRR Tolkein,Test1 Book,5,-1,Harry Potter and the Chamber of Secrets,JK Rowling,Test2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started