uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit c643b41f12d4165155427c7a2ad9b025accdc049
parent cc0c7d8b5f56ba7e17bfa38cd29e1f1e43d40396
Author: Christos Margiolis <christos@margiolis.net>
Date:   Mon,  4 May 2020 20:01:37 +0300

improved 2.4, added review read/write

Diffstat:
Massignment-2.4-inheritance/bin/inheritance | 0
Massignment-2.4-inheritance/obj/app.o | 0
Massignment-2.4-inheritance/obj/appsystem.o | 0
Massignment-2.4-inheritance/obj/main.o | 0
Massignment-2.4-inheritance/res/appdata.csv | 8++++----
Massignment-2.4-inheritance/res/appdata_out.csv | 10+++++-----
Massignment-2.4-inheritance/res/manfdata.csv | 6+++---
Massignment-2.4-inheritance/res/manfdata_out.csv | 10+++++-----
Aassignment-2.4-inheritance/res/revs.csv | 4++++
Aassignment-2.4-inheritance/res/revs_out.csv | 4++++
Massignment-2.4-inheritance/src/app.cpp | 13++++++++++++-
Massignment-2.4-inheritance/src/app.h | 5++++-
Massignment-2.4-inheritance/src/appsystem.cpp | 178++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Massignment-2.4-inheritance/src/appsystem.h | 11+++++++++--
Massignment-2.4-inheritance/src/main.cpp | 86+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
Massignment-2.4-inheritance/src/manufacturer.h | 5+++--
Massignment-2.5-spreadsheets/bin/spreadsheets | 0
Massignment-2.5-spreadsheets/obj/main.o | 0
18 files changed, 247 insertions(+), 93 deletions(-)

diff --git a/assignment-2.4-inheritance/bin/inheritance b/assignment-2.4-inheritance/bin/inheritance Binary files differ. diff --git a/assignment-2.4-inheritance/obj/app.o b/assignment-2.4-inheritance/obj/app.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/appsystem.o b/assignment-2.4-inheritance/obj/appsystem.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/main.o b/assignment-2.4-inheritance/obj/main.o Binary files differ. diff --git a/assignment-2.4-inheritance/res/appdata.csv b/assignment-2.4-inheritance/res/appdata.csv @@ -1,5 +1,5 @@ Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions -Game,123,LoL,Windows,Riot,0,MOBA,Yes,N/A -Game,345,CS:GO,Cross,Valve,14,FPS,Yes,N/A -Office,567,LibreOffice,Linux,GNU,0,N/A,N/A,.doc|.xls|.odt|.ods|.ppt| -Office,789,Vim,Linux,GNU,0,N/A,N/A,.txt|.md|.tex|.cpp|.sh| +Game,0001,LoL,MAD Robot 4.5,Riot,0,MOBA,Yes,N/A +Game,0002,CS:GO,MAD Robot 3.0,Valve,14,FPS,Yes,N/A +Office,0003,LibreOffice,MAD Robot 2.0,GNU,0,N/A,N/A,.doc|.xls|.odt|.ods|.ppt| +Office,0004,Vim,MAD Robot 1.0,GNU,0,N/A,N/A,.txt|.md|.tex|.cpp|.sh| diff --git a/assignment-2.4-inheritance/res/appdata_out.csv b/assignment-2.4-inheritance/res/appdata_out.csv @@ -1,6 +1,6 @@ Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions -Game,123,LoL,Windows,Riot,0,MOBA,No,N/A -Game,345,CS:GO,Cross,Valve,14,FPS,No,N/A -Office,567,LibreOffice,Linux,GNU,0,N/A,N/A,.doc|.xls|.odt|.ods|.ppt| -Office,789,Vim,Linux,GNU,0,N/A,N/A,.txt|.md|.tex|.cpp|.sh| -Office,459,OpenOffice,Linux 2.2,Company,0,N/A,N/A,.doc|.xls|.ppt| +Game,0001,LoL,MAD Robot 4.5,Riot,0,MOBA,Yes,N/A +Game,0002,CS:GO,MAD Robot 3.0,Valve,14,FPS,Yes,N/A +Office,0003,LibreOffice,MAD Robot 2.0,GNU,0,N/A,N/A,.doc|.xls|.odt|.ods|.ppt| +Office,0004,Vim,MAD Robot 1.0,GNU,0,N/A,N/A,.txt|.md|.tex|.cpp|.sh| +Office,0004,OpenOffice,MAD Robot 2.2,Company,0,N/A,N/A,.doc|.xls|.ppt| diff --git a/assignment-2.4-inheritance/res/manfdata.csv b/assignment-2.4-inheritance/res/manfdata.csv @@ -1,4 +1,4 @@ SN,Name,Email -741,Riot,riot@riot.com -987,Valve,valve@valve.com -456,GNU,gnu@gnu.com +0001,Riot,riot@riot.com +0002,Valve,valve@valve.com +0003,GNU,gnu@gnu.com diff --git a/assignment-2.4-inheritance/res/manfdata_out.csv b/assignment-2.4-inheritance/res/manfdata_out.csv @@ -1,6 +1,6 @@ SN,Name,Email -124,Company,comp@comp.com -568,Chris,chris@cm.com -741,Riot,riot@riot.com -987,Valve,valve@valve.com -456,GNU,gnu@gnu.com +0004,Company,comp@comp.com +0005,Chris,chris@chris.com +0001,Riot,riot@riot.com +0002,Valve,valve@valve.com +0003,GNU,gnu@gnu.com diff --git a/assignment-2.4-inheritance/res/revs.csv b/assignment-2.4-inheritance/res/revs.csv @@ -0,0 +1,4 @@ +AppName,Stars,Username,Comment +LoL,3,randomuser1,Nice +Vim,4,coolguy,Epic +Vim,5,nerd,Best editor diff --git a/assignment-2.4-inheritance/res/revs_out.csv b/assignment-2.4-inheritance/res/revs_out.csv @@ -0,0 +1,4 @@ +AppName,Stars,Username,Comment +LoL,3,randomuser1,Nice +Vim,4,coolguy,Epic +Vim,5,nerd,Best editor diff --git a/assignment-2.4-inheritance/src/app.cpp b/assignment-2.4-inheritance/src/app.cpp @@ -32,10 +32,16 @@ App::App(const char *serialnum, const std::string& name, } } +#include <iostream> App::~App() { if (serialnum != nullptr) delete[] serialnum; - if (!reviews.empty()) reviews.clear(); + if (!reviews.empty()) + { + for (auto& rev :reviews) + delete rev; + reviews.clear(); + } } char *App::convsn(const char *serialnum) @@ -51,6 +57,11 @@ void App::addrev(Review *rev) reviews.push_back(rev); } +void App::addrevs(const std::vector<Review *> revs) +{ + reviews.insert(reviews.end(), revs.begin(), revs.end()); +} + const char *App::get_serialnum() const { return serialnum; diff --git a/assignment-2.4-inheritance/src/app.h b/assignment-2.4-inheritance/src/app.h @@ -25,8 +25,8 @@ class App const std::string& os, Manufacturer *manf, int price); virtual ~App(); - char *convsn(const char *serialnum); void addrev(Review *rev); + void addrevs(const std::vector<Review *> revs); const char *get_serialnum() const; const std::string& get_name() const; @@ -40,6 +40,9 @@ class App void set_os(const std::string& os); void set_manf(Manufacturer *manf); void set_price(int price); + + private: + char *convsn(const char *serialnum); }; #endif /* APP_H */ diff --git a/assignment-2.4-inheritance/src/appsystem.cpp b/assignment-2.4-inheritance/src/appsystem.cpp @@ -30,14 +30,14 @@ AppSystem& AppSystem::operator+= (Manufacturer *man) } template<> -bool AppSystem::read_data<Manufacturer>(const char *fpath) +bool AppSystem::import_data<Manufacturer>(const char *fpath) { std::ifstream f; f.exceptions(std::ifstream::badbit); try { std::string strpath(fpath); - if (strpath.find(".csv") == std::string::npos) throw strpath; + if (!valid_path(strpath)) throw strpath; f.open(fpath); if (f.is_open()) @@ -51,11 +51,30 @@ bool AppSystem::read_data<Manufacturer>(const char *fpath) } catch (const std::string& strpath) { - throw std::runtime_error("Error. File must be of format \'.csv\'."); + throw std::runtime_error(err_csv(strpath)); } catch (const std::ifstream::failure& e) { - std::cerr << "Error reading file \'" << fpath << "\'." << std::endl << + std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; + return false; + } + return true; +} + +bool AppSystem::read_manf(std::ifstream& f) +{ + try + { + std::string sn, name, email; + std::getline(f, sn, ','); + std::getline(f, name, ','); + std::getline(f, email); + if (f.eof()) return true; + manfs.push_back(new Manufacturer(sn.c_str(), name.c_str(), email)); + } + catch (const std::ifstream::failure& e) + { + std::cerr << "Error reading manufacturer data." << std::endl << e.what() << std::endl; return false; } @@ -63,14 +82,14 @@ bool AppSystem::read_data<Manufacturer>(const char *fpath) } template<> -bool AppSystem::read_data<App>(const char *fpath) +bool AppSystem::import_data<App>(const char *fpath) { std::ifstream f; f.exceptions(std::ifstream::badbit); try { std::string strpath(fpath); - if (strpath.find(".csv") == std::string::npos) throw strpath; + if (!valid_path(strpath)) throw strpath; f.open(fpath); if (f.is_open()) @@ -91,32 +110,11 @@ bool AppSystem::read_data<App>(const char *fpath) } catch (const std::string& strpath) { - throw std::runtime_error("Error. File must be of format \'.csv\'."); + throw std::runtime_error(err_csv(strpath)); } catch (const std::ifstream::failure& e) { - std::cerr << "Error reading file \'" << fpath << "\'." << std::endl << - e.what() << std::endl; - return false; - } - return true; -} - -bool AppSystem::read_manf(std::ifstream& f) -{ - try - { - std::string sn, name, email; - std::getline(f, sn, ','); - std::getline(f, name, ','); - std::getline(f, email); - if (f.eof()) return true; - manfs.push_back(new Manufacturer(sn.c_str(), name.c_str(), email)); - } - catch (const std::ifstream::failure& e) - { - std::cerr << "Error reading manufacturer data." << std::endl << - e.what() << std::endl; + std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; return false; } return true; @@ -127,13 +125,15 @@ bool AppSystem::read_game(std::ifstream& f) try { std::string sn, name, os, manf, price, genre, online; + std::string skip; std::getline(f, sn, ','); std::getline(f, name, ','); std::getline(f, os, ','); std::getline(f, manf, ','); std::getline(f, price, ','); std::getline(f, genre, ','); - std::getline(f, online); + std::getline(f, online, ','); + std::getline(f, skip); bool onl = online == "Yes"; if (!manfs.empty()) @@ -205,6 +205,41 @@ const std::vector<std::string> AppSystem::read_office_exts(std::ifstream& f) } template<> +bool AppSystem::import_data<Review>(const char *fpath) +{ + std::ifstream f; + try + { + std::string strpath(fpath); + if (!valid_path(strpath)) throw strpath; + f.open(fpath); + std::string skip; + std::getline(f, skip); + while (f.good()) + { + std::string appname, stars, username, comment; + std::getline(f, appname, ','); + std::getline(f, stars, ','); + std::getline(f, username, ','); + std::getline(f, comment); + for (auto& app : apps) + if (appname == app->get_name()) + app->addrev(new Review(std::stoi(stars), username, comment)); + } + } + catch (const std::string& strpath) + { + throw std::runtime_error(err_csv(strpath)); + } + catch (const std::ifstream::failure& e) + { + std::cerr << "Error reading reviews." << std::endl << e.what() << std::endl; + return false; + } + return true; +} + +template<> bool AppSystem::export_data<Manufacturer>(const char *fpath) { std::ofstream f; @@ -221,8 +256,7 @@ bool AppSystem::export_data<Manufacturer>(const char *fpath) } catch (const std::ofstream::failure& e) { - std::cerr << "Error writing to file (" << fpath << ")." << std::endl << - e.what() << std::endl; + std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; return false; } return true; @@ -235,6 +269,8 @@ bool AppSystem::export_data<App>(const char *fpath) f.exceptions(std::ofstream::failbit | std::ofstream::badbit); try { + std::string strpath(fpath); + if (!valid_path(strpath)) throw strpath; f.open(fpath); f << "Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions\n"; for (auto& app : apps) @@ -255,22 +291,61 @@ bool AppSystem::export_data<App>(const char *fpath) Office *of = dynamic_cast<Office *>(app); write_office_exts(of, f); f << std::endl; - } + } } f.close(); } + catch (const std::string& strpath) + { + throw std::runtime_error(err_csv(strpath)); + } catch (const std::ofstream::failure& e) { - std::cerr << "Error writing to file (" << fpath << ")." << std::endl << - e.what() << std::endl; + std::cerr << err_write(fpath) << e.what() << std::endl; + return false; + } + return true; +} + +template<> +bool AppSystem::export_data<Review>(const char *fpath) +{ + std::ofstream f; + f.exceptions(std::ofstream::failbit | std::ofstream::badbit); + try + { + std::string strpath(fpath); + if (!valid_path(strpath)) throw strpath; + f.open(fpath); + f << "AppName,Stars,Username,Comment\n"; + for (auto& app : apps) + { + const std::vector<Review *> revs = app->get_revs(); + if (!revs.empty()) + for (auto& rev : revs) + f << + app->get_name() << ',' << + rev->get_stars() << ',' << + rev->get_username() << ',' << + rev->get_comment() << std::endl; + } + f.close(); + } + catch (const std::string& strpath) + { + throw std::runtime_error(err_csv(strpath)); + } + catch (const std::ofstream::failure& e) + { + std::cerr << err_write(fpath) << e.what() << std::endl; return false; } return true; } -void AppSystem::write_office_exts(Office *o, std::ofstream& f) +void AppSystem::write_office_exts(Office *of, std::ofstream& f) { - std::vector<std::string> exts = o->get_exts(); + std::vector<std::string> exts = of->get_exts(); for (auto& ext : exts) f << ext << '|'; } @@ -282,6 +357,13 @@ void AppSystem::newrev(const std::string& appname, Review *rev) app->addrev(rev); } +void AppSystem::newrevs(const std::string& appname, const std::vector<Review *> revs) +{ + for (auto& app : apps) + if (app->get_name() == appname) + app->addrevs(revs); +} + void AppSystem::chserialnum(const std::string& appname, const char *serialnum) { for (auto& app : apps) @@ -390,7 +472,6 @@ const std::vector<Game *> AppSystem::get_goodgames() const count++; } if (sum / count > 4) ggames.push_back(o); - } } } @@ -406,3 +487,24 @@ const std::vector<Manufacturer *>& AppSystem::get_manfs() const { return manfs; } + +bool AppSystem::valid_path(const std::string& strpath) +{ + return (strpath.find(".csv") != std::string::npos); +} + +const std::string AppSystem::err_csv(const std::string& strpath) +{ + return "Error. File must be of format \'.csv\'. (" + + strpath + ")."; +} + +const std::string AppSystem::err_read(const char *fpath) +{ + return "Error reading file \'" + std::string(fpath) + "\'."; +} + +const std::string AppSystem::err_write(const char *fpath) +{ + return "Error writing to file (" + std::string(fpath) + ")."; +} diff --git a/assignment-2.4-inheritance/src/appsystem.h b/assignment-2.4-inheritance/src/appsystem.h @@ -21,9 +21,10 @@ class AppSystem AppSystem& operator+= (App *app); AppSystem& operator+= (Manufacturer *manf); - template<typename T> bool read_data(const char *fpath); + template<typename T> bool import_data(const char *fpath); template<typename T> bool export_data(const char *fpath); void newrev (const std::string& appname, Review *rev); + void newrevs (const std::string& appname, const std::vector<Review *> revs); void chserialnum(const std::string& appname, const char *serialnum); void chname (const std::string& appname, const std::string& name); void chos (const std::string& appname, const std::string& os); @@ -45,7 +46,13 @@ class AppSystem bool read_game(std::ifstream& f); bool read_office(std::ifstream& f); const std::vector<std::string> read_office_exts(std::ifstream& f); - void write_office_exts(Office *o, std::ofstream& f); + const std::vector<Review *> + read_reviews(const std::string& appname, const char *rpath); + void write_office_exts(Office *of, std::ofstream& f); + bool valid_path(const std::string& strpath); + const std::string err_csv(const std::string& fpath); + const std::string err_read(const char *fpath); + const std::string err_write(const char *fpath); }; #endif /* APPSYSTEM_H */ diff --git a/assignment-2.4-inheritance/src/main.cpp b/assignment-2.4-inheritance/src/main.cpp @@ -1,42 +1,64 @@ #include "appsystem.h" +#include <iomanip> std::ostream& operator<< (std::ostream& stream, const AppSystem& sys) { + stream << + std::left << std::setw(7) << "SN" << + std::left << std::setw(20) << "Name" << + std::left << std::setw(20) << "OS" << + std::left << std::setw(7) << "Price" << + std::left << std::setw(7) << "SN" << + std::left << std::setw(15) << "Manufacturer" << + std::left << std::setw(25) << "Email" << + std::left << std::setw(10) << "Genre" << + std::left << std::setw(10) << "Online" << + std::left << std::setw(25) << "Extensions" << std::endl << std::endl; + std::vector<App *> apps = sys.get_apps(); for (auto& app : apps) { + + stream << + std::left << std::setw(7) << app->get_serialnum() << + std::left << std::setw(20) << app->get_name() << + std::left << std::setw(20) << app->get_os() << + std::left << std::setw(7) << app->get_price(); + Manufacturer m = app->get_manf(); stream << - app->get_serialnum() << " " << - app->get_name() << " " << - app->get_os() << " " << - app->get_price() << " "; + std::left << std::setw(7) << m.get_serialnum() << + std::left << std::setw(15) << m.get_name() << + std::left << std::setw(25) << m.get_email(); - if (Office *o = dynamic_cast<Office *>(app)) + Game *o = dynamic_cast<Game *>(app); + stream << std::left << std::setw(10) << (o ? o->get_genre() : "N/A"); + stream << std::left << std::setw(10) << + (o ? (o->get_online() ? "Yes" : "No") : "N/A"); + + if (Office *of = dynamic_cast<Office *>(app)) { - std::vector<std::string> exts = o->get_exts(); + std::vector<std::string> exts = of->get_exts(); for (auto& ext : exts) - stream << ext << " "; - } - else if (Game *o = dynamic_cast<Game *>(app)) - { - stream << - o->get_genre() << " " << - (o->get_online() ? "Yes" : "No") << " "; + stream << ext << " "; } + else stream << "N/A"; std::vector<Review *> revs = app->get_revs(); - for (auto& rev : revs) - stream << - rev->get_stars() << " " << - rev->get_username() << " " << - rev->get_comment() << " "; - - stream << - m.get_serialnum() << " " << - m.get_name() << " " << - m.get_email(); + if (!revs.empty()) + { + stream << std::endl << std::endl << std::left << "Reviews:" << std::endl; + stream << + std::left << std::setw(7) << "Stars" << + std::left << std::setw(25) << "Username" << + std::left << "Comment" << std::endl << std::endl; + for (auto& rev : revs) + stream << + std::left << std::setw(7) << rev->get_stars() << + std::left << std::setw(25) << rev->get_username() << + std::left << rev->get_comment() << std::endl; + } stream << std::endl; } return stream; @@ -45,22 +67,21 @@ std::ostream& operator<< (std::ostream& stream, const AppSystem& sys) int main(int argc, char **argv) { AppSystem sys; - Manufacturer *comp = new Manufacturer("124", "Company", "comp@comp.com"); - Manufacturer *chris = new Manufacturer("568", "Chris", "chris@cm.com"); + Manufacturer *comp = new Manufacturer("0004", "Company", "comp@comp.com"); + Manufacturer *chris = new Manufacturer("0005", "Chris", "chris@chris.com"); sys += comp; sys += chris; - if (!sys.read_data<Manufacturer>("res/manfdata.csv")) return -1; - if (!sys.read_data<App>("res/appdata.csv")) return -1; + if (!sys.import_data<Manufacturer>("res/manfdata.csv")) return -1; + if (!sys.import_data<App>("res/appdata.csv")) return -1; + if (!sys.import_data<Review>("res/revs.csv")) return -1; std::vector<std::string> ext = {".doc", ".xls", ".ppt"}; - sys += new Office("459", "OpenOffice", "Linux 2.2", comp, 0, ext); - sys += new Game("731", "minecurses", "Linux 4.5", chris, 0, "Puzzle", false); + sys += new Office("0004", "OpenOffice", "MAD Robot 2.2", comp, 0, ext); + sys += new Game("0005", "minecurses", "MAD Robot 1.0", chris, 0, "Puzzle", false); - Review rev(4, "Name Surnaming", "Good"); - sys.newrev("minecurses", &rev); + sys.newrev("minecurses", new Review(4, "Name Surnaming", "Good")); std::cout << sys << std::endl; sys.removebad(chris); - std::cout << sys << std::endl; std::vector<Office *> fapps = sys.get_freeapps(); std::vector<Game *> ggames = sys.get_goodgames(); @@ -71,6 +92,7 @@ int main(int argc, char **argv) if (!sys.export_data<Manufacturer>("res/manfdata_out.csv")) return -1; if (!sys.export_data<App>("res/appdata_out.csv")) return -1; + if (!sys.export_data<Review>("res/revs_out.csv")) return -1; return 0; } diff --git a/assignment-2.4-inheritance/src/manufacturer.h b/assignment-2.4-inheritance/src/manufacturer.h @@ -19,8 +19,6 @@ class Manufacturer Manufacturer(const Manufacturer& m); ~Manufacturer(); - char *convstr(const char *str); - const char *get_serialnum() const; const char *get_name() const; const std::string& get_email() const; @@ -28,6 +26,9 @@ class Manufacturer void set_serialnum(const char *serialnum); void set_name(const char *name); void set_email(const std::string& email); + + private: + char *convstr(const char *str); }; #endif /* MANUFACTURER_H */ diff --git a/assignment-2.5-spreadsheets/bin/spreadsheets b/assignment-2.5-spreadsheets/bin/spreadsheets Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/main.o b/assignment-2.5-spreadsheets/obj/main.o Binary files differ.