C++ This program will have names and addresses saved in a linked list. In additi
ID: 3747994 • Letter: C
Question
C++
This program will have names and addresses saved in a linked list. In addition, a birthday and anniversary date will be saved with each record. When the program is run, it will search for a birthday or an anniversary using the current date to compare with the saved date. It will then generate the appropriate card message. Because this will be an interactive system, your program should begin by displaying a menu. Items on the menu should include:
• Enter a new name into the address book
• Delete a name from the address book
• Change a name or date in the address book
• Display the whole address book
• Generate birthday cards
• Generate anniversary cards
• Exit the card program
Each of these sections will call individual functions to perform their appropriate task. This address book is to be sorted in alphabetical order. Be aware of this when you are entering, deleting, or changing the name, and plan your code accordingly. Use classes and objects where appropriate. Be sure to comment your code and use appropriate variable, function, and class names so that it is clear how the program flows. The user gets the menu and creates information to be stored in your address book. (To save sanity in testing, you may fill in 5 or 6 records within the code or written to a file to start your address book.) The user can add a new record to your existing ones, delete a record, modify a record or print the whole address book on the screen. For each of the options, make sure you give suitable prompts on the screen so that the user knows what is expected by the program. Expect that the user will enter both a birthday and anniversary date. (If you’d like, you can handle the situation where the user chooses not to enter an anniversary date.) A year may be entered or omitted in birthday and anniversary dates. Also be sure to redisplay the menu any time a function has concluded, until the user presses the Exit option. Create and display the card created on the screen (you do not need to print it). You may design the layout of the card as you wish. For example, it could be: Dear , Hope your birthday is really wonderful and this coming year is the best yet! Love, Joanne “Generate Birthday Cards” and “Generate Anniversary Cards” will use the system date (today) as the date to match. Using this date, display the card below it on the screen. Be ready to print multiple cards if more than one birthday or anniversary falls on the same day. “Display the Whole Address Book” will be done on the screen. Output should look something like:
Wilson, Fred
123 Main Street
Anytown, NJ 00000
Birthday: May 7
Anniversary: June 25
Or include the structure variable names you’ve used:
lname: Wilson
fname: Fred
addr: 123 Main Street
city: Anytown
state: NJ
zip: 00000
bday: May 7
aday: June 25
Explanation / Answer
Ans:
#include <iostream>
#include <stdlib.h>
#include "LinkedList.h"
#include "AddressBook.h"
using namespace std;
void mainMenu(AddressBook& addresses);
void enterName(AddressBook& addresses);
void deleteName(AddressBook& addresses);
void changeName(AddressBook& addresses);
void generateBirthdayCards(AddressBook& addresses);
void generateAnniversaryCards(AddressBook& addresses);
void resetInput();
int main()
{
AddressBook addresses;
addresses.load();
mainMenu(addresses);
return 0;
}
void mainMenu(AddressBook& addresses)
{
cout << "------------------Main menu------------------" << endl;
cout << "[1]: Enter a new name into the address book" << endl;
cout << "[2]: Delete a name from the address book" << endl;
cout << "[3]: Change a name or date in the address book" << endl;
cout << "[4]: Generate birthday cards" << endl;
cout << "[5]: Generate anniversary cards" << endl;
cout << "[6]: Save and exit the card program" << endl;
cout << "[7]: Print address book." << endl;
cout << "Enter code here: ";
resetInput();
int code;
cin >> code;
cout << endl;
while(cin.fail())
{
resetInput();
cout << "Input failed. Enter code here: ";
cin >> code;
cout << endl;
}
switch(code)
{
case 1:
cout << "--------Enter Name--------" << endl;
enterName(addresses);
break;
case 2:
cout << "-----Delete Name---------" << endl;
deleteName(addresses);
break;
case 3:
cout << "--------Change Name--------" << endl;
changeName(addresses);
break;
case 4:
cout << "-------Birthday Cards-------" << endl;
generateBirthdayCards(addresses);
break;
case 5:
cout << "-------Anniversary Cards-------" << endl;
generateAnniversaryCards(addresses);
break;
case 6:
cout << "Now closing..." << endl;
addresses.save();
exit(0);
break;
case 7:
cout << "----------------Address List-----------------" << endl;
addresses.print();
break;
default:
cerr << "Sorry, wrong number." << endl;
break;
};
mainMenu(addresses);
}
void enterName(AddressBook& addresses)
{
resetInput();
Address cur;
cout << "Enter name:";
getline(cin, cur.name);
cout << "Enter Address:";
getline(cin, cur.streetAddress);
cout << "Enter Anniversary Date:";
getline(cin, cur.anniversaryDate);
cout << "Enter Birthday Date:";
getline(cin, cur.birthdayDate);
cout << endl;
addresses.add(cur);
}
void deleteName(AddressBook& addresses)
{
resetInput();
string name;
cout << "Enter name to delete: ";
getline(cin, name);
cout << endl;
addresses.remove(name);
}
void changeName(AddressBook& addresses)
{
resetInput();
string name;
cout << "Enter name to rename: ";
getline(cin, name);
Address addressToBeDeleted = addresses.get(name);
if(addressToBeDeleted.name == "NULL")
{
cerr << "Entered name does not exist!" << endl;
return;
}
Address* changeAddress = new Address;
changeAddress->streetAddress = addressToBeDeleted.streetAddress;
changeAddress->anniversaryDate = addressToBeDeleted.anniversaryDate;
changeAddress->birthdayDate = addressToBeDeleted.birthdayDate;
addresses.remove(name);
cout << "Enter new name: ";
getline(cin, name);
cout << endl;
changeAddress->name = name;
addresses.add(*changeAddress);
}
void generateBirthdayCards(AddressBook& addresses)
{
resetInput();
string date;
cout << "Enter date: ";
cin >> date;
addresses.generateBirthdayCards(date);
}
void generateAnniversaryCards(AddressBook& addresses)
{
resetInput();
string date;
cout << "Enter date: ";
cin >> date;
addresses.generateAnniversaryCards(date);
}
void resetInput()
{
if(cin.fail())
{
cin.clear();
}
if(cin.peek() == ' ')
{
cin.ignore();
}
}
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <stdio.h>
#include <iostream>
template <typename Type>
class LinkedList
{
public:
LinkedList();
virtual ~LinkedList();
int getLength();
virtual void print();
void add(Type value);
void remove(Type value);
protected:
int length;
struct Node
{
Type val;
Node* next;
};
Node* head, *tail;
};
template <typename Type>
LinkedList<Type>::LinkedList()
{
head = tail = NULL;
length = 0;
}
template <typename Type>
LinkedList<Type>::~LinkedList()
{
Node* prev = NULL;
Node* it = head;
while(it != NULL)
{
prev = it;
it = it->next;
delete prev;
}
}
template <typename Type>
void LinkedList<Type>::print()
{
int index = 0;
Node* it = head;
while(it != NULL)
{
std::cout << "{" << index << ":" << it->val << "}";
if(it->next != NULL)
{
std::cout << ", ";
}
it = it->next;
}
}
template <typename Type>
int LinkedList<Type>::getLength()
{
return length;
}
template <typename Type>
void LinkedList<Type>::add(Type value)
{
Node* newNode = new Node;
newNode->val = value;
newNode->next = NULL;
if(head == NULL)
{
head = tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
length++;
}
template <typename Type>
void LinkedList<Type>:: remove(Type value)
{
Node* it = head;
Node* prev = NULL;
while(it->next != NULL)
{
if(it->val == value)
{
if(prev == NULL)
{
head = it->next;
delete it;
}
else
{
prev->next = it->next;
delete it;
}
}
prev = it;
it = it ->next;
}
length--;
}
#endif
AddressBook.h
#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H
#include "LinkedList.h"
#include "Address.h"
class AddressBook : public LinkedList<Address>
{
public:
AddressBook();
virtual ~AddressBook();
void print();
void add(Address curAddress);
void remove(std::string name);
Address get(std::string name);
void generateBirthdayCards(std::string date);
void generateAnniversaryCards(std::string date);
void load();
void save();
protected:
private:
void printBirthdayCard(Address currAdress);
void printAnniversaryCard(Address currAdress);
};
#endif
Address.h
#ifndef ADDRESS_H
#define ADDRESS_H
#include <iostream>
class Address
{
public:
Address(std::string name = "Not Entered",
std::string streetAddress = "Not Entered",
std::string anniversaryDate = "XX-XX-XXXX",
std::string birthdayDate = "XX-XX-XXXX");
virtual ~Address();
friend std::ostream& operator<<(std::ostream& os, const Address& curAddress);
std::string name;
std::string streetAddress;
std::string anniversaryDate;
std::string birthdayDate;
protected:
private:
};
#endif
AddressBook.cpp
#include "AddressBook.h"
#include <iostream>
#include <stdlib.h>
#include <fstream>
AddressBook::AddressBook()
{
}
AddressBook::~AddressBook()
{
}
void AddressBook::print()
{
Node* it = head;
while(it != NULL)
{
std::cout << it->val << std::endl;
it = it->next;
}
}
void AddressBook::remove(std::string name)
{
Node* it = head;
Node* prev = NULL;
while(it != NULL)
{
std::string curName = ((Address)it->val).name;
if(curName == name)
{
if(prev == NULL)
{
head = it->next;
delete it;
}
else
{
prev->next = it->next;
delete it;
}
length--;
}
prev = it;
it = it ->next;
}
}
Address AddressBook::get(std::string name)
{
Node* it = head;
while(it != NULL)
{
std::string curName = ((Address)it->val).name;
if(curName == name)
{
return it->val;
}
it = it ->next;
}
Address nullAdd;
nullAdd.name = "NULL";
return nullAdd;
}
void AddressBook::generateBirthdayCards(std::string date)
{
Node* it = head;
while(it != NULL)
{
Address curAddress = ((Address)it->val);
if(date == curAddress.birthdayDate)
{
printBirthdayCard(curAddress);
}
it = it->next;
}
}
void AddressBook::printBirthdayCard(Address curAddress)
{
std::cout << "-------------------------------------" << std::endl;
std::cout << "Dear " << curAddress.name << "," << std::endl << std::endl;
std::cout << "Hope your birthday is really wonderful and this coming year is the best yet!" << std::endl;
std::cout << "Love," << std::endl << std::endl;
std::cout << "John Doe" << std::endl;
}
void AddressBook::generateAnniversaryCards(std::string date)
{
Node* it = head;
while(it != NULL)
{
Address curAddress = ((Address)it->val);
if(date == curAddress.anniversaryDate)
{
printAnniversaryCard(curAddress);
}
it = it->next;
}
}
void AddressBook::add(Address newAddress)
{
Node* newNode = new Node;
newNode->val = newAddress;
newNode->next = NULL;
if(head == NULL)
{
head = tail = newNode;
}
else
{
Node* prev = NULL;
Node* it = head;
while(it != NULL)
{
std::string curName = ((Address)it->val).name;
if(curName > newAddress.name)
{
if(prev == NULL)
{
head = newNode;
newNode->next = it;
}
else
{
prev->next = newNode;
newNode->next = it;
}
return;
}
else
{
if(it->next == NULL)
{
it->next = newNode;
it = newNode->next;
}
else
{
prev = it;
it = it ->next;
}
}
}
}
length++;
}
void AddressBook::printAnniversaryCard(Address curAddress)
{
std::cout << "-------------------------------------" << std::endl;
std::cout << "Dear " << curAddress.name << "," << std::endl << std::endl;
std::cout << "Hope your anniversary is really wonderful and this coming year is the best yet!" << std::endl;
std::cout << "Love," << std::endl << std::endl;
std::cout << "John Doe" << std::endl;
}
void AddressBook::load()
{
std::ifstream in;
in.open("addresses.properties");
if(!in.good())
{
std::cerr << "Cannot open addresses.properties" << std::endl;
return;
}
while(in.good())
{
Address* curAddress = new Address;
std::string temp;
getline(in, temp);
curAddress->name = temp;
getline(in, temp);
curAddress->streetAddress = temp;
getline(in, temp);
curAddress->anniversaryDate = temp;
getline(in, temp);
curAddress->birthdayDate = temp;
if(in.good())
{
add(*curAddress);
}
}
in.close();
}
void AddressBook::save()
{
std::ofstream out;
out.open("addresses.properties");
if(!out.good())
{
std::cerr << "Cannot open addresses.properties" << std::endl;
return;
}
Node* it = head;
while(it != NULL)
{
Address curAddress = (Address)it->val;
out << curAddress.name << std::endl;
out << curAddress.streetAddress << std::endl;
out << curAddress.anniversaryDate << std::endl;
out << curAddress.birthdayDate << std::endl;
it = it->next;
}
out.close();
}
Address.cpp
#include "Address.h"
Address::Address(std::string name, std::string streetAddress, std::string anniversaryDate, std::string birthdayDate)
{
this->name = name;
this->streetAddress = streetAddress;
this->anniversaryDate = anniversaryDate;
this->birthdayDate = birthdayDate;
}
Address::~Address()
{
}
std::ostream& operator<<(std::ostream& outStream, const Address& curAddress)
{
outStream.clear();
outStream << "Name: " << curAddress.name << std::endl
<< "Address: "<< curAddress.streetAddress << std::endl
<< "Anniversary Date: " << curAddress.anniversaryDate << std::endl
<< "Birthday Date: " << curAddress.birthdayDate << std::endl;
return outStream;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.