Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- #define MAX 100
- enum typeC{standard=0, loyal=1, vip=2};
- class UserExistsException{
- private:
- char msg[100];
- public:
- UserExistsException(char * msg = "") {
- strcpy(this->msg, msg);
- }
- void showMessage() {
- cout<<msg;
- }
- };
- class Customer { //Да се креира класа Customer за опишување на купувачи на една книжара.
- private://За секој купувач се чуваат податоци за:
- char ime[50]; //името (низа од 50 знаци),
- char email[50]; //електронска адреса (низа од 50 знаци),
- typeC vid; //вид на купувач (стандардни, лојални или вип),
- static int osnoven; //основен попуст (цел број изразен во проценти),
- const static int dopolnitelen; //дополнителен попуст (цел број изразен во проценти) и
- int kupeni; //број на купени производи. (5 поени)
- public:
- Customer(char * ime = "", char * email = "", typeC vid = (typeC)0, int kupeni = 0) {
- strcpy(this->ime, ime);
- strcpy(this->email, email);
- this->vid = vid;
- this->osnoven = osnoven;
- this->kupeni = kupeni;
- }
- Customer(const Customer ©) {
- strcpy(this->ime, copy.ime);
- strcpy(this->email, copy.email);
- this->vid = copy.vid;
- this->osnoven = copy.osnoven;
- this->kupeni = copy.kupeni;
- }
- Customer &operator=(const Customer ©)
- {
- if(this!=©) {
- strcpy(this->ime, copy.ime);
- strcpy(this->email, copy.email);
- this->vid = copy.vid;
- this->osnoven = copy.osnoven;
- this->kupeni = copy.kupeni;
- }
- return *this;
- }
- int getkupeni() {
- return kupeni;
- }
- char * getemail() {
- return email;
- }
- typeC gettip() {
- return vid;
- }
- void settip(typeC vid) {
- this->vid = vid;
- }
- static void setDiscount1(int newosnoven){
- osnoven = newosnoven;
- }
- int popust() { //Сите лојални купувачи со покажување на клуб картичка имаат право на основниот попуст при
- if(vid==loyal) //купување од книжарата. Основниот попуст е ист за сите лојални купувачи и изнесува 10%.
- return osnoven;
- if(vid==vip) //Оваа вредност може да се смени со одлука на раководството на книжарата. Дополнителниот
- return osnoven+dopolnitelen;
- else return 0; //попуст е фиксен и може да го користат само вип купувачите и изнесува 20%. Стандардните
- } // купувачи немаат право на попуст (5 поени).
- friend ostream &operator<<(ostream &o, Customer &print) { //За оваа класа да се имплементира оператор <<
- //за печатење на купувач во формат:
- o<<print.ime<<endl;//[ime_na_kupuvac]
- o<<print.email<<endl;//[email_na_kupuvac]
- o<<print.kupeni<<endl;//[broj_na_proizvodi]
- if(print.vid==standard) o<<"standard ";//[vid_na_kupuvac]
- else if(print.vid==loyal) o<<"loyal ";//[popust_sto_moze_da_go_koristi]
- else o<<"vip ";
- o<<print.popust()<<endl;
- return o;
- }
- };
- int Customer::osnoven = 10;
- const int Customer::dopolnitelen = 20;
- class FINKI_bookstore{ //Да се креира класа за онлајн книжара FINKI-bookstore
- private: //во која се чува
- Customer * niza; //низа од регистрирани купувачи (динамички алоцирана низа)
- int broj; //и број на купувачи. (5 поени)
- public://За класата да се обезбедат:
- FINKI_bookstore(Customer * niza = NULL, int broj = 0) {
- this->broj = broj;
- this->niza = new Customer[broj];
- for(int i=0; i<broj; i++)
- this->niza[i] = niza[i];
- }
- FINKI_bookstore(const FINKI_bookstore ©) {
- this->broj = copy.broj;
- this->niza = new Customer[copy.broj];
- for(int i=0; i<copy.broj; i++)
- this->niza[i] = copy.niza[i];
- }
- FINKI_bookstore &operator=(const FINKI_bookstore ©) {
- if(this!=©) {
- this->broj = copy.broj;
- this->niza = new Customer[copy.broj];
- for(int i=0; i<copy.broj; i++)
- this->niza[i] = copy.niza[i];
- }
- return *this;
- }
- void setCustomers(Customer * niza, int broj) {
- this->broj = broj;
- this->niza = new Customer[broj];
- for(int i=0; i<broj; i++)
- this->niza[i] = niza[i];
- }
- FINKI_bookstore &operator+=(Customer &add) { //operator+= (10 поени) за додавање купувач во
- int flag=0;//листата од купувачи, но само ако веќе не е дел од неа (ако во листата нема купувач со иста електронска адреса).
- for(int i=0;i<broj;i++)
- if(strcmp(niza[i].getemail(), add.getemail())==0)
- flag=1;// Ако во листата постои корисник со иста електронска адреса, треба да се генерира исклучок UserExistsException.
- if(flag==1) // Потребно е да се обезбеди справување со исклучокот во функцијата main на означеното место.
- throw UserExistsException("The user already exists in the list!\n");//Bо ваква ситуација се печати порака "The user already exists in the list" (5 поени).
- if(flag==0)
- {
- Customer * tmp = new Customer[broj+1];
- for(int i=0;i<broj;i++)
- tmp[i] = niza[i];
- tmp[broj++] = add;
- delete [] niza;
- niza = tmp;
- }
- return *this;
- }
- void update() { //Функција update со која
- for(int i=0; i<broj;i++) { //а сите лојални купувачи со над 10 купени производи, стануваат вип (5 поени).
- if(niza[i].getkupeni() > 5 && niza[i].gettip() == standard) // сите стандардни купувачи со купени повеќе
- niza[i].settip(loyal);//од 5 производи стануваат лојални,
- else if(niza[i].getkupeni() > 10 && niza[i].gettip() == loyal)
- niza[i].settip(vip);
- }
- }
- friend ostream &operator<<(ostream &o, FINKI_bookstore &print) {// Оператор << за печатење на информациите за сите
- for(int i=0;i<print.broj;i++)
- o<<print.niza[i];// регистрирани купувачи (5 поени).
- return o;
- }
- };
- int main() {
- int testCase;
- cin >> testCase;
- char name[MAX];
- char email[MAX];
- int tC;
- int discount;
- int numProducts;
- if (testCase == 1){
- cout << "===== Test Case - Customer Class ======" << endl;
- cin.get();
- cin.getline(name, MAX);
- cin.getline(email, MAX);
- cin >> tC;
- cin >> numProducts;
- cout << "===== CONSTRUCTOR ======" << endl;
- Customer c(name, email, (typeC) tC, numProducts);
- cout << c;
- }
- if (testCase == 2){
- cout << "===== Test Case - Static Members ======" << endl;
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- cout << "===== CONSTRUCTOR ======" << endl;
- Customer c(name, email, (typeC) tC, numProducts);
- cout << c;
- c.setDiscount1(5);
- cout << c;
- }
- if (testCase == 3){
- cout << "===== Test Case - FINKI-bookstore ======" << endl;
- FINKI_bookstore fc;
- int n;
- cin >> n;
- Customer customers[MAX];
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- customers[i] = c;
- }
- fc.setCustomers(customers, n);
- cout << fc <<endl;
- }
- if (testCase == 4){
- cout << "===== Test Case - operator+= ======" << endl;
- FINKI_bookstore fc;
- int n;
- cin >> n;
- Customer customers[MAX];
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- customers[i] = c;
- }
- fc.setCustomers(customers, n);
- cout << "OPERATOR +=" << endl;
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- fc+=c;
- cout << fc;
- }
- if (testCase == 5){
- cout << "===== Test Case - operator+= (exception) ======" << endl;
- FINKI_bookstore fc;
- int n;
- cin >> n;
- Customer customers[MAX];
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- customers[i] = c;
- }
- fc.setCustomers(customers, n);
- cout << "OPERATOR +=" << endl;
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- try {
- fc+=c;
- }
- catch (UserExistsException &ex) {
- ex.showMessage();
- }
- cout << fc;
- }
- if (testCase == 6){
- cout << "===== Test Case - update method ======" << endl << endl;
- FINKI_bookstore fc;
- int n;
- cin >> n;
- Customer customers[MAX];
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- customers[i] = c;
- }
- fc.setCustomers(customers, n);
- cout << "Update:" << endl;
- fc.update();
- cout << fc;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement