To help you develop and test your container class (Storage) for the Customer objects, we'll walk through the necessary steps, including encapsulating the iterator class within the Storage class, overloading the input/output operators for the Customer class, and implementing the required methods for managing the container (like push_back, push_front, insert, pop_back, pop_front, and erase). Additionally, we'll provide a comprehensive main function to test all functionalities.
Here's the complete, refined code with detailed explanations:
#include <iostream>
#include <string>
using namespace std;
// Class representing a Customer
class Customer {
private:
string surname;
float priceBasicTariff;
float priceSocialTariff;
int countMonthsTariff;
public:
// Default constructor
Customer()
: surname(""), priceBasicTariff(0.0f), priceSocialTariff(0.0f),
countMonthsTariff(0) {
// Optional: You can comment out the debug messages if not needed
// cout << "Default constructor called for Customer." << endl;
}
// Parameterized constructor
Customer(string s, float pBasic, float pSocial, int months)
: surname(s), priceBasicTariff(pBasic), priceSocialTariff(pSocial),
countMonthsTariff(months) {
// cout << "Parameterized constructor called for Customer: " << surname << endl;
}
// Copy constructor
Customer(const Customer& other)
: surname(other.surname), priceBasicTariff(other.priceBasicTariff),
priceSocialTariff(other.priceSocialTariff),
countMonthsTariff(other.countMonthsTariff) {
// cout << "Copy constructor called for Customer: " << other.surname << endl;
}
// Destructor
~Customer() {
// cout << "Destructor called for Customer: " << surname << endl;
}
// Setters
void setSurname(const string& s) { surname = s; }
void setPriceBasicTariff(float p) { priceBasicTariff = p; }
void setPriceSocialTariff(float p) { priceSocialTariff = p; }
void setCountMonthsTariff(int m) { countMonthsTariff = m; }
// Getters
const string& getSurname() const { return surname; }
const float& getPriceBasicTariff() const { return priceBasicTariff; }
const float& getPriceSocialTariff() const { return priceSocialTariff; }
const int& getCountMonthsTariff() const { return countMonthsTariff; }
// Function to print Customer information
void printInfo() const {
cout << "Surname: " << surname << endl;
cout << "Basic Tariff: " << priceBasicTariff << endl;
cout << "Social Tariff: " << priceSocialTariff << endl;
cout << "Months: " << countMonthsTariff << endl;
}
// Overloading the extraction operator for Customer
friend istream& operator>>(istream& is, Customer& c) {
cout << "Enter surname: ";
is >> c.surname;
cout << "Enter basic tariff: ";
is >> c.priceBasicTariff;
cout << "Enter social tariff: ";
is >> c.priceSocialTariff;
cout << "Enter count of months: ";
is >> c.countMonthsTariff;
return is;
}
// Overloading the insertion operator for Customer
friend ostream& operator<<(ostream& os, const Customer& c) {
os << "Surname: " << c.surname
<< ", Basic Tariff: " << c.priceBasicTariff
<< ", Social Tariff: " << c.priceSocialTariff
<< ", Months: " << c.countMonthsTariff;
return os;
}
};
// Container class for storing Customer objects
class Storage {
private:
Customer* p; // Pointer to the array of Customers
int size; // Current number of elements
public:
// Nested Iterator class
class Iterator {
private:
Customer* current; // Pointer to the current Customer
public:
// Constructor
Iterator(Customer* ptr = nullptr) : current(ptr) {}
// Overloading dereference operator
Customer& operator*() const {
return *current;
}
// Overloading arrow operator
Customer* operator->() const {
return current;
}
// Overloading pre-increment operator
Iterator& operator++() {
++current;
return *this;
}
// Overloading post-increment operator
Iterator operator++(int) {
Iterator temp = *this;
++current;
return temp;
}
// Overloading inequality operator
bool operator!=(const Iterator& other) const {
return current != other.current;
}
// Overloading equality operator
bool operator==(const Iterator& other) const {
return current == other.current;
}
};
// Default constructor
Storage(int n = 0) : size(n) {
if (size > 0) {
p = new Customer[size];
// Optionally initialize Customers
for (int i = 0; i < size; ++i) {
p[i] = Customer();
}
} else {
p = nullptr;
}
}
// Copy constructor
Storage(const Storage& other) : size(other.size) {
if (size > 0) {
p = new Customer[size];
for (int i = 0; i < size; ++i) {
p[i] = other.p[i];
}
} else {
p = nullptr;
}
}
// Destructor
~Storage() {
delete[] p;
}
// Assignment operator
Storage& operator=(const Storage& other) {
if (this != &other) {
delete[] p;
size = other.size;
if (size > 0) {
p = new Customer[size];
for (int i = 0; i < size; ++i) {
p[i] = other.p[i];
}
} else {
p = nullptr;
}
}
return *this;
}
// Overloading the extraction operator for Storage
friend istream& operator>>(istream& is, Storage& s) {
for (int i = 0; i < s.size; ++i) {
cout << "Entering data for Customer " << i + 1 << ":" << endl;
is >> s.p[i];
}
return is;
}
// Overloading the insertion operator for Storage
friend ostream& operator<<(ostream& os, const Storage& s) {
os << "Storage contains " << s.size << " Customers:" << endl;
for (int i = 0; i < s.size; ++i) {
os << i + 1 << ". " << s.p[i] << endl;
}
return os;
}
// Method to compute cost based on some criteria (example implementation)
double cost(string code) const {
double v = 0;
// Example implementation, assuming Customer has getCode(), getCount(), getPrice()
// Since these methods are not defined in Customer, this is just a placeholder
/*
for (int i = 0; i < size; ++i) {
if (p[i].getCode() == code) {
v += p[i].getCount() * p[i].getPrice();
}
}
*/
// Since getCode(), getCount(), getPrice() are not defined, return 0
return v;
}
// Begin iterator
Iterator begin() const { return Iterator(p); }
// End iterator
Iterator end() const { return Iterator(p + size); }
// Push back method: add Customer at the end
void push_back(const Customer& x) {
Customer* new_p = new Customer[size + 1];
for (int i = 0; i < size; ++i) {
new_p[i] = p[i];
}
new_p[size] = x;
delete[] p;
p = new_p;
++size;
}
// Push front method: add Customer at the beginning
void push_front(const Customer& x) {
Customer* new_p = new Customer[size + 1];
new_p[0] = x;
for (int i = 0; i < size; ++i) {
new_p[i + 1] = p[i];
}
delete[] p;
p = new_p;
++size;
}
// Insert method: add Customer at position pos (0-based index)
void insert(const Customer& x, int pos) {
if (pos < 0 || pos > size) {
cout << "Invalid position for insert." << endl;
return;
}
Customer* new_p = new Customer[size + 1];
for (int i = 0; i < pos; ++i) {
new_p[i] = p[i];
}
new_p[pos] = x;
for (int i = pos; i < size; ++i) {
new_p[i + 1] = p[i];
}
delete[] p;
p = new_p;
++size;
}
// Pop back method: remove the last Customer
void pop_back() {
if (size == 0) {
cout << "Storage is already empty. Cannot pop_back." << endl;
return;
}
Customer* new_p = nullptr;
if (size - 1 > 0) {
new_p = new Customer[size - 1];
for (int i = 0; i < size - 1; ++i) {
new_p[i] = p[i];
}
}
delete[] p;
p = new_p;
--size;
}
// Pop front method: remove the first Customer
void pop_front() {
if (size == 0) {
cout << "Storage is already empty. Cannot pop_front." << endl;
return;
}
Customer* new_p = nullptr;
if (size - 1 > 0) {
new_p = new Customer[size - 1];
for (int i = 1; i < size; ++i) {
new_p[i - 1] = p[i];
}
}
delete[] p;
p = new_p;
--size;
}
// Erase method: remove Customer at position pos (0-based index)
void erase(int pos) {
if (pos < 0 || pos >= size) {
cout << "Invalid position for erase." << endl;
return;
}
if (size == 0) {
cout << "Storage is empty. Cannot erase." << endl;
return;
}
Customer* new_p = nullptr;
if (size - 1 > 0) {
new_p = new Customer[size - 1];
for (int i = 0; i < pos; ++i) {
new_p[i] = p[i];
}
for (int i = pos + 1; i < size; ++i) {
new_p[i - 1] = p[i];
}
}
delete[] p;
p = new_p;
--size;
}
// Method to copy content from another Storage
void copyFrom(const Storage& other) {
if (this == &other)
return;
delete[] p;
size = other.size;
if (size > 0) {
p = new Customer[size];
for (int i = 0; i < size; ++i) {
p[i] = other.p[i];
}
} else {
p = nullptr;
}
}
// Method to get the current size
int getSize() const { return size; }
};
// Function to display menu options
void displayMenu() {
cout << "\n--- Storage Menu ---\n";
cout << "1. Input Customers\n";
cout << "2. Display Customers\n";
cout << "3. Add Customer to Back (push_back)\n";
cout << "4. Add Customer to Front (push_front)\n";
cout << "5. Insert Customer at Position (insert)\n";
cout << "6. Remove Last Customer (pop_back)\n";
cout << "7. Remove First Customer (pop_front)\n";
cout << "8. Erase Customer at Position (erase)\n";
cout << "9. Copy Storage to Another Storage\n";
cout << "0. Exit\n";
cout << "Choose an option: ";
}
// Main function to test the Storage container
int main() {
Storage s; // Initialize empty Storage
Storage sCopy; // To test copying
int choice;
do {
displayMenu();
cin >> choice;
switch (choice) {
case 1: { // Input Customers
int n;
cout << "Enter number of Customers to input: ";
cin >> n;
// Resize storage accordingly
for (int i = 0; i < n; ++i) {
Customer c;
cin >> c;
s.push_back(c);
}
break;
}
case 2: { // Display Customers
cout << s;
break;
}
case 3: { // Add Customer to Back
Customer c;
cout << "Enter details for Customer to add to back:" << endl;
cin >> c;
s.push_back(c);
break;
}
case 4: { // Add Customer to Front
Customer c;
cout << "Enter details for Customer to add to front:" << endl;
cin >> c;
s.push_front(c);
break;
}
case 5: { // Insert Customer at Position
int pos;
cout << "Enter position to insert (0-based index): ";
cin >> pos;
if (pos < 0 || pos > s.getSize()) {
cout << "Invalid position." << endl;
break;
}
Customer c;
cout << "Enter details for Customer to insert at position " << pos << ":" << endl;
cin >> c;
s.insert(c, pos);
break;
}
case 6: { // Remove Last Customer
s.pop_back();
cout << "Last Customer removed." << endl;
break;
}
case 7: { // Remove First Customer
s.pop_front();
cout << "First Customer removed." << endl;
break;
}
case 8: { // Erase Customer at Position
int pos;
cout << "Enter position to erase (0-based index): ";
cin >> pos;
if (pos < 0 || pos >= s.getSize()) {
cout << "Invalid position." << endl;
break;
}
s.erase(pos);
cout << "Customer at position " << pos << " erased." << endl;
break;
}
case 9: { // Copy Storage to Another Storage
sCopy.copyFrom(s);
cout << "Storage copied to another Storage object." << endl;
cout << "Copied Storage content:" << endl;
cout << sCopy;
break;
}
case 0: { // Exit
cout << "Exiting program." << endl;
break;
}
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 0);
return 0;
}
Explanation of the Code
-
Customer Class Enhancements:
- Operator Overloading: Added
operator>> and operator<< for easy input and output of Customer objects.
- Encapsulation: All member variables are kept private with appropriate getters and setters.
- Constructors and Destructor: Properly defined to manage resources and provide meaningful debug messages (commented out for cleaner output).
-
Storage Class Enhancements:
- Nested Iterator Class:
- The
Iterator class is now nested within the Storage class.
- It allows traversal using standard iterator operations (
*, ->, ++, !=, ==).
- Dynamic Array Management:
- Managed using raw pointers (
Customer* p) with appropriate constructors, destructor, and assignment operator to handle deep copies and resource management.
- Container Operations:
- push_back: Adds a
Customer to the end of the container.
- push_front: Adds a
Customer to the beginning of the container.
- insert: Inserts a
Customer at a specified position.
- pop_back: Removes the last
Customer.
- pop_front: Removes the first
Customer.
- erase: Removes a
Customer at a specified position.
- copyFrom: Copies content from another
Storage object.
- Operator Overloading:
- Overloaded
operator>> and operator<< to handle input and output for the entire container.
- Cost Method:
- Placeholder provided. You can implement it based on your specific requirements.
-
Main Function:
- Menu-Driven Interface: Provides a user-friendly interface to test all functionalities of the
Storage container.
- Operations:
Usage Example
Here's how you can interact with the program:
--- Storage Menu ---
1. Input Customers
2. Display Customers
3. Add Customer to Back (push_back)
4. Add Customer to Front (push_front)
5. Insert Customer at Position (insert)
6. Remove Last Customer (pop_back)
7. Remove First Customer (pop_front)
8. Erase Customer at Position (erase)
9. Copy Storage to Another Storage
0. Exit
Choose an option: 1
Enter number of Customers to input: 2
Entering data for Customer 1:
Enter surname: Smith
Enter basic tariff: 50
Enter social tariff: 30
Enter count of months: 12
Entering data for Customer 2:
Enter surname: Johnson
Enter basic tariff: 60
Enter social tariff: 35
Enter count of months: 24
--- Storage Menu ---
...
You can continue to choose different options to test all functionalities.
Points to Note
- Error Handling: Basic error handling is implemented for invalid positions during
insert and erase operations. You can enhance it further based on your requirements.
- Memory Management: All dynamic memory allocations are properly managed to prevent memory leaks. However, for a production-level code, consider using smart pointers or standard containers like
std::vector for better safety and efficiency.
- Further Enhancements:
- Implement the
cost method based on your specific criteria.
- Add more functionalities as needed, such as searching for a
Customer, sorting the container, etc.
This comprehensive implementation should help you effectively test and utilize your Storage container class for managing Customer objects.