Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class ExistingGame{
- private:
- char *message;
- public:
- ExistingGame(){
- message = new char[0];
- }
- ExistingGame(char *message){
- this->message = new char[strlen(message)+1];
- strcpy(this->message,message);
- }
- void msg(){
- cout<<"File with name"<<message<< " already exists in this folder"<<endl;
- }
- };
- class Game{
- protected:
- char ime[100];
- double cena;
- bool r;
- public:
- Game (){
- }
- Game( char *ime, double cena, bool r){
- strcpy(this->ime, ime);
- this->cena=cena;
- this->r=r;
- }
- Game( const Game &g){
- strcpy(this->ime, g.ime);
- this->cena=g.cena;
- this->r=g.r;
- }
- friend ostream& operator<<(ostream &out, Game &g){
- out<<"Name:"<<" "<<g.ime<<"Cena:"<<" "<<g.cena<<endl;
- return out;
- }
- friend istream& operator>>(istream &in,Game &g){
- in>>g.ime>>g.cena>>g.r;
- return in;
- }
- bool operator==(Game &g)
- {
- return strcmp(this->ime,g.ime)==0;
- }
- ~Game(){
- }
- };
- class SubscriptionGame : public Game{
- protected:
- double nadomest;
- int datum;
- int godina;
- public:
- SubscriptionGame(){
- }
- SubscriptionGame( char *ime, double cena, bool r, double nadomest, int datum, int godina): Game(ime,cena,r)
- {
- this->nadomest=nadomest;
- this->datum=datum;
- this->godina=godina;
- }
- SubscriptionGame(const SubscriptionGame &s )
- {
- this->nadomest=s.nadomest;
- this->datum=s.datum;
- this->godina=s.godina;
- }
- ~SubscriptionGame(){
- }
- };
- class User{
- protected:
- char kime[100];
- char *kolekcija;
- public:
- User(){
- kolekcija = new char[0];
- }
- User(char *kime, char kolekcija){
- strcpy(this->kime,kime);
- this->kolekcija= new char[strlen(kolekcija)+1];
- strcpy(this->kolekcija, kolekcija);
- }
- User(const User &u){
- strcpy(this->kime,u.kime);
- this->kolekcija= new char[strlen(u.kolekcija)+1];
- strcpy(this->kolekcija, u.kolekcija);
- }
- ~User(){
- delete []kolekcija;
- }
- User& operator+=(char nova)
- {
- char *temp = new char[kolekcija+1];
- for(int i=0; i<kolekcija; i++)
- {
- temp[i] = kolekcija[i];
- }
- temp[kolekcija] = nova;
- delete []kolekcija;
- kolekcija = temp;
- kolekcija++;
- return *this;
- }
- total_spent(){
- }
- int getGame(){
- return ime;
- }
- int totalSpent(){
- return kolekcija * cena;
- }
- char *getName(){
- return kime;
- }
- char getKolekcija(){
- return kolekcija;
- }
- };
- int main() {
- int test_case_num;
- cin>>test_case_num;
- // for Game
- char game_name[100];
- float game_price;
- bool game_on_sale;
- // for SubscritionGame
- float sub_game_monthly_fee;
- int sub_game_month, sub_game_year;
- // for User
- char username[100];
- int num_user_games;
- if (test_case_num == 1){
- cout<<"Testing class Game and operator<< for Game"<<std::endl;
- cin.get();
- cin.getline(game_name,100);
- cin>>game_price>>game_on_sale;
- Game g(game_name, game_price, game_on_sale);
- cout<<g;
- }
- else if (test_case_num == 2){
- cout<<"Testing class SubscriptionGame and operator<< for SubscritionGame"<<std::endl;
- cin.get();
- cin.getline(game_name, 100);
- cin>>game_price>>game_on_sale;
- cin>>sub_game_monthly_fee;
- cin>>sub_game_month>>sub_game_year;
- SubscriptionGame sg(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
- cout<<sg;
- }
- else if (test_case_num == 3){
- cout<<"Testing operator>> for Game"<<std::endl;
- Game g;
- cin>>g;
- cout<<g;
- }
- else if (test_case_num == 4){
- cout<<"Testing operator>> for SubscriptionGame"<<std::endl;
- SubscriptionGame sg;
- cin>>sg;
- cout<<sg;
- }
- else if (test_case_num == 5){
- cout<<"Testing class User and operator+= for User"<<std::endl;
- cin.get();
- cin.getline(username,100);
- User u(username);
- int num_user_games;
- int game_type;
- cin >>num_user_games;
- try {
- for (int i=0; i<num_user_games; ++i){
- cin >> game_type;
- Game *g;
- // 1 - Game, 2 - SubscriptionGame
- if (game_type == 1){
- cin.get();
- cin.getline(game_name, 100);
- cin>>game_price>>game_on_sale;
- g = new Game(game_name, game_price, game_on_sale);
- }
- else if (game_type == 2){
- cin.get();
- cin.getline(game_name, 100);
- cin>>game_price>>game_on_sale;
- cin>>sub_game_monthly_fee;
- cin>>sub_game_month>>sub_game_year;
- g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
- }
- u+=(*g);
- }
- }catch(ExistingGame &ex){
- ex.message();
- }
- cout<<u;
- }
- else if (test_case_num == 6){
- cout<<"Testing exception ExistingGame for User"<<std::endl;
- cin.get();
- cin.getline(username,100);
- User u(username);
- int num_user_games;
- int game_type;
- cin >>num_user_games;
- for (int i=0; i<num_user_games; ++i){
- cin >> game_type;
- Game *g;
- // 1 - Game, 2 - SubscriptionGame
- if (game_type == 1){
- cin.get();
- cin.getline(game_name, 100);
- cin>>game_price>>game_on_sale;
- g = new Game(game_name, game_price, game_on_sale);
- }
- else if (game_type == 2){
- cin.get();
- cin.getline(game_name, 100);
- cin>>game_price>>game_on_sale;
- cin>>sub_game_monthly_fee;
- cin>>sub_game_month>>sub_game_year;
- g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
- }
- //cout<<(*g);
- try {
- u+=(*g);
- }
- catch(ExistingGame &ex){
- ex.message();
- }
- }
- cout<<u;
- }
- else if (test_case_num == 7){
- cout<<"Testing total_spent method() for User"<<std::endl;
- cin.get();
- cin.getline(username,100);
- User u(username);
- int num_user_games;
- int game_type;
- cin >>num_user_games;
- for (int i=0; i<num_user_games; ++i){
- cin >> game_type;
- Game *g;
- // 1 - Game, 2 - SubscriptionGame
- if (game_type == 1){
- cin.get();
- cin.getline(game_name, 100);
- cin>>game_price>>game_on_sale;
- g = new Game(game_name, game_price, game_on_sale);
- }
- else if (game_type == 2){
- cin.get();
- cin.getline(game_name, 100);
- cin>>game_price>>game_on_sale;
- cin>>sub_game_monthly_fee;
- cin>>sub_game_month>>sub_game_year;
- g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
- }
- //cout<<(*g);
- u+=(*g);
- }
- cout<<u;
- cout<<"Total money spent: $"<<u.total_spent()<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement