Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Introduction Modern shipping companies keep track of the location of time sensit

ID: 3739457 • Letter: I

Question

Introduction

Modern shipping companies keep track of the location of time sensitive deliveries. Tracking numbers are numbers given to packages when they are shipped. Both senders and receivers can use tracking numbers to view most recent shipping status and trace back to previous status, as shown in the picture above. The first status comes from the company shipping the package. In the example shown in the picture, the first status is “Package has left seller facility and is in transit to carrier”. The other statuses are scans at various distribution points within the shipper's system. In this project you will write C++ code to model package tracking.

in addition, when asked, your program should keep track of every shipping status and when it was updated. Since we do not know how many updates the shipping will have, we will have a Linked List that will keep track of every status.

You are given a simple text file containing actions: back, forward, or new. If the action is listed as new, the next line contains three items, time, location and status, separated by semicolon. See TBA688567081000.txt file for more details. TBA688567081000.txt shows you the example same as in the picture. You are to simulate package tracking based on this text file.

Objective

You are given partial implementations of two classes. ShippingStatus is a class that holds shipping status including location and status of the package, as well as when the status was recorded. The time visited is the number of seconds from the UNIX epoch, 00:00 Jan 1, 1970 UTC. C++ has a variable type that can handle this, named time_t.

PackageTracking is where the bulk of your work will be done. PackageTracking stores a linked list representation of all the status. It will be able to read the history from a text file.

The text file will have 3 basic commands: new, back, and forward. Back and forward will allow users to view the previous status and the next status of a package. New will provide a newly updated status.

You are to complete the implementations of these classes, adding public/private member variables and functions as needed.

Your code is tested in the provided main.cpp.

Source Code Files

You are given “skeleton” code files with many blank areas. Your assignment is to fill in the missing parts so that the code is complete and works properly when tested.

?     ShippingStatus.h and ShippingStatus.cpp: Stores location and status of the package, as well as when the status was recorded.

?     PackageTracking.h and PackageTracking.cpp: Stores a linked list representation of all the shipping status for a given package.

?     This class contains a method to read item information from a text file. m_readTrackingFile() will read the full tracking chain from a file and follow the commands as specified in the file. Hint: use ifstream, istringstream, getline().

?    m_printPreviousUpdates() will print all previous status in the shipping chain when the package was shipped, all the way up to (but not including) the current status that you are viewing.

?     m_printFollowingUpdates() will print all status following the current status that you are viewing (inclusive) to the last status in the tracking chain.

?     m_printFullTracking() will print all the status updates in the tracking chain.    

?     Main.cpp: The entry point to the application. The main() function will test the output of your functions. This is already completed but feel free to change it for your own testing (during grading we will use the original main file with more test examples).

Hints

Read code comments for more details of function descriptions.

Start by implementing the ShippingStatus class, then the PackageTracking class. It can be overwhelming working on the PackageTracking class so start with the constructor, then the m_addUpdate() function, then the m_moveBackward()and m_moveForward()functions.

Remember the PackageTracking class will include a linked list for the shipping history. It will also need an iterator or pointer to point to a specific status in the linked list.

Iterators are very similar to pointers. Both iterators and pointers can be tricky. Make sure you’re keeping track of whether you’re talking about an address or the object at that address. Remember to use the -> operator!

---------------------------------------------------------------------------

ShippingStatus.h

#ifndef ShippingStatus_h

ShippingStatus.cpp

PackageTracking.h

PackageTracking.cpp

TBA688567081000.txt

Main.cpp

#ifndef ShippingStatus_h

#define ShippingStatus_h #pragma once #include <string> using namespace std; class ShippingStatus { public: ShippingStatus(); ShippingStatus(const string& status, const string& location, const time_t& timeUpdated ); string m_getStatus(); string m_getLocation(); time_t m_getTime(); private: }; #endif /* ShippingStatus_h */

Explanation / Answer

main.cpp


#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <stdexcept>

#include "PackageTracking.hpp"
#include "ShippingStatus.hpp"

using namespace std;


template <typename T>
bool testAnswer(const string &nameOfTest, const T& received, const T& expected) {
if (received == expected) {
cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
return true;
}
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}

template <typename T>
bool testArrays(const string& nameOfTest, const T& received, const T& expected, const int& size) {
for(int i = 0; i < size; i++) {
if(received[i] != expected[i]) {
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}
}
  
cout << "PASSED " << nameOfTest << ": expected and received matching arrays" << endl;
return true;
}


int main(int argc, char *argv[]) {
// Test only ShippingStatus class
ShippingStatus testStatus01("Package has left seller facility and is in transit to carrier", "N/A", 1515978000, nullptr,nullptr);
testAnswer("testStatus01.m_getLocation() test", testStatus01.m_getLocation(), string("N/A"));
testAnswer("testStatus01.m_getStatus() test", testStatus01.m_getStatus(), string("Package has left seller facility and is in transit to carrier"));
testAnswer("testStatus01.m_getTime() test", testStatus01.m_getTime(), time_t(1515978000));
  
ShippingStatus testStatus02("Shipment arrived at Amazon facility", "Hebron, KENTUCKY US", 1516111440, nullptr,nullptr);
testAnswer("testStatus02.m_getLocation() test", testStatus02.m_getLocation(), string("Hebron, KENTUCKY US"));
testAnswer("testStatus02.m_getStatus() test", testStatus02.m_getStatus(), string("Shipment arrived at Amazon facility"));
testAnswer("testStatus02.m_getTime() test", testStatus02.m_getTime(), time_t(1516111440));
  
ShippingStatus testStatus03("Shipment arrived at Amazon facility", "San Bernardino, CALIFORNIA US", 1516366740, nullptr,nullptr);
testAnswer("testStatus03.m_getLocation() test", testStatus03.m_getLocation(), string("San Bernardino, CALIFORNIA US"));
testAnswer("testStatus03.m_getStatus() test", testStatus03.m_getStatus(), string("Shipment arrived at Amazon facility"));
testAnswer("testStatus03.m_getTime() test", testStatus03.m_getTime(), time_t(1516366740));

  
// Test PackageTracking class
string tmp_strtrackingnumber;//
tmp_strtrackingnumber = "TBA688567081000";//hard-coded testing, could change to use argv for testing
PackageTracking testPackageTracking(tmp_strtrackingnumber);
testPackageTracking.m_addUpdate(testStatus01.m_getStatus(), testStatus01.m_getLocation(), testStatus01.m_getTime());
testPackageTracking.m_addUpdate(testStatus02.m_getStatus(), testStatus02.m_getLocation(), testStatus02.m_getTime());
testPackageTracking.m_addUpdate(testStatus03.m_getStatus(), testStatus03.m_getLocation(), testStatus03.m_getTime());
  
testPackageTracking.m_viewUpdate(testStatus01.m_getTime());
testAnswer("testPackageTracking.m_getLocation()", testPackageTracking.m_getLocation(), string("N/A"));
testAnswer("testPackageTracking.m_getStatus( )", testPackageTracking.m_getStatus( ), string("Package has left seller facility and is in transit to carrier"));
  
testPackageTracking.m_viewUpdate(testStatus02.m_getTime());
testAnswer("testPackageTracking.m_getLocation()", testPackageTracking.m_getLocation(), string("Hebron, KENTUCKY US"));
testAnswer("testPackageTracking.m_getStatus( )", testPackageTracking.m_getStatus( ), string("Shipment arrived at Amazon facility"));
  
// Test back and forward
testPackageTracking.m_moveForward();
testAnswer("testPackageTracking.m_moveForward()", testPackageTracking.m_getLocation(), string("San Bernardino, CALIFORNIA US"));
testAnswer("testPackageTracking.m_getStatus( )", testPackageTracking.m_getStatus( ), string("Shipment arrived at Amazon facility"));
testAnswer("testPackageTracking.m_getTime( )", testPackageTracking.m_getTime( ), time_t(1516366740));
testPackageTracking.m_moveBackward();
testAnswer("testPackageTracking.m_moveBackward()", testPackageTracking.m_getLocation(), string("Hebron, KENTUCKY US"));
testAnswer("testPackageTracking.m_getStatus( )", testPackageTracking.m_getStatus( ), string("Shipment arrived at Amazon facility"));
testAnswer("testPackageTracking.m_getTime( )", testPackageTracking.m_getTime( ), time_t(1516111440));
  
  
// Test PackageTracking reading from a file
PackageTracking testPackageTracking01(tmp_strtrackingnumber);
string tmp_filename = tmp_strtrackingnumber + ".txt";
testPackageTracking01.readTrackingFile(tmp_filename);
testAnswer("testPackageTracking01.m_getLocation()", testPackageTracking01.m_getLocation(), string("Chino, US"));
testAnswer("testPackageTracking01.m_getStatus( )", testPackageTracking01.m_getStatus( ), string("Package arrived at a carrier facility"));
testAnswer("testPackageTracking01.m_getTime( )", testPackageTracking01.m_getTime( ), time_t(1516410060));

  
// Test history printing
cout << " Printing all previous updates: ";
testPackageTracking01.m_printPreviousUpdates();
cout << " Printing all following updates: ";
testPackageTracking01.m_printFollowingUpdates();
cout << " Printing Full History: ";
testPackageTracking01.m_printFullTracking();
  
return 1;
}


PackageTracking.cpp

#include "PackageTracking.hpp"

PackageTracking::PackageTracking(const string& strnum){
m_numUpdate = 0; // initially empty
m_header = new ShippingStatus; // create sentinels
m_trailer = new ShippingStatus;
m_header->m_setNext(m_trailer); // have them point to each other
m_trailer->m_setPrev(m_header);
m_cur = m_header; //set iterator
m_trackingnumber = strnum;
  
}
PackageTracking::~PackageTracking(){
if (!m_isEmpty()) m_clear(); // remove all but sentinels
delete m_header; // remove the sentinels
delete m_trailer;
  
}
// add a new update before trailer end
int PackageTracking::m_addUpdate( const string& status, const string& location, const time_t& timeUpdated){
ShippingStatus* ptr_newupdate = new ShippingStatus(status, location, timeUpdated, nullptr, nullptr);
if(!ptr_newupdate) return -1;
ptr_newupdate->m_setNext(m_trailer);
ShippingStatus* ptr_pretrailer = m_trailer->m_getPrev();
ptr_newupdate->m_setPrev(ptr_pretrailer);
ptr_pretrailer->m_setNext(ptr_newupdate);
m_trailer->m_setPrev(ptr_newupdate);
m_numUpdate++;
m_cur = ptr_newupdate;// move cur pointer
  
return 1;
}


int PackageTracking::m_removeUpdate()// remove the update before trailer end
{
ShippingStatus* ptr_pretrailer = m_trailer->m_getPrev();
if((ptr_pretrailer == m_header)||!ptr_pretrailer)
return -1;
ShippingStatus* ptr_prepretrailer = ptr_pretrailer->m_getPrev();
if(!ptr_prepretrailer) return -1;
ptr_prepretrailer->m_setNext(m_trailer);
m_trailer->m_setPrev(ptr_prepretrailer);
m_numUpdate--;
delete ptr_pretrailer;
  
return 1;
}

int PackageTracking::m_moveBackward()//move iterator one step towards header
{
ShippingStatus* ptr_curpre = m_cur->m_getPrev();
if(ptr_curpre == m_header)
return -1;
m_cur = ptr_curpre;
return 1;
}

int PackageTracking::m_moveForward()//move iterator one step towards trailer
{
ShippingStatus* ptr_curnext = m_cur->m_getNext();
if(ptr_curnext == m_trailer)
return -1;
m_cur = ptr_curnext;
return 1;
}

string PackageTracking::m_getLocation( )//return the location of the current update
{
if(!m_cur) return "N/A";
return m_cur->m_getLocation();
}

time_t PackageTracking::m_getTime( )//return the time of the current update
{
if(!m_cur) return 0;
time_t tmp_tt = m_cur->m_getTime();
return tmp_tt;
}

string PackageTracking::m_getStatus( )//return the status of the current update
{
if(!m_cur) return "N/A";
return m_cur->m_getStatus();
}

int PackageTracking::m_getNumofUpdate() const // get the total numbers of shipping status updates
{
return m_numUpdate;
}

int PackageTracking::m_printPreviousUpdates() //print all previous updates in the shipping chain when the package was shipped, all the way up to (but not including) the current update you are viewing (may not be the most recent update).
{

if(m_isEmpty() || (m_cur->m_getPrev() == m_header))
{
cout<<"no previous updates"<<endl;
return -1;
}
ShippingStatus* ptr_cur = m_cur;
while(m_cur->m_getPrev()!=m_header)
{
m_moveBackward();
cout<<m_getTime()<<": "<<m_getStatus()<<": "<<m_getLocation()<<endl;
  
}
m_cur = ptr_cur;// reset m_cur
return 1;
}
int PackageTracking::m_printFollowingUpdates()//print all following updates including the current update you are viewing. From the current update you are viewing to the last update in the tracking chain.
{

cout<<m_getTime()<<": "<<m_getStatus()<<": "<<m_getLocation()<<endl;
if(m_isEmpty() ||(m_cur->m_getNext()==m_trailer))
{
cout<<"no following updates"<<endl;
return -1;
}
ShippingStatus* ptr_cur = m_cur;
while(m_cur->m_getNext()!=m_trailer)
{
m_moveForward();
cout<<m_getTime()<<": "<<m_getStatus()<<": "<<m_getLocation()<<endl;
}
m_cur = ptr_cur;// reset m_cur
return 1;
}

int PackageTracking::m_printFullTracking()//print all the updates in the tracking chain.
{
cout<<"Tracking number: "<< m_getTrackingnumber()<<endl;
if(m_isEmpty()){
cout<<"no updates"<<endl;
return -1;
}
ShippingStatus* ptr_cur = m_cur;
while(m_cur!=m_header->m_getNext())// move m_cur to the first element
m_moveBackward();
  
while(m_cur!=m_trailer->m_getPrev())//iterate and print
{
cout<<m_getTime()<<": "<<m_getStatus()<<": "<<m_getLocation()<<endl;
m_moveForward();
}
if(m_cur==m_trailer->m_getPrev())//the only data element or the last element
cout<<m_getTime()<<": "<<m_getStatus()<<": "<<m_getLocation()<<endl;
  
m_cur = ptr_cur;
return 1;
}

bool PackageTracking::m_isEmpty() const//check empty
{
if(m_header->m_getNext()==m_trailer)
return true;
return false;
}

void PackageTracking::m_clear()//remove all but sentinels
{
while (!m_isEmpty())
m_removeUpdate();
}

string PackageTracking::m_getTrackingnumber(){
return m_trackingnumber;
}

int PackageTracking::m_viewUpdate(const time_t& timeUpdated)//view an update.
{
if(m_isEmpty()) return -1;
m_cur = m_header->m_getNext();
while((m_cur->m_getTime()!=timeUpdated)&&(m_cur->m_getNext()!=m_trailer))
m_moveForward();
return 1;
}
//read the full tracking chain from a file and follow the commands as specified in the file
int PackageTracking::readTrackingFile(string fileName) {

ifstream infile;
infile.open(fileName);

while (infile)
{
string tmp_strline;
if (!getline( infile, tmp_strline )) break;
  
//cout<<tmp_strline<<endl;
if(tmp_strline == "new"){
if (!getline( infile, tmp_strline )) break;//get next line
istringstream tmp_str( tmp_strline );
string tmp_loc, tmp_sta,tmp_strtime;
time_t tmp_tt;
if (!getline( tmp_str, tmp_sta, ';' )) break;
if (!getline( tmp_str, tmp_loc, ';' )) break;
if (!getline( tmp_str, tmp_strtime, ';' )) break;
stringstream geek(tmp_strtime);
int i_time = 0;
geek >> i_time;
tmp_tt = (time_t)i_time;
//cout<<tmp_loc<<";"<<tmp_sta<<";"<<tmp_strtime<<endl;
m_addUpdate(tmp_sta,tmp_loc,tmp_tt);
  
}else if(tmp_strline == "back") {
m_moveBackward();
} else if(tmp_strline == "forward") {
m_moveForward();
} else {
return -2;
}
}
if (!infile.eof())
{
return -1;
}
return 1;
}

PackageTracking.hpp


#ifndef PackageTracking_h
#define PackageTracking_h

#pragma once

#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <sstream>
#include <stdexcept>
#include "ShippingStatus.h"
using namespace std;

class PackageTracking {
public:
PackageTracking(const string& strnum);
~PackageTracking();
  
int m_addUpdate( const string& status, const string& location, const time_t& timeUpdated);// add a new update before trailer end
//int m_addUpdate(const ShippingStatus& newupdate);// add a new update before trailer end
int m_removeUpdate();// remove the update before trailer end
  
int m_moveBackward();//move iterator one step towards header
int m_moveForward();//move iterator one step towards trailer

string m_getLocation( );//return the location of the current update
time_t m_getTime( );//return the time of the current update
string m_getStatus( );//return the status of the current update
int m_getNumofUpdate() const; // get the total numbers of shipping status updates
  
int m_viewUpdate(const time_t& timeUpdated);//view an update.
  
int m_printPreviousUpdates(); //print all previous updates in the shipping chain when the package was shipped, all the way up to (but not including) the current update you are viewing (may not be the most recent update).
int m_printFollowingUpdates();//print all following updates including the current update you are viewing. From the current update you are viewing to the last update in the tracking chain.
  
int m_printFullTracking();//print all the status updates in the tracking chain.
bool m_isEmpty() const;//check empty
void m_clear();//remove all but sentinels

string m_getTrackingnumber();
//read the full tracking chain from a file and follow the commands as specified in the file
int readTrackingFile(string fileName);
  
  
private:
ShippingStatus *m_header, *m_trailer, *m_cur;//header, trailer, iterator
int m_numUpdate;//number of updates
string m_trackingnumber;//package tracking number
};

#endif /* PackageTracking_h */

ShippingStatus.cpp


#include "ShippingStatus.hpp"


ShippingStatus::ShippingStatus() {
m_strStatus = "";
m_strLocation = "";
m_ttTime = 0;
m_prev = nullptr;
m_next = nullptr;
}

ShippingStatus::~ShippingStatus()
{
  
}

ShippingStatus::ShippingStatus(const string& status, const string& location, const time_t& timeUpdated, ShippingStatus* prev, ShippingStatus* next) {
m_strStatus = status;
m_strLocation = location;
m_ttTime = timeUpdated;
m_prev = prev;
m_next = next;
}

string ShippingStatus::m_getStatus(){
return m_strStatus;
}

string ShippingStatus::m_getLocation(){
return m_strLocation;
}


time_t ShippingStatus::m_getTime() {
return m_ttTime;
}

ShippingStatus* ShippingStatus::m_getPrev(){
return m_prev;
}

ShippingStatus* ShippingStatus::m_getNext(){
return m_next;
}

int ShippingStatus::m_setStatus(const string& status){
m_strStatus = status;
return 1;
}
int ShippingStatus::m_setLocation(const string& location){
m_strLocation = location;
return 1;
}
int ShippingStatus::m_setTime(const time_t& timeUpdated){
m_ttTime = timeUpdated;
return 1;
}
int ShippingStatus::m_setPrev(ShippingStatus* prev){
m_prev = prev;
return 1;
}
int ShippingStatus::m_setNext(ShippingStatus* next){
m_next = next;
return 1;
}

ShippingStatus.hpp

#ifndef ShippingStatus_h
#define ShippingStatus_h

#pragma once

#include <stdio.h>
#include <string>
using namespace std;

class ShippingStatus {
public:
ShippingStatus();
ShippingStatus(const string& status, const string& location, const time_t& timeUpdated, ShippingStatus* prev, ShippingStatus* next);
~ShippingStatus();
string m_getStatus();
string m_getLocation();
time_t m_getTime();
ShippingStatus* m_getPrev();
ShippingStatus* m_getNext();

int m_setStatus(const string& status);
int m_setLocation(const string& location);
int m_setTime(const time_t& timeUpdated);
int m_setPrev( ShippingStatus* prev);
int m_setNext( ShippingStatus* next);
  
private:
string m_strStatus;
string m_strLocation;
time_t m_ttTime;
ShippingStatus *m_prev, *m_next;
};


#endif /* ShippingStatus_h */

one.txt

new
Package has left seller facility and is in transit to carrier;N/A;1515978000
new
Shipment arrived at Amazon facility;Hebron, KENTUCKY US;1516111440
new
Shipment departed from Amazon facility;Hebron, KENTUCKY US;1516188120
new
Shipment arrived at Amazon facility;San Bernardino, CALIFORNIA US;1516366740
new
Shipment departed from Amazon facility;San Bernardino, CALIFORNIA US;1516392780
new
Package arrived at a carrier facility;Chino, US;1516410060
new
Out for delivery;Chino, US;1516441740
new
Delivered;Diamond Bar, US;1516468200
back
back
back
back
forward
forward

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote