CSIS211: Assignment 05 Exceptions Write coding in C++ but in different files like example main.cpp, perosn.cpp, person.h, addressbook.h , addressbook.cpp, addresssbookexpection.h, addressbookexpection.cpp, player.h, and player.cpp
CSIS211: Assignment 05 Exceptions
Write coding in C++ but in different files like example main.cpp, perosn.cpp, person.h, addressbook.h , addressbook.cpp, addresssbookexpection.h, addressbookexpection.cpp, player.h, and player.cpp
The coding method: C++
C++ coding assigment 5
I am having trouble getting my coding working because I addressBook.cpp and addressbook.h is not working well with my Person so I need help figure out the problem because I have don't know how to fix my coding so that both person and addressbook work for the main.
remainder: Please don't solve answer in Java because I never take Java and mostly take C++ classes. Please explain how solve this problem in C++. Make sure to type c++ in different files like Main.cpp, Person.cpp, Person.j, AddressBook.cpp, and AddressBook.h
Instruction:
AddressBook Exception
Create an AddressBookException class that you can use to throw exceptions in your address book. Your class should have a constructor that will allow you to set the particular message that corresponds to the error.
You should use your custom exception class to throw an error in the findPerson method if the person being searched for cannot be found, in the getPerson method if the AddressBook is empty. You should also throw an error any time that the addressbook file that you are attempting to read or write from cannot be opened.
In main test your AddressBook exception class by placing a try - catch around any code that could possibly throw an error. Supply a catch block to catch the AddressBook exception and provide a catch all block just in case an exception is thrown the you may have missed.
Reference:
I need this coding I send to you for reference into different files: main.cpp, Person.h, Person.cpp, Player.h, Player.cpp, AddressBook.h, Adressbook.cpp, addresssbookexpection.h, addressbookexpection.cpp, player.h, and player.cpp
C++ CODE-
#include
#include
#include
#include
#include
using namespace std;
class AddressBookException{ string exception; public: AddressBookException(string exception){ this->exception = exception; } AddressBookException(){ } void print_exception(){ cout << exception << endl; } };
class Person
{
private:
string firstName;
string lastName;
string address;
public:
Person()
{
setFirstName("");
setLastName("");
setAddress("");
}
Person(string firstName, string lastName)
{
setFirstName(firstName);
setLastName(lastName);
// set the address to blank
setAddress("");
}
Person(string firstName, string lastName, string address)
{
setFirstName(firstName);
setLastName(lastName);
// set the address to blank
setAddress(address);
}
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
string getAddress() { return address; }
void setFirstName(string firstName) { this->firstName = firstName; }
void setLastName(string lastName) { this->lastName = lastName; }
void setAddress(string address) { this->address = address; }
virtual void print()
{
cout << firstName << " " << lastName << ", " << address << endl;
}
};
class Player : public Person
{
private:
double average;
double onBasePercentage;
double sluggingPercentage;
public:
Player()
{
// set all the players to 0
setAverage(0);
setOBP(0);
setSlugging(0);
}
Player(string first, string last, string address, double average, double slugging, double onBase)
{
setFirstName(first);
setLastName(last);
setAddress(address);
setAverage(average);
setOBP(onBase);
setSlugging(slugging);
}
// setter for the values
void setAverage(double average) { this->average = average; }
void setSlugging(double slug) { this->sluggingPercentage = slug; }
void setOBP(double OnBase) { this->onBasePercentage = OnBase; } // the print void print() { cout << getFirstName() << " " << getLastName() << ", " << getAddress() << ", " << average << ", " << sluggingPercentage << "%, " << onBasePercentage << "%" << endl; }
};
class AddressBook : public vector
{
private:
int index = 0;
public:
AddressBook() {}
AddressBook(string filename) {
ifstream in(filename);
if (!in) {
throw AddressBookException("Error: AddressBook input file not found");
//exit(0);
}
string line;
while (getline(in, line)) {
stringstream ss(line);
string token, fName, lName, address;
int i = 0;
while (getline(ss, token, ',')) {
if (i == 0) {
fName = token;
i++;
}
else if (i == 1) {
lName = token;
i++;
}
else {
address = token;
i = 0;
}
}
setPerson(fName, lName, address);
}
in.close();
}
AddressBook(string first, string last) { push_back(new Person(first, last, "")); }
AddressBook(string first, string last, string address) { push_back(new Person(first, last, address)); }
void setPerson(string first, string last, string address) { push_back(new Person(first, last, address)); }
Person* getPerson()
{
if (size() == 0)
throw AddressBookException("Error: AddressBook is empty");
Person* nextPerson = at(index);
index = (index + 1) % size();
return nextPerson;
}
Person* findPerson(string last)
{ if(size() == 0) throw AddressBookException("Pass: Address Book is empty when created!");
for (int i = 0; i < size(); i++)
{
if (at(i)->getLastName() == last)
return at(i);
}
throw AddressBookException("Error: Person with last name not found!");
}
Person* findPerson(string first, string last)
{
// find the person with this last name
for (int i = 0; i < size(); i++)
{
if (at(i)->getLastName() == last && at(i)->getFirstName() == first)
return at(i);
} throw AddressBookException("Error: Person with last name and first name not found!"); }
void print()
{
for (int i = 0; i < size(); i++)
{
at(i)->print();
}
}
void writeToFile(string filename) {
ofstream out(filename); if(!out){ throw AddressBookException("Error: AddressBook output file not found"); } for (int i = 0; i < size(); i++)
{
out << at(i)->getFirstName() << "," << at(i)->getLastName() << "," << at(i)->getAddress() << endl;
}
out.close();
}
};
int main()
{ try{ //AddressBook addressBook1; AddressBook addressBook1("address.txt"); try{ if(addressBook1.findPerson("Wayne") != NULL) cout << "Error: Address Book must be empty!" << endl; } catch(AddressBookException exception){ exception.print_exception(); } try{ if(addressBook1.getPerson() != NULL) cout << "Pass: Next person found!" << endl; } catch(AddressBookException exception){ exception.print_exception(); } addressBook1.setPerson("Bruce", "Wayne", "Wayne Tower");
addressBook1.setPerson("Clark", "Kent", "Daily Mail");
addressBook1.setPerson("Thor", "Son of Odin", "Asgard");
try{ if(addressBook1.findPerson("Wayne") != NULL) cout << "Pass: Person with last name found!" << endl; } catch(AddressBookException exception){ exception.print_exception(); } try{ if(addressBook1.getPerson() != NULL) cout << "Pass: Next person found!" << endl; } catch(AddressBookException exception){ exception.print_exception(); } try{ if(addressBook1.findPerson("Clark", "Kent") != NULL) cout << "Pass: Person with last name and first name found!" << endl; } catch(AddressBookException exception){ exception.print_exception(); } try{ if(addressBook1.findPerson("Bruce", "Banner") != NULL) cout << "Pass: Person with last name and first name found!" << endl; } catch(AddressBookException exception){ exception.print_exception(); } cout << endl;
// print the address book
addressBook1.print();
cout << endl;
//Call function to write into file try{ addressBook1.writeToFile("address.txt"); } catch(AddressBookException exception){ exception.print_exception(); } } catch(AddressBookException exception){ exception.print_exception(); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started