Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective You are given partial implementations of two classes. Webpage is a class that holds both the web page url as well as the time

Objective

You are given partial implementations of two classes. Webpage is a class that holds both the web page url as well as the time it was first visited. 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.

BrowserHistory is where the bulk of your work will be done. BrowserHistory stores both a linked list representation of the users navigation history, as well as an overall history of all sites theyve visited. 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 simulate the user pressing the back and forward arrow buttons of their browser. New will be followed by the web pages url and the time the site was visited.

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

You are encouraged to use the C++ Standard Library containers (such as std::list, and std::list::iterator) for this project.

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.

Webpage.h: Stores both a web pages url and the time it was visited.

BrowserHistory.h: stores both the users navigation history as well as their full history of sites visited.

This class contains a method to read item information from a text file.

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 (we will use the original main file).

Hints

Start by implementing the Webpage class, then the BrowserHistory class. It can be overwhelming working on the BrowserHistory class so start with the constructor, then the visitSite() function, then the back() and forward() functions.

Remember the BrowserHistory class will include two linked lists, one for the navigation history and one for the full history of every site visited. It will also need an iterator or pointer to point to a specific position in the linked list.

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

webpage.h

#pragma once
#include
using namespace std;
class Webpage {
public:
Webpage();
Webpage(const string& webpageUrl, const time_t& timeVisited);
string getUrl();
time_t getTime();
private:
string url;
time_t time;

};

Browserhistory.h

#pragma once
#include
#include
#include
#include
#include
#include "Webpage.h"
using namespace std;
class BrowserHistory {
public:
BrowserHistory();
void visitSite(Webpage newSite);
string back();
string forward();
void readHistory(string fileName);
string getUrl();
size_t getNavSize();
list getSitesVisited();
private:
list navHistory;
list::iterator navPos;
list sitesVisited;
int numSites;
};
void BrowserHistory::readHistory(string fileName) {
string temp;
int newTime;
Webpage newSite;
ifstream inFile(fileName.c_str());
while(inFile >> temp) {
if(temp == "new") {
inFile >> temp >> newTime;
newSite = Webpage(temp, time_t(newTime));
visitSite(newSite);
} else if(temp == "back") {
back();
} else if(temp == "forward") {
forward();
} else {
throw logic_error("invalid command");
}
}

}

main.cpp

#include
#include
#include
#include
#include
#include "Webpage.h"
#include "BrowserHistory.h"
using namespace std;
////////////////////////////////////////////////////////////////////////////////
// DO NOT EDIT THIS FILE (except for your own testing)
// CODE WILL BE GRADED USING A MAIN FUNCTION SIMILAR TO THIS
////////////////////////////////////////////////////////////////////////////////
static int testCount = 0;
template
bool testAnswer(const string &nameOfTest, const T& received, const T& expected);
int main() {
// Test only Webpage class
Webpage testPage01("http://www.google.com", 1506134400);
testAnswer("Webpage01.getUrl() test", testPage01.getUrl(), string("http://www.google.com"));
testAnswer("Webpage01.getTime() test", testPage01.getTime(), time_t(1506134400));
Webpage testPage02("http://twitter.com", 1506134420);
testAnswer("Webpage02.getUrl() test", testPage02.getUrl(), string("http://twitter.com"));
testAnswer("Webpage02.getTime() test", testPage02.getTime(), time_t(1506134420));
Webpage testPage03("http://stackoverflow.com", 1506134440);
testAnswer("Webpage03.getUrl() test", testPage03.getUrl(), string("http://stackoverflow.com"));
testAnswer("Webpage03.getTime() test", testPage03.getTime(), time_t(1506134440));
// Test BrowserHistory class
BrowserHistory testHistory;
// Expect the next test to fail
string nameOfTest = "BrowserHistory.getUrl() empty history";
try {
testAnswer(nameOfTest, testHistory.getUrl(), string("http://www.bing.com"));
cout << "FAILED " << nameOfTest << ": expected to recieve an error but didn't" << endl;
} catch (const logic_error& e) {
cout << "PASSED " << nameOfTest << ": expected and received error " << e.what() << endl;
testCount++;
}
testHistory.visitSite(testPage01);
testAnswer("BrowserHistory.getNavSize()", testHistory.getNavSize(), size_t(1));
testAnswer("BrowserHistory.getUrl()", testHistory.getUrl(), string("http://www.google.com"));
testHistory.visitSite(testPage02);
testAnswer("BrowserHistory.getNavSize()", testHistory.getNavSize(), size_t(2));
testAnswer("BrowserHistory.getUrl()", testHistory.getUrl(), string("http://twitter.com"));
// Test BrowserHistory navigation
testAnswer("BrowserHistory.back()", testHistory.back(), string("http://www.google.com"));
testAnswer("BrowserHistory.getUrl()", testHistory.getUrl(), string("http://www.google.com"));
testAnswer("BrowserHistory.forward()", testHistory.forward(), string("http://twitter.com"));
testHistory.back();
testHistory.visitSite(testPage03);
testAnswer("BrowserHistory.getNavSize()", testHistory.getNavSize(), size_t(2));
testAnswer("BrowserHistory.getUrl()", testHistory.getUrl(), string("http://stackoverflow.com"));
// Test BrowserHistory reading from a file
BrowserHistory desktop;
desktop.readHistory("desktop.txt");
testAnswer("BrowserHistory.getNavSize()", desktop.getNavSize(), size_t(3));
testAnswer("BrowserHistory.getUrl()", desktop.getUrl(),
string("https://en.wikipedia.org/wiki/International_Space_Station"));
desktop.readHistory("mobile.txt");
testAnswer("BrowserHistory.getNavSize()", desktop.getNavSize(), size_t(7));
testAnswer("BrowserHistory.getUrl()", desktop.getUrl(), string("http://www.binomial.info/"));
// Test Back and Forward Again
testAnswer("BrowserHistory.back() 02", desktop.back(), string("https://twitter.com/sehulrburt"));
desktop.forward();
testAnswer("BrowserHistory.forward() 02", desktop.forward(), string("https://sites.google.com/site/richgel99/"));
desktop.back();
// Test BrowserHistory full history
string expected[] = {
"https://en.wikipedia.org/",
"https://en.wikipedia.org/wiki/NASA",
"https://en.wikipedia.org/wiki/Neil_Armstrong",
"https://en.wikipedia.org/wiki/Apollo_11",
"https://en.wikipedia.org/wiki/International_Space_Station",
"https://twitter.com/",
"https://twitter.com/sehulrburt",
"http://stephaniehurlburt.com/",
"http://www.binomial.info/",
"https://sites.google.com/site/richgel99/"
};
int i = 0;
for(auto site : desktop.getSitesVisited()) {
testAnswer("BrowserHistor full history " + to_string(i), site.getUrl(), expected[i]);
i++;
}
cout << endl << testCount << " tests passed out of 32 total tests" << endl;
return 0;
}
template
bool testAnswer(const string &nameOfTest, const T& received, const T& expected) {
if (received == expected) {
cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
testCount++;
return true;
}
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago