feat(display): enhance display
This commit is contained in:
parent
83b7ce780a
commit
bb8ceff5ea
@ -1,6 +1,7 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
class WeatherDisplayBasic implements WeatherDisplay {
|
class WeatherDisplayBasic implements WeatherDisplay {
|
||||||
private ArrayList<WeatherDataAPI> apis;
|
private ArrayList<WeatherDataAPI> apis;
|
||||||
@ -9,42 +10,102 @@ class WeatherDisplayBasic implements WeatherDisplay {
|
|||||||
this.apis = new ArrayList<WeatherDataAPI>();
|
this.apis = new ArrayList<WeatherDataAPI>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void displayHeader(int days) {
|
private void displayHeader(int days, double sourceColumnSize, double dayColumnSize) {
|
||||||
StringBuilder line = new StringBuilder();
|
StringBuilder line = new StringBuilder();
|
||||||
line.append("Source\t");
|
line.append("Source\t");
|
||||||
|
for (double index = line.length(); index < sourceColumnSize; index++)
|
||||||
|
line.append(" ");
|
||||||
|
|
||||||
for (int i = 0 ; i < days ; ++i) {
|
for (int i = 0 ; i < days ; ++i) {
|
||||||
line.append("\tJ + ")
|
line.append("\tJ + ")
|
||||||
.append(i);
|
.append(i);
|
||||||
|
for (double index = "\tJ + ".length(); index < dayColumnSize; index++)
|
||||||
|
line.append(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
System.err.println(line);
|
System.out.println(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void displayWeatherDatas(String ApiName, ArrayList<WeatherData> weatherDatas) {
|
private double getColumnSize(HashMap<WeatherDataAPI, ArrayList<WeatherData>> weatherDataAPIArrayListHashMap) {
|
||||||
|
double max = 0;
|
||||||
|
for (WeatherDataAPI api: weatherDataAPIArrayListHashMap.keySet()) {
|
||||||
|
for (WeatherData w: weatherDataAPIArrayListHashMap.get(api)){
|
||||||
|
max = Math.max(max, w.toString().length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void displayWeatherDatas(String apiName, ArrayList<WeatherData> weatherDatas, String startColumnString, double sourceColumnSize, double dayColumnSize) {
|
||||||
StringBuilder line = new StringBuilder();
|
StringBuilder line = new StringBuilder();
|
||||||
line.append(ApiName);
|
String weatherDataString;
|
||||||
|
line.append(apiName);
|
||||||
|
for (double index = apiName.length(); index < sourceColumnSize; index++)
|
||||||
|
line.append(" ");
|
||||||
|
|
||||||
for (WeatherData weatherData: weatherDatas) {
|
for (WeatherData weatherData: weatherDatas) {
|
||||||
line.append('\t')
|
weatherDataString = weatherData.toString();
|
||||||
.append(weatherData.toString());
|
line.append(startColumnString)
|
||||||
|
.append(weatherDataString);
|
||||||
|
// fill the columns with blanks
|
||||||
|
for (double index = weatherDataString.length(); index < dayColumnSize; index++)
|
||||||
|
line.append(" ");
|
||||||
|
}
|
||||||
|
//complete column
|
||||||
|
line.append(startColumnString);
|
||||||
|
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void displaySeparatorLine(int days, double sourceColumnSize, double dayColumnSize) {
|
||||||
|
String mainSeparator = "-";
|
||||||
|
String secondSeparator = "+";
|
||||||
|
StringBuilder line = new StringBuilder();
|
||||||
|
// source Column
|
||||||
|
for (double index = 0; index < sourceColumnSize; index++)
|
||||||
|
line.append(mainSeparator);
|
||||||
|
line.append(secondSeparator);
|
||||||
|
for (int day = 0; day < days; day++){
|
||||||
|
for (double index = 0; index < dayColumnSize; index++)
|
||||||
|
line.append(mainSeparator);
|
||||||
|
line.append(secondSeparator);
|
||||||
|
}
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void displayAllWeatherDatas(HashMap<WeatherDataAPI, ArrayList<WeatherData>> weatherDataAPIArrayListHashMap, int days) {
|
||||||
|
double dayColumnSize = this.getColumnSize(weatherDataAPIArrayListHashMap);
|
||||||
|
|
||||||
|
// Adjust column size
|
||||||
|
String startColumnString = "| ";
|
||||||
|
dayColumnSize += startColumnString.length();
|
||||||
|
|
||||||
|
double sourceColumnSize = dayColumnSize;
|
||||||
|
|
||||||
|
// Header
|
||||||
|
displayHeader(days, sourceColumnSize, dayColumnSize);
|
||||||
|
this.displaySeparatorLine(days, sourceColumnSize, dayColumnSize);
|
||||||
|
|
||||||
|
// Apis
|
||||||
|
for (WeatherDataAPI api: weatherDataAPIArrayListHashMap.keySet()) {
|
||||||
|
displayWeatherDatas(api.getAPIName(), weatherDataAPIArrayListHashMap.get(api), startColumnString, sourceColumnSize, dayColumnSize);
|
||||||
|
this.displaySeparatorLine(days, sourceColumnSize, dayColumnSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.err.println(line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void display(int days, String city) {
|
public void display(int days, String city) {
|
||||||
ArrayList<WeatherData> weatherDatas;
|
HashMap<WeatherDataAPI, ArrayList<WeatherData>> weatherDatasMap = new HashMap<>();
|
||||||
displayHeader(days);
|
|
||||||
|
|
||||||
for (WeatherDataAPI w: apis) {
|
for (WeatherDataAPI w: apis) {
|
||||||
try {
|
try {
|
||||||
weatherDatas = w.getTemperatures(days, city);
|
weatherDatasMap.put(w, w.getTemperatures(days, city));
|
||||||
displayWeatherDatas(w.getAPIName(), weatherDatas);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println(e);
|
System.err.println(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.displayAllWeatherDatas(weatherDatasMap, days);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addAPI(WeatherDataAPI w) {
|
public void addAPI(WeatherDataAPI w) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user