Use the code below in c++ to use try and catch to handle exceptions in at least
ID: 3917331 • Letter: U
Question
Use the code below in c++ 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 a NegativePriceException is caught by a client code, your code should generate an error message to that effect and continue. When a NegativeQuantityException is caughtby a client code, the client should generate an error message to that effect and set the QuantityonHand data member to 0.
Submit screenshots of your program execution for each test case and a copy of your code in text file.
*Note: price is not given as a data member in the InventoryItem class (PLEASE ADD PRICE DATA MEMBER)
PLEASE let me know if there is any more information needed! * data.csv in bold - Main.cpp, Data.csv, Inventory.cpp, Book.h were all seperated in different files when I compiled this.
******************************************************My program follows***********************
//Main.cpp
#include
#include
#include
#include
#include
#include "Book.h"
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 bookList;
Book temp;
while (file)
{
temp.read(file);
bookList.push_back(temp);
}
for (int i = 0; i < bookList.size(); ++i)
bookList[i].display();
system("pause");
return 0;
}
//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
//Inventory.h
#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() {
return quantityOnHand;
}
double getPrice() {
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);
};
#endif
//Inventory.cpp
#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()) {
}
}
//Book.h
#define BOOK_H
#include "Inventory.h"
#include
#include
using namespace std;
class Book : InventoryItem {
private:
string Title;
string NameOfAuthor;
string Publisher;
public:
Book() {
Title = "";
NameOfAuthor = " ";
Publisher = " ";
}
Book(string z, int x, double q, string a, string b, string c) {
Title = a;
NameOfAuthor = b;
Publisher = c;
setDescription(z);
setQuantityOnHand(x);
setPrice(q);
}
void setTitle(string a) {
Title = a;
}
void setAuthor(string b) {
NameOfAuthor = b;
}
void setPublisher(string c) {
Publisher = c;
}
string getTitle() {
return Title;
}
string getAuthor() {
return NameOfAuthor;
}
string getPublisher() {
return Publisher;
}
void display() {
cout << "Title:" << Title << endl;
cout << "Author :" << NameOfAuthor << endl;
cout << "Publisher:" << Publisher << endl;
}
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);
}
};
#endif
Explanation / Answer
I create an excepion class in the InventoryItem class which is "Exception" ant it prevent price and quantity on hand from ever being negative.
If user input a negative value then message will show.
I edit this program .
***************************** ************************************************************************************
//Inventory.h
#pragma once
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include
#include
#include
//InventoryItem.cpp
using namespace std;
public class InventoryItem // Public Class
{
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
//Inventory.cpp
#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()) {
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.