CSI 250L Lab 11 Solution, Spring 2012
American University of Science and Technology · Programming II Lab · 6 pages · Dr. Saeed Raheel · with solution
More CSI 250L files
- Lab 1 Solution · Spring 2012Lab · with solutionView
- Lab 1 · Spring 2012LabView
- Lab 10 Solution · Spring 2012Lab · with solutionView
- Lab 10 · Spring 2012LabView
- Lab 11 · Spring 2012LabView
- Lab 12 Solution · Spring 2012Lab · with solutionView
- Lab 12 · Spring 2012LabView
- Lab 13 Solution · Spring 2012Lab · with solutionView
Show the text of this lab
// Pizza.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Pizza
{
private:
int type;
string size;
public:
Pizza( int t = 1, string s = "small" )
{
setType( t );
setSize( s );
}
void setType( int t )
{
if( t == 1 || t == 2 || t == 3)
type = t;
else
type = 1;
}
void setSize( string s )
{
if( size == "small" || size == "medium" || size == "large" )
size = s;
else
size = "small";
}
int getType()
{
return type;
}
string getSize()
{
return size;
}
void print()
{
cout << "Type: " << type << "\nSize: " << size << endl;
}
int computePrice()
{
int price;
if( type == 1 )
price = 8;
else if( type == 2 )
price = 12;
else
price = 15;
if( size == "small" )
price += 2;
else if( size == "medium" )
price += 4;
else
price += 6;
return price;
}
};
void main()
{
int N, type, totalPrice = 0;
string size;
cout << "Enter the number of pizzas: ";
cin >> N;
Pizza * p = new Pizza[N];
for( int i = 0; i < N; i++ )
{
cout << "Enter the type of Pizza: ";
cin >> type;
cout << "Enter the size of Pizza: ";
cin >> size;
p[i].setType( type );
p[i].setSize( size );
totalPrice += p[i].computePrice();
}
cout << "Total Price = " << totalPrice << endl;
ofstream myFile( "Pizzas.txt", ios::out );
if( !myFile )
{
cerr << "File could not be opened" << endl;
exit( 1 );
}
for( int i = 0; i < N; i++ )
myFile << p[i].getType() << " " << p[i].getSize() << endl;
system( "pause" );
}
// LabWork11_P2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Bill
{
protected:
int billNum;
string date;
public:
Bill()
{
billNum = 1;
date = "1/1/2012";
}
Bill( int b, string d )
{
billNum = b;
date = d;
}
virtual double billSum() = 0;
void print()
{
cout << "Bill Number: " << billNum << "\nDate: " << date << endl;
}
};
class CustomerBill:public Bill
{
private:
string customer;
double charge;
int type;
public:
CustomerBill()
:Bill()
{
customer = "XYZ";
charge = 0.0;
type = 1;
}
CustomerBill( int b, string d, string c, double ch, int t)
:Bill(b, d)
{
customer = c;
charge = ch;
if( t == 1 || t == 2 )
type = t;
else
type = 1;
}
double billSum()
{
if( type == 1 )
return charge * 0.93;
else
return charge;
}
void print()
{
Bill::print();
cout << "Customer: " << customer << "\nCharge: " << charge << "Type:
" << type << endl;
}
~CustomerBill()
{
cout << "Destructor for customer: " << customer << "is running" <<
endl;
}
};
class SupplierBill:public Bill
{
private:
string supplier;
double charge;
bool local;
public:
SupplierBill()
:Bill()
{
supplier = "MNP";
charge = 0.0;
local = true;
}
SupplierBill( int b, string d, string s, double c, bool l)
:Bill(b, d)
{
supplier = s;
charge = c;
local = l;
}
double billSum()
{
if( local )
return charge;
else
return charge * 1.05;
}
void print()
{
Bill::print();
cout << "Supplier: " << supplier << "\nCharge: " << charge <<
"Local: " << local << endl;
}
~SupplierBill()
{
cout << "Destructor for supplier: " << supplier << "is running" <<
endl;
}
};
void main()
{
int N, billNum, type, choice;
string date, customer, supplier;
double charge;
bool local;
char isLocal;
cout << "Enter the number of bills: ";
cin >> N;
vector <Bill *> b(N);
for( int i = 0; i < N; i++ )
{
cout << "Enter the number of the bill: ";
cin >> billNum;
cout << "Enter the date of the bill: ";
cin >> date;
cout << "Enter your choice: 1 for CustomerBill & 2 for SupplierBill:
";
cin >> choice;
if( choice == 1 )
{
cout << "Enter the name of the customer: ";
cin >> customer;
cout << "Enter the value of charge: ";
cin >> charge;
cout << "Enter the type of the customer: ";
cin >> type;
b[i] = new CustomerBill( billNum, date, customer, charge, type
);
}
else if( choice == 2 )
{
cout << "Enter the name of the supplier: ";
cin >> supplier;
cout << "Enter the value of charge: ";
cin >> charge;
cout << "Is the supplier a local one (Y/N): ";
cin >> isLocal;
if( isLocal == 'Y' )
local = true;
else
local = false;
b[i] = new SupplierBill( billNum, date, supplier, charge,
local );
}
else
cout << "Invalid choice." << endl;
}
for( int i = 0; i < N; i++)
{
CustomerBill *ptr = dynamic_cast <CustomerBill *>(b[i]);
if( ptr != 0 )
if( b[i]->billSum() > 2000 )
ptr->print();
}
system( "pause" );
}