Advertisement
add1ctus

6. Phone donations

May 15th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class MyException {
  8. protected:
  9.     char message[50];
  10. public:
  11.     MyException(){}
  12.     MyException(char *msg)
  13.     {
  14.         strcpy(message,msg);
  15.     }
  16.     void print()
  17.     {
  18.         cout<<message<<endl;
  19.     }
  20. };
  21.  
  22.  
  23. class DonationNumber {
  24.     static float balance;
  25.     static int donations;
  26.     char phoneNumber[15];
  27. public:
  28.     DonationNumber()
  29.     {
  30.         strcpy(phoneNumber,"");
  31.     }
  32.     DonationNumber(char *nr)
  33.     {
  34.         strcpy(phoneNumber,nr);
  35.     }
  36.     void setPhoneNumber(char *nr)
  37.     {
  38.         strcpy(phoneNumber,nr);
  39.     }
  40.     void acceptDonation(float amount)
  41.     {
  42.         if(amount>=0)
  43.         {
  44.             balance+=amount;
  45.             donations++;
  46.         }
  47.         else
  48.             throw MyException("Exception: Invalid donation amount");
  49.     }
  50.     static void state()
  51.     {
  52.         cout<<"Balance: "<<balance<<", number of donations: "<<donations<<endl;
  53.     }
  54. };
  55.  
  56.  
  57. class User{
  58.     char phoneNumber[15];
  59.     float balance;
  60. public:
  61.     User()
  62.     {
  63.         strcpy(phoneNumber,"");
  64.         balance=0;
  65.     }
  66.     User(char *number, float b)
  67.     {
  68.         strcpy(phoneNumber,number);
  69.         balance=b;
  70.     }
  71.     void setPhoneNumber(char *number)
  72.     {
  73.         strcpy(phoneNumber,number);
  74.     }
  75.     void setBalance(float b)
  76.     {
  77.         balance=b;
  78.     }
  79.     void donate(DonationNumber &nr, float amount)
  80.     {
  81.         try
  82.         {
  83.             if(balance>=amount)
  84.             {
  85.                 balance-=amount;
  86.                 nr.acceptDonation(amount);
  87.                 cout<<"Donated "<<amount<<" denars!"<<endl;
  88.             }
  89.             else
  90.                 throw MyException("Not enough credit for donation!");
  91.         }
  92.         catch(MyException exc)
  93.         {
  94.             exc.print();
  95.         }
  96.     }
  97. };
  98.  
  99.  
  100. /*
  101.  
  102. Your code here!!!
  103.  
  104. */
  105.  
  106. float DonationNumber::balance=0;
  107. int DonationNumber::donations=0;
  108.  
  109. int main(){
  110.     int d, k, t;
  111.     char tb[15];
  112.  
  113.     float s, b;
  114.  
  115.     DonationNumber broevi[3];
  116.     User korisnici[10];
  117.  
  118.     cin>>d;
  119.  
  120.     for (int i=0; i<d; i++){
  121.         cin>>tb;
  122.         broevi[i].setPhoneNumber(tb);
  123.     }
  124.     cin>>d;
  125.     for (int i=0; i<d; i++){
  126.         cin>>tb;
  127.         cin>>b;
  128.         korisnici[i].setPhoneNumber(tb);
  129.         korisnici[i].setBalance(b);
  130.     }
  131.  
  132.     while (true){
  133.         cin>>t;
  134.         if (t==-1){
  135.             break;
  136.         }
  137.         cin>>k;
  138.         cin>>s;
  139.         korisnici[k].donate(broevi[k], s);
  140.         DonationNumber::state();
  141.     }
  142.  
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement