Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class MyException {
- protected:
- char message[50];
- public:
- MyException(){}
- MyException(char *msg)
- {
- strcpy(message,msg);
- }
- void print()
- {
- cout<<message<<endl;
- }
- };
- class DonationNumber {
- static float balance;
- static int donations;
- char phoneNumber[15];
- public:
- DonationNumber()
- {
- strcpy(phoneNumber,"");
- }
- DonationNumber(char *nr)
- {
- strcpy(phoneNumber,nr);
- }
- void setPhoneNumber(char *nr)
- {
- strcpy(phoneNumber,nr);
- }
- void acceptDonation(float amount)
- {
- if(amount>=0)
- {
- balance+=amount;
- donations++;
- }
- else
- throw MyException("Exception: Invalid donation amount");
- }
- static void state()
- {
- cout<<"Balance: "<<balance<<", number of donations: "<<donations<<endl;
- }
- };
- class User{
- char phoneNumber[15];
- float balance;
- public:
- User()
- {
- strcpy(phoneNumber,"");
- balance=0;
- }
- User(char *number, float b)
- {
- strcpy(phoneNumber,number);
- balance=b;
- }
- void setPhoneNumber(char *number)
- {
- strcpy(phoneNumber,number);
- }
- void setBalance(float b)
- {
- balance=b;
- }
- void donate(DonationNumber &nr, float amount)
- {
- try
- {
- if(balance>=amount)
- {
- balance-=amount;
- nr.acceptDonation(amount);
- cout<<"Donated "<<amount<<" denars!"<<endl;
- }
- else
- throw MyException("Not enough credit for donation!");
- }
- catch(MyException exc)
- {
- exc.print();
- }
- }
- };
- /*
- Your code here!!!
- */
- float DonationNumber::balance=0;
- int DonationNumber::donations=0;
- int main(){
- int d, k, t;
- char tb[15];
- float s, b;
- DonationNumber broevi[3];
- User korisnici[10];
- cin>>d;
- for (int i=0; i<d; i++){
- cin>>tb;
- broevi[i].setPhoneNumber(tb);
- }
- cin>>d;
- for (int i=0; i<d; i++){
- cin>>tb;
- cin>>b;
- korisnici[i].setPhoneNumber(tb);
- korisnici[i].setBalance(b);
- }
- while (true){
- cin>>t;
- if (t==-1){
- break;
- }
- cin>>k;
- cin>>s;
- korisnici[k].donate(broevi[k], s);
- DonationNumber::state();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement