Skip to content

Commit

Permalink
Use return early pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
tttzach committed Jun 18, 2020
2 parents 0207f37 + 0f15f74 commit cef2430
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 71 deletions.
11 changes: 8 additions & 3 deletions portfolio/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@
<version>1.9.59</version>
</dependency>
<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>1.2</version>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.5</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

package com.google.sps.servlets;

import com.opencsv.CSVReaderHeaderAware;
import com.google.gson.Gson;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -27,25 +29,12 @@
@WebServlet("/coronavirus-data")
public class CoronavirusDataServlet extends HttpServlet {

private final LinkedHashMap<String, Integer> coronavirusCases = new LinkedHashMap<>();
private final Map<String, Integer> coronavirusCases = new HashMap<>();

@Override
public void init() {
Scanner scanner = new Scanner(getServletContext().getResourceAsStream(
"/WEB-INF/coronavirus-stats-by-country.csv"));
// Ignore header row = "Country/Region,Confirmed,Active,..."
if (scanner.hasNextLine()) {
scanner.nextLine();
}
while (scanner.hasNextLine()) {
// line = "Afghanistan,20917,369.0,..."
String line = scanner.nextLine();
String[] cells = line.split(",");
String country = String.valueOf(cells[0]);
Integer confirmedCases = Integer.valueOf(cells[1]);
coronavirusCases.put(country, confirmedCases);
}
scanner.close();
String filePath = "../../src/main/webapp/WEB-INF/coronavirus-stats-by-country.csv";
readCsv(filePath);
}

@Override
Expand All @@ -55,5 +44,20 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
String json = gson.toJson(coronavirusCases);
response.getWriter().println(json);
}


private void readCsv(String filePath) {
try {
CSVReaderHeaderAware csvReader = new CSVReaderHeaderAware(new FileReader(filePath));
Map<String, String> line;
while ((line = csvReader.readMap()) != null) {
String country = line.get("Country/Region");
Integer confirmedCases = Integer.valueOf(line.get("Confirmed"));
coronavirusCases.put(country, confirmedCases);
}
csvReader.close();
} catch (IOException exception) {
System.out.println("File not found.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ private void deleteAll(DatastoreService datastore, PreparedQuery results) {
datastore.delete(entityKey);
}
}

}
7 changes: 3 additions & 4 deletions portfolio/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ <h2>About Me</h2>
<p>Hello! My name is Zachary, I am a rising junior at the University of Waterloo, Canada pursuing a major
in Combinatorics & Optimization and a minor in Computer Science.<br/>Feel free to click on the other
tabs for more not-so-exciting information about me.
<br/><br/>
<div id="map"></div>
<br/><br/>
<div id="pie-chart"></div>
<div id="pie-chart" class="chart"></div>
<br/><br/>
<div id="regions-chart"></div>
<div id="regions-chart" class="chart"></div>
<br/><br/>
<div id="coronavirus-chart"></div>
<div id="coronavirus-chart" class="chart"></div>
<br/><br/>
<p><img class="about-me-img" src="images/meme.jpg" alt="Picture of Meme">
</div>
Expand Down
12 changes: 1 addition & 11 deletions portfolio/src/main/webapp/libraries.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#coronavirus-chart {
.chart {
border: thin solid black;
display: inline-block;
}
Expand All @@ -10,16 +10,6 @@
display: inline-block;
}

#pie-chart {
border: thin solid black;
display: inline-block;
}

#regions-chart {
border: thin solid black;
display: inline-block;
}

#comments-form {
display: none;
}
73 changes: 40 additions & 33 deletions portfolio/src/main/webapp/libraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawPieChart);
google.charts.setOnLoadCallback(drawRegionsChart);
google.charts.setOnLoadCallback(drawCoronavirusChart);

// Coordinates of the University of Waterloo from manually checking Google Maps
const UWATERLOO = { lat: 43.473, lng: -80.545 };
// Show all the main buildings of the University of Waterloo
const ZOOM = 16;
// Match height and width to other visual elements on the About Me tab
const DEFAULT_WIDTH = 700;
const DEFAULT_HEIGHT = 500;

function createMap() {
const uWaterloo = { lat: 43.473, lng: -80.545 };
const map = mapInit(uWaterloo);
const marker = createMapMarker(map, uWaterloo);
const map = mapInit(UWATERLOO);
const marker = createMapMarker(map, UWATERLOO);
changeMapToTerrain(map);
createMapInfoWindow(map, marker);
}
Expand All @@ -24,9 +36,8 @@ function mapInit(position) {
const map = new google.maps.Map(
document.getElementById('map'), {
center: position,
zoom: 16
}
);
zoom: ZOOM
});
return map;
}

Expand All @@ -43,21 +54,21 @@ function changeMapToTerrain(map) {
}

function createMapInfoWindow(map, marker) {
const contentString = '<div id="infowindow">' +
'<div id="siteNotice">' +
'</div>' +
'<h1 id="firstHeading" class="firstHeading">University of Waterloo</h1>' +
'<div id="bodyContent">' +
'<p>The <b>University of Waterloo</b> (commonly referred to as <b>Waterloo</b>, <b>UW</b>, or ' +
'<b>UWaterloo</b>) is a public research university with a main campus in Waterloo, Ontario, ' +
'Canada. The main campus is on 404 hectares of land adjacent to Uptown Waterloo and Waterloo ' +
'Park. The university also operates three satellite campuses and four affiliated university ' +
'colleges.</p>' +
'<p>Attribution: University of Waterloo, <a href="https://en.wikipedia.org/wiki/University_of_Waterloo">' +
'https://en.wikipedia.org/wiki/University_of_Waterloo</a> ' +
'(last visited June 8, 2020).</p>' +
'</div>' +
'</div>';
const contentString = `<div id="infowindow">
<div id="siteNotice"></div>
<h1 id="firstHeading" class="firstHeading">University of Waterloo</h1>
<div id="bodyContent">
<p>The <b>University of Waterloo</b> (commonly referred to as <b>Waterloo</b>, <b>UW</b>, or
<b>UWaterloo</b>) is a public research university with a main campus in Waterloo, Ontario,
Canada. The main campus is on 404 hectares of land adjacent to Uptown Waterloo and Waterloo
Park. The university also operates three satellite campuses and four affiliated university
colleges.
<p>Attribution: University of Waterloo,
<a href="https://en.wikipedia.org/wiki/University_of_Waterloo">
https://en.wikipedia.org/wiki/University_of_Waterloo</a>
(last visited June 8, 2020).
</div>
</div>`;
const infoWindow = new google.maps.InfoWindow({
content: contentString
});
Expand All @@ -66,11 +77,6 @@ function createMapInfoWindow(map, marker) {
});
}

google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawPieChart);
google.charts.setOnLoadCallback(drawRegionsChart);
google.charts.setOnLoadCallback(drawCoronavirusChart);

function drawPieChart() {
const data = new google.visualization.DataTable();
data.addColumn('string', 'Animal');
Expand All @@ -83,8 +89,8 @@ function drawPieChart() {

const options = {
'title': 'Zoo Animals',
'width': 700,
'height': 500
'width': DEFAULT_WIDTH,
'height': DEFAULT_HEIGHT
};

const chart = new google.visualization.PieChart(document.getElementById('pie-chart'));
Expand All @@ -104,16 +110,17 @@ function drawRegionsChart() {

const options = {
'title': 'Sample Regions Chart',
'width': 700,
'height': 500
'width': DEFAULT_WIDTH,
'height': DEFAULT_HEIGHT
};

const chart = new google.visualization.GeoChart(document.getElementById('regions-chart'));
chart.draw(data, options);
}

function drawCoronavirusChart() {
fetch('/coronavirus-data').then(response => response.json())
fetch('/coronavirus-data')
.then(response => response.json())
.then((coronavirusCases) => {
const data = new google.visualization.DataTable();
data.addColumn('string', 'Country');
Expand All @@ -124,8 +131,8 @@ function drawCoronavirusChart() {

const options = {
'title': 'Coronavirus Cases',
'width': 700,
'height': 500,
'width': DEFAULT_WIDTH,
'height': DEFAULT_HEIGHT,
'colorAxis': { colors: ['lightcoral', 'darkred'] },
'backgroundColor': 'lightblue',
'datalessRegionColor': 'white',
Expand Down

0 comments on commit cef2430

Please sign in to comment.