Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

introduction Modern browsers keep track of browsing history which allows users to recall previously visited web pages using the back arrow button.. When a user

introduction

Modern browsers keep track of browsing history which allows users to recall previously visited web pages using the back arrow button.. When a user navigates back to a previous web page, they also have the option to navigate forward in the navigation history using the forward arrow button. However, if the user navigates back to a previous page and then goes to a new web page, all web pages that previously could be accessed with the forward arrow button are discarded. This behavior is well suited for Linked Lists. In this project you will write C++ code to model this behavior.

For a good video overview of this, check out this link.

In addition, when asked, your program should respond with every web page visited and when it was visited. Since we do not know how many sites the user will visit, we will have a second Linked List that will keep track of every site visited.

You are given a simple text file of a users browsing history containing their actions: back, forward, or new. If their action is listed as new, it will be followed with a web address. Simulate their browsing history based on this text file.

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.

Need help filling out the missing code for the back and forward function in browserHistory header file and every thing that says TO BE COMPLETED on the browserHistory cpp file.

//////moblie.txt

new https://twitter.com/ 1506134520

new https://twitter.com/sehulrburt 1506134525

new http://stephaniehurlburt.com/ 1506134530

back

back

forward

new http://www.binomial.info/ 1506134540

new https://sites.google.com/site/richgel99/ 1506134545

back

//desktop.txt

new https://en.wikipedia.org/ 1506134400

new https://en.wikipedia.org/wiki/NASA 1506134420

new https://en.wikipedia.org/wiki/Neil_Armstrong 1506134440

new https://en.wikipedia.org/wiki/Apollo_11 1506134460

back

forward

back

back

new https://en.wikipedia.org/wiki/International_Space_Station 1506134500

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

////////////////////////////////////////////////////////////////////////////////

template

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

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() {

// 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;

testHistory.visitSite(testPage01);

testAnswer("BrowserHistory.getNavSize()", testHistory.getNavSize(), size_t(1));

//here is where things break

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

cout << " Printing Back Sites: ";

desktop.printBackSites();

cout << " Printing Forward Sites: ";

desktop.printForwardSites();

cout << " Printing Full History: ";

desktop.printFullHistory();

return 0;

}

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

WebPage.cpp

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

#include "Webpage.h"

//default constuctor

Webpage::Webpage()

{

webpageURL = "NULL";

timeVisited = NULL;

}

//constructor

Webpage::Webpage(const string& webpageURL, const time_t& timeVisited)

{

this->webpageURL = webpageURL;

this->timeVisited = timeVisited;

}

//return url

string Webpage::getURL()

{

return webpageURL;

}

//return time

time_t Webpage::getTime()

{

return timeVisited;

}

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

Webpage.h

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

#pragma once

#include

using namespace std;

class Webpage {

public:

Webpage(); //default constructor

Webpage(const string& webpageURL, const time_t& timeVisited);

string getURL(); //returns url

time_t getTime(); //returns time

private:

string webpageURL;

time_t timeVisited;

Webpage *prev;

Webpage *next;

friend class BrowserHistory;

};

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

BrowserHistory.cpp

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

#include "BrowserHistory.h"

//default constructor

BrowserHistory::BrowserHistory()

{

//list::iterator

itN=navHis.begin();//iterator for navHis

//list::iterator

itF=fullHis.begin();//iterator for fullHis

}

//destructor

BrowserHistory::~BrowserHistory()

{

navHis.clear();

fullHis.clear();

}

void BrowserHistory::visitSite(Webpage newSite)

{

itN = navHis.end();

//itF = fullHis.begin();

navHis.insert(itN, newSite);

//fullHis.insert(itF, newSite);

//navHis.push_back(newSite);

fullHis.push_back(newSite);

}

//return url

string BrowserHistory::getURL()

{

*itN

//list *ptemp;

//Webpage temp;

//temp = *itN;

//Webpage temp;

//temp = *itN;

//Webpage pls;

//pls = *itN;

//return pls.getURL();

//return navHis.back().getURL();

//return *itN.getURL();

//return temp.getURL();

//return this->itN.getURL();

//cout << "here";

//return (*itF)->getURL();

return ;

}

//return navsize

size_t BrowserHistory::getNavSize()

{

//cout << navHis.size();

return navHis.size();

}

//go to previous webpage

string BrowserHistory::back()

{

//with the iterators

if(itN != navHis.begin()){

itN--;

return itN->getURL();

//add to fullHis

}

else{

cout<< "There is no previous page" << endl;

}

}

//go to next webpage

string BrowserHistory::forward()

{

//with the iterators

if(itN != navHis.end()){

itN++;

return itN->getURL();

//add to full and navHis

}

else{

cout<< "There is no page to move forward to." << endl;

}

}

//read from file

void BrowserHistory::readHistory(string fileName)

{

ifstream myfile(fileName);

if (myfile.is_open()) {

cout << "Successfully opened file " << fileName << endl;

char ch;

string input;

while (myfile >> ch)

{

fileName.push_back(ch);

myfile >> input;

if (input == "new")

{

time_t time;

string url;

myfile >> url >> time;

Webpage temp(url, time);

visitSite(temp);

}

else if (input == "back")

{

back();

}

else if (input == "foward")

{

forward();

}

}

myfile.close();

}

else throw invalid_argument("Could not open file " + fileName);

}

void BrowserHistory::printBackSites()

{

list::iterator current;

current = itN;

for(itN = current; itN != navHis.begin(); itN--){

cout << itN->getURL() << endl;

}

}

void BrowserHistory::printForwardSites()

{

list::iterator current;

current = itN;

for (itN = current; itN != navHis.end(); itN++) {

cout << itN->getURL() << endl;

}

}

void BrowserHistory::printFullHistory()

{

for (itF = fullHis.begin(); itF != fullHis.end(); itF++) {

cout << itF->getURL() << endl;

}

}

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

BrowserHistory.h

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

#pragma once

#include

#include

#include

#include

#include

#include "Webpage.h"

using namespace std;

class BrowserHistory {

public:

BrowserHistory(); //default constructor

~BrowserHistory(); //destructor

void visitSite(Webpage newSite); //reads/stores new site

string back(); //previous webpage

string forward(); //next webpage

void readHistory(string fileName);

string getURL(); //get url

size_t getNavSize(); //get size

void printBackSites(); //print sites from p to head

void printForwardSites(); // print sites from p to tail

void printFullHistory(); // print sites from head to tail

private:

list fullHis;

Webpage* cursorN;

//list* cursorF;

Webpage *head;

Webpage *tail;

int countern;

//list::iterator itN;//iterator for navHis

list::iterator itF;//iterator for fullHis

};

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

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions