languages

A collection of programs made with different programming languages.
git clone git://evanalba.com/languages
Log | Files | Refs

todo_ui.cpp (4395B)


      1 /*
      2 * Name        : todo_ui.cpp
      3 * Author      : Evan Alba
      4 * Description : CPP File for class TodoUI.
      5 */
      6 
      7 #include "todo_ui.h"
      8 
      9 TodoUI::TodoUI() {
     10   interface_ = new TodoList;
     11 }
     12 
     13 TodoUI::~TodoUI() {
     14   delete interface_;
     15 }
     16 
     17 void TodoUI::menu() {
     18   std::cout << "Welcome to the console-based Todo List.\n\nPlease type any " <<
     19   "integer number to start." << std::endl;
     20   reader.readInt();
     21   int choice = -1;
     22   while (choice != 0) {
     23     std::cout <<
     24     "Please type a number to select one of the following options below:\n\n"
     25     << "0. Exit the program.\n" << "1. Create a new item.\n"
     26     << "2. Edit an item.\n" << "3. Delete an item.\n"
     27     << "4. Delete all items.\n" << "5. View a specific item.\n"
     28     << "6. View all items.\n" << std::endl;
     29     choice = reader.readInt();
     30     switch (choice) {
     31       case 0:
     32         choice = 0;
     33         break;
     34       case 1:
     35         NewItem();
     36         break;
     37       case 2:
     38         EditItem();
     39         break;
     40       case 3:
     41         DeleteItem();
     42         break;
     43       case 4:
     44         DeleteItems();
     45         break;
     46       case 5:
     47         ViewItem();
     48         break;
     49       case 6:
     50         ViewItems();
     51         break;
     52     }
     53   }
     54 }
     55 
     56 /* PRIVATE */
     57 void TodoUI::NewItem() {
     58   std::cout << "Please type a description for your item:" << std::endl;
     59   std::string desc = reader.readString();
     60   std::cout <<
     61   "Please type the number from 1-5 (1 = Highest) for your item's priority:"
     62   << std::endl;
     63   int num = reader.readInt(1, 5);
     64   std::cout <<
     65   "Has the item been completed?" <<
     66   " Please type the word true if the item is completed." <<
     67   "If the item is not completed, please type the word false: "
     68   << std::endl;
     69   bool status = reader.readBool();
     70   interface_->AddItem(new TodoItem(desc, num, status));
     71 }
     72 
     73 void TodoUI::EditItem() {
     74   if (interface_->GetSize() == 0) {
     75     std::cout << "No items on the Todo List to edit.\n" << std::endl;
     76     return;
     77   }
     78   std::cout <<
     79   "Please type the number corresponding to the location" <<
     80   " of the item you want to edit:"
     81   << std::endl;
     82   int location = reader.readInt(1, interface_->GetSize());
     83   std::cout << "Please type the number corresponding on what part " <<
     84   "you want to edit of the item you have chosen. (1 = Description" <<
     85   " | 2 = Priority | 3 = Completion Status)\n\n" << std::endl;
     86   int option = reader.readInt(1, 3);
     87   if (option == 1) {
     88     std::cout << "Please type new description you want to set:" << std::endl;
     89     std::string desc = reader.readString();
     90     interface_->GetItem(location)->set_description(desc);
     91   } else if (option == 2) {
     92     std::cout << "Please type a new number from 1 to 5 to set the new " <<
     93     "priority of the item:" << std::endl;
     94     int num = reader.readInt(1, 5);
     95     interface_->GetItem(location)->set_priority(num);
     96   } else if (option == 3) {
     97     std::cout << "Has the item been completed? Please type the word " <<
     98     "true if the item is completed. If the item is not completed, " <<
     99     "please type the word false:" << std::endl;
    100     bool status = reader.readBool();
    101     interface_->GetItem(location)->set_completed(status);
    102   }
    103 }
    104 
    105 void TodoUI::DeleteItem() {
    106   if (interface_->GetSize() == 0) {
    107     std::cout << "No items on the Todo List to delete.\n" << std::endl;
    108     return;
    109   }
    110   std::cout << "Please type the location of the item you want to " <<
    111   "remove from the Todo List:" << std::endl;
    112   int num = reader.readInt(1, interface_->GetSize());
    113   interface_->DeleteItem(num);
    114 }
    115 
    116 void TodoUI::DeleteItems() {
    117   if (interface_->GetSize() == 0) {
    118     std::cout << "No items on the Todo List to delete.\n" << std::endl;
    119     return;
    120   }
    121   std::cout << "All items on the Todo List have been deleted.\n" << std::endl;
    122   while (interface_->GetItem(1) != nullptr) {
    123     interface_->DeleteItem(1);
    124   }
    125 }
    126 
    127 void TodoUI::ViewItem() {
    128   std::cout << "Please type the number corresponding to the location " <<
    129   "of the item you want to view:" << std::endl;
    130   int location = reader.readInt();
    131   std::cout << "\nDescription:\n" <<
    132   interface_->GetItem(location)->description()
    133   << "\nPriority:\n" << interface_->GetItem(location)->priority() <<
    134   "\nIs it completed? (1 = True | 2 = False)\n" <<
    135   interface_->GetItem(location)->completed() <<
    136   "\n\n" << std::endl;
    137 }
    138 
    139 void TodoUI::ViewItems() {
    140   if (interface_->GetSize() == 0) {
    141     std::cout << "No items on the Todo List to view.\n" << std::endl;
    142     return;
    143   }
    144   std::cout << *interface_ << std::endl;
    145 }
    146