Advertisement
metalni

OOP Ispitni Zadaca 2 (FINKI_bookstore)

Jun 21st, 2020
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #define MAX 100
  4.  
  5. using namespace std;
  6.  
  7. enum typeC{standard,loyal,vip};
  8.  
  9. class UserExistsException{
  10.     private:
  11.         char msg[256];
  12.     public:
  13.         UserExistsException(const char * msg){
  14.             strcpy(this->msg, msg);
  15.         }
  16.         const void print(){
  17.             cout << this->msg;
  18.         }
  19. };
  20.  
  21.  
  22. class Customer{
  23.     private:
  24.         char name[50];
  25.         char email[50];
  26.         typeC type;
  27.         int noBought;
  28.         static int baseSale;
  29.         static int addSale;
  30.         const void copy_obj(const Customer &orig){
  31.             strcpy(this->name, orig.name);
  32.             strcpy(this->email, orig.email);
  33.             this->type = orig.type;
  34.             this->noBought = orig.noBought;
  35.         }
  36.     public:
  37.         Customer(){
  38.             strcpy(this->name, "Unknown");
  39.             strcpy(this->email, "none@none.com");
  40.             this->type = (typeC)0;
  41.             this->noBought = 0;
  42.         }
  43.         Customer(const char * name, const char * email, typeC type, const int noBought){
  44.             strcpy(this->name, name);
  45.             strcpy(this->email, email);
  46.             this->type = type;
  47.             this->noBought = noBought;
  48.         }
  49.         Customer(const Customer &orig){
  50.             this->copy_obj(orig);
  51.         }
  52.         Customer &operator=(const Customer &orig){
  53.             if(this != &orig)
  54.                 this->copy_obj(orig);
  55.             return *this;
  56.         }
  57.         const int sale(){
  58.             if(this->type == standard)
  59.                 return 0;
  60.             else if(this->type == loyal)
  61.                 return baseSale;
  62.             else
  63.                 return addSale + baseSale;
  64.         }
  65.         static const void setDiscount1(const int newDisc){
  66.             baseSale = newDisc;
  67.         }
  68.         friend ostream &operator << (ostream &os, Customer &orig){
  69.             os << orig.name << "\n" << orig.email << "\n" << orig.noBought << "\n";
  70.             if(orig.type == standard)
  71.                 os << "standard ";
  72.             else if(orig.type == loyal)
  73.                 os <<"loyal ";
  74.             else
  75.                 os << "vip ";
  76.             os << orig.sale() << "\n";
  77.  
  78.             return os;
  79.         }
  80.         const char * getEmail(){
  81.             return this->email;
  82.         }
  83.         const typeC getType(){
  84.             return this->type;
  85.         }
  86.         const int getNoBought(){
  87.             return this->noBought;
  88.         }
  89.         const void setType(typeC newType){
  90.             this->type = newType;
  91.         }
  92. };
  93. int Customer::baseSale = 10;
  94. int Customer::addSale = 20;
  95.  
  96. class FINKI_bookstore{
  97.     private:
  98.         Customer * cs;
  99.         int noCs;
  100.         const void copy_obj(const FINKI_bookstore &orig){
  101.             this->cs = new Customer[orig.noCs+1];
  102.             for(int i=0; i<orig.noCs; i++)
  103.                 this->cs[i] = orig.cs[i];
  104.             this->noCs = orig.noCs;
  105.         }
  106.     public:
  107.         FINKI_bookstore(){
  108.             this->cs = new Customer[0];
  109.             this->noCs = 0;
  110.         }
  111.         FINKI_bookstore(const FINKI_bookstore &orig){
  112.             this->copy_obj(orig);
  113.         }
  114.         FINKI_bookstore &operator=(const FINKI_bookstore &orig){
  115.             if(this != &orig){
  116.                 delete [] this->cs;
  117.                 this->copy_obj(orig);
  118.             }
  119.             return *this;
  120.         }
  121.         ~FINKI_bookstore(){
  122.             delete [] this->cs;
  123.         }
  124.         FINKI_bookstore &operator += (Customer &add){
  125.             for(int i=0; i<this->noCs; i++)
  126.                 if(!(strcmp(this->cs[i].getEmail(), add.getEmail())))
  127.                     throw UserExistsException("The user already exists in the list!\n");
  128.             Customer * tmp = new Customer[this->noCs+1];
  129.             for(int i=0; i<this->noCs; i++)
  130.                 tmp[i] = this->cs[i];
  131.             tmp[this->noCs++] = add;
  132.             delete [] this->cs;
  133.             this->cs = tmp;
  134.  
  135.             return *this;
  136.         }
  137.         const void setCustomers(Customer * customers, int n){
  138.             this->noCs = n;
  139.             this->cs = new Customer[n+1];
  140.             for(int i=0; i<n; i++)
  141.                 this->cs[i] = customers[i];
  142.         }
  143.         const void update(){
  144.             for(int i=0; i<this->noCs; i++){
  145.                 if(this->cs[i].getType() == standard && this->cs[i].getNoBought() > 5)
  146.                     this->cs[i].setType(loyal);
  147.                 else if(this->cs[i].getType() == loyal && this->cs[i].getNoBought() > 10)
  148.                     this->cs[i].setType(vip);
  149.             }
  150.         }
  151.         friend ostream &operator << (ostream &os, FINKI_bookstore &orig){
  152.             for(int i=0; i<orig.noCs; i++)
  153.                 os << orig.cs[i];
  154.             return os;
  155.         }
  156. };
  157.  
  158. int main(){
  159.   int testCase;
  160.   cin >> testCase;
  161.  
  162.   char name[MAX];
  163.   char email[MAX];
  164.   int tC;
  165.   int discount;
  166.   int numProducts;
  167.  
  168.  
  169.   if (testCase == 1){
  170.     cout << "===== Test Case - Customer Class ======" << endl;
  171.     cin.get();
  172.     cin.getline(name,MAX);
  173.     cin.getline(email,MAX);
  174.     cin >> tC;
  175.     cin >> numProducts;
  176.     cout << "===== CONSTRUCTOR ======" << endl;
  177.     Customer c(name, email, (typeC) tC, numProducts);
  178.     cout << c;
  179.  
  180.   }
  181.  
  182.   if (testCase == 2){
  183.     cout << "===== Test Case - Static Members ======" << endl;
  184.     cin.get();
  185.     cin.getline(name,MAX);
  186.     cin.getline(email,MAX);
  187.     cin >> tC;
  188.     cin >> numProducts;
  189.     cout << "===== CONSTRUCTOR ======" << endl;
  190.     Customer c(name, email, (typeC) tC, numProducts);
  191.     cout << c;
  192.  
  193.     c.setDiscount1(5);
  194.  
  195.     cout << c;
  196.   }
  197.  
  198.   if (testCase == 3){
  199.     cout << "===== Test Case - FINKI-bookstore ======" << endl;
  200.     FINKI_bookstore fc;
  201.     int n;
  202.     cin >> n;
  203.     Customer customers[MAX];
  204.     for(int i = 0; i < n; ++i) {
  205.       cin.get();
  206.       cin.getline(name,MAX);
  207.       cin.getline(email,MAX);
  208.       cin >> tC;
  209.       cin >> numProducts;
  210.       Customer c(name, email, (typeC) tC, numProducts);
  211.       customers[i] = c;
  212.     }
  213.  
  214.     fc.setCustomers(customers, n);
  215.  
  216.     cout << fc <<endl;
  217.   }
  218.  
  219.   if (testCase == 4){
  220.     cout << "===== Test Case - operator+= ======" << endl;
  221.     FINKI_bookstore fc;
  222.     int n;
  223.     cin >> n;
  224.     Customer customers[MAX];
  225.     for(int i = 0; i < n; ++i) {
  226.       cin.get();
  227.       cin.getline(name,MAX);
  228.       cin.getline(email,MAX);
  229.       cin >> tC;
  230.       cin >> numProducts;
  231.       Customer c(name, email, (typeC) tC, numProducts);
  232.       customers[i] = c;
  233.     }
  234.  
  235.     fc.setCustomers(customers, n);
  236.     cout << "OPERATOR +=" << endl;
  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.     fc+=c;
  244.  
  245.     cout << fc;
  246.   }
  247.  
  248.   if (testCase == 5){
  249.     cout << "===== Test Case - operator+= (exception) ======" << endl;
  250.     FINKI_bookstore fc;
  251.     int n;
  252.     cin >> n;
  253.     Customer customers[MAX];
  254.     for(int i = 0; i < n; ++i) {
  255.       cin.get();
  256.       cin.getline(name,MAX);
  257.       cin.getline(email,MAX);
  258.       cin >> tC;
  259.       cin >> numProducts;
  260.       Customer c(name, email, (typeC) tC, numProducts);
  261.       customers[i] = c;
  262.     }
  263.  
  264.     fc.setCustomers(customers, n);
  265.     cout << "OPERATOR +=" << endl;
  266.     cin.get();
  267.     cin.getline(name,MAX);
  268.     cin.getline(email,MAX);
  269.     cin >> tC;
  270.     cin >> numProducts;
  271.     Customer c(name, email, (typeC) tC, numProducts);
  272.    
  273.     try{
  274.         fc+=c;
  275.     }
  276.    
  277.     catch(UserExistsException &ex){
  278.         ex.print();
  279.     }
  280.    
  281.     cout << fc;
  282.   }
  283.  
  284.   if (testCase == 6){
  285.     cout << "===== Test Case - update method  ======" << endl << endl;
  286.     FINKI_bookstore fc;
  287.     int n;
  288.     cin >> n;
  289.     Customer customers[MAX];
  290.     for(int i = 0; i < n; ++i) {
  291.       cin.get();
  292.       cin.getline(name,MAX);
  293.       cin.getline(email,MAX);
  294.       cin >> tC;
  295.       cin >> numProducts;
  296.       Customer c(name, email, (typeC) tC, numProducts);
  297.       customers[i] = c;
  298.     }
  299.  
  300.     fc.setCustomers(customers, n);
  301.  
  302.     cout << "Update:" << endl;
  303.     fc.update();
  304.     cout << fc;
  305.   }
  306.   return 0;
  307.    
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement