A company wants to know the percentages of total sales and total expenses attributable to each salesperson. The data is stored in a text file. In the file, each person has a pair of data lines. The first line contains his or her name, last name name first. The second line contains his or her sales (int) and expenses (float).

Define a class named SalesPerson that holds one salesperson's information. And then create another class named SalesReport that associates with the list of salespersons. And searching and sorting are based on comparisons of strings (salespersons' names).

Write a program that produces a report (text file) with a header line containing the total sales and total expenses. Following this header should be a table with each salesperson's name, percentage of total sales, and percentage of total expenses, sorted by salesperson's name.

Did you take Foundations of programming?

To solve this problem, you'll need to define two classes: `SalesPerson` and `SalesReport`.

First, let's define the `SalesPerson` class to hold one salesperson's information. The class should have the following attributes:

- `name`: A string that represents the salesperson's name.
- `sales`: An integer that represents the sales made by the salesperson.
- `expenses`: A float that represents the expenses incurred by the salesperson.

To define the `SalesPerson` class, you can use the following code:

```python
class SalesPerson:
def __init__(self, name, sales, expenses):
self.name = name
self.sales = sales
self.expenses = expenses
```

Next, let's define the `SalesReport` class that associates with the list of salespersons and performs the required operations. The class should have the following attributes and methods:

- `salespersons`: A list that stores the `SalesPerson` objects.
- `total_sales`: A variable that keeps track of the total sales of all salespersons.
- `total_expenses`: A variable that keeps track of the total expenses of all salespersons.

The `SalesReport` class should also have the following methods:

- `add_salesperson`: A method that takes a `SalesPerson` object as a parameter and adds it to the `salespersons` list.
- `calculate_totals`: A method that calculates the total sales and total expenses based on the data of all salespersons.
- `generate_report`: A method that generates the report by writing the header line, followed by the table containing each salesperson's name, percentage of total sales, and percentage of total expenses, sorted by the salesperson's name.

Here's an example implementation of the `SalesReport` class:

```python
class SalesReport:
def __init__(self):
self.salespersons = []
self.total_sales = 0
self.total_expenses = 0

def add_salesperson(self, salesperson):
self.salespersons.append(salesperson)

def calculate_totals(self):
for salesperson in self.salespersons:
self.total_sales += salesperson.sales
self.total_expenses += salesperson.expenses

def generate_report(self, output_file):
# Calculate the percentages
for salesperson in self.salespersons:
sales_percentage = (salesperson.sales / self.total_sales) * 100
expenses_percentage = (salesperson.expenses / self.total_expenses) * 100
salesperson.sales_percentage = sales_percentage
salesperson.expenses_percentage = expenses_percentage

# Sort the salespersons by name
self.salespersons.sort(key=lambda x: x.name)

# Write the report to the output file
with open(output_file, 'w') as f:
# Write the header line
f.write(f"Total Sales: {self.total_sales}, Total Expenses: {self.total_expenses}\n")

# Write the table
for salesperson in self.salespersons:
f.write(
f"{salesperson.name}: {salesperson.sales_percentage}% of Total Sales, {salesperson.expenses_percentage}% of Total Expenses\n"
)
```

To use these classes and generate the report, you can follow these steps:

1. Create a `SalesReport` object.
2. Read the data from the text file and create `SalesPerson` objects for each salesperson. Add these objects to the `SalesReport` object using the `add_salesperson` method.
3. Call the `calculate_totals` method to calculate the total sales and total expenses.
4. Call the `generate_report` method, passing the output file name to generate the report.

Here's an example usage:

```python
report = SalesReport()

with open("data.txt", "r") as file:
lines = file.readlines()
for i in range(0, len(lines), 2):
name = lines[i].strip()
sales, expenses = map(float, lines[i + 1].split())
salesperson = SalesPerson(name, sales, expenses)
report.add_salesperson(salesperson)

report.calculate_totals()
report.generate_report("report.txt")
```

Make sure to replace `"data.txt"` with the actual file path containing the salesperson data, and `"report.txt"` with the desired output file path for the generated report.