COMP 2012 Object-Oriented Programming and Data
Object-Oriented Programming and Data
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
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);
COMP 2012 Assignment 1: Food Ordering System 29/03/2024, 23:23
https://course.cse.ust.hk/comp2012/assignments/assignment1/#submission Page 5 of 19
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;
};
COMP 2012 Assignment 1: Food Ordering System 29/03/2024, 23:23
https://course.cse.ust.hk/comp2012/assignments/assignment1/#submission Page 6 of 19
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.