Created
June 16, 2016 13:36
-
-
Save boldijar/60e481080ec7d36dcf92402723c2b89a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// invat_pt_scris.cpp : Defines the entry point for the console application. | |
// | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <assert.h> | |
using namespace std; | |
class DuplicatedIDException { | |
private: | |
string msg; | |
public: | |
DuplicatedIDException(string msg) | |
{ | |
this->msg = msg; | |
} | |
string getMessage() | |
{ | |
return this->msg; | |
} | |
}; | |
class Payable | |
{ | |
private: | |
string ID; | |
public: | |
Payable(string id) { | |
this->ID = id; | |
} | |
string getId() { | |
return this->ID; | |
} | |
virtual float monthlyIncome()=0; | |
void toString() { | |
cout << this->ID << " " << monthlyIncome()<<endl; | |
} | |
}; | |
class Student: public Payable { | |
private: | |
float scolarship; | |
public: | |
Student(string id, float scolarship) :Payable(id) | |
{ | |
this->scolarship = scolarship; | |
} | |
float monthlyIncome() { | |
return this->scolarship; | |
} | |
}; | |
class Teacher :public Payable { | |
private: | |
float salary; | |
public: | |
Teacher(string id, float salary) :Payable(id) { | |
this->salary = salary; | |
} | |
float monthlyIncome() { | |
return this->salary; | |
} | |
}; | |
class University { | |
private: | |
string name; | |
vector<Payable> payables; | |
public: | |
University(string name) { | |
this->name = name; | |
} | |
void addPayable(Payable &p1) | |
{ | |
for (Payable &p2 : this->payables) { | |
if (p1.getId().compare(p2.getId()) == 0) | |
{ | |
throw DuplicatedIDException("Already exists!"); | |
} | |
} | |
this->payables.push_back(p1); | |
} | |
Payable &findPayableById(string id) | |
{ | |
for (Payable &p : this->payables) { | |
if (id.compare(p.getId()) == 0) | |
{ | |
return p; | |
} | |
} | |
} | |
vector<Payable> getAllPayables() { | |
return this->payables; | |
} | |
float totalAmountToPay() { | |
float sum=0; | |
for (Payable &p : this->payables) { | |
sum += p.monthlyIncome(); | |
} | |
return sum; | |
} | |
}; | |
int main() { | |
University university("Babes"); | |
university.addPayable(Student("S1", 10)); | |
university.addPayable(Student("S2", 10)); | |
university.addPayable(Student("S3", 10)); | |
assert(university.getAllPayables().size() == 3); | |
try { | |
university.addPayable(Student("S3",20)); | |
assert(false); | |
} | |
catch (DuplicatedIDException ex) | |
{ | |
assert(true); | |
} | |
university.addPayable(Teacher("abc", 20)); | |
for (Payable payable : university.getAllPayables()) | |
{ | |
payable.toString(); | |
} | |
int b; | |
cin >> b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment