COMP 2012 Object-Oriented Programming and Data

COMP 2012 Assignment 1: Food Ordering System 29/03/2024, 23:23
COMP 2012 Object-Oriented Programming and Data
Structures
Assignment 1 Food Ordering System
Source: DALL-E
Introduction
In this assignment, we will combine the concepts of Object-Oriented Programming (OOP) principles and utilizes C++ to construct a food ordering system. We will leverage the power of OOP concepts like classes and objects to model real-world entities such as Order,
Buyer, and Menu. These classes will encapsulate relevant data and functionalities, ensuring proper implementation. Additionally, we will employ basic C++ constructs like functions, loops, and conditional statements to implement functionalities like adding items to the menu, placing orders, and managing user interactions.
Furthermore, for this assignment you will work on these major functionalities.
Seller Actions
View menu
Update menu
Add food item
Update food price
Remove food item
View orders
Process orders
Buyer Tasks
View menu
Place order
View my order
Copy an order
End of Introduction
Code overview
The following section describes the given classes and their purposes. You are given the
following files:
Buyer.h
Food.h
Menu.h
Order.h
OrderList.h
main.cpp
Now let's have a quick go through for each files that includes classes and function to create a Food Ordering System.
Buyer Class (Buyer.h)
The file Buyer.h is a C++ header file defining a class named Buyer. The Buyer class provides a way to create and manage buyer objects with their unique IDs and names. It offers methods to access this information securely and modify it when necessary.
class Buyer {
private:
int buyerID;
string buyerName;
public:
//Default Constructor
Buyer(int id = 0, const string& name = "");
//Setter and getter methods for Buyer ID
int getBuyerId() const;
void setBuyerId(int newId);
//Setter and getter methods for Buyer Name
string getBuyerName() const;
void setBuyerName(const string& newName);
};
Explanation of Buyer Class
The provided code defines a class named `Buyer` that represents a buyer in a system.
Member Variables
buyerID: An integer variable to store the unique identifier of the buyer.
buyerName: A string variable to store the name of the buyer.
Constructor
Buyer(int id = 0, const string& name = "")
This is the constructor for the `Buyer` class. It takes two optional arguments to initialize a new `Buyer` object:
id (integer): An integer representing the buyer's ID (defaults to 0). name (const string&): A constant string reference to the buyer's name (defaults to an empty string).
Member Functions (Getters and Setters)
The `Buyer` class provides methods to access and modify its member variables in a controlled manner:
getBuyerId() const: This function retrieves the current value of the `buyerID`
member variable and returns it as an integer. It's a **const** function, meaning it guarantees not to modify the object's state.
setBuyerId(int newId): This function allows you to set a new value for the `buyerID` member variable. It takes an integer argument (`newId`) representing the new buyer ID.
getBuyerName() const: This function retrieves the current value of the `buyerName` member variable and returns it as a string. Similar to `getBuyerId`, it's a
**const** function.
setBuyerName(const string& newName): This function allows you to set a new value for the `buyerName` member variable. It takes a constant string reference (`newName`) representing the buyer's new name.
By using getters and setters, the `Buyer` class promotes data encapsulation. It restricts direct access to the member variables and provides controlled ways to retrieve and update their values.
Food Class (Food.h)
The file Food.h is a C++ header file defining a Food class. The Food class provides a structured way to represent and manage food items with their unique IDs, names, and prices. It offers methods to control access to these attributes and allows for creating deep copies of existing food items.
class Food {
private:
int foodID;
string foodName;
float price;
public:
//Default Constructor
Food(int id = 0, const string& name = "", float price = 0.0);
// Copy constructor for Food
Food(const Food& other);
//Setter and getter method for Food ID
void setFoodId(int newId);
int getFoodId() const;
//Setter and getter method for Food Name
void setFoodName(const string& newName);
string getFoodName() const;
//Setter and getter method for Food Price
void setPrice(float newPrice);
float getPrice() const;
};
Explanation of Food Class
The provided code defines a class named `Food` that represents a food item within a system.
Member Variables
foodID: An integer variable to store the unique identifier of the food item.
foodName: A string variable to store the name of the food item.
price: A float variable to store the price of the food item.
Constructors
The `Food` class includes two constructors for creating new `Food` objects:
Default Constructor:
Food(int id = 0, const string& name = "", float price = 0.0);
This constructor takes three optional arguments for initializing a new `Food` object: id (integer): An integer representing the food item's ID (defaults to 0). name (const string&): A constant string reference to the food item's name (defaults to an empty string).
price (float): A floating-point number representing the price of the food item (defaults to 0.0).
Copy Constructor:
Food(const Food& other);
This constructor creates a deep copy of an existing `Food` object, ensuring independent memory allocation for the copied data. While completing the copy constructor task make sure you implement this as a delegated constructor.
Member Functions (Getters and Setters)
The `Food` class provides methods to access and modify its member variables:
getFoodId(): Returns the current value of the `foodID` member variable.
setFoodId(int newId): Sets a new value for the `foodID` member variable.
getFoodName(): Returns the current value of the `foodName` member variable.
setFoodName(const string& newName): Sets a new value for the `foodName`
member variable.
getPrice(): Returns the current value of the `price` member variable.
setPrice(float newPrice): Sets a new value for the `price` member variable.
Menu Class (Menu.h)
The file Menu.h is a C++ header file defining a Menu class. The Menu class provides a
structured way to manage a collection of food items within a menu system. It provides
functionalities to add, remove, display, and manage food items within a menu.
Explanation of Menu Class
class Menu {
private:
Food* foodItems; // Dynamic array of food items
int currentSize; // Current number of food items
int capacity; // Maximum capacity of the array
int nextFoodId; // Track the next available food item ID
public:
Menu(); // Constructor to initialize currentSize and allocate memory for foodItems
~Menu(); // Destructor to free allocated memory
void addFood(const Food& food);
bool removeFood(int id);
void displayMenu() const;
void manageFoodItems();
const Food* getFoodById(int id) const;
private: //Helper Functions
bool isFull() const { return currentSize == capacity; }
void resizeArray(); // Resizes the array if needed
int findFoodIndex(int id) const;
};
The provided code defines a class named `Menu` that represents a menu system for managing food items.
Member Variables
foodItems: A pointer to a dynamic array of `Food` objects. This array stores the actual food items included in the menu.
currentSize: An integer variable that keeps track of the current number of food items stored in the `foodItems` array.
capacity: An integer variable that specifies the maximum number of food items the `foodItems` array can hold.
nextFoodId: An integer variable that is used as a counter to track the next available food item ID which should be a unique value. This variable is then used to assign new Food item ID (a new and unique value) when added to menu.
Public Member Functions

