In: Computer Science
Please fulfill the requirements. Thank you
Objectives:
Description:
Write a menu-driven program that provides the following options:
It allows the user to select a menu option to display all expenses, add new entry, search for a substring and find the list of entries greater a given amount.
Requirements:
Required error handling:
The program MUST perform the following checks:
Sample run:
D:\>TrackExpensesUsingArray.exe
Welcome to my expense tracker.
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 1
There is no expense entry available.
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 2
Please enter the description for the expense: Monthly telephone and Internet services
Please enter the amount: 45.25
AMOUNT(45.25) DESC(Monthly telephone and Internet services)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 2
Please enter the description for the expense: Monthly electric, water and gas
Please enter the amount: 200.20
AMOUNT(200.2) DESC(Monthly electric, water and gas)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 2
Please enter the description for the expense: Rent
Please enter the amount: 1200
AMOUNT(1200) DESC(Rent)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 2
Please enter the description for the expense: Netflix membership
Please enter the amount: 12.90
AMOUNT(12.9) DESC(Netflix membership)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 2
Please enter the description for the expense: Amazon membership
Please enter the amount: 99
AMOUNT(99) DESC(Amazon membership)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 2
Please enter the description for the expense: Monthly gym membership
Please enter the amount: 50
AMOUNT(50) DESC(Monthly gym membership)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 1
Expenses:
AMOUNT(45.25) DESC(Monthly telephone and Internet services)
AMOUNT(200.2) DESC(Monthly electric, water and gas)
AMOUNT(1200) DESC(Rent)
AMOUNT(12.9) DESC(Netflix membership)
AMOUNT(99) DESC(Amazon membership)
AMOUNT(50) DESC(Monthly gym membership)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 3
Please enter the search string: membership
AMOUNT(12.9) DESC(Netflix membership)
AMOUNT(99) DESC(Amazon membership)
AMOUNT(50) DESC(Monthly gym membership)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 3
Please enter the search string: MEMBERSHIP
AMOUNT(12.9) DESC(Netflix membership)
AMOUNT(99) DESC(Amazon membership)
AMOUNT(50) DESC(Monthly gym membership)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 4
Please enter the amount: 50
AMOUNT(200.2) DESC(Monthly electric, water and gas)
AMOUNT(1200) DESC(Rent)
AMOUNT(99) DESC(Amazon membership)
AMOUNT(50) DESC(Monthly gym membership)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 4
Please enter the amount: 200
AMOUNT(200.2) DESC(Monthly electric, water and gas)
AMOUNT(1200) DESC(Rent)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 4
Please enter the amount: 1000
AMOUNT(1200) DESC(Rent)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 2
Please enter the description for the expense: Home repair and improvement
Please enter the amount: -1
Invalid amount. Amount cannot be negative or string. Please try it again.
Please enter the amount: -100
Invalid amount. Amount cannot be negative or string. Please try it again.
Please enter the amount: -1000
Invalid amount. Amount cannot be negative or string. Please try it again.
Please enter the amount: 175.75
AMOUNT(175.75) DESC(Home repair and improvement)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 1
Expenses:
AMOUNT(45.25) DESC(Monthly telephone and Internet services)
AMOUNT(200.2) DESC(Monthly electric, water and gas)
AMOUNT(1200) DESC(Rent)
AMOUNT(12.9) DESC(Netflix membership)
AMOUNT(99) DESC(Amazon membership)
AMOUNT(50) DESC(Monthly gym membership)
AMOUNT(175.75) DESC(Home repair and improvement)
Expense Tracking Menu:
1. show all
2. spend
3. search expenses containing this string
4. search expenses with greater than or equal to this amount
5. exit
Enter your option: 5
D:\>
#include<bits/stdc++.h>
using namespace std;
struct data{
double amount;
vector<string> desc;
};
void printDesc(vector<string> &desc)
{
int i,n=desc.size();
for(i=0;i<n;i++)
{
cout<<desc[i]<<" ";
}
}
void showAll(vector<data> &arr)
{
int i,j,k,n=arr.size();
if(n==0)
{
cout<<"There is no expense entry available\n";
return;
}
for(i=0;i<n;i++)
{
cout<<"AMOUNT("<<arr[i].amount<<") DESC(";
printDesc(arr[i].desc);
cout<<")\n";
}
}
void split_string(string& des,vector<string>&
desc_arr)
{
int i=0,n=des.length();
while(i<n)
{
string temp="";
while(i<n && des[i]!=' ')
{
temp+=des[i];
i++;
}
desc_arr.push_back(temp);
i++;
}
}
void spend(vector<data> &arr)
{
string des;
double amt;
cout<<"Please enter the description for the expense:";
cin.ignore();
getline(cin,des);
while(des=="")
{
cout<<"Please enter the description for the expense:";
getline(cin,des);
}
vector<string> desc_arr;
split_string(des,desc_arr);
cout<<"Please enter the amount:";
cin>>amt;
while(amt<=0.0)
{
cout<<"Invaid amountAmount cannot be negative or string.
Please try it again.\n";
cout<<"Please enter the amount:";
cin>>amt;
}
cout<<"AMOUNT("<<amt<<")
DESC("<<des<<")\n";
data newData {amt,desc_arr};
arr.push_back(newData);
}
void searchAmount(vector<data> &arr)
{
int i,j,n=arr.size();
double amt;
cout<<"Enter Amount: "
cin>>amt;
for(i=0;i<n;i++)
{
if(amt<=arr[i].amount)
{
cout<<"AMOUNT("<<arr[i].amount<<") DESC(";
printDesc(arr[i].desc);
cout<<")\n";
}
}
}
bool search(string& str,vector<string>& des)
{
int i,n=des.size();
for(i=0;i<n;i++)
{
string temp = des[i];
transform(temp.begin(),temp.end(),temp.begin(),::tolower);
if(temp==str)
return true;
}
return false;
}
void searchString(vector<data> &arr){
cout<<"Please Enter the String Search\n";
string str;
cin>>str;
transform(str.begin(),str.end(),str.begin(),::tolower);
int i,n=arr.size();
for(i=0;i<n;i++)
{
bool found = search(str,arr[i].desc);
if(found){
cout<<"AMOUNT("<<arr[i].amount<<") DESC(";
printDesc(arr[i].desc);
cout<<")\n";
}
}
}
int main()
{
vector<data> arr;
while(1)
{
int opt;
cout<<"1.show all\n";
cout<<"2.spend\n";
cout<<"3.search expenses containing this string\n";
cout<<"4. search expenses with greater than or equal to this
amount\n";
cout<<"5.exit\n";
cout<<"Enter your option: ";
cin>>opt;
if(opt==1)
{
showAll(arr);
}else if(opt==2)
{
spend(arr);
}else if(opt==3)
{
searchString(arr);
}else if(opt==4)
{
searchAmount(arr);
}else if(opt==5){
break;
}else{
cout<<"Invalid input\n";
}
}
}