Question: can you simplify this code and change sort from high to low to total expenses. Cannot use vector.h : #include #include using namespace std; class

can you simplify this code and change "sort from high to low" to "total expenses".
Cannot use vector.h :
#include
#include
using namespace std;
class Expense {
public:
Expense(double amount =0.0, const string& category ="") : amount(amount), category(category){}
void setAmount(double amount){
this->amount = amount;
}
double getAmount() const {
return amount;
}
void setCategory(const string& category){
this->category = category;
}
const string& getCategory() const {
return category; }
private:
double amount;
string category;};
class FinanceTracker {
public:
FinanceTracker() : expenses(nullptr), count(0), capacity(1){
expenses = new Expense[capacity]; }
~FinanceTracker(){
delete[] expenses; }
void addExpense(const Expense& expense){
if (count == capacity){
resize(); }
expenses[count++]= expense; }
void displayExpenses() const {
if (count ==0){
cout <<"No expenses found." << endl;
return;
} cout << "Expenses:";
for (int i =0; i < count; ++i){
cout << "Amount: $"<< expenses[i].getAmount()<<", Category: "<< expenses[i].getCategory()<< endl; }}
Expense* searchByCategory(const string& category, int& resultsCount) const {
resultsCount =0;
for (int i =0; i < count; ++i){
if (expenses[i].getCategory()== category){
resultsCount++; }}
if (resultsCount ==0){ return nullptr; }
Expense* results = new Expense[resultsCount];
int index =0;
for (int i =0; i < count; ++i){
if (expenses[i].getCategory()== category){
results[index++]= expenses[i]; }}
return results; }
void sortByAmount(bool ascending = true){
for (int i =0; i < count -1; ++i){
for (int j =0; j < count - i -1; ++j){
if ((ascending && expenses[j].getAmount()> expenses[j +1].getAmount())||
(!ascending && expenses[j].getAmount()< expenses[j +1].getAmount())){
swap(expenses[j], expenses[j +1]); }}}}
private:
Expense* expenses;
int count; int capacity;
void resize(){ capacity *=2;
Expense* newExpenses = new Expense[capacity];
for (int i =0; i < count; ++i){
newExpenses[i]= expenses[i]; }
delete[] expenses;
expenses = newExpenses; }
void swap(Expense& a, Expense& b){
Expense temp = a;
a = b; b = temp;
}};
int main(){
FinanceTracker tracker;
int choice;
double amount;
string category;
do {
cout << "Personal Finance Tracker
"; cout <<"1. Add Expense
"; cout <<"2. Display Expenses
"; cout <<"3. Search by Category
"; cout <<"4. Sort Expenses (Low to High)
"; cout <<"5. Sort Expenses (High to Low)
"; cout <<"6. Exit
"; cout << "Enter your choice: ";
while (!(cin >> choice)){
cout << "Invalid input. Please enter a number: ";
cin.clear();
cin.ignore(numeric_limits::max(),''); }
cin.ignore(numeric_limits::max(),'
'); // clear the buffer
switch (choice){ case 1:
cout << "Enter amount: ";
while (!(cin >> amount)){
cout << "Invalid input. Please enter a number: ";
cin.clear();
cin.ignore(numeric_limits::max(),''); }
cin.ignore(numeric_limits::max(),'
'); // clear the buffer
cout << "Enter category: ";
getline(cin, category);
tracker.addExpense(Expense(amount, category)); break;
case 2:
tracker.displayExpenses(); break;
case 3:
cout << "Enter category to search: ";
getline(cin, category); {
int resultsCount;
Expense* results = tracker.searchByCategory(category, resultsCount);
if (results == nullptr){
cout <<"No expenses found for that category." << endl;
} else {
cout << "Search Results:
"; for (int i =0; i < resultsCount; ++i){
cout << "Amount: $"<< results[i].getAmount()<<", Category: "<< results[i].getCategory()<< endl;
}
delete[] results;
}} break;
case 4:
tracker.sortByAmount();
cout << "Expenses sorted from low to high." << endl;case 5:
tracker.sortByAmount(false);
cout << "Expenses sorted from high to low." << endl; break;
case 6:cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}} while (choice !=6);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!