|
| 1 | +package com.eprogrammerz.examples.ds.browser; |
| 2 | + |
| 3 | +/** |
| 4 | + * Design a browser |
| 5 | + * 1. Enter address on address bar |
| 6 | + * 2. Click on link to go to new page |
| 7 | + * 2. User should be able to click on back button to get to the earlier back page |
| 8 | + * 3. User should be able to click on forward to get the earlier forward page |
| 9 | + * 3. If user enters new address on address bar, loses forward pages but retains backward pages |
| 10 | + */ |
| 11 | +public class Browser { |
| 12 | + private class BrowserState { |
| 13 | + String address; |
| 14 | + BrowserState back; |
| 15 | + BrowserState forward; |
| 16 | + |
| 17 | + BrowserState(String address) { |
| 18 | + this.address = address; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + private BrowserState browserState; |
| 23 | + |
| 24 | + /** |
| 25 | + * Browser starts with no particular address |
| 26 | + */ |
| 27 | + public Browser() { |
| 28 | + this.browserState = new BrowserState(""); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * When new address is entered in url input, then it adds new node into list |
| 33 | + * @param address |
| 34 | + */ |
| 35 | + void newPage(String address) { |
| 36 | + BrowserState current = browserState; |
| 37 | + if (current.forward != null) { |
| 38 | + current.forward.back = null; |
| 39 | + } |
| 40 | + |
| 41 | + BrowserState newState = new BrowserState(address); |
| 42 | + newState.forward = null; |
| 43 | + newState.back = current; |
| 44 | + |
| 45 | + current.forward = newState; |
| 46 | + |
| 47 | + browserState = newState; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * If link within current page is linked, history will be updated |
| 52 | + * @param newAddress |
| 53 | + * @return |
| 54 | + */ |
| 55 | + String click(String newAddress) { |
| 56 | + BrowserState newState = new BrowserState(newAddress); |
| 57 | + newState.forward = null; |
| 58 | + newState.back = browserState; |
| 59 | + |
| 60 | + browserState.forward = newState; |
| 61 | + browserState = newState; |
| 62 | + return browserState.address; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Clicking on back doesn't takes to the previous page in the history if exists |
| 67 | + * |
| 68 | + * @return |
| 69 | + */ |
| 70 | + String back() { |
| 71 | + BrowserState currentState = browserState; |
| 72 | + if (currentState.back != null) { |
| 73 | + currentState = currentState.back; |
| 74 | + browserState = currentState; |
| 75 | + } |
| 76 | + return browserState.address; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Clicking on forward takes to the forward page in history if exists |
| 81 | + * @return |
| 82 | + */ |
| 83 | + String forward() { |
| 84 | + if (browserState.forward != null) { |
| 85 | + browserState = browserState.forward; |
| 86 | + return browserState.address; |
| 87 | + } |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Gives current address |
| 93 | + * @return |
| 94 | + */ |
| 95 | + String getCurrentAddress() { |
| 96 | + return this.browserState.address; |
| 97 | + } |
| 98 | +} |
0 commit comments