Question
Detail Requirements: Use the following Person class (Person.cpp , Person.h ). Right click the links and select Save Target As.. or Save Link As... to
Detail Requirements:
Use the following Person class (Person.cpp, Person.h). Right click the links and select Save Target As.. or Save Link As... to save the files to your computer. You will be writing Person objects to a random access file.
Create and initialize a random access file named nameage.dat with 50 records that store the values of 0 for the id, unassigned for the first and last name and 0 for the age. This code only needs to be executed once. Once you have written the code to create and initialize this file comment out this code (do not delete the code because you are being graded on this code also).
The user of your program should see a menu allowing them to write a Person object to the file, update an existing Person object's age, delete a Person object, display a particular Person object from the file, or exit the program.
a. | The first four options of the menu should ask the user for a record number (id) which will identify where in the random access file to write, update, delete, or display the Person object. Write the code to make sure the value the user inputs is from 1 to 50 only (an id of 1 should write to position 0 in the file). |
b. | Make sure the user does not overwrite an existing record when writing a new Person object to the file. |
c. | If the user wants to update an existing Person's age, make sure a valid Person object exists in the file for the id the user enters before asking for and updating the age. A valid Person object would be a Person object that does not have, for example, a 0 for the id or unassigned for the first and last name. |
d. | Deleting a Person object from the file involves writing a blank Person object to the file (id set to 0, first and last name set to unassigned, and age set to 0). |
e. | If the user wants to display a Person object, make sure a valid Person object exists in the file for the id the user enters before displaying the Person object. |
-----------------------------------------------------------------------------------------------------------
// Person.h // Class Person definition #ifndef PERSON_H #define PERSON_H
#include using namespace std;
class Person { public: // default Person constructor Person( int = 0, string = "unassigned", string = "unassigned", int = 0 );
// accessor functions for id void setID( int ); int getID() const;
// accessor functions for lastName void setLastName( string ); string getLastName() const;
// accessor functions for firstName void setFirstName( string ); string getFirstName() const;
// accessor functions for age void setAge( int ); int getAge() const; private: int id; char lastName[ 15 ]; char firstName[ 15 ]; int age; }; // end class Person
#endif
---------------------------------------------------------------------------------------------------------
// Person.cpp
// Class Person stores customer's credit information.
#include
#include "Person.h"
using namespace std;
// default Person constructor
Person::Person( int idValue,
string lastNameValue, string firstNameValue, int AgeValue )
{
setID( idValue );
setLastName( lastNameValue );
setFirstName( firstNameValue );
setAge( AgeValue );
} // end Person constructor
// get id value
int Person::getID() const
{
return id;
} // end function geID
// set id value
void Person::setID( int idValue )
{
id = idValue; // should validate
} // end function setID
// get last-name value
string Person::getLastName() const
{
return lastName;
} // end function getLastName
// set last-name value
void Person::setLastName( string lastNameString )
{
// copy at most 14 characters from string to lastName
int length = lastNameString.size();
length = ( length
lastNameString._Copy_s(lastName, 15, length);
lastName[ length ] = '\0'; // append null character to lastName
} // end function setLastName
// get first-name value
string Person::getFirstName() const
{
return firstName;
} // end function getFirstName
// set first-name value
void Person::setFirstName( string firstNameString )
{
// copy at most 14 characters from string to firstName
int length = firstNameString.size();
length = ( length
firstNameString._Copy_s(firstName, 15, length);
firstName[ length ] = '\0'; // append null character to firstName
} // end function setFirstName
// get age value
int Person::getAge() const
{
return age;
} // end function getAge
// set age value
void Person::setAge( int ageValue )
{
age = ageValue;
} // end function setAge
------------------------------------------------------------------------------------------------------------
Sample Output:
Below shows a partial run of programming assignment 4. The values the user inputs are highlighted in red. You should follow a similar format when coding your program. The menu should continue to display after each option until the user decides to exit the program.
******* MENU ******* 1 - Write a new person 2 - Update the age of an existing person 3 - Delete a person 4 - Display a person 5 - Exit Enter an integer for what you want to do: 1 Please enter a record number (1-50): 23 Enter the persons first name : Jane Enter the persons last name : Doe Enter the persons age : 25 New person added ID First name Last name Age 23 Jane Doe 25 ******* MENU ******* 1 - Write a new person 2 - Update the age of an existing person 3 - Delete a person 4 - Display a person 5 - Exit Enter an integer for what you want to do: 1 Please enter a record number (1-50): 23 A person already exists with that number returning to menu. ******* MENU ******* 1 - Write a new person 2 - Update the age of an existing person 3 - Delete a person 4 - Display a person 5 - Exit Enter an integer for what you want to do: 4 Please enter a record number (1-50): 44 There is no person with that id. ******* MENU ******* 1 - Write a new person 2 - Update the age of an existing person 3 - Delete a person 4 - Display a person 5 - Exit Enter an integer for what you want to do: 4 Please enter a record number (1-50): 23 ID First name Last name Age 23 Jane Doe 25 ******* MENU ******* 1 - Write a new person 2 - Update the age of an existing person 3 - Delete a person 4 - Display a person 5 - Exit Enter an integer for what you want to do: |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
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