uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

main.cpp (4482B)


      1 #include <memory>
      2 #include "appsystem.hpp"
      3 
      4 static std::ostream& operator<< (std::ostream& stream, const AppSystem& sys);
      5 static void cont(const char *s);
      6 static void pluseqs(AppSystem& sys);
      7 static void edit(AppSystem& sys);
      8 static void remove(AppSystem& sys);
      9 static void getapps(const AppSystem& sys);
     10 
     11 /* 
     12  * main uses smart pointers just so I don't have to delete
     13  * them manually. There's obviously no good reason to do it
     14  * as there's only one object, but it looks cool.
     15  *
     16  * I also used some features that might make the program harder
     17  * to understand, but I wanted to take a chance and try out some of
     18  * these features, for fun mainly, even though they might seem
     19  * (and be) useless.
     20  */
     21 int
     22 main(int argc, char **argv)
     23 {
     24     std::unique_ptr<AppSystem> sys = std::make_unique<AppSystem>();
     25     system("clear || cls");
     26     sys->import_data<Manufacturer>("res/manfdata.csv");
     27     sys->import_data<App>("res/appdata.csv");
     28     sys->import_data<Review>("res/revs.csv");
     29 
     30     cont("Imported data");
     31     std::cout << *sys << std::endl;
     32     
     33     pluseqs(*sys);
     34     cont("Additional data");
     35     std::cout << *sys << std::endl;
     36 
     37     edit(*sys);
     38     cont("Editing data");
     39     std::cout << *sys << std::endl;
     40 
     41     remove(*sys);
     42     cont("Removing data");
     43     std::cout << *sys << std::endl;
     44 
     45     cont("");
     46     getapps(*sys);
     47 
     48     cont("");
     49     sys->export_data<Manufacturer>("res/manfdata_out.csv");
     50     sys->export_data<App>("res/appdata_out.csv");
     51     sys->export_data<Review>("res/revs_out.csv");
     52     std::printf("\nThank you :)\n");
     53 
     54     return 0;
     55 }
     56 
     57 std::ostream&
     58 operator<< (std::ostream& stream, const AppSystem& sys)
     59 {
     60     stream <<
     61         std::left << std::setw(7) << "SN" <<
     62         std::left << std::setw(20) << "Name" <<
     63         std::left << std::setw(20) << "OS" <<
     64         std::left << std::setw(7) << "Price" <<
     65         std::left << std::setw(7) << "SN" <<
     66         std::left << std::setw(15) << "Manufacturer" <<
     67         std::left << std::setw(25) << "Email" <<
     68         std::left << std::setw(10) << "Genre" <<
     69         std::left << std::setw(10) << "Online" <<
     70         std::left << std::setw(25) << "Extensions" << std::endl << std::endl;
     71     std::vector<App *> apps = sys.get_apps();
     72     for (const auto& app : apps)
     73         app->print(stream);
     74     return stream;  
     75 }
     76 
     77 void
     78 cont(const char *s)
     79 {
     80     std::printf("\nPress <ENTER> to continue. . .");
     81     if (std::cin.get()) system("clear || cls");
     82     if (strlen(s) > 0) std::printf("%s\n\n", s);
     83 }
     84 
     85 void
     86 pluseqs(AppSystem& sys)
     87 {
     88     Manufacturer *comp = new Manufacturer("0004", "Company", "comp@comp.com");
     89     Manufacturer *chris = new Manufacturer("0005", "Chris", "chris@chris.com");
     90     sys += comp;
     91     sys += chris;
     92     sys += new Office("0004", "zathura", "NiceOS 1.1", comp, 0, {".pdf", ".md"});
     93     sys += new Game("0005", "minecurses", "NiceOS 0.5", chris, 0, "Puzzle", false);
     94 }
     95 
     96 void
     97 edit(AppSystem& sys)
     98 {
     99     sys.call<Review *>("minecurses", new Review(5, "gamer", "Good game"),
    100             &App::addrev);
    101     sys.call<const std::vector<Review *>>("Vim",
    102             {new Review(2, "user1", "Not so good"),
    103              new Review(4, "user2", "Good app"),
    104              new Review(5, "user3", "Very good :)")},
    105              &App::addrevs);
    106     sys.call<const char *>("zathura", "1254", &App::set_serialnum);
    107     sys.call<const std::string&>("minecurses", "minesweeper", &App::set_name);
    108     sys.call<const std::string&>("Vim", "GoodOS", &App::set_os);
    109     sys.call<Manufacturer *>("LibreOffice",
    110             new Manufacturer("0006", "FreeSoftware", "freesoft@freesoft.com"),
    111             &App::set_manf);
    112     sys.call<int>("LoL", 155, &App::set_price);
    113     sys.cast_call<const std::string&, Game>("CS:GO", "Shooter", &Game::set_genre);
    114     sys.cast_call<bool, Game>("minesweeper", true, &Game::set_online);
    115     sys.cast_call<const std::vector<std::string>&, Office>("zathura",
    116             {".tex", ".epub", ".ms"}, &Office::set_exts);
    117 }
    118 
    119 void
    120 remove(AppSystem& sys)
    121 {
    122     sys.removebad("0003");
    123 }
    124 
    125 void
    126 getapps(const AppSystem& sys)
    127 {
    128     const std::vector<Office *>& fapps = sys.get_freeapps();
    129     const std::vector<Game *>& ggames = sys.get_goodgames();
    130     std::cout << "Free office apps: " << std::endl << std::endl;
    131     for (const auto& fapp : fapps)
    132         std::cout << fapp->get_name() << std::endl;
    133     std::cout << std::endl;
    134     std::cout << "Games with >4 rating: " << std::endl << std::endl;
    135     for (const auto& ggame : ggames)
    136         std::cout << ggame->get_name() << std::endl;
    137 }