Publishing Company

 /* OOP Group A - 3

Q3. Imagine A Publishing Company Which Does Make Marketing For Books And Audio Cassetes Versions. Create A 
Class Publication That Stores Title(type string) And Price(Type float) Of Publications. From This Class
Derive Two Classes : Book Which Adds Pagecount(type int) And Tape Which Adds PlayingTime(type float) In
Minutes. Write A Program That Instantiates The Book And Tape Class, Allows User To Enter Data And Displays
The Data Members. If An Exception Is Caught, Replace All Data Member Values With Zero Value.
*/

#include <iostream>
#include <string>
using namespace std;

class Publication
{
string title;
float price;

public:
Publication(); // Default Constructer
Publication(string title, float price); // Parameterized Constructer
~Publication() {} // Destructer

void getData()
{
cout << "Enter The Title\n";
cin.clear(); // Clearing Previous Input
cin.ignore(); // Discarding Previous Input
getline(cin, title); // Taking UserInput With Spaces
cout << "Enter The Price\n";
cin >> price;

while (!cin.good())
{
cout << "Wrong Input, Please Enter Integers Or Floats Only!!\n";
cin.clear(); // Clearing Previous Input
cin.ignore(100, '\n'); // Discarding Previous Input
cout << "\nEnter The Price\n";
cin >> price;
}
}

int printData()
{
bool flag = true;
try
{
if (title.length() < 2)
{
flag = false;
throw title;
}
}

catch (string)
{
cout << "\nThe Length Of Title Should be Greater Than 2 Characters\n";
title = "No Title";
}

cout << "Title : " << title << endl
<< endl;

try
{
if (price < 0.0)
{
flag = false;
throw price;
}
}

catch (float)
{
cout << "The Price Should Be Greater Than 0\n";
price = 0.0;
}

cout << "Price : " << price << "/-" << endl
<< endl;
return flag;
}
};

// Publication Class Default Constructer Definition
Publication ::Publication()
{
// title = "No Title";
price = 0.0;
}

// Publication Class Parameterized Constructer Definition
Publication ::Publication(string title, float price)
{
this->title = title;
this->price = price;
}

class Book : public Publication
{
int pageCount;

public:
Book()
{
pageCount = 0;
}

Book(int pageCount, string title, float price) : Publication(title, price) // Constructer
{
this->pageCount = pageCount;
}

void getData()
{
Publication ::getData();
cout << "Enter The Number Of Pages\n";
cin >> pageCount;
while (!cin.good())
{
cout << "Wrong Input, Please Enter Integers Only!!\n";
cin.clear(); // Clearing Previous Input
cin.ignore(100, '\n'); // Discarding Previous Input
cout << "\nEnter The Number Of Pages\n";
cin >> pageCount;
}
}

int printData()
{
bool flag;
flag = Publication ::printData();
try
{
if (pageCount < 0)
{
flag = false;
throw pageCount;
}
}

catch (int)
{
cout << "The Number Of Pages Should Be Greater Than 0\n";
pageCount = 0;
}

cout << "No. Of Pages : " << pageCount << endl
<< endl;
return flag;
}
};

class Tape : public Publication
{
float playingTime;

public:
Tape()
{
playingTime = 0.0;
}

Tape(float playingTime, string title, float price) : Publication(title, price) // Constructer
{
this->playingTime = playingTime;
}

void getData()
{
Publication ::getData();
cout << "Enter The Playing Time In Minutes\n";
cin >> playingTime;
while (!cin.good())
{
cout << "Wrong Input, Please Enter Integers Only!!\n";
cin.clear(); // Clearing Previous Input
cin.ignore(100, '\n'); // Discarding Previous Input
cout << "\nEnter The Playing Time In Minutes\n";
cin >> playingTime;
}
}

int printData()
{
bool flag;
flag = Publication ::printData();
try
{
if (playingTime < 0)
{
flag = false;
throw playingTime;
}
}

catch (float)
{
cout << "The Playing Time Should Be Greater Than 0 Minutes\n";
playingTime = 0.0;
}

cout << "Playing Time : " << playingTime << " Minutes" << endl
<< endl;
return flag;
}
};

int main()
{
int bookNo, tapeNo;
bool flag;
char userInput;

cout << "Enter The Number Of Books\n";
cin >> bookNo;

while (!cin.good() >> bookNo)
{
cout << "Wrong Input, Please Enter Integers Only!!\n";
cin.clear(); // Clearing Previous Input
cin.ignore(100, '\n'); // Discarding Previous Input
cout << "\nEnter The Number Of Books\n";
cin >> bookNo;
}

cout << "\nEnter The Number Of Tapes\n";
cin >> tapeNo;

while (!cin.good() >> tapeNo)
{
cout << "Wrong Input, Please Enter Integers Only!!\n";
cin.clear(); // Clearing Previous Input
cin.ignore(100, '\n'); // Discarding Previous Input
cout << "\nEnter The Number Of Tapes\n";
cin >> tapeNo;
}

Book *bk = NULL;
bk = new (nothrow) Book[bookNo];

if (!bk)
{
cout << "Memory Allocation Failed\n";
}

else
{
for (int i = 0; i < bookNo; i++)
{
cout << "\n***************BOOK" << i + 1 << "***************\n";
bk->getData();
cout << "\n***************BOOK" << i + 1 << "***************\n";
flag = bk->printData();

if (!flag)
{
cout << "Do You Want To Try It Again (y / n)\n";
cin >> userInput;

if (userInput == 'y')
{
bk->getData();
bk->printData();
}

else if (userInput == 'n')
{
continue;
}

else
{
cout << "Do You Want To Try It Again (y / n)\n";
cin >> userInput;
if (userInput == 'y')
{
bk->getData();
bk->printData();
}

else if (userInput == 'n')
{
continue;
}
}
}
}
}

Tape *tp = NULL;
tp = new (nothrow) Tape[tapeNo];

if (!tp)
{
cout << "Memory Allocation Failed\n";
}

else
{
for (int i = 0; i < tapeNo; i++)
{
cout << "\n***************TAPE" << i + 1 << "***************\n";
tp->getData();
cout << "\n***************TAPE" << i + 1 << "***************\n";
flag = tp->printData();

if (!flag)
{
cout << "Do You Want To Try It Again (y / n)\n";
cin >> userInput;

if (userInput == 'y')
{
tp->getData();
tp->printData();
}

else if (userInput == 'n')
{
continue;
}

else
{
cout << "Do You Want To Try It Again (y / n)\n";
cin >> userInput;
if (userInput == 'y')
{
tp->getData();
tp->printData();
}

else if (userInput == 'n')
{
continue;
}
}
}
}
}

delete[] bk;
delete[] tp;

return 0;
}

Comments

Popular posts from this blog

Ticket Booking System

Student Database

Generalised Linked List