diff --git a/20-OODesign/Ehtesham-Handouts.pdf b/20-OODesign/Ehtesham-Handouts.pdf new file mode 100644 index 0000000..dd8e465 Binary files /dev/null and b/20-OODesign/Ehtesham-Handouts.pdf differ diff --git a/20-OODesign/ehtesham1.cpp b/20-OODesign/ehtesham1.cpp new file mode 100644 index 0000000..202c593 --- /dev/null +++ b/20-OODesign/ehtesham1.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +using namespace std; + +// Since we have not learned exception handling... +void error(string message) { + cerr << message << endl; + abort(); +} + +class Employee { +public: + Employee(string id, string n) { + if (id == "" || n == "") error("Employee ID and name cannot be empty"); + emp_id = id; + name = n; + bedehi = 0; + } +private: + int bedehi; + string emp_id; + string name; +}; + +class MenuItem { +public: + MenuItem(string t, int p) { + if (t == "") error("Menu item title cannot be empty"); + if (p <= 0) error("Menu item price must be positive"); + title = t; + price = p; + } +private: + string title; + int price; +}; + +class Catering { +public: + Catering(string n, vector m) { + if (n == "") error("Catering name must be non-empty"); + name = n; + menu = m; + } +private: + string name; + vector menu; +}; + +int main() { + Catering farsi("Farsi", { MenuItem("Kabab", 50000), MenuItem("Jooj", 30000) }); + Catering alaa("Alaa", { MenuItem("Kabab", 40000), MenuItem("Maahi", 35000) }); + Employee jamshid("1001", "Jamshid"); + Employee mahvash("1002", "Mahvash"); + Employee hooshang("1003", "Hooshang"); +} \ No newline at end of file diff --git a/20-OODesign/ehtesham2.cpp b/20-OODesign/ehtesham2.cpp new file mode 100644 index 0000000..c5f4e66 --- /dev/null +++ b/20-OODesign/ehtesham2.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +using namespace std; + +// Since we have not learned exception handling... +void error(string message) { + cerr << message << endl; + abort(); +} + +class Employee { +public: + Employee(string id, string n) { + if (id == "" || n == "") error("Employee ID and name cannot be empty"); + emp_id = id; + name = n; + bedehi = 0; + } +private: + int bedehi; + string emp_id; + string name; +}; + +class MenuItem { +public: + MenuItem(string t, int p) { + if (t == "") error("Menu item title cannot be empty"); + if (p <= 0) error("Menu item price must be positive"); + title = t; + price = p; + } +private: + string title; + int price; +}; + +class Catering { +public: + Catering(string n, vector m) { + name = n; + menu = m; + } +private: + string name; + vector menu; +}; + +class Ehtesham { +public: + Ehtesham(vector c, vector e) { + caterings = c; + employees = e; + } +private: + vector caterings; + vector employees; +}; + +int main() { + Ehtesham ehtesham( + { + Catering("Farsi", { MenuItem("Kabab", 50000), MenuItem("Jooj", 30000) }), + Catering("Alaa", { MenuItem("Kabab", 40000), MenuItem("Maahi", 35000) }) + }, + { + Employee("1001", "Jamshid"), + Employee("1002", "Mahvash"), + Employee("1003", "Hooshang") + } + ); +} \ No newline at end of file diff --git a/20-OODesign/ehtesham3.cpp b/20-OODesign/ehtesham3.cpp new file mode 100644 index 0000000..934d09d --- /dev/null +++ b/20-OODesign/ehtesham3.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +using namespace std; + +// Since we have not learned exception handling... +void error(string message) { + cerr << message << endl; + abort(); +} + +class Employee { +public: + Employee(string id, string n) { + if (id == "" || n == "") error("Employee ID and name cannot be empty"); + emp_id = id; + name = n; + bedehi = 0; + } +private: + int bedehi; + string emp_id; + string name; +}; + +class MenuItem { +public: + MenuItem(string t, int p) { + if (t == "") error("Menu item title cannot be empty"); + if (p <= 0) error("Menu item price must be positive"); + title = t; + price = p; + } +private: + string title; + int price; +}; + +class EmployeeRequest { +public: + EmployeeRequest(Employee* e, MenuItem* m) { + if (e == NULL || m == NULL) error("Employee request parameters must be non-null"); + item = m; + emp = e; + } +private: + Employee* emp; + MenuItem* item; +}; + +class Catering { +public: + Catering(string n, vector m) { + name = n; + menu = m; + } +private: + string name; + vector menu; +}; + +class Ehtesham { +public: + Ehtesham(vector c, vector e) { + caterings = c; + employees = e; + } +private: + vector caterings; + vector employees; +}; + +int main() { + Ehtesham ehtesham( + { + Catering("Farsi", { MenuItem("Kabab", 50000), MenuItem("Jooj", 30000) }), + Catering("Alaa", { MenuItem("Kabab", 40000), MenuItem("Maahi", 35000) }) + }, + { + Employee("1001", "Jamshid"), + Employee("1002", "Mahvash"), + Employee("1003", "Hooshang") + } + ); +} \ No newline at end of file diff --git a/20-OODesign/ehtesham4.cpp b/20-OODesign/ehtesham4.cpp new file mode 100644 index 0000000..a6f5764 --- /dev/null +++ b/20-OODesign/ehtesham4.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +using namespace std; + +// Since we have not learned exception handling... +void error(string message) { + cerr << message << endl; + abort(); +} + +class Employee { +public: + Employee(string id, string n) { + if (id == "" || n == "") error("Employee ID and name cannot be empty"); + emp_id = id; + name = n; + bedehi = 0; + } +private: + int bedehi; + string emp_id; + string name; +}; + +class MenuItem { +public: + MenuItem(string t, int p) { + if (t == "") error("Menu item title cannot be empty"); + if (p <= 0) error("Menu item price must be positive"); + title = t; + price = p; + } +private: + string title; + int price; +}; + +class EmployeeRequest { +public: + EmployeeRequest(Employee* e, MenuItem* m) { + if (e == NULL || m == NULL) error("Employee request parameters must be non-null"); + item = m; + emp = e; + } +private: + Employee* emp; + MenuItem* item; +}; + +class Catering { +public: + Catering(string n, vector m) { + name = n; + menu = m; + } +private: + string name; + vector menu; + vector requests; +}; + +class Ehtesham { +public: + Ehtesham(vector c, vector e) { + caterings = c; + employees = e; + } +private: + vector caterings; + vector employees; +}; + +int main() { + Ehtesham ehtesham( + { + Catering("Farsi", { MenuItem("Kabab", 50000), MenuItem("Jooj", 30000) }), + Catering("Alaa", { MenuItem("Kabab", 40000), MenuItem("Maahi", 35000) }) + }, + { + Employee("1001", "Jamshid"), + Employee("1002", "Mahvash"), + Employee("1003", "Hooshang") + } + ); +} diff --git a/20-OODesign/ehtesham5-dump.cpp b/20-OODesign/ehtesham5-dump.cpp new file mode 100644 index 0000000..7e4aa60 --- /dev/null +++ b/20-OODesign/ehtesham5-dump.cpp @@ -0,0 +1,174 @@ +#include +#include +#include +#include +#include +using namespace std; + +// Since we have not learned exception handling... +void error(string message) { + cerr << message << endl; + abort(); +} + +class Employee { +public: + Employee(string id, string n) { + if (id == "" || n == "") error("Employee ID and name cannot be empty"); + emp_id = id; + name = n; + bedehi = 0; + } + string get_id() { return emp_id; } + string to_string() { + ostringstream os; + os << '[' << emp_id << "] " << name; + return os.str(); + } +private: + int bedehi; + string emp_id; + string name; +}; + +class MenuItem { +public: + MenuItem(string t, int p) { + if (t == "") error("Menu item title cannot be empty"); + if (p <= 0) error("Menu item price must be positive"); + title = t; + price = p; + } + string get_title() { return title; } + string to_string() { + ostringstream os; + os << title << '(' << price << ')'; + return os.str(); + } +private: + string title; + int price; +}; + +class EmployeeRequest { +public: + EmployeeRequest(Employee* e, MenuItem* m) { + if (e == NULL || m == NULL) error("Employee request parameters must be non-null"); + item = m; + emp = e; + } + string to_string() { + ostringstream os; + os << emp->to_string() << '\t' << item->to_string(); + return os.str(); + } +private: + Employee* emp; + MenuItem* item; +}; + +class Catering { +public: + Catering(string n, vector m) { + name = n; + menu = m; + } + string get_name() { return name; } + + void add_employee_request(Employee* emp, string item_title) { + requests.push_back(EmployeeRequest(emp, find_item_by_title(item_title))); + } + string to_string() { + ostringstream os; + os << "Requests placed for catering " << name << endl; + for (EmployeeRequest request : requests) + os << request.to_string() << endl; + return os.str(); + } +private: + MenuItem* find_item_by_title(string item_title) { + for (int i = 0; i < menu.size(); i++) + if (menu[i].get_title() == item_title) + return &menu[i]; + error("cannot find menu item"); + } + + string name; + vector menu; + vector requests; +}; + +class Ehtesham { +public: + Ehtesham(vector c, vector e) { + caterings = c; + employees = e; + } + string to_string() { + ostringstream os; + os << "Employees:" << endl; + for (Employee employee : employees) + os << employee.to_string() << endl; + os << endl; + for (Catering catering : caterings) + os << catering.to_string() << endl; + return os.str(); + } + + void add_employee_request(string empid, string catering_name, string item) { + Catering* cat = find_catering_by_name(catering_name); + Employee* emp = find_employee_by_id(empid); + cat->add_employee_request(emp, item); + } +private: + Employee* find_employee_by_id(string empid) { + for (int i = 0; i < employees.size(); i++) + if (employees[i].get_id() == empid) + return &employees[i]; + error("cannot find employee"); + } + + Catering* find_catering_by_name(string name) { + for (int i = 0; i < caterings.size(); i++) + if (caterings[i].get_name() == name) + return &caterings[i]; + error("cannot find catering"); + } + + vector caterings; + vector employees; +}; + +int main() { + Ehtesham ehtesham( + { + Catering("Farsi", { MenuItem("Kabab", 50000), MenuItem("Jooj", 30000) }), + Catering("Alaa", { MenuItem("Kabab", 40000), MenuItem("Maahi", 35000) }) + }, + { + Employee("1001", "Jamshid"), + Employee("1002", "Mahvash"), + Employee("1003", "Hooshang") + } + ); + + string command; + while (cin >> command) { + if (command == "request") { + string empid; + string catering; + string item; + + cin >> empid >> catering >> item; + ehtesham.add_employee_request(empid, catering, item); + } else if (command == "dump") { + cout << ehtesham.to_string() << endl; + } else { + error("unknown command"); + } + } +} + + + + diff --git a/20-OODesign/ehtesham5.cpp b/20-OODesign/ehtesham5.cpp new file mode 100644 index 0000000..49e0010 --- /dev/null +++ b/20-OODesign/ehtesham5.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include +using namespace std; + +// Since we have not learned exception handling... +void error(string message) { + cerr << message << endl; + abort(); +} + +class Employee { +public: + Employee(string id, string n) { + if (id == "" || n == "") error("Employee ID and name cannot be empty"); + emp_id = id; + name = n; + bedehi = 0; + } + string get_id() { return emp_id; } +private: + int bedehi; + string emp_id; + string name; +}; + +class MenuItem { +public: + MenuItem(string t, int p) { + if (t == "") error("Menu item title cannot be empty"); + if (p <= 0) error("Menu item price must be positive"); + title = t; + price = p; + } + string get_title() { return title; } +private: + string title; + int price; +}; + +class EmployeeRequest { +public: + EmployeeRequest(Employee* e, MenuItem* m) { + if (e == NULL || m == NULL) error("Employee request parameters must be non-null"); + item = m; + emp = e; + } +private: + Employee* emp; + MenuItem* item; +}; + +class Catering { +public: + Catering(string n, vector m) { + name = n; + menu = m; + } + string get_name() { return name; } + + void add_employee_request(Employee* emp, string item_title) { + requests.push_back(EmployeeRequest(emp, find_item_by_title(item_title))); + } +private: + MenuItem* find_item_by_title(string item_title) { + for (int i = 0; i < menu.size(); i++) + if (menu[i].get_title() == item_title) + return &menu[i]; + error("cannot find menu item"); + } + + string name; + vector menu; + vector requests; +}; + +class Ehtesham { +public: + Ehtesham(vector c, vector e) { + caterings = c; + employees = e; + } + + void add_employee_request(string empid, string catering_name, string item) { + Catering* cat = find_catering_by_name(catering_name); + Employee* emp = find_employee_by_id(empid); + cat->add_employee_request(emp, item); + } +private: + Employee* find_employee_by_id(string empid) { + for (int i = 0; i < employees.size(); i++) + if (employees[i].get_id() == empid) + return &employees[i]; + error("cannot find employee"); + } + + Catering* find_catering_by_name(string name) { + for (int i = 0; i < caterings.size(); i++) + if (caterings[i].get_name() == name) + return &caterings[i]; + error("cannot find catering"); + } + + vector caterings; + vector employees; +}; + +int main() { + Ehtesham ehtesham( + { + Catering("Farsi", { MenuItem("Kabab", 50000), MenuItem("Jooj", 30000) }), + Catering("Alaa", { MenuItem("Kabab", 40000), MenuItem("Maahi", 35000) }) + }, + { + Employee("1001", "Jamshid"), + Employee("1002", "Mahvash"), + Employee("1003", "Hooshang") + } + ); + + string command; + while (cin >> command) { + if (command == "request") { + string empid; + string catering; + string item; + + cin >> empid >> catering >> item; + ehtesham.add_employee_request(empid, catering, item); + } else { + error("unknown command"); + } + } +} + + + + diff --git a/20-OODesign/ehtesham6.cpp b/20-OODesign/ehtesham6.cpp new file mode 100644 index 0000000..a551bb9 --- /dev/null +++ b/20-OODesign/ehtesham6.cpp @@ -0,0 +1,281 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +// Since we have not learned exception handling... +void error(string message) { + cerr << message << endl; + abort(); +} + +class Employee { +public: + Employee(string id, string n) { + if (id == "" || n == "") error("Employee ID and name cannot be empty"); + emp_id = id; + name = n; + bedehi = 0; + } + string get_id() { return emp_id; } + int get_bedehi() { return bedehi; } + + void charge(int amount) { + bedehi += amount; + } + + string to_string() { + ostringstream os; + os << '[' << emp_id << "] " << name; + return os.str(); + } +private: + int bedehi; + string emp_id; + string name; +}; + +class MenuItem { +public: + MenuItem(string t, int p) { + if (t == "") error("Menu item title cannot be empty"); + if (p <= 0) error("Menu item price must be positive"); + title = t; + price = p; + } + string get_title() { return title; } + int get_price() { return price; } + string to_string() { + ostringstream os; + os << title << '(' << price << ')'; + return os.str(); + } +private: + string title; + int price; +}; + +class EmployeeRequest { +public: + EmployeeRequest(Employee* e, MenuItem* m) { + if (e == NULL || m == NULL) error("Employee request parameters must be non-null"); + item = m; + emp = e; + } + MenuItem* get_item() { return item; } + Employee* get_employee() { return emp; } + string to_string() { + ostringstream os; + os << emp->to_string() << '\t' << item->to_string(); + return os.str(); + } +private: + Employee* emp; + MenuItem* item; +}; + +class CateringOrderLine { +public: + CateringOrderLine(MenuItem* m, int q = 1) { + item = m; + quantity = q; + } + MenuItem* get_item() { return item; } + + void increase_quantity(int q = 1) { quantity += q; } + + string to_string() { + ostringstream os; + os << item->to_string() << " " << quantity; + return os.str(); + } +private: + MenuItem* item; + int quantity; +}; + +class CateringOrder { +public: + void add_employee_request(EmployeeRequest req) { + for (CateringOrderLine& line : orderLines) + if (line.get_item() == req.get_item()) { + line.increase_quantity(); + return; + } + orderLines.push_back(CateringOrderLine(req.get_item())); + } + string to_string() { + ostringstream os; + for (CateringOrderLine line : orderLines) + os << '\t' << line.to_string() << endl; + return os.str(); + } +private: + vector orderLines; +}; + +class Catering { +public: + Catering(string n, vector m) { + name = n; + menu = m; + } + string get_name() { return name; } + string to_string() { + ostringstream os; + os << "Requests placed for catering " << name << endl; + for (EmployeeRequest request : requests) + os << request.to_string() << endl; + return os.str(); + } + void add_employee_request(Employee* emp, string item_title) { + requests.push_back(EmployeeRequest(emp, find_item_by_title(item_title))); + } + + CateringOrder gen_catering_order() { + CateringOrder order; + for (EmployeeRequest req : requests) { + order.add_employee_request(req); + } + return order; + } + + + + void delivered(int delivery_cost) { + set employees; + for (EmployeeRequest request : requests) { + Employee* emp = request.get_employee(); + emp->charge(request.get_item()->get_price()); + employees.insert(emp); + } + for (Employee* emp : employees) + emp->charge(delivery_cost / employees.size()); + } +private: + MenuItem* find_item_by_title(string item_title) { + for (int i = 0; i < menu.size(); i++) + if (menu[i].get_title() == item_title) + return &menu[i]; + error("cannot find menu item"); + } + + string name; + vector menu; + vector requests; +}; + +class Ehtesham { +public: + Ehtesham(vector c, vector e) { + caterings = c; + employees = e; + } + string to_string() { + ostringstream os; + os << "Employees:" << endl; + for (Employee employee : employees) + os << employee.to_string() << endl; + os << endl; + for (Catering catering : caterings) + os << catering.to_string() << endl; + return os.str(); + } + void add_employee_request(string empid, string catering_name, string item) { + Catering* cat = find_catering_by_name(catering_name); + Employee* emp = find_employee_by_id(empid); + cat->add_employee_request(emp, item); + } + + + + CateringOrder gen_catering_order(string catering_name) { + Catering* cat = find_catering_by_name(catering_name); + return cat->gen_catering_order(); + } + + + + void delivery_from(string catering_name, int delivery_cost) { + Catering* cat = find_catering_by_name(catering_name); + cat->delivered(delivery_cost); + } + string bedehi_report() { + ostringstream os; + for (Employee emp : employees) { + os << emp.to_string() << "\t" << emp.get_bedehi() << endl; + } + return os.str(); + } +private: + Employee* find_employee_by_id(string empid) { + for (int i = 0; i < employees.size(); i++) + if (employees[i].get_id() == empid) + return &employees[i]; + error("cannot find employee"); + } + + Catering* find_catering_by_name(string name) { + for (int i = 0; i < caterings.size(); i++) + if (caterings[i].get_name() == name) + return &caterings[i]; + error("cannot find catering"); + } + + vector caterings; + vector employees; +}; + +int main() { + Ehtesham ehtesham( + { + Catering("Farsi", { MenuItem("Kabab", 50000), MenuItem("Jooj", 30000) }), + Catering("Alaa", { MenuItem("Kabab", 40000), MenuItem("Maahi", 35000) }) + }, + { + Employee("1001", "Jamshid"), + Employee("1002", "Mahvash"), + Employee("1003", "Hooshang") + } + ); + + string command; + while (cin >> command) { + if (command == "request") { + string empid; + string catering; + string item; + + cin >> empid >> catering >> item; + ehtesham.add_employee_request(empid, catering, item); + + cout << "Request from employee " << empid << " handled!" << endl << endl; + + } else if (command == "catorder") { + string catering_name; + cin >> catering_name; + CateringOrder order = ehtesham.gen_catering_order(catering_name); + cout << "Order for catering " << catering_name << ":\n"; + cout << order.to_string() << endl; + + } else if (command == "delivery") { + string catering_name; + int delivery_cost; + + cin >> catering_name; + cin >> delivery_cost; + ehtesham.delivery_from(catering_name, delivery_cost); + cout << "Delivery from " << catering_name << " handled!" << endl << endl; + } else if (command == "bedehi") { + cout << "Bedehi Report: " << endl << ehtesham.bedehi_report() << endl; + } else if (command == "dump") { + cout << ehtesham.to_string() << endl; + } else { + cerr << "unknown command\n"; + abort(); + } + } +} \ No newline at end of file diff --git a/20-OODesign/ehtesham7.cpp b/20-OODesign/ehtesham7.cpp new file mode 100644 index 0000000..890ee28 --- /dev/null +++ b/20-OODesign/ehtesham7.cpp @@ -0,0 +1,289 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +// Since we have not learned exception handling... +void error(string message) { + cerr << message << endl; + abort(); +} + +class Employee { +public: + Employee(string id, string n) { + if (id == "" || n == "") error("Employee ID and name cannot be empty"); + emp_id = id; + name = n; + bedehi = 0; + } + string get_id() { return emp_id; } + int get_bedehi() { return bedehi; } + + void charge(int amount) { + bedehi += amount; + } + + string to_string() { + ostringstream os; + os << '[' << emp_id << "] " << name; + return os.str(); + } +private: + int bedehi; + string emp_id; + string name; +}; + +class MenuItem { +public: + MenuItem(string t, int p) { + if (t == "") error("Menu item title cannot be empty"); + if (p <= 0) error("Menu item price must be positive"); + title = t; + price = p; + } + string get_title() { return title; } + int get_price() { return price; } + string to_string() { + ostringstream os; + os << title << '(' << price << ')'; + return os.str(); + } +private: + string title; + int price; +}; + +class EmployeeRequest { +public: + EmployeeRequest(Employee* e, MenuItem* m) { + if (e == NULL || m == NULL) error("Employee request parameters must be non-null"); + item = m; + emp = e; + } + MenuItem* get_item() { return item; } + Employee* get_employee() { return emp; } + string to_string() { + ostringstream os; + os << emp->to_string() << '\t' << item->to_string(); + return os.str(); + } +private: + Employee* emp; + MenuItem* item; +}; + +class CateringOrderLine { +public: + CateringOrderLine(MenuItem* m, int q = 1) { + item = m; + quantity = q; + } + MenuItem* get_item() { return item; } + + void increase_quantity(int q = 1) { quantity += q; } + + string to_string() { + ostringstream os; + os << item->to_string() << " " << quantity; + return os.str(); + } +private: + MenuItem* item; + int quantity; +}; + +class CateringOrder { +public: + void add_employee_request(EmployeeRequest req) { + for (CateringOrderLine& line : orderLines) + if (line.get_item() == req.get_item()) { + line.increase_quantity(); + return; + } + orderLines.push_back(CateringOrderLine(req.get_item())); + } + string to_string() { + ostringstream os; + for (CateringOrderLine line : orderLines) + os << '\t' << line.to_string() << endl; + return os.str(); + } +private: + vector orderLines; +}; + +class Catering { +public: + Catering(string n, vector m) { + name = n; + menu = m; + } + string get_name() { return name; } + string to_string() { + ostringstream os; + os << "Requests placed for catering " << name << endl; + for (EmployeeRequest request : requests) + os << request.to_string() << endl; + return os.str(); + } + void add_employee_request(Employee* emp, string item_title) { + requests.push_back(EmployeeRequest(emp, find_item_by_title(item_title))); + } + + CateringOrder gen_catering_order() { + CateringOrder order; + for (EmployeeRequest req : requests) { + order.add_employee_request(req); + } + return order; + } + + void delivered(int delivery_cost) { + set employees; + for (EmployeeRequest request : requests) { + Employee* emp = request.get_employee(); + emp->charge(request.get_item()->get_price()); + employees.insert(emp); + } + for (Employee* emp : employees) + emp->charge(delivery_cost / employees.size()); + } +private: + MenuItem* find_item_by_title(string item_title) { + for (int i = 0; i < menu.size(); i++) + if (menu[i].get_title() == item_title) + return &menu[i]; + error("cannot find menu item"); + return NULL; // just to prevent warnings + } + + string name; + vector menu; + vector requests; +}; + +class Ehtesham { +public: + Ehtesham(vector c, vector e) { + caterings = c; + employees = e; + } + string to_string() { + ostringstream os; + os << "Employees:" << endl; + for (Employee employee : employees) + os << employee.to_string() << endl; + os << endl; + for (Catering catering : caterings) + os << catering.to_string() << endl; + return os.str(); + } + + void add_employee_request(string empid, string catering_name, string item) { + Catering* cat = find_catering_by_name(catering_name); + Employee* emp = find_employee_by_id(empid); + cat->add_employee_request(emp, item); + } + + CateringOrder gen_catering_order(string catering_name) { + Catering* cat = find_catering_by_name(catering_name); + return cat->gen_catering_order(); + } + + void delivery_from(string catering_name, int delivery_cost) { + Catering* cat = find_catering_by_name(catering_name); + cat->delivered(delivery_cost); + } + + string bedehi_report() { + ostringstream os; + for (Employee emp : employees) { + os << emp.to_string() << "\t" << emp.get_bedehi() << endl; + } + return os.str(); + } + + void add_new_employee(string id, string name) { + employees.push_back(Employee(id, name)); + } +private: + Employee* find_employee_by_id(string empid) { + for (int i = 0; i < employees.size(); i++) + if (employees[i].get_id() == empid) + return &employees[i]; + error("cannot find employee"); + return NULL; // just to prevent warnings + } + + Catering* find_catering_by_name(string name) { + for (int i = 0; i < caterings.size(); i++) + if (caterings[i].get_name() == name) + return &caterings[i]; + error("cannot find catering"); + return NULL; // just to prevent warnings + } + + vector caterings; + vector employees; +}; + +int main() { + Ehtesham ehtesham( + { + Catering("Farsi", { MenuItem("Kabab", 50000), MenuItem("Jooj", 30000) }), + Catering("Alaa", { MenuItem("Kabab", 40000), MenuItem("Maahi", 35000) }) + }, + { + Employee("1001", "Jamshid"), + Employee("1002", "Mahvash"), + Employee("1003", "Hooshang") + } + ); + + string command; + while (cin >> command) { + if (command == "request") { + string empid; + string catering; + string item; + + cin >> empid >> catering >> item; + ehtesham.add_employee_request(empid, catering, item); + + cout << "Request from employee " << empid << " handled!" << endl << endl; + + } else if (command == "catorder") { + string catering_name; + cin >> catering_name; + CateringOrder order = ehtesham.gen_catering_order(catering_name); + cout << "Order for catering " << catering_name << ":\n"; + cout << order.to_string() << endl; + + } else if (command == "delivery") { + string catering_name; + int delivery_cost; + + cin >> catering_name; + cin >> delivery_cost; + ehtesham.delivery_from(catering_name, delivery_cost); + cout << "Delivery from " << catering_name << " handled!" << endl << endl; + } else if (command == "bedehi") { + cout << "Bedehi Report: " << endl << ehtesham.bedehi_report() << endl; + } else if (command == "newemployee") { + string empid; + string empname; + cin >> empid >> empname; + ehtesham.add_new_employee(empid, empname); + } else if (command == "dump") { + cout << ehtesham.to_string() << endl; + } else { + cerr << "unknown command\n"; + abort(); + } + } +} \ No newline at end of file diff --git a/20-OODesign/ehtesham8.cpp b/20-OODesign/ehtesham8.cpp new file mode 100644 index 0000000..1acaad4 --- /dev/null +++ b/20-OODesign/ehtesham8.cpp @@ -0,0 +1,297 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +// Since we have not learned exception handling... +void error(string message) { + cerr << message << endl; + abort(); +} + +class Employee { +public: + Employee(string id, string n) { + if (id == "" || n == "") error("Employee ID and name cannot be empty"); + emp_id = id; + name = n; + bedehi = 0; + } + string get_id() { return emp_id; } + int get_bedehi() { return bedehi; } + + void charge(int amount) { + bedehi += amount; + } + + string to_string() { + ostringstream os; + os << '[' << emp_id << "] " << name; + return os.str(); + } +private: + int bedehi; + string emp_id; + string name; +}; + +class MenuItem { +public: + MenuItem(string t, int p) { + if (t == "") error("Menu item title cannot be empty"); + if (p <= 0) error("Menu item price must be positive"); + title = t; + price = p; + } + string get_title() { return title; } + int get_price() { return price; } + string to_string() { + ostringstream os; + os << title << '(' << price << ')'; + return os.str(); + } +private: + string title; + int price; +}; + +class EmployeeRequest { +public: + EmployeeRequest(Employee* e, MenuItem* m) { + if (e == NULL || m == NULL) error("Employee request parameters must be non-null"); + item = m; + emp = e; + } + MenuItem* get_item() { return item; } + Employee* get_employee() { return emp; } + string to_string() { + ostringstream os; + os << emp->to_string() << '\t' << item->to_string(); + return os.str(); + } +private: + Employee* emp; + MenuItem* item; +}; + +class CateringOrderLine { +public: + CateringOrderLine(MenuItem* m, int q = 1) { + item = m; + quantity = q; + } + MenuItem* get_item() { return item; } + + void increase_quantity(int q = 1) { quantity += q; } + + string to_string() { + ostringstream os; + os << item->to_string() << " " << quantity; + return os.str(); + } +private: + MenuItem* item; + int quantity; +}; + +class CateringOrder { +public: + void add_employee_request(EmployeeRequest req) { + for (CateringOrderLine& line : orderLines) + if (line.get_item() == req.get_item()) { + line.increase_quantity(); + return; + } + orderLines.push_back(CateringOrderLine(req.get_item())); + } + string to_string() { + ostringstream os; + for (CateringOrderLine line : orderLines) + os << '\t' << line.to_string() << endl; + return os.str(); + } +private: + vector orderLines; +}; + +class Catering { +public: + Catering(string n, vector m) { + name = n; + menu = m; + } + string get_name() { return name; } + string to_string() { + ostringstream os; + os << "Requests placed for catering " << name << endl; + for (EmployeeRequest request : requests) + os << request.to_string() << endl; + return os.str(); + } + void add_employee_request(Employee* emp, string item_title) { + requests.push_back(EmployeeRequest(emp, find_item_by_title(item_title))); + } + + CateringOrder gen_catering_order() { + CateringOrder order; + for (EmployeeRequest req : requests) { + order.add_employee_request(req); + } + return order; + } + + void delivered(int delivery_cost) { + set employees; + for (EmployeeRequest request : requests) { + Employee* emp = request.get_employee(); + emp->charge(request.get_item()->get_price()); + employees.insert(emp); + } + for (Employee* emp : employees) + emp->charge(delivery_cost / employees.size()); + } +private: + MenuItem* find_item_by_title(string item_title) { + for (int i = 0; i < menu.size(); i++) + if (menu[i].get_title() == item_title) + return &menu[i]; + error("cannot find menu item"); + return NULL; // just to prevent warnings + } + + string name; + vector menu; + vector requests; +}; + +class Ehtesham { +public: + Ehtesham(vector c, vector e) { + caterings = c; + employees = e; + } + string to_string() { + ostringstream os; + os << "Employees:" << endl; + for (Employee* employee : employees) + os << employee->to_string() << endl; + os << endl; + for (Catering* catering : caterings) + os << catering->to_string() << endl; + return os.str(); + } + + void add_employee_request(string empid, string catering_name, string item) { + Catering* cat = find_catering_by_name(catering_name); + Employee* emp = find_employee_by_id(empid); + cat->add_employee_request(emp, item); + } + + CateringOrder gen_catering_order(string catering_name) { + Catering* cat = find_catering_by_name(catering_name); + return cat->gen_catering_order(); + } + + void delivery_from(string catering_name, int delivery_cost) { + Catering* cat = find_catering_by_name(catering_name); + cat->delivered(delivery_cost); + } + + string bedehi_report() { + ostringstream os; + for (Employee* emp : employees) { + os << emp->to_string() << "\t" << emp->get_bedehi() << endl; + } + return os.str(); + } + + void add_new_employee(string id, string name) { + employees.push_back(new Employee(id, name)); + } + + void delete_everything() { + for (Catering* catering : caterings) + delete catering; + for (Employee* employee : employees) + delete employee; + } +private: + Employee* find_employee_by_id(string empid) { + for (int i = 0; i < employees.size(); i++) + if (employees[i]->get_id() == empid) + return employees[i]; + error("cannot find employee"); + return NULL; // just to prevent warnings + } + + Catering* find_catering_by_name(string name) { + for (Catering* catering : caterings) + if (catering->get_name() == name) + return catering; + error("cannot find catering"); + return NULL; // just to prevent warnings + } + + vector caterings; + vector employees; +}; + +int main() { + Ehtesham ehtesham( + { + new Catering("Farsi", { MenuItem("Kabab", 50000), MenuItem("Jooj", 30000) }), + new Catering("Alaa", { MenuItem("Kabab", 40000), MenuItem("Maahi", 35000) }) + }, + { + new Employee("1001", "Jamshid"), + new Employee("1002", "Mahvash"), + new Employee("1003", "Hooshang") + } + ); + + string command; + while (cin >> command) { + if (command == "request") { + string empid; + string catering; + string item; + + cin >> empid >> catering >> item; + ehtesham.add_employee_request(empid, catering, item); + + cout << "Request from employee " << empid << " handled!" << endl << endl; + + } else if (command == "catorder") { + string catering_name; + cin >> catering_name; + CateringOrder order = ehtesham.gen_catering_order(catering_name); + cout << "Order for catering " << catering_name << ":\n"; + cout << order.to_string() << endl; + + } else if (command == "delivery") { + string catering_name; + int delivery_cost; + + cin >> catering_name; + cin >> delivery_cost; + ehtesham.delivery_from(catering_name, delivery_cost); + cout << "Delivery from " << catering_name << " handled!" << endl << endl; + } else if (command == "bedehi") { + cout << "Bedehi Report: " << endl << ehtesham.bedehi_report() << endl; + } else if (command == "newemployee") { + string empid; + string empname; + cin >> empid >> empname; + ehtesham.add_new_employee(empid, empname); + } else if (command == "dump") { + cout << ehtesham.to_string() << endl; + } else { + cerr << "unknown command\n"; + abort(); + } + } + ehtesham.delete_everything(); +} \ No newline at end of file diff --git a/20-OODesign/input1.txt b/20-OODesign/input1.txt new file mode 100644 index 0000000..adee33d --- /dev/null +++ b/20-OODesign/input1.txt @@ -0,0 +1,4 @@ +request 1001 Farsi Kabab +request 1002 Farsi Kabab +request 1003 Alaa Maahi +request 1002 Farsi Jooj diff --git a/20-OODesign/input2.txt b/20-OODesign/input2.txt new file mode 100644 index 0000000..691341c --- /dev/null +++ b/20-OODesign/input2.txt @@ -0,0 +1,5 @@ +request 1001 Farsi Kabab +request 1002 Farsi Kabab +request 1003 Alaa Maahi +request 1002 Farsi Jooj +dump diff --git a/20-OODesign/input3.txt b/20-OODesign/input3.txt new file mode 100644 index 0000000..db25c37 --- /dev/null +++ b/20-OODesign/input3.txt @@ -0,0 +1,10 @@ +request 1001 Farsi Kabab +request 1002 Farsi Kabab +request 1003 Alaa Maahi +request 1002 Farsi Jooj +catorder Farsi +catorder Alaa +delivery Farsi 10000 +bedehi +delivery Alaa 10000 +bedehi \ No newline at end of file diff --git a/20-OODesign/input4.txt b/20-OODesign/input4.txt new file mode 100644 index 0000000..fc5effa --- /dev/null +++ b/20-OODesign/input4.txt @@ -0,0 +1,11 @@ +request 1001 Farsi Kabab +request 1002 Farsi Kabab +request 1003 Alaa Maahi +request 1002 Farsi Jooj +catorder Farsi +catorder Alaa +newemployee 1004 Hakeem +delivery Farsi 10000 +bedehi +delivery Alaa 10000 +bedehi