Advertisement
196040

OOP ispitni zadaci Zadaca 2

May 30th, 2020
2,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.33 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. #define MAX 100
  5. enum typeC{standard=0, loyal=1, vip=2};
  6. class UserExistsException{
  7.     private:
  8.     char msg[100];
  9.     public:
  10.     UserExistsException(char * msg = "") {
  11.         strcpy(this->msg, msg);
  12.     }
  13.     void showMessage() {
  14.         cout<<msg;
  15.     }
  16. };
  17. class Customer { //Да се креира класа Customer за опишување на купувачи на една книжара.
  18. private://За секој купувач се чуваат податоци за:
  19. char ime[50]; //името (низа од 50 знаци),
  20. char email[50]; //електронска адреса (низа од 50 знаци),
  21. typeC vid; //вид на купувач (стандардни, лојални или вип),
  22. static int osnoven; //основен попуст (цел број изразен во проценти),
  23. const static int dopolnitelen; //дополнителен попуст (цел број изразен во проценти) и
  24. int kupeni; //број на купени производи. (5 поени)
  25. public:
  26. Customer(char * ime = "", char * email = "", typeC vid = (typeC)0, int kupeni = 0) {
  27.     strcpy(this->ime, ime);
  28.     strcpy(this->email, email);
  29.     this->vid = vid;
  30.     this->osnoven = osnoven;
  31.     this->kupeni = kupeni;
  32. }
  33. Customer(const Customer &copy) {
  34.     strcpy(this->ime, copy.ime);
  35.     strcpy(this->email, copy.email);
  36.     this->vid = copy.vid;
  37.     this->osnoven = copy.osnoven;
  38.     this->kupeni = copy.kupeni;
  39. }
  40. Customer &operator=(const Customer &copy)
  41.  {
  42.      if(this!=&copy) {
  43.          strcpy(this->ime, copy.ime);
  44.     strcpy(this->email, copy.email);
  45.     this->vid = copy.vid;
  46.     this->osnoven = copy.osnoven;
  47.     this->kupeni = copy.kupeni;
  48.      }
  49.      return *this;
  50.  }
  51.  int getkupeni() {
  52.      return kupeni;
  53.  }
  54.  char * getemail() {
  55.      return email;
  56.  }
  57.  typeC gettip() {
  58.      return vid;
  59.  }
  60.  void settip(typeC vid) {
  61.      this->vid = vid;
  62.  }
  63.  static void setDiscount1(int newosnoven){
  64.     osnoven = newosnoven;
  65.  }
  66. int popust() { //Сите лојални купувачи со покажување на клуб картичка имаат право на основниот попуст при
  67. if(vid==loyal) //купување од книжарата. Основниот попуст е ист за сите лојални купувачи и изнесува 10%.
  68. return osnoven;
  69.     if(vid==vip) //Оваа вредност може да се смени со одлука на раководството на книжарата. Дополнителниот
  70.     return osnoven+dopolnitelen;
  71.     else return 0; //попуст е фиксен и може да го користат само вип купувачите и изнесува 20%. Стандардните
  72.    } //  купувачи немаат право на попуст (5 поени).
  73. friend ostream &operator<<(ostream &o, Customer &print) { //За оваа класа да се имплементира оператор <<
  74. //за печатење на купувач во формат:
  75. o<<print.ime<<endl;//[ime_na_kupuvac]
  76. o<<print.email<<endl;//[email_na_kupuvac]
  77. o<<print.kupeni<<endl;//[broj_na_proizvodi]
  78. if(print.vid==standard) o<<"standard ";//[vid_na_kupuvac]
  79. else if(print.vid==loyal) o<<"loyal ";//[popust_sto_moze_da_go_koristi]
  80. else o<<"vip ";
  81. o<<print.popust()<<endl;
  82. return o;
  83. }
  84. };
  85. int Customer::osnoven = 10;
  86. const int Customer::dopolnitelen = 20;
  87. class FINKI_bookstore{ //Да се креира класа за онлајн книжара FINKI-bookstore
  88. private: //во која се чува
  89. Customer * niza; //низа од регистрирани купувачи (динамички алоцирана низа)
  90. int broj; //и број на купувачи. (5 поени)
  91. public://За класата да се обезбедат:
  92. FINKI_bookstore(Customer * niza = NULL, int broj = 0) {
  93.     this->broj = broj;
  94.     this->niza = new Customer[broj];
  95.     for(int i=0; i<broj; i++)
  96.     this->niza[i] = niza[i];
  97.     }
  98.     FINKI_bookstore(const FINKI_bookstore &copy) {
  99.         this->broj = copy.broj;
  100.     this->niza = new Customer[copy.broj];
  101.     for(int i=0; i<copy.broj; i++)
  102.     this->niza[i] = copy.niza[i];
  103.     }
  104.     FINKI_bookstore &operator=(const FINKI_bookstore &copy) {
  105.         if(this!=&copy) {
  106.               this->broj = copy.broj;
  107.     this->niza = new Customer[copy.broj];
  108.     for(int i=0; i<copy.broj; i++)
  109.     this->niza[i] = copy.niza[i];
  110.         }
  111.         return *this;
  112.     }
  113.     void setCustomers(Customer * niza, int broj) {
  114.          this->broj = broj;
  115.     this->niza = new Customer[broj];
  116.     for(int i=0; i<broj; i++)
  117.     this->niza[i] = niza[i];
  118.     }
  119. FINKI_bookstore &operator+=(Customer &add) { //operator+= (10 поени) за додавање купувач во
  120. int flag=0;//листата од купувачи, но само ако веќе не е дел од неа (ако во листата нема купувач со иста електронска адреса).
  121. for(int i=0;i<broj;i++)
  122. if(strcmp(niza[i].getemail(), add.getemail())==0)
  123. flag=1;// Ако во листата постои корисник со иста електронска адреса, треба да се генерира исклучок UserExistsException.
  124. if(flag==1) // Потребно е да се обезбеди справување со исклучокот во функцијата main на означеното место.
  125. throw UserExistsException("The user already exists in the list!\n");//Bо ваква ситуација се печати порака "The user already exists in the list" (5 поени).
  126. if(flag==0)
  127.  {
  128.      Customer * tmp = new Customer[broj+1];
  129.      for(int i=0;i<broj;i++)
  130.      tmp[i] = niza[i];
  131.      tmp[broj++] = add;
  132.      delete [] niza;
  133.      niza = tmp;
  134.  }
  135.  return *this;
  136. }
  137.    void update() { //Функција update со која
  138.        for(int i=0; i<broj;i++) { //а сите лојални купувачи со над 10 купени производи, стануваат вип (5 поени).
  139.     if(niza[i].getkupeni() > 5 && niza[i].gettip() == standard) // сите стандардни купувачи со купени повеќе
  140.    niza[i].settip(loyal);//од 5 производи стануваат лојални,
  141.    else if(niza[i].getkupeni() > 10 && niza[i].gettip() == loyal)
  142.    niza[i].settip(vip);
  143.        }
  144.     }
  145.    friend ostream &operator<<(ostream &o, FINKI_bookstore &print) {// Оператор << за печатење на информациите за сите
  146.    for(int i=0;i<print.broj;i++)
  147.    o<<print.niza[i];// регистрирани купувачи (5 поени).
  148.    return o;
  149.    }
  150. };
  151. int main() {
  152.   int testCase;
  153.   cin >> testCase;
  154.   char name[MAX];
  155.   char email[MAX];
  156.   int tC;
  157.   int discount;
  158.   int numProducts;
  159.   if (testCase == 1){
  160.     cout << "===== Test Case - Customer Class ======" << endl;
  161.     cin.get();
  162.     cin.getline(name, MAX);
  163.     cin.getline(email, MAX);
  164.     cin >> tC;
  165.     cin >> numProducts;
  166.     cout << "===== CONSTRUCTOR ======" << endl;
  167.     Customer c(name, email, (typeC) tC, numProducts);
  168.     cout << c;
  169.   }
  170.   if (testCase == 2){
  171.     cout << "===== Test Case - Static Members ======" << endl;
  172.     cin.get();
  173.     cin.getline(name,MAX);
  174.     cin.getline(email,MAX);
  175.     cin >> tC;
  176.     cin >> numProducts;
  177.     cout << "===== CONSTRUCTOR ======" << endl;
  178.     Customer c(name, email, (typeC) tC, numProducts);
  179.     cout << c;
  180.     c.setDiscount1(5);
  181.     cout << c;
  182.   }
  183.   if (testCase == 3){
  184.     cout << "===== Test Case - FINKI-bookstore ======" << endl;
  185.     FINKI_bookstore fc;
  186.     int n;
  187.     cin >> n;
  188.     Customer customers[MAX];
  189.     for(int i = 0; i < n; ++i) {
  190.       cin.get();
  191.       cin.getline(name,MAX);
  192.       cin.getline(email,MAX);
  193.       cin >> tC;
  194.       cin >> numProducts;
  195.       Customer c(name, email, (typeC) tC, numProducts);
  196.       customers[i] = c;
  197.     }
  198.     fc.setCustomers(customers, n);
  199.     cout << fc <<endl;
  200.   }
  201.  
  202.   if (testCase == 4){
  203.     cout << "===== Test Case - operator+= ======" << endl;
  204.     FINKI_bookstore fc;
  205.     int n;
  206.     cin >> n;
  207.     Customer customers[MAX];
  208.     for(int i = 0; i < n; ++i) {
  209.       cin.get();
  210.       cin.getline(name,MAX);
  211.       cin.getline(email,MAX);
  212.       cin >> tC;
  213.       cin >> numProducts;
  214.       Customer c(name, email, (typeC) tC, numProducts);
  215.       customers[i] = c;
  216.     }
  217.  
  218.     fc.setCustomers(customers, n);
  219.     cout << "OPERATOR +=" << endl;
  220.     cin.get();
  221.     cin.getline(name,MAX);
  222.     cin.getline(email,MAX);
  223.     cin >> tC;
  224.     cin >> numProducts;
  225.     Customer c(name, email, (typeC) tC, numProducts);
  226.     fc+=c;
  227.     cout << fc;
  228.   }
  229.  
  230.   if (testCase == 5){
  231.     cout << "===== Test Case - operator+= (exception) ======" << endl;
  232.     FINKI_bookstore fc;
  233.     int n;
  234.     cin >> n;
  235.     Customer customers[MAX];
  236.     for(int i = 0; i < n; ++i) {
  237.       cin.get();
  238.       cin.getline(name,MAX);
  239.       cin.getline(email,MAX);
  240.       cin >> tC;
  241.       cin >> numProducts;
  242.       Customer c(name, email, (typeC) tC, numProducts);
  243.       customers[i] = c;
  244.         }
  245.     fc.setCustomers(customers, n);
  246.     cout << "OPERATOR +=" << endl;
  247.     cin.get();
  248.     cin.getline(name,MAX);
  249.     cin.getline(email,MAX);
  250.     cin >> tC;
  251.     cin >> numProducts;
  252.           Customer c(name, email, (typeC) tC, numProducts);
  253.       try {
  254.     fc+=c;
  255.       }
  256.          catch (UserExistsException &ex) {
  257.       ex.showMessage();
  258.       }
  259.     cout << fc;
  260.   }
  261.   if (testCase == 6){
  262.     cout << "===== Test Case - update method  ======" << endl << endl;
  263.     FINKI_bookstore fc;
  264.     int n;
  265.     cin >> n;
  266.     Customer customers[MAX];
  267.     for(int i = 0; i < n; ++i) {
  268.       cin.get();
  269.       cin.getline(name,MAX);
  270.       cin.getline(email,MAX);
  271.       cin >> tC;
  272.       cin >> numProducts;
  273.       Customer c(name, email, (typeC) tC, numProducts);
  274.       customers[i] = c;
  275.     }
  276.     fc.setCustomers(customers, n);
  277.     cout << "Update:" << endl;
  278.     fc.update();
  279.     cout << fc;
  280.    }
  281.   return 0;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement