*********90% of this is done************ C++ (Preferablly the Eclipse IDE) This
ID: 3738796 • Letter: #
Question
*********90% of this is done************
C++ (Preferablly the Eclipse IDE)
This assignment includes 3 classes and a Main routine. The classes are Customer, CustomerAddress, and Utils. The first two classes have been defined. I just need help in writing some of the methods in the Utils class.
Utils Class
The Utils class is made up primarily of static methods, as shown below You will implement the methods findCustomerAddress, linkCustomersToAddresses, and readCustomerAddresses.
class Utils {
public:
// The files the program reads to build the customer and customer
// address vectors; values assigned in .cpp file
const static std::string CUSTOMER_INPUT_FILE;
const static std::string CUSTOMER_ADDRESS_INPUT_FILE;
Utils();
static CustomerAddress* findCustomerAddress (Customer *,
std::vector ) ;
static void linkCustomersToAddresses (std::vector ,
std::vector );
static std::vector readCustomerData ();
static std::vector readCustomerAddresses ();
virtual ~Utils();
Method findCustomerAddress
This method takes two arguments:
A Customer instance that holds the id (an int) of the CustomerAddress in the vector of CustomerAddresses.
A vector of CustomerAddress pointers, one of which points to the object that is this customer’s address.
The implementation should search the vector of CustomerAddress values to find the right address object. THe code should use a binary search to perform the search operation. Note: the values in the vector are in sorted ascending order by id.
This method returns the following:
A pointer to the correct address object or nullptr if no correct object exists.
Method linkCustomersToAddresses
This method returns the following:
A vector of Customer pointers
This method performs the following:
Links each Customer instance to the unique CustomerAddress object that is its address. That is, the code should set the pointer in Customer to point to the CustomerAddress object whose id is stored in the Customer instance
Method readCustomerData
This method performs the following:
Reads data from a comma delimited file of customer information; this file has a header which should be read but ignored. The data on the file was extracted from a spreadsheet and has the typical CSV format as noted in the samples that follow.
customer_id,first_name,last_name,email,address_id
The implementation should mimic that in readCustomerAddresses, but be specialized to read values for Customer and not addresses.
This method returns the following:
A vector of Customer pointers
My mission is to modify the three methods in Utils so those methods meet the requirements described. The places in the code where your modifications should occur are clearly marked.
CustomerAddress * Utils:: findCustomerAddress (Customer *customer, std::vector addresses) {
int customerAddressId = customer->getCustomerAddressId();
int lowerBound = 0;
int upperBound = addresses.size() - 1;
int middle = upperBound / 2;
CustomerAddress *address = nullptr;
//****************************
//
// Write the code that will perform the binary search and return
// the matching address
//
//****************************
return address;
}
void Utils:: linkCustomersToAddresses (std::vector customers, std::vector addresses) {
//****************************
//
// Write the code that will link customers to their addresses
//
//****************************
}
std::vector Utils::readCustomerData() {
//****************************
//
// Replace the code here with code that will read Customer
// data off the appropriate file and create a vector of Customer
// pointers
//
//****************************
std::vector emptyVector;
return emptyVector;
}
THIS IS WHAT I HAVE SO FAR:
* Utils.h
*
* Created on: Mar 20, 2018
* Author: two
*/
#ifndef UTILS_H_
#define UTILS_H_
#include
#include
#include "Customer.h"
#include "CustomerAddress.h"
class Utils {
public:
// The files the program reads to build the customer and customer
// address vectors; values assigned in .cpp file
const static std::string CUSTOMER_INPUT_FILE;
const static std::string CUSTOMER_ADDRESS_INPUT_FILE;
Utils();
static CustomerAddress* findCustomerAddress (Customer *, std::vector ) ;
static void linkCustomersToAddresses (std::vector , std::vector );
static std::vector readCustomerData ();
static std::vector readCustomerAddresses ();
virtual ~Utils();
};
#endif /* UTILS_H_ */
/*
* Customer.cpp
*
* Created on: Mar 20, 2018
* Author: two
*/
#include
#include
#include "Customer.h"
Customer::Customer(std::string id, std::string firstName, std::string lastName,
std::string emailAddress, std::string customerAddressId) {
this->id = stringToInt(id);
this->firstName = firstName;
this->lastName = lastName;
this->emailAddress = emailAddress;
this->customerAddressId = stringToInt(customerAddressId);
this->customerAddress = nullptr;
}
Customer::Customer(int id, std::string firstName, std::string lastName,
std::string emailAddress, CustomerAddress *customerAddress) {
this->id = id;
this->firstName = firstName;
this->lastName = lastName;
this->emailAddress = emailAddress;
this->customerAddress = customerAddress;
if (customerAddress != nullptr) this->customerAddressId = customerAddress->getId();
else customerAddressId = -1;
}
const CustomerAddress* Customer::getCustomerAddress() const {
return customerAddress;
}
int Customer::getCustomerAddressId() const {
return customerAddressId;
}
const std::string Customer::getEmailAddress() const {
return emailAddress;
}
const std::string Customer::getFirstName() const {
return firstName;
}
int Customer::getId() const {
return id;
}
const std::string Customer::getLastName() const {
return lastName;
}
void Customer::print () const {
std::cout << "Customer [ id: " << id <<
", firstName: " << firstName <<
", lastName: " << lastName <<
", email Address: " << emailAddress <<
", address id: " << customerAddressId <<
"]" << std::endl;
}
void Customer::printWithAddress() const {
this->print();
if (customerAddress != nullptr) {
std::cout << "---- address: " ;
customerAddress->print();
std::cout << std::endl << std::endl;
}
}
void Customer::setCustomerAddress( CustomerAddress* customerAddress ) {
this->customerAddress = customerAddress;
}
void Customer::setCustomerAddressId(int customerAddressId) {
this->customerAddressId = customerAddressId;
}
void Customer::setEmailAddress(const std::string emailAddress) {
this->emailAddress = emailAddress;
}
void Customer::setFirstName(const std::string firstName) {
this->firstName = firstName;
}
void Customer::setId(int id) {
this->id = id;
}
void Customer::setLastName(const std::string lastName) {
this->lastName = lastName;
}
Customer::~Customer() {
}
int Customer::stringToInt(std::string aString) {
std::stringstream converter(aString);
int intValue;
converter >> intValue;
return intValue;
}
* Customer.h
*
* Created on: Mar 20, 2018
* Author: two
*/
#ifndef CUSTOMER_H_
#define CUSTOMER_H_
#include
#include "CustomerAddress.h"
//
// This class holds information on a single customer
//
// The class references another class that is the customer
// address (CustomerAddress). Until the Customer instance
// and CustomerAddress instance are "linked up", the customerAddress
// pointer is null.
//
// The "linking up" process requires that the CustomerAddress pointer in
// Customer be validly set to reference a CustomerAddress object.
// That CustomerAddress object is identified by the int value customerAddressId
// read in from the data file and stored in the Customer object
//
class Customer {
private:
int id; // the unique id
std::string firstName; // customer first name
std::string lastName; // customer last name
std::string emailAddress; // customer email address
int customerAddressId; // id of the CustomerAddress object
CustomerAddress *customerAddress = nullptr;
int stringToInt (std::string);
public:
// Constructors
Customer(std::string id, std::string firstName, std::string lastName, std::string emailAddress, std::string customerAddressId);
Customer(int id, std::string firstName, std::string lastName, std::string emailAddress, CustomerAddress*);
// getters
const CustomerAddress* getCustomerAddress() const;
int getCustomerAddressId() const;
const std::string getEmailAddress() const;
const std::string getFirstName() const;
int getId() const;
const std::string getLastName() const;
// utility methods
void print () const;
void printWithAddress () const;
// setters
void setCustomerAddress( CustomerAddress* customerAddress = nullptr);
void setCustomerAddressId(int customerAddressId);
void setEmailAddress(const std::string emailAddress);
void setFirstName(const std::string firstName);
void setId(int id);
void setLastName(const std::string lastName);
// destructor
virtual ~Customer();
};
#endif /* CUSTOMER_H_ */
* Address.cpp
*
* Created on: Mar 19, 2018
* Author: two
*/
#include "CustomerAddress.h"
#include
#include
#include
#include
#include
#include
CustomerAddress::CustomerAddress(int id, std::string address, std::string district,
std::string city, std::string country, std::string postalCode,
std::string phone) {
this->id = id;
this->address = address;
this->city = city;
this->country = country;
this->district = district;
this->phone = phone;
this->postalCode = postalCode;
}
CustomerAddress::CustomerAddress(std::string id, std::string address,
std::string district, std::string city, std::string country,
std::string postalCode, std::string phone) {
this->id = stringToInt(id);
this->address = address;
this->city = city;
this->country = country;
this->district = district;
this->phone = phone;
this->postalCode = postalCode;
}
const std::string CustomerAddress::getAddress() const {
return address;
}
const std::string CustomerAddress::getCity() const {
return city;
}
const std::string CustomerAddress::getCountry() const {
return country;
}
const std::string CustomerAddress::getDistrict() const {
return district;
}
int CustomerAddress::getId() const {
return id;
}
const std::string CustomerAddress::getPhone() const {
return phone;
}
const std::string CustomerAddress::getPostalCode() const {
return postalCode;
}
void CustomerAddress::print () const {
std::cout << "CustomerAddress [ id: " << id <<
", address: " << address <<
", city: " << city <<
", district: " << district <<
", country: " << country <<
", postal code: " << postalCode <<
", phone: " << phone <<
"] " << std::endl;
}
void CustomerAddress::setAddress(const std::string address) {
this->address = address;
}
void CustomerAddress::setCity(const std::string city) {
this->city = city;
}
void CustomerAddress::setCountry(const std::string country) {
this->country = country;
}
void CustomerAddress::setDistrict(const std::string district) {
this->district = district;
}
void CustomerAddress::setId(int id) {
this->id = id;
}
void CustomerAddress::setPhone(const std::string phone) {
this->phone = phone;
}
void CustomerAddress::setPostalCode(const std::string postalCode) {
this->postalCode = postalCode;
}
CustomerAddress::~CustomerAddress() {
// TODO Auto-generated destructor stub
}
int CustomerAddress::stringToInt(std::string aString) {
int aValue = atoi(aString.c_str());
std::stringstream converter(aString);
int intValue;
converter >> intValue;
return intValue;
}
* CustomerAddress.h
*
* Created on: Mar 19, 2018
* Author: two
*/
#ifndef CUSTOMERADDRESS_H_
#define CUSTOMERADDRESS_H_
#include
//
// CustomerAddress holds information on a Customer's home
// The Customer class references the CustomerAddress class
// Customer has one unique CustomerAddress
//
class CustomerAddress {
private:
int id; // the unique id
std::string address; // the street address
std::string district; // a location within a city
std::string city; // the city
std::string country; // the country
std::string postalCode; // zip code (sorta)
std::string phone; // phone number
int stringToInt(std::string);
public:
// Constructors
CustomerAddress(int, std::string, std::string, std::string, std::string,
std::string, std::string);
CustomerAddress(std::string, std::string, std::string, std::string, std::string,
std::string, std::string);
// getters
const std::string getAddress() const;
const std::string getCity() const;
const std::string getCountry() const;
const std::string getDistrict() const;
int getId() const;
const std::string getPhone() const;
const std::string getPostalCode() const;
// utility function
void print () const;
// setters
void setAddress(const std::string address);
void setCity(const std::string city);
void setCountry(const std::string country);
void setDistrict(const std::string district);
void setId(int id);
void setPhone(const std::string phone);
void setPostalCode(const std::string postalCode);
// destructor
virtual ~CustomerAddress();
};
#endif /* CUSTOMERADDRESS_H_ */
* Main.cpp
*
* Created on: Mar 20, 2018
* Author: two
*/
#include
#include
#include
#include
#include "CustomerAddress.h"
#include "Utils.h"
int main () {
std::vector customers = Utils::readCustomerData();
std::vector addresses = Utils::readCustomerAddresses();
Utils::linkCustomersToAddresses(customers, addresses);
}
Explanation / Answer
1. for first fun():
findCustomerAddress:
we can use iterative or recursive sol. to binary search:
int l=lower_bound;
int h=upper_bound;
binary_search:
while(l<=r){
int m=(l+r)/2 //m=l+(r-l)/2;
if(arr[m]==val)
return m;
else if(arr[m]>val){
h=m-1;}
else{
l=m+1;}
2. for second fun()
linkCustomersToAddresses :
we can link customer with the help of linked list or we can use map
unordered_map<int,int> m
3. for third fun()
readCustomerData() :
we can create vector and run a loop until the end of file and we can update information in vector
for eg. -
vector<int> v;
lets suppose we have to construct vector from array of size n;
for(int i=0;i<n;i++){
v.push_back(a[i]);
}
then finally return vector;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.