Menu() (Constructor): This constructor initializes the `Menu` object. It sets `currentSize` to 0, 'nextFoodId' to 1, and allocates memory for the `foodItems` array with a chosen initial capacity. 

NOTE: Assign capacity value to 10 in the beginning. 

~Menu() (Destructor): This destructor is responsible for freeing the memory allocated for the `foodItems` array when a `Menu` object goes out of scope, preventing memory leaks. 

addFood(const Food& food): This function adds a new food item to the menu. It takes a reference to a `Food` object (`food`) and adds it to the `foodItems` array. 

removeFood(int id): This function removes a food item from the menu based on its ID (`id`). It searches for the food item with the matching ID and removes it from the `foodItems` array. 

displayMenu() const: This function displays the information about all food items currently in the menu. It iterates through the `foodItems` array and displays details of each food item. manageFoodItems(): This function provides an interface for managing food items in the menu. It offer options to add, remove, edit, or perform other operations on the food items.

getFoodById(int id) const: This function retrieves a pointer to a `Food` object from the menu based on its ID (`id`). It searches for the food item with the matching ID and returns a pointer to it, or `nullptr` if not found.
Private Helper Functions
isFull() const: This helper function checks if the `foodItems` array is at full capacity. It returns `true` if the `currentSize` equals the `capacity`.
resizeArray(): This private function handles resizing the `foodItems` array if needed. It called by `addFood` if the array is full, increasing the capacity and reallocating memory for the array.
findFoodIndex(int id) const: This helper function searches for the index of a food item with a specific ID (`id`) within the `foodItems` array. It iterates through the array and returns the index if a match is found, or -1 if not found.
Order Class (Order.h)
The file Order.h is a C++ header file defining a Order class. The Order class provides a way to represent and manage customer orders, including the ordered food items, buyer information, and unique order ID. It offers functionalities to add items (with max size 10), display order details, create order clones, and retrieve relevant information.
Menu
Introduction
Description
Resources & Sample
I/O
Submission &
Deadline
Explanation of Order Class
The provided code defines a class named `Order` that represents a customer's order within a system. It manages the details of the ordered items, buyer information, and order ID.
Member Variables `orderId (private)`: This integer variable stores a unique identifier that distinguishes each order within the system, ensuring no duplicate orders are created. `currentNum (private)`: This integer variable tracks the current number of food items included in the order, indicating the order's overall size. `orderedItems (private)`: This array of `Food` objects, with a fixed capacity of 10, stores the individual food items that have been added to the order. It acts as a central repository for the order's contents. NOTE: DO NOT CHANGE THE ARRAY SIZE. `buyer (private)`: This member variable holds an object of the `Buyer` class, containing information about the customer who placed the order. This enables association of orders with specific buyers.
Public Member Functions
Order() (Default Constructor): This function automatically initializes a new `Order`
object upon its creation. It will assign the 'orderId' and 'currentNum' value to 0.
addItem(const Food& item): This function is responsible for adding a new `Food`
item to the `orderedItems` array. It takes a reference to a `Food` object (`item`) as
input. However, it needs to manage the fixed size of the array, by returning a
success/failure indicator.
displayOrder() const: This function presents a visual summary of the order's details, including the order ID, buyer information, and a list of the ordered food items with their relevant details. It provides a clear readout of the order's contents. cloneOrder(const Order& other): This function generates a self-contained copy of another existing `Order` object (`other`). It involves replicating the `currentNum`, and performing deep copies of the `orderedItems` array to ensure independent memory storage.
getBuyer() const: This function grants read-only access to the `buyer` object
within the order, enabling retrieval of buyer information without the ability to modify the
class Order {
private:
int orderId;
int currentNum;
Food orderedItems[10]; // Array to hold ordered food items (fixed size)
Buyer buyer;
public:
Order(); // Default Constructor
bool addItem(const Food& item);
void displayOrder() const;
void cloneOrder(const Order& other);
const Buyer& getBuyer() const;
void setBuyer(int newBuyerId, string newBuyerName);
int getcurrentNum() const;
void setOrderId(int newId);
int getOrderId() const;
};
Frequently Asked
Questions
Changelog
Page maintained by
USMAN Muhammad
Last Modified:
03/29/2024 11:50:38
Homepage
Course Homepage
original object. It maintains data integrity.
getCurrentNum() const: This function reveals the current count of food items within the order, providing insight into the order's size and contents. setOrderId() const: Sets a new value for the `orderId` member variable.
getOrderId() const: This function returns the unique order ID, allowing for external identification and tracking of specific orders within the system.
Order List Class (OrderList.h)
The file OrderList.h is a C++ header file defining a OrderList class. The OrderList class provides a way to manage a collection of orders using a linked list. It offers functionalities to add, remove, find, and display orders in the system.
Explanation of OrderList Class
The provided code defines a class named `OrderList` that manages a collection of `Order` objects using a linked list data structure. It provides functionalities to add, remove, find, display, and filter orders based on buyer ID.
Member Variables
**Private:**
head: A pointer to the first node (`OrderNode`) in the linked list. This serves as the entry point for traversing the list.
Nested Struct: OrderNode
The `OrderList` class utilizes a private nested struct named `OrderNode`. This struct defines the building block of the linked list, representing a single node that stores an `Order` object and a pointer to the next node in the list. order: An `Order` object, holding the details of a customer's order.
class OrderList {
public:
OrderList();
~OrderList();
void addOrder(const Order& order);
bool removeOrder(int orderId);
Order* findOrder(int orderId) const;
void displayOrderList() const;
void displayOrdersForBuyer(int buyerId) const;
bool isEmpty() const;
private:
struct OrderNode {
Order order;
OrderNode* next;
const Buyer& buyer = order.getBuyer(); // Get a reference to the Buyer object
OrderNode(const Order& order, OrderNode* next = nullptr) : order
};
OrderNode* head; // Pointer to the first node in the linked list
};
next: A pointer to the next `OrderNode` in the linked list. It allows traversing the list by following these pointers from node to node.
buyer (const Buyer&): A constant reference to the `Buyer` object associated with the `order`. This reference is retrieved using the `getBuyer()` method of the `Order` object within the node.
OrderNode(const Order& order, OrderNode* next = nullptr)
(Constructor): This constructor initializes a new `OrderNode` object. It takes an `Order` object by reference (`order`) and an optional pointer to the next node (`next`).
It creates a new node storing the order and setting the `next` pointer accordingly (defaults to `nullptr` if not provided).
Public Member Functions
OrderList() (Constructor): This constructor initializes the `OrderList` object. It set the `head` pointer to `nullptr`, indicating an empty list.

~OrderList() (Destructor): This destructor is responsible for deallocating all the nodes in the linked list when an `OrderList` object goes out of scope, preventing memory leaks. It iterate through the list, starting from the `head`, and delete each node appropriately, ensuring proper memory management. 

addOrder(const Order& order): This function adds a new `Order` object to the `OrderList`. It creates a new `OrderNode` with the provided `order` and inserts it at the end of list.

removeOrder(int orderId): This function attempts to remove an order from the list based on its unique ID (`orderId`). It searches for the node containing the matching order and removes it from the linked list.  findOrder(int orderId) const: This function searches for an order in the list based on its ID (`orderId`). It iterates through the linked list and returns a pointer to the `Order` containing the matching order, or `nullptr` if not found.

displayOrderList() const: This function displays information about all orders currently in the list. It iterates through the linked list and calls the `displayOrder`

function of each `OrderNode`'s `order` object to display details.
displayOrdersForBuyer(int buyerId) const: This function displays information about orders placed by a specific buyer identified by their ID (`buyerId`). It iterates through the list and checks the `buyer` reference within each `OrderNode` using the `getBuyer().getId()` (assuming `Buyer` has a `getId` function) to see if it matches the provided `buyerId`. If there's a match, the order details are displayed. isEmpty() const: This function check if the order list is empty or not.
Main Class (main.cpp)
The main.cpp file already implemented for your use. Please don't make any changes in main.cpp file. You are only required to complete other TODO tasks as shown in next section.
End of Code overview
Description
This section describes your tasks in this assignment. You will need to finish 5 files:
1. Buyer.cpp
2. Food.cpp
3. Menu.cpp
4. Order.cpp
5. OrderList.cpp
These files may contain some functions that are already implemented for you. You should not modify them, otherwise your submission may have unexpected behaviours in the automatic grading system.
Assignment Tasks
TODO 1
Complete the Buyer.cpp file. Here are the tasks list:
Task 1.1
Buyer::Buyer(int id, const string& name)
Complete the default constructor for buyer class.
Task 1.2
void Buyer::setBuyerId(int newId)
Complete setter method for Buyer Id. This function should assign the newId with current buyer Id.
Task 1.3
int Buyer::getBuyerId() const
Complete getter method for Buyer Id. This function return the current buyer Id value.
Task 1.4
void Buyer::setBuyerName(const string& newName)
Complete setter method for Buyer Name. This function should assign the newName with current buyer Name.
Task 1.5
string Buyer::getBuyerName() const
Complete getter method for Buyer Name. This function return the current buyer Name value.
TODO 2
Complete the Food.cpp file. Here are the tasks list:
Task 2.1
Food::Food(int id, const string& name, float price)
Complete the default constructor for Food class.
Task 2.2
Food::Food(const Food& other)
Complete the copy constructor for Food class. Copy constructor should be implemented as a delegated constructor.
Task 2.3
void Food::setFoodId(int newId)
Complete setter method for Food Id. This function should assign the newId with current food
Id.
Task 2.4
int Food::getFoodId() const
Complete getter method for Food Id. This function return the current food Id value.
Task 2.5
void Food::setFoodName(const string& newName)
Complete setter method for Food Name. This function should assign the newName with current food name.
Task 2.6
string Food::getFoodName() const
Complete getter method for Food Name. This function return the current food Name value.
Task 2.7
void Food::setPrice(float newPrice)
Complete setter method for Food Price. This function should assign the newPrice with
current food price.
Task 2.8
float Food::getPrice() const
Complete getter method for Food Price. This function return the current food Price value.
TODO 3
Complete the Menu.cpp file. Here are the tasks list:
Task 3.1
Menu::Menu()
Complete the default constructor for Menu class.
Task 3.2
Menu::~Menu()
Complete the destructor for Food class.
Task 3.3
void Menu::addFood(const Food& food)
Complete addFood method for Menu class. This function adds a new food item to the menu.
It takes a reference to a Food object food and adds it to the foodItems array.
Task 3.4
bool Menu::removeFood(int id)
Complete removeFood method for Menu class. This function removes a food item from the menu based on its id. It searches for the food item with the matching ID and removes it from the foodItems array.
Task 3.5
int Menu::findFoodIndex(int id) const
Complete findFoodIndex method for Menu class. This helper function searches for the index of a food item with a specific id within the foodItems array. It iterates through the array and returns the index if a match is found, or -1 if not found.
Task 3.6
const Food* Menu::getFoodById(int id) const
Complete getFoodById method for Menu class. This function retrieves a pointer to a Food object from the menu based on its id. It searches for the food item with the matching ID and returns a pointer to it, allowing access to its details.
Task 3.7
void Menu::resizeArray()
Complete resizeArray method for Menu class. This private function handles resizing the foodItems array if needed. It called by addFood if the array is full, increasing the capacity and reallocating memory for the array. NOTE: INCREASE THE CAPACITY BY 2 TIMES.
TODO 4
Complete the Order.cpp code file. Here are the tasks list:
Task 4.1
Order::Order()
Complete the default constructor for Order class.
Task 4.2
const Buyer& Order::getBuyer() const
Complete getBuyer method for Order class. This function grants read-only access to the buyer object within the order, enabling retrieval of buyer information without the ability to modify the original object. It maintains data integrity. In return this function should return a buyer object.
Task 4.3
int Order::getcurrentNum() const
Complete getter method for currentNum variable. This function return the value of
currentNum variable.
Task 4.4
void Order::setOrderId(int newId)
Complete setter method for Order Id. This function should assign the newId with current order Id.
Task 4.5
int Order::getOrderId() const
Complete getter method for Order Id. This function return the current order Id value.
Task 4.6
void Order::setBuyer(int newBuyerId, string newBuyerName)
Complete setter method for Buyer Id and Buyer Name. This function should assign the newBuyerId and newBuyerName with current buyer class variable by using it's setter methods.
Task 4.7
bool Order::addItem(const Food& item)
Complete addItem method for Order class. This function is responsible for adding a new Food item to the orderedItems array. It takes a reference to a Food object item as input.
Task 4.8
void Order::cloneOrder(const Order& other)
Complete cloneOrder method for Order class. This function generates a self-contained copy of another existing Order object other. NOTE: THIS METHOD USE DEEP COPY APPROACH.
TODO 5
Complete the OrderList.cpp file. Here are the tasks list:
Task 5.1
OrderList::OrderList()
Complete the default constructor for OrderList class.
Task 5.2
OrderList::~OrderList()
Complete the destructor for OrderList class. This destructor is responsible for deallocating
all the nodes in the linked list when an `OrderList` object goes out of scope, preventing
memory leaks.
Task 5.3
void OrderList::addOrder(const Order& order)
Complete addOrder method for OrderList class. This function adds a new Order object to
the OrderList. NOTE: MAKE SURE YOU INSERT NEW ORDER IN LIST AT THE END.
Task 5.4
bool OrderList::removeOrder(int orderId)
Complete removeOrder method for OrderList class. This function attempts to remove an
order from the list based on its unique ID orderId.
Task 5.5
Order* OrderList::findOrder(int orderId) const
Complete findOrder method for OrderList class. This function searches for an order in the
list based on its ID orderId.
Task 5.6
bool OrderList::isEmpty() const
Complete isEmpty method for OrderList class. This function check if the order list is empty
or not.
Task 5.7
void OrderList::displayOrderList() const
Complete displayOrderList method for OrderList class. This function displays information about all orders currently in the list.
Task 5.8
void OrderList::displayOrdersForBuyer(int buyerId) const
Complete displayOrdersForBuyer method for OrderList class. This function displays information about orders placed by a specific buyer identified by their ID buyerId
End of Description
Resources & Sample I/O
Skeleton code: skeleton.zip
Demo program (executable for Windows): PA1.exe (Last updated 11:48 AM March
29)
End of Download
Test Cases
Testing main: main_test.zip (Last updated 05:12 PM March 16)
main_test.cpp: Contains all the test cases to check your TODOs
make: Modified Make file to run the main_test.cpp
input: A folder contains all the input commands for main_test.cpp file
output: A folder contains all the expected output
To test your all .cpp files before submission you can test them using main_test.cpp file.
NOTE: While testing your code please use the given modified make file inside the
main_test.zip
Once you put all the files and folders into your main PA1 directory then you can run the
main_test.cpp file by typing the following command in terminal.
make test
When you execute the program, the prompt will ask you to input the TODOs number to test from 1 to 5 and input the task number for each TODO. Alternatively, you can pass a text file to the program as input:
./PA1_test.exe < input/inputX.txt
NOTE: Change the value of X in range from 1-21 to get the output for desired test case.
Then compare your results with respective given output results inside output/outputX.txt
Note: If your machine cannot run the files/commands above, you may consider using one of the CS Lab machines. For more details please refer to Lab1 and Lab 3 tutorials
End of Test Cases
Submission and Deadline
Deadline: 23:59:00 on Saturday, March 30, 2024.
ZINC Submission
Create a single zip file that contains Buyer.cpp, Food.cpp, Menu.cpp, Order.cpp, and OrderList.cpp files. Zip only these files, NOT a folder containing them. Submit the zip file to ZINC. ZINC usage instructions can be found here.
Notes:
You may submit your file multiple times, but only the latest version will be graded.
Submit early to avoid any last-minute problem. Only ZINC submissions will be accepted.
The ZINC server will be very busy in the last day especially in the last few hours, so you should expect you would get the grading result report not-very-quickly. However, as long as your submission is successful, we would grade your latest submission with all test cases after the deadline.
If you have encountered any server-side problem or webpage glitches with ZINC, you may post on the ZINC support forum to get attention and help from the ZINC team quickly and directly. If you post on Piazza, you may not get the fastest response as we need to forward your report to them, and then forward their reply to you, etc.
NOTE: The program is compiled using the -fsanitize flag. You will lose points if your program contains memory leak or other runtime issues.
Compilation Requirement
IMPORTANT: You are strongly recommended to finish the function prototypes such that the program can compile before you start implementing the functions. If your program cannot compile, your score on ZINC is automatically 0. If your program can compile, you may get partial credits for tasks you have finished.
It is required that your submissions can be compiled and run successfully in our online autograder ZINC. If we cannot even compile your work, it won't be graded. Therefore, for parts that you cannot finish, just put in dummy implementation so that your whole program can be compiled for ZINC to grade the other parts that you have done. Empty implementations can be like:
Reminders
Make sure you actually upload the correct version of your source files - we only grade what you upload. Some students in the past submitted an empty file or a wrong file or an exe file which is worth zero mark. So you must double-check the file you have submitted.
Late Submission Policy
There will be a penalty of -1 point (out of a maximum 100 points) for every minute you are late. For instance, since the deadline of assignment 1 is 23:59:00 on March 30th, if you submit your solution at 1:00:00 on March 31st , there will be a penalty of -61 points for your assignment. Howeve

发表评论

电子邮件地址不会被公开。 必填项已用*标注