Write an application to maintain a list of friends. Array of Objects (Links to a
ID: 3806085 • Letter: W
Question
Write an application to maintain a list of friends. Array of Objects (Links to an external site.) Specifications Create a simple Friend class with, as a minimum, the following: firstName, lastName, age, and cellPhone data members appropriate constructors get/set functions display() function - prints a friend's name and cell phone number Write a program that uses a vector to manage your list of friends. Run the program from a menu with the following options: Add a Friend Remove a Friend Display all Friends Sorted by last name Sorted by phone number Sorted by age Exit
Explanation / Answer
// FriendManager.cpp : Defines the entry point for the console application.
//note: pls make small methods to minimize of main method
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Friend
{
private: string firstname;
string lastname;
int age;
int cellphone;
public :
string getfirstname()
{
return firstname;
}
string getlastname()
{
return lastname;
}
int getage()
{
return age;
}
int getcellphone()
{
return cellphone;
}
void setfirstname(string name)
{
firstname = name;
}
void setlastname(string name)
{
lastname = name;
}
void setage(int i)
{
age=i;
}
void setcellphone(int i)
{
cellphone=i;
}
};
int main()
{
int option=0;
std::vector <Friend> friends;
string string;
int number;
Friend b;
Friend temp;
cout << "1) Add a Friend "
"2)Remove a Friend "
"3)Display all Friends "
"4)Sorted by last name "
"5)Sorted by phone number "
"6)Sorted by age "
"7)Exit " << endl;
while (option != 7)
{
cout << "pls enter an option ";
cin >> option;
switch (option) {
case 1:
cout << " enter firstname";
cin >> string;
b.setfirstname(string);
cout << " enter lastname";
cin >> string;
b.setlastname(string);
cout << " enter agee";
cin >> number;
b.setage(number);
cout << " enter cellphone";
cin >> number;
b.setcellphone(number);
friends.push_back(b);
break; //optional
case 2: if (!friends.empty())
{
friends.pop_back();
cout << "removed last entered friend ";
}
else
{
cout << "list is empty";
}
break; //optional
case 3: for (int i = 0; i < friends.size(); i++)
{
cout << friends[i].getfirstname() << endl;
}
break; //optional
case 4:
for (int i = 0; i < friends.size(); i++)
{
for (int j = i + 1; j < friends.size(); j++)
{
if (friends[i].getlastname() > friends[j].getlastname())
{
temp = friends[i];
friends[i] = friends[j];
friends[j] = temp;
}
}
}
cout << "sorted by lastname ";
break; //optional
case 5:if (!friends.empty())
{
for (int i = 0; i < friends.size(); i++)
{
for (int j = i + 1; j < friends.size(); j++)
{
if (friends[i].getcellphone() > friends[j].getcellphone())
{
temp = friends[i];
friends[i] = friends[j];
friends[j] = temp;
}
}
}
cout << "sorted by cellphone ";
}
else
{
cout << "list is empty";
}
break; //optional
case 6:if (!friends.empty())
{
for (int i = 0; i < friends.size(); i++)
{
for (int j = i + 1; j < friends.size(); j++)
{
if (friends[i].getage() > friends[j].getage())
{
temp = friends[i];
friends[i] = friends[j];
friends[j] = temp;
}
}
}
cout << "sorted by age ";
}
else
{
cout << "list is empty";
}
break; //optional
case 7:cout<<"bye";
break; //optional
// you can have any number of case statements.
default: //Optional
cout << "invalid input";
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.