diff options
| author | rPatrickWarner <[email protected]> | 2024-03-04 17:11:54 -0800 |
|---|---|---|
| committer | rPatrickWarner <[email protected]> | 2024-03-04 17:11:54 -0800 |
| commit | e5eb6f396d9d7a959c45abf34a095a57b2295a24 (patch) | |
| tree | cd09a47418711839b533b90299f1b5cf11eb8e0f /Homework8 | |
| parent | init (diff) | |
| download | homework-8-reecepwarner-e5eb6f396d9d7a959c45abf34a095a57b2295a24.tar.xz homework-8-reecepwarner-e5eb6f396d9d7a959c45abf34a095a57b2295a24.zip | |
changes
Diffstat (limited to 'Homework8')
| -rw-r--r-- | Homework8/Homework8/main.cpp | 38 | ||||
| -rw-r--r-- | Homework8/MyStructures/Contact.hpp | 288 | ||||
| -rw-r--r-- | Homework8/MyStructures/ContactList.hpp | 226 | ||||
| -rw-r--r-- | Homework8/MyStructures/MenuHelper.hpp | 149 | ||||
| -rw-r--r-- | Homework8/MyStructures/MyStructures.vcxproj | 1 | ||||
| -rw-r--r-- | Homework8/MyStructures/MyStructures.vcxproj.filters | 1 |
6 files changed, 613 insertions, 90 deletions
diff --git a/Homework8/Homework8/main.cpp b/Homework8/Homework8/main.cpp index 37aad74..13b9f64 100644 --- a/Homework8/Homework8/main.cpp +++ b/Homework8/Homework8/main.cpp @@ -1,21 +1,35 @@ -//Name:Reece Waner - -#include "Contact.hpp" - -using namespace MyStructures; //bringing scope into the main - - -int main() -{ - - Contact<char> newContact; - +//Name:Reece Warner +//Date:March 3rd 2024 +//Homework#8 +//AddressBook +#define _CRT_SECURE_NO_WARNINGS +#include "MenuHelper.hpp" +using namespace MyStructures; +int main() +{ + Contact NewContact; + NewContact.SetFirstName(PromptCharInput("Whats your first name?", 101)); + NewContact.SetLastName(PromptCharInput("Whats your last name?", 101)); + NewContact.SetEmail(PromptCharInput("Whats your email?", 101)); + NewContact.SetStreet(PromptCharInput("Whats your street address?", 101)); + NewContact.SetState(PromptCharInput("What state do you live in?", 101)); + NewContact.SetCity(PromptCharInput("What city do you live in?", 101)); + NewContact.SetZip(InputInt("What is your zip code?")); + + ContactList<Contact> AddressBook; + + AddressBook.Append(NewContact); + + AddressBook.PrintList(); + + + //MainMenu(); diff --git a/Homework8/MyStructures/Contact.hpp b/Homework8/MyStructures/Contact.hpp index f7fda6c..ac480be 100644 --- a/Homework8/MyStructures/Contact.hpp +++ b/Homework8/MyStructures/Contact.hpp @@ -1,108 +1,300 @@ #ifndef CONTACT_HPP #define CONTACT_HPP +#include <iostream> +#include <cstring> +#define _CRT_SECURE_NO_WARNINGS -namespace MyStructures //variables declared here act as a global variable +using std::strcpy; +using std::cout; +using std::endl; +using std::cin; +using std::numeric_limits; +using std::streamsize; +constexpr size_t MAX_STREAM_SIZE = numeric_limits<streamsize>::max(); + + +namespace MyStructures //variables declared here act as a global variable { - template <typename T> + class Contact { public: Contact() = default; - Contact(const char* name, const short age); + Contact(char* name, const short age); + + ~Contact(); + + Contact(const Contact& copy); //copy constructor + Contact& operator=(const Contact& rhs); //copy assignment + + Contact(Contact&& move); //move constructor + Contact& operator=(Contact&& rhs); //move assignment + + + const char* GetFirstName(); + void SetFirstName(char* firstname); + + const char* GetLastName(); + void SetLastName(char* lastname); + + const char* GetEmail(); + void SetEmail(char* email); - ~Contact() = default; + const char* GetStreet(); + void SetStreet(char* street); - Contact(const Contact& copy); //copy constructor - Contact& operator=(const Contact& rhs); //copy assignment + const char* GetState(); + void SetState(char* state); - Contact(Contact&& move); //move constructor - Contact& operator=(Contact&& rhs); //move assignment + const char* GetCity(); + void SetCity(char* city); + int GetZip(); + void SetZip(int zip); - short GetAge(); - const char* GetName(); + /*int InputInt(const char* prompt);*/ + /*char* PromptCharInput(const char* prompt, long long maxlen);*/ - void SetAge(short age); - void SetName(const char* name); + bool operator != (const Contact & rhs) const; + + + void Print() const; - void Print(); - T GetCustomValue(T& newValue); private: - const char* name_{}; - short age_{ 0 }; - T custom_value_{}; //one type is declared, it will be custom + char* firstname_{ nullptr }; + char* lastname_{ nullptr }; + char* email_{ nullptr }; + char* street_{ nullptr }; + char* state_{ nullptr }; + char* city_{ nullptr }; + int zip_ = { 0 }; + }; + + + + inline Contact::Contact(char* name, const short age) + { + firstname_ = name; + + } + + inline Contact::~Contact() + { + + delete[] firstname_; + delete[] lastname_; + delete[] email_; + delete[] street_; + delete[] state_; + delete[] city_; + } - }; - - template<typename T> - Contact<T>::Contact(const char* name, const short age) + inline Contact::Contact(const Contact& copy) + :firstname_ (copy.firstname_), + lastname_ (copy.lastname_), + email_(copy.email_), + state_(copy.state_), + city_(copy.city_), + street_(copy.street_), + zip_(copy.zip_) { + + strcpy(firstname_, copy.firstname_); + strcpy(lastname_, copy.lastname_); + strcpy(email_, copy.email_); + strcpy(street_, copy.street_); + strcpy(state_, copy.state_); + strcpy(city_, copy.city_); + zip_ = copy.zip_; + + } - template<typename T> - Contact<T>::Contact(const Contact& copy) + + inline Contact& Contact::operator=(const Contact& rhs) { + if (this != &rhs) + { + firstname_ = rhs.firstname_; + lastname_ = rhs.lastname_; + email_ = rhs.email_; + street_ = rhs.street_; + state_ = rhs.state_; + city_ = rhs.city_; + zip_ = rhs.zip_; + + } + return *this; } - template<typename T> - Contact<T>& Contact<T>::operator=(const Contact& rhs) + + inline Contact::Contact(Contact&& move) { - // TODO: insert return statement here + *this = move; } - template<typename T> - Contact<T>::Contact(Contact&& move) + + inline Contact& Contact::operator=(Contact&& rhs) { + if (this != &rhs) + { + firstname_ = rhs.firstname_; + lastname_ = rhs.lastname_; + email_ = rhs.email_; + street_ = rhs.street_; + state_ = rhs.state_; + city_ = rhs.city_; + zip_ = rhs.zip_; + } + return *this; } - template<typename T> - Contact<T>& Contact<T>::operator=(Contact&& rhs) + inline const char* Contact::GetFirstName() { - // TODO: insert return statement here + return firstname_; } + void Contact::SetFirstName(char* firstname) + { + firstname_ = firstname; + } - template<typename T> - short Contact<T>::GetAge() + inline const char* Contact::GetLastName() { - return 0; + return lastname_; } - template<typename T> - const char* Contact<T>::GetName() + inline void Contact::SetLastName(char* lastname) { - return nullptr; + lastname_ = lastname; } - template<typename T> - void Contact<T>::SetAge(short age) + inline const char* Contact::GetEmail() { + return email_; } - template<typename T> - void Contact<T>::SetName(const char* name) + inline void Contact::SetEmail(char* email) { + email_ = email; } - template<typename T> - inline void Contact<T>::Print() + inline const char* Contact::GetStreet() { + return street_; } - template<typename T> - T Contact<T>::GetCustomValue(T& newValue) + inline void Contact::SetStreet(char* street) { - custom_value_ = newValue - return T(); + street_ = street; } -} + inline const char* Contact::GetState() + { + return state_; + } + inline void Contact::SetState(char* state) + { + state_ = state; + } + + inline const char* Contact::GetCity() + { + return city_; + } + + inline void Contact::SetCity(char* city) + { + city_ = city; + } + + inline int Contact::GetZip() + { + return zip_; + } + + inline void Contact::SetZip(int zip) + { + zip_ = zip; + } + + + /*inline int Contact::InputInt(const char* prompt) + { + std::cout << prompt << std::endl; + + std::cout.flush(); + + int data = 0; + std::cin >> data; + while (!std::cin) + { + std::cout << prompt << std::endl; + std::cin.clear(); + cin.ignore(MAX_STREAM_SIZE, '\n'); + std::cin >> data; + + } + + return data; + }*/ + + //inline char* Contact::PromptCharInput(const char* prompt, long long maxlen) + //{ + // char* input = new char[maxlen]; + // std::cout.flush(); + + // std::cout << prompt << std::endl; + // cin.get(input, maxlen, '\n'); + // cin.ignore(MAX_STREAM_SIZE, '\n'); + // + // while (!std::cin) + // { + // std::cout << prompt << std::endl; + + // std::cin.clear(); + // + // cin.get(input, maxlen, '\n'); + + // } + // + // return input; + //} + + inline bool Contact::operator!=(const Contact& rhs) const + { + if (std::strcmp(firstname_, rhs.firstname_) != 0) { return true; } + if (std::strcmp(lastname_, rhs.lastname_) != 0) { return true; } + if (std::strcmp(email_, rhs.email_) != 0) { return true; } + if (std::strcmp(street_, rhs.street_) != 0) { return true; } + if (std::strcmp(state_, rhs.state_) != 0) { return true; } + if (std::strcmp(city_, rhs.city_) != 0) { return true; } + if (zip_ != rhs.zip_) { return true; } + + return false; + } + + + + void Contact::Print() const + { + std::cout << "Name: " << firstname_ << " " << lastname_ << std::endl; + std::cout << "Email: " << email_ << std::endl; + std::cout << "Street Address: " << street_ << std::endl; + std::cout << "State: " << state_ << std::endl; + std::cout << "City: " << city_ << std::endl; + std::cout << "Zip Code: " << zip_ << std::endl; + + std::cout << std::endl; + + } +} #endif
\ No newline at end of file diff --git a/Homework8/MyStructures/ContactList.hpp b/Homework8/MyStructures/ContactList.hpp index dd0f4ee..e7959a8 100644 --- a/Homework8/MyStructures/ContactList.hpp +++ b/Homework8/MyStructures/ContactList.hpp @@ -1,6 +1,9 @@ #ifndef CONTACT_LIST_HPP #define CONTACT_LIST_HPP +#include "Contact.hpp" + + namespace MyStructures { template<class C> @@ -8,23 +11,52 @@ namespace MyStructures { public: ContactList() = default; - ContactList(size_t size); //Creates contact list of this size - ContactList(const C* contacts, const size_t& length); //Makes a copy of another contact list + ContactList(size_t size); //Creates contact list of this size + ContactList(const C* contacts, const size_t& length); //Makes a copy of another contact list ~ContactList(); - ContactList(const ContactList& copy); //deep copy constructor - ContactList& operator=(const ContactList& rhs); //deep copy assignment - - ContactList(ContactList&& move); //reference(move) constructor - ContactList& operator=(ContactList&& rhs); //reference(move) assignment + ContactList(const ContactList& copy); //deep copy constructor + ContactList& operator=(const ContactList& rhs); //deep copy assignment - private: - C* contacts_{ nullptr }; - size_t length_{ 0 }; //entire length of array - size_t size_{ 0 }; //number of actual elements + ContactList(ContactList&& move); //reference(move) constructor + ContactList& operator=(ContactList&& rhs); //reference(move) assignment + + void PrintList() const; //Print the Contact List, calling print on each element + size_t Size() const; //Return the size of the contact list + + void Append(const C& data); //add contact to the back of the list + void Prepend(const C& data); //add contact as the first item + void RemoveLast(); //remove last + void RemoveFirst(); //remove first + void Extract(const C& data); //delete this data + void InsertAfter(const C& data, const C& after); //Insert contact data after 'after' + void InsertBefore(const C& data, const C& before); //Insert contact data before 'before' + void Clear(); // //deletes all contacts, empties list, nothing left + + + C& Last(); //returning a reference to contact, it gives last contact in list + C Last() const; //const version of this, unchanging because its const and its a copy + C& First(); //returning first contact in list + C First() const; //returning first contact copy + + + C& operator[](const int& index); //like a getter/ returns contact reference via squarebracket overload + C operator[](const int& index) const; //returns a contact copy via squarebracket overload + explicit operator bool() const; //overloading class name to ask if its true or false; this allows you to use your contactlist in boolean statements + //operator. also explicit tells the compiler not to make it implic + + bool operator==(const ContactList<C>& rhs) const; //overloading == e.g if(newcontactlist == oldcontactlist) + bool Empty() const; //is the list empty + + private: + C* contacts_{ nullptr }; + size_t length_{ 0 }; //entire length of array + size_t size_{ 0 }; //number of actual elements C* AllocateContactList(const size_t& length); }; + + template<class C> ContactList<C>::ContactList(size_t size) { @@ -33,7 +65,7 @@ namespace MyStructures template<class C> ContactList<C>::ContactList(const C* contacts, const size_t& length) { - contacts_ AllocateContactList(length); + contacts_ = AllocateContactList(length); for (auto i = 0u; i < length; ++i) { contacts_[i] = contacts[i]; @@ -52,7 +84,7 @@ namespace MyStructures size_ = copy.size_; contacts_ = AllocateContactList(copy.length_); - contacts_ AllocateContactList(length); + contacts_ = AllocateContactList(length_); for (auto i = 0u; i < length_; ++i) { contacts_[i] = copy[i]; @@ -62,54 +94,188 @@ namespace MyStructures template<class C> ContactList<C>&ContactList<C>::operator=(const ContactList& rhs) { - if (this != &rhs) //asking if they're the same object + if (this != &rhs) //asking if they're the same object { delete[] contacts_; contacts_ = nullptr; - contacts_ = AllocateContactList(rhs.length_); - size_ = rhs.size_; - contacts_ AllocateContactList(length); - - for (auto i = 0u; i < length_; ++i) + if (rhs.contacts_ != nullptr) { - contacts_[i] = rhs[i]; + + contacts_ = AllocateContactList(rhs.length_); + size_ = rhs.size_; + contacts_ = AllocateContactList(length_); + + for (auto i = 0u; i < length_; ++i) + { + contacts_[i] = rhs[i]; + } } + else + contacts_ = nullptr; + } return *this; } template<class C> ContactList<C>::ContactList(ContactList&& move) { - *this = std::move(move); //move shifts addresses + *this = std::move(move); //move shifts addresses } template<class C> ContactList<C>& ContactList<C>::operator=(ContactList&& rhs) { - if(this != &rhs) + if (this != &rhs) { delete[] contacts_; - contacts = nullptr; + contacts_ = nullptr; contacts_ = rhs.contacts_; - + length_ = rhs.length_; size_ = rhs.size_; - rhs.contacts_ = nullptr; //Invalidate the other object + rhs.contacts_ = nullptr; //Invalidate the other object + + } + + return *this; } + template<class C> - C* ContactList<C>::AllocateContactList(const size_t& length) + void ContactList<C>::PrintList() const { - C* storage = nullptr - length_ - length; + for (auto i = 0u; i < length_; ++i) + { + contacts_[i].Print(); + } + } - storage = newC[length]{}; + template<class C> + size_t ContactList<C>::Size() const + { + return size_t(); + } + + template<class C> + C* ContactList<C>::AllocateContactList(const size_t& length) + { + C* storage = nullptr; + length_ = length; + storage = new C[length]{}; return storage; } + template<class C> + void ContactList<C>::Append(const C& data) + { + size_t index_locate = 0; + + //what if its the beginning + if (size_ == 0) + { + index_locate = 0; + } + //what if the middle + if (size_ > 0 && size_ < length_) + { + index_locate = size_; + } + + if (size_ >= length_) + { + *this = ContactList(contacts_, length_ * 2); + } + //Add to end of the array + contacts_[index_locate] = data; + size_++; + } + template<class C> + void ContactList<C>::Prepend(const C& data) + { + + } + template<class C> + void ContactList<C>::RemoveLast() + { + } + template<class C> + void ContactList<C>::RemoveFirst() + { + } + template<class C> + void ContactList<C>::Extract(const C& data) + { + } + template<class C> + void ContactList<C>::InsertAfter(const C& data, const C& after) + { + } + template<class C> + void ContactList<C>::InsertBefore(const C& data, const C& before) + { + } + template<class C> + void ContactList<C>::Clear() + { + } + template<class C> + inline C& ContactList<C>::Last() + { + // TODO: insert return statement here + } + template<class C> + inline C ContactList<C>::Last() const + { + return C(); + } + template<class C> + inline C& ContactList<C>::First() + { + // TODO: insert return statement here + } + template<class C> + inline C ContactList<C>::First() const + { + return C(); + } + template<class C> + inline C& ContactList<C>::operator[](const int& index) + { + // TODO: insert return statement here + } + template<class C> + inline C ContactList<C>::operator[](const int& index) const + { + return index; + } + template<class C> + inline ContactList<C>::operator bool() const + { + bool var = contacts_ != nullptr; + return (var); + } + template<class C> + inline bool ContactList<C>::operator==(const ContactList<C>& rhs) const + { + if (contacts_ == rhs.contacts_) return true; + + if (size_ != rhs.size_) return false; + + if (length_ != rhs.length_) return false; + + for (auto i = 0u; i < size_; ++i) + { + if (contacts_[i] != rhs.contacts_[i]) return false; + } + return true; + } + template<class C> + inline bool ContactList<C>::Empty() const + { + return size_ == 0; + } }; -} diff --git a/Homework8/MyStructures/MenuHelper.hpp b/Homework8/MyStructures/MenuHelper.hpp new file mode 100644 index 0000000..81d11e0 --- /dev/null +++ b/Homework8/MyStructures/MenuHelper.hpp @@ -0,0 +1,149 @@ +#ifndef MENU_HELPER_HPP +#define MENU_HELPER_HPP + +#define _CRT_SECURE_NO_WARNINGS +#include "ContactList.hpp" + +using namespace MyStructures; + + +void MainMenu(); + +Contact InputContact(); + +char* PromptCharInput(const char* prompt, long long maxlen); + +int InputInt(const char* prompt); + +void PrintContact(ContactList<Contact>& contacts); + +Contact NewContact(); + +void PrintContact(ContactList<Contact>& contacts); + + + +void MainMenu() +{ + char Options = '\0'; + + + do + { + std::cout << "Welcome to the contacts menu\n" + << "1)Add New Contact\n" + << "2)Print All Contacts\n" + << "3)Delete Contact\n" + << "4)Exit\n"; + std::cin >> Options; + switch (Options) + { + case('1'): + + break; + case('2'): + //PrintContact(contacts); + break; + case('3'): + //contacts.DeleteContact("Which contact would you like to delete?", contacts); + break; + case('4'): + std::cout << "Thank you, have a great day!" << std::endl; + + break; + default: + std::cout << "Invalid Input, Try Again!" << std::endl; + } + + } while (Options != '4'); +} + +Contact InputContact() +{ + + + Contact newContact; + newContact.SetFirstName(PromptCharInput("What is your first name?", 101)); + + newContact.SetLastName(PromptCharInput("What is your last name?: ", 101)); + newContact.SetStreet(PromptCharInput("What is your street address?: ", 101)); + newContact.SetEmail(PromptCharInput("What is your email?: ", 101)); + newContact.SetState(PromptCharInput("What is your state?: ", 101)); + newContact.SetCity(PromptCharInput("What is your city?: ", 101)); + newContact.SetZip(InputInt("What is your zip?")); + + + return newContact; + + + +} + +void PrintContact(ContactList<Contact>& contacts) +{ + contacts.PrintList(); +} + + + +Contact NewContact() +{ + Contact newContact; + + return newContact; +} + + + +char* PromptCharInput(const char* prompt, long long maxlen) +{ + std::cout.flush(); + std::cout << prompt << std::endl; + + char* input = new char[maxlen]; + + do + { + std::cin.clear(); + cin.get(input, maxlen, '\n'); + cin.ignore(MAX_STREAM_SIZE, '\n'); + + } while (!std::cin); + + return input; + +} +int InputInt(const char* prompt) +{ + std::cout << prompt << std::endl; + + std::cout.flush(); + + int data = 0; + std::cin >> data; + + while (!std::cin) + { + std::cout << prompt << std::endl; + std::cin.clear(); + + cin.ignore(MAX_STREAM_SIZE, '\n'); + std::cin >> data; + + } + + return data; +} + + + + + + + + + + + + +#endif
\ No newline at end of file diff --git a/Homework8/MyStructures/MyStructures.vcxproj b/Homework8/MyStructures/MyStructures.vcxproj index 92eff89..5c6cead 100644 --- a/Homework8/MyStructures/MyStructures.vcxproj +++ b/Homework8/MyStructures/MyStructures.vcxproj @@ -129,6 +129,7 @@ <ItemGroup> <ClInclude Include="Contact.hpp" /> <ClInclude Include="ContactList.hpp" /> + <ClInclude Include="MenuHelper.hpp" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> diff --git a/Homework8/MyStructures/MyStructures.vcxproj.filters b/Homework8/MyStructures/MyStructures.vcxproj.filters index deeda6d..4fdf85d 100644 --- a/Homework8/MyStructures/MyStructures.vcxproj.filters +++ b/Homework8/MyStructures/MyStructures.vcxproj.filters @@ -3,5 +3,6 @@ <ItemGroup> <ClInclude Include="Contact.hpp" /> <ClInclude Include="ContactList.hpp" /> + <ClInclude Include="MenuHelper.hpp" /> </ItemGroup> </Project>
\ No newline at end of file |