CSI 250L Lab 14 Solution, Spring 2012
American University of Science and Technology · Programming II Lab · 31 pages · Dr. Saeed Raheel · with solution
Showing the first 10 of 31 pages. Download the PDF to see all of it.
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 Solution · Spring 2012Lab · with solutionView
- Lab 11 · Spring 2012LabView
- Lab 12 Solution · Spring 2012Lab · with solutionView
- Lab 12 · Spring 2012LabView
Show the text of this lab
#include <string>
using std::string;
#include "Account.h"
ostream &operator<<(ostream &output, const Account &a)
{
output << "ID : " << a.getID() << ", First Name : " << a.getFName() << ",
Last Name : " << a.getLName() << ", Balance : " << a.getBalance() << endl;
return output;
}
istream &operator>>(istream &input, Account &a)
{
input >> a.ID;
input >> a.FName;
input >> a.LName;
input >> a.balance;
return input;
}
Account::Account( string id, string ln, string fn, double b )
{
setID( id );
setLName( ln );
setFName( fn );
setBalance( b );
}
string Account::getID() const
{
return ID;
}
void Account::setID( string id )
{
ID = id;
}
string Account::getLName() const
{
return LName;
}
void Account::setLName( string ln )
{
LName = ln;
}
string Account::getFName() const
{
return FName;
}
void Account::setFName( string fn )
{
FName = fn;
}
double Account::getBalance() const
{
return balance;
}
void Account::setBalance( double b )
{
if (b>0.0)
balance = b;
else
balance = 0.0;
}
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <string>
#include <iostream>
using namespace std;
using std::string;
class Account
{
friend ostream &operator<<(ostream &, const Account &);
friend istream &operator>>(istream &, Account &);
public:
Account( string = "0", string = "", string = "", double = 0.0 );
void setID( string );
string getID() const;
void setLName( string );
string getLName() const;
void setFName( string );
string getFName() const;
void setBalance( double );
double getBalance() const;
private:
string ID;
string LName;
string FName;
double balance;
};
#endif
#include<iostream.h>
#include<string.h>
#include"Customer.h"
#include"Bill.h"
Customer::Customer(int num,char* dt,char*n,float ch,int t):Bill(num,dt){
name=new char[strlen(n)+1];
strcpy(name,n);
charge=ch;
if(t!=1&&t!=2){
cout<<"Invalid type set to 1."<<endl;
type=1;
}
else type=t;
}
Customer::~Customer(){
delete[]name;
name=0;
}
float Customer::billsum(){
if(type==1)
return charge-0.07f*charge;
else return charge;
}
void Customer::print(){
Bill::print();
cout<<"Customer name:"<<name
<<"\nCustomer type:"<<type
<<"\nCharge:"<<billsum()<<endl;
}
#include"Bill.h"
#ifndef CUSTOMER_H
#define CUSTOMER_H
class Customer:public Bill{
private:
char* name;
float charge;
int type;
public:
Customer(int=0,char[]="",char[]="",float=0.0,int=1);
~Customer();
float billsum();
void print();
};
#endif
#include<iostream>
#include<string>
#include"Customer_Invoice.h"
using namespace std;
Customer_Invoice::Customer_Invoice(int num, char* dt, char*n, float ch, int
t):Invoice(num,dt){
name = new char[strlen(n)+1];
strcpy(name,n);
Ordered_Items = ch;
if(t!=1&&t!=2){
cout<<"Invalid type set to 1."<<endl;
type=1;
}
else type=t;
}
Customer_Invoice::~Customer_Invoice(){
delete[]name;
name=0;
}
float Customer_Invoice::Invoice_Total(){
if(type==1)
return Ordered_Items-0.07f*Ordered_Items;
else return Ordered_Items;
}
void Customer_Invoice::Print(){
Invoice::print();
cout<<"Customer name:"<<name
<<"\nCustomer type:"<<type
<<"\nCharge:"<<Invoice_Total()<<endl;
}
#include"Invoice.h"
#ifndef CUSTOMER_H
#define CUSTOMER_H
class Customer_Invoice:public Invoice
{
private:
char* name;
float Ordered_Items;
int type;
public:
Customer_Invoice(int=0,char[]="",char[]="",float=0.0,int=1);
~Customer_Invoice();
float Invoice_Total();
void Print();
};
#endif
#include<fstream.h>
#include<stdlib.h>
#include"Supplier.h"
#include"Customer.h"
int main(){
int n,c,bn,tp;
float ch;
char nm[20],dt[20];
cout<<"How many Bills you have?"<<endl;
cin>>n;
Bill **B=new Bill*[n];
for(int i=0;i<n;i++){
cout<<"enter the type of the bill:\n 1-Customer.\t2-
Supplier."<<endl;
cin>>c;
if(c==1){
cout<<"Enter the Customer specification:"
<<"\n(bill num,date,customer name,charge,type)";
cin>>bn>>dt>>nm>>ch>>tp;
B[i]=new Customer(bn,dt,nm,ch,tp);
}
else{
cout<<"Enter the Supplier specification:"
<<"\n(bill num,date,supplier name,charge,1-local or 2-
nonlocal)";
cin>>bn>>dt>>nm>>ch>>tp;
B[i]=new Supplier(bn,dt,nm,ch,tp);
}
}
fstream out("Bill.txt",ios::out);
if(!out){
cerr<<"Cannot find file!";
exit(1);
}
for(int j=0;j<n;j++)
out<<B[j]->getnum()<<" "<<B[j]->billsum()<<endl;
return 0;
}
#include<iostream>
#include"Invoice.h"
using namespace std;
int Invoice::count = 0;
Invoice::Invoice(int n, char* dt):num(n)
{
setDate(dt);
++count;
}
Invoice::~Invoice()
{
delete[] Date;
Date=0;
--count;
}
void Invoice::print()
{
cout << "Invoice : " << "\nNumber : " << getnum() << "\tDate : " <<
getDate() << endl;
}
int Invoice::getnum(){
return num;
}
void Invoice::setDate(char* dt)
{
Date = new char[strlen(dt)+1];
strcpy(Date, dt);
}
char *Invoice::getDate()
{
return Date;
}
int Invoice::getCount()
{
return count;
}
#ifndef BILL_H
#define BILL_H
class Invoice{
private:
const int num;
char* Date;
static int count;
public:
Invoice(int=0,char[]="");
~Invoice();
int getnum();
void setnum(int);
void setDate(char*);
char* getDate();
virtual float Invoice_Total()=0;
void print();
static int getCount();
};
#endif
#include <fstream>
#include <iostream>
#include "Supplier_Invoice.h"
#include "Customer_Invoice.h"
using namespace std;
int main(){
int n, c, in, tp;
float oi, si;
char *nm="";
char *dt="";
cout << "How many Invoices would you like to create?"<<endl;
cin >> n;
Invoice **I = new Invoice*[n];
for(int i=0;i<n;i++){
cout << "Enter the type of the Invoice:\n 1-Customer.\t2-
Supplier."<<endl;
cin >> c;
if(c==1){
cout<<"Enter the Customer's specification:"
<<"\n(Invoice num, Date, Customer name, Ordered Items,
Type)";
cin >> in >> dt >> nm >> oi >> tp;
I[i] = new Customer_Invoice(in, dt, nm, oi, tp);
}
else{
cout<<"Enter the Supplier's specification:"
<<"\n(Invoice num,date,supplier name,charge,1-local or
2-nonlocal)";
cin >> in >> dt >> nm >> si >> tp;
I[i] = new Supplier_Invoice(in, dt, nm, si, tp);
}
}
fstream out("Invoice.dat", ios::out);
for(int j=0;j<n;j++)
out << I[j]->getnum() << " " << I[j]->Invoice_Total() << endl;
return 0;
}
#include<iostream.h>
#include<string.h>
#include"Bill.h"
#include"Supplier.h"
Supplier::Supplier(int num,char*dt,char*nm,float ch,int n):Bill(num,dt){
supname=new char[strlen(nm)+1];
strcpy(supname,nm);
charge=ch;
if(n==1)
local=true;
else local=false;
}
Supplier::~Supplier(){
delete[]supname;
supname=0;
}
float Supplier::billsum(){
if(local)
return charge-0.05f*charge;
else return charge;
}
void Supplier::print(){
Bill::print();
cout<<"Supplier name:"<<supname
<<"\nCharge:"<<charge<<endl;
if(local==true)
cout<<"The supplier is local."<<endl;
else cout<<"The supplier is nonlocal."<<endl;
}
void Supplier::islocal(int n){
if(n==1)
local=true;
else local=false;
}
#include"Bill.h"
#ifndef SUPPLIER_H
#define SUPPLIER_H
class Supplier:public Bill{
private:
char*supname;
float charge;
bool local;
int type;
public:
Supplier::Supplier(int=0,char[]="",char[]="",float=0.0,int=1);
~Supplier();
float billsum();
void print();
void islocal(int);
};
#endif
#include<iostream>
#include<string>
#include"Supplier_Invoice.h"
using namespace std;
Supplier_Invoice::Supplier_Invoice(int num,char*dt,char*nm,float ch,int
n):Invoice(num,dt){
supname=new char[strlen(nm)+1];
strcpy(supname,nm);
Supplied_Items=ch;
if(n==1)
local=true;
else
local=false;
}
Supplier_Invoice::~Supplier_Invoice(){
delete[]supname;
supname=0;
}
float Supplier_Invoice::Invoice_Total(){
if(local)
return Supplied_Items-0.05f*Supplied_Items;
else
return Supplied_Items;
}
void Supplier_Invoice::Print()
{
Invoice::print();
cout<<"Supplier name:"<<supname
<<"\nSupplied Items:"<<Supplied_Items<<endl;
if(local==true)
cout<<"The supplier is local."<<endl;
else cout<<"The supplier is nonlocal."<<endl;
}
void Supplier_Invoice::islocal(int n){
if(n==1)
local=true;
else local=false;
}
#include"Invoice.h"
#ifndef SUPPLIER_H
#define SUPPLIER_H
class Supplier_Invoice:public Invoice{
private:
char* supname;
float Supplied_Items;
bool local;
public:
Supplier_Invoice::Supplier_Invoice(int=0,char[]="",char[]="",float=0.0,int=1);
~Supplier_Invoice();
float Invoice_Total();
void Print();
void islocal(int);
};
#endif