Created
January 29, 2014 14:19
-
-
Save GrantSchiller/8688927 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class Browser { | |
private boolean browsing; | |
private WebVisit currentVisit; | |
private WebVisit noname; | |
/** | |
* Main method for the application. Create a new Browser object and set it to run. | |
*/ | |
public static void main(String[] args) { | |
Browser b = new Browser(); | |
b.run(); | |
} | |
/** | |
* Constructor for the Browser. Welcome the user and initialize the instance variables. | |
*/ | |
public Browser() { | |
browsing = true; | |
currentVisit = null; | |
System.out.println("Welcome to FCS Browser v0.01"); | |
} | |
/** | |
* The primary method for the web browser: while the user is browsing the web, continually prompt, read, and execute commands. | |
*/ | |
public void run() { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
while (browsing) { | |
try { | |
promptUser(); | |
String cmd = br.readLine(); | |
interpretAndExecute(cmd); | |
} catch (IOException e) { | |
System.out.println("Oops! There was an error reading your command."); | |
} | |
} | |
} | |
/** | |
* Prompt the user with a message to the command line. | |
*/ | |
private void promptUser() { | |
System.out.println("\nBrowser ready.\n(Commands: visit *web url*; history; back; quit; forward ;)"); | |
} | |
/** | |
* Given a string, determine which command (if any) is being issued and execute it | |
* hint: splitting at " " will be useful | |
* hint: notice that comparing Strings requires use of the .equals() method. Let's talk about it. | |
* @param cmd The complete string entered by the user | |
*/ | |
private void interpretAndExecute(String cmd) { | |
String[] commands = cmd.split(" "); | |
if (cmd.equals("back")) { | |
goBack(); | |
} else if (cmd.equals("history")) { | |
viewHistory(); | |
} else if (commands[0].equals("visit")) { | |
visitPage(commands[1]); | |
} else if (cmd.equals("quit")) { | |
quit(); | |
} else if (cmd.equals("forward")){ | |
goForward(); | |
}else { | |
System.out.println("Command not recognized"); | |
} | |
} | |
/** | |
* Create a new WebVisit object according to the URL passed in, move it to the top of the stack. | |
* @param url The URL of the new webpage the user would like to visit | |
*/ | |
private void visitPage(String url) { | |
System.out.println("Now going to visit "+url); | |
WebVisit wv = new WebVisit(url, currentVisit,nn); | |
nn = null; | |
currentVisit = wv; | |
System.out.println(currentVisit); | |
} | |
/** | |
* If there is a previous visit in the browser's history, pop the current one off the top of the stack | |
* and print out the browser's new page (if it exists). If there is no page to go back to, alert the user and do nothing. | |
*/ | |
private void goBack() { | |
if (currentVisit) { | |
System.out.println("Going back..."); | |
nextVisit= currentVisit; | |
currentVisit = currentVisit.getPreviousNode(); | |
if (currentVisit != null) { | |
System.out.println(currentVisit); | |
} | |
} else { | |
System.out.println("No web visits in browser history"); | |
} | |
} | |
private void goForward() { | |
if (currentVisit) { | |
System.out.print("Going forward..."); | |
currentVisit = currentVisit.getNextNode(); | |
nextVisit = currentVisit.getNextNode(); | |
if (currentVisit) { | |
System.out.println(currentVisit); | |
} else { | |
System.out.println("No next page to show."); | |
} | |
} | |
} | |
/** | |
* View a list of all WebVisits in the browser's history; list the total number of visits. | |
*/ | |
private void viewHistory() { | |
System.out.println("Showing browser history..."); | |
WebVisit tmp = currentVisit; | |
int counter = 0; | |
while (tmp != null) { | |
System.out.println("\t~"+tmp); | |
tmp = tmp.getPreviousNode(); | |
counter++; | |
} | |
System.out.println("\t\t"+counter+" total"); | |
} | |
/** | |
* Say goodbye; quit the program. | |
*/ | |
private void quit() { | |
System.out.println("Quitting now..."); | |
browsing = false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
public class WebVisit { | |
private String url; | |
private WebVisit previousNode; | |
private WebVisit nextNode; | |
private Date timestamp; | |
/** | |
* Constructor for a web visit. Initialize the three instance variables. | |
* @param u The URL of this visit | |
* @param pn The WebVisit previous to this one | |
*/ | |
public WebVisit(String u, WebVisit pn, WebVisit nn) { | |
url = u; | |
previousNode = pn; | |
nextNode = null; | |
timestamp = new Date(); | |
} | |
/** | |
* Return the previous WebVisit | |
* @return the previous WebVisit object | |
*/ | |
public WebVisit getPreviousNode() { | |
return previousNode; | |
} | |
public WebVisit getNextNode() { | |
return nextNode; | |
} | |
/** | |
* Return a neatly formatted String to represent this object so that it can be printed out for the user | |
*/ | |
public String toString() { | |
return "Web visit: "+url+" (at "+timestamp+")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
!!!!)))