diff --git a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/CopyToFile.java b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/CopyToFile.java index ba8018ffb5fa26d934706bf4c4299939ec384b4a..b417ffdbac2fe56d9b89af9cc435242280115a67 100755 --- a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/CopyToFile.java +++ b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/CopyToFile.java @@ -5,18 +5,21 @@ import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; +import java.net.URL; import java.util.Calendar; public class CopyToFile { static String filePath; + + /** + * Copy data from the URL to a file, + * only if the file doesn't exist already or + * if it's content is older than 10 minutes. + */ public static void CopyURLToFile(String pathname) { - /** - * Copy data from the URL to a file, - * only if the file doesn't exist already or - * if it's content is older than 10 minutes. - */ + if (new File(pathname).isFile() || (Calendar.getInstance().getTimeInMillis() - new File(pathname).lastModified() > 600000) && new File(pathname).lastModified() > 0) { @@ -35,10 +38,11 @@ public class CopyToFile { } + /** + *Parse the weather data from the file. + */ public static Weather CopyFileToWeather(String filePath) { - /** - *Parse the weather data from the file. - */ + Weather weatherObject = null; try { weatherObject = WeatherDataParser.parse(filePath); diff --git a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/SearchingAndComparing.java b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/SearchingAndComparing.java index 634cddf1d356cd55981d91cf693bc5e4d8e14212..890339c3c48b0d44256e325f1deef6b44d806805 100755 --- a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/SearchingAndComparing.java +++ b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/SearchingAndComparing.java @@ -50,7 +50,7 @@ public class SearchingAndComparing { if (citySelector[1] != null) { City[] multipleResults = Arrays.copyOf(citySelector, k); - for (int i = 0; i < multipleResults.length; i++) { + for (int i = 0; i < multipleResults.length ; i++) { System.out.println(i + 1 + ". " + multipleResults[i].getName() + " \n country: " + multipleResults[i].getCountry() + " | coordinates: " + FormatOutput.cleanCoordOutput(citySelector[i].getAdditionalProperties().toString())); } diff --git a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/URLCreator.java b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/URLCreator.java index cbc2385a6354a67647b7a955954eb340656c003a..3f097ea1a48498108f6819dade181657dd994ec6 100755 --- a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/URLCreator.java +++ b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/URLCreator.java @@ -8,8 +8,13 @@ public class URLCreator { * create the URL with the corresponding * city ID value created from SearchingAndComparing. */ - public static URL CreateUrl () throws MalformedURLException { - URL linkWithId = new URL("https://api.openweathermap.org/data/2.5/forecast?lang=de&APPID=41d009d32d4e302e0cd3d0f55bf5da24&units=metric&id="+ SearchingAndComparing.GetID()); + public static URL CreateUrl () { + URL linkWithId = null; + try { + linkWithId = new URL("https://api.openweathermap.org/data/2.5/forecast?lang=de&APPID=41d009d32d4e302e0cd3d0f55bf5da24&units=metric&id=" + SearchingAndComparing.GetID()); + } catch (MalformedURLException e) { + System.out.println("ERROR while creating URL"); + } return linkWithId; } } diff --git a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/WeatherOutput.java b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/WeatherOutput.java index 88a3b60ac4902fdd688e5831103b2364c812b6c1..8d14736282642ff053481de402a2bdd90b8b83bf 100755 --- a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/WeatherOutput.java +++ b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/WeatherOutput.java @@ -11,7 +11,6 @@ public class WeatherOutput { * of current day + 5 in 3 hour segments * @param i timestamps for weatherdata */ - for (int i = 0; i < weatherObject.getList().length - 1; i++) { if ((weatherObject.getList()[i].getDtTxt().substring(11, 13).equals("00")) || i == 0) { @@ -22,7 +21,7 @@ public class WeatherOutput { System.out.print(Math.round(weatherObject.getList()[i].getMain().getTemp()) + "°C,"); - System.out.print(WeatherOutput.SkyCondidtionOutput(weatherObject.getList()[i].getClouds().getAll())); + System.out.print(WeatherOutput.PrintSkyCondidtion(weatherObject.getList()[i].getClouds().getAll())); } @@ -34,12 +33,12 @@ public class WeatherOutput { * @param SkyPercent * @return skycondition as String */ - public static String SkyCondidtionOutput(int SkyPercent) { - if (SkyPercent < 25 && SkyPercent >= 0) { + public static String PrintSkyCondidtion(int SkyPercent) { + if (SkyPercent < 15 && SkyPercent >= 0) { return " Klarer Himmel" + "\n"; - } else if (SkyPercent > 25 && SkyPercent < 50) { + } else if (SkyPercent > 15 && SkyPercent < 40) { return " Ein paar Wolken" + "\n"; - } else if (SkyPercent > 50 && SkyPercent < 75) { + } else if (SkyPercent > 40 && SkyPercent < 75) { return " Wolkig" + "\n"; } else if (SkyPercent > 75 && SkyPercent <= 100) { return " Bedeckter Himmel" + "\n"; diff --git a/src/test/java/de/hdm_stuttgart/mi/sd1/weather/ErrorInput.java b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/ErrorInput.java deleted file mode 100755 index a08e514e6363e650d9be6f8d35def533cdb361ab..0000000000000000000000000000000000000000 --- a/src/test/java/de/hdm_stuttgart/mi/sd1/weather/ErrorInput.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - -package de.hdm_stuttgart.mi.sd1.weather; - -import org.junit.Assert; -import org.junit.Test; -public class ErrorInput { - @Test - int SkyPercent = 50; - int SkyPercent2 = 101; - public void TestErrorInput(){ - Assert.assertEquals(" Something wrong with the sky - just run" + "\n", ); - } - - - - - - - - - - - -} -*/ \ No newline at end of file diff --git a/src/test/java/de/hdm_stuttgart/mi/sd1/weather/URLCreatorTest.java b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/URLCreatorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..905dd11fc54b994a645360cc6bfc9b55837b9a02 --- /dev/null +++ b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/URLCreatorTest.java @@ -0,0 +1,27 @@ +package de.hdm_stuttgart.mi.sd1.weather; + +import de.hdm_stuttgart.mi.sd1.weather.model.SearchingAndComparing; +import de.hdm_stuttgart.mi.sd1.weather.model.URLCreator; +import org.junit.Assert; +import org.junit.Test; + +import java.net.MalformedURLException; +import java.net.URL; + +public class URLCreatorTest { + + @Test + public void CreateURLTest() throws MalformedURLException { + + URL testURL = new URL("https://www.google.de/"); + Assert.assertEquals(testURL.getClass(),URLCreator.CreateUrl().getClass()); + } + + + @Test + public void CreateURLTest2() throws MalformedURLException { + + URL testURL = new URL("https://api.openweathermap.org/data/2.5/forecast?lang=de&APPID=41d009d32d4e302e0cd3d0f55bf5da24&units=metric&id=" + SearchingAndComparing.GetID()); + Assert.assertEquals(testURL,URLCreator.CreateUrl()); + } +} diff --git a/src/test/java/de/hdm_stuttgart/mi/sd1/weather/WeatherOutputTest.java b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/WeatherOutputTest.java index ad5a9a6943a459b6bc18964a5a7880f83119383b..14bbcfd07eecf653a5738c6ded430c757a240151 100644 --- a/src/test/java/de/hdm_stuttgart/mi/sd1/weather/WeatherOutputTest.java +++ b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/WeatherOutputTest.java @@ -12,20 +12,20 @@ public class WeatherOutputTest { @Test public void TestSkyConditionOutput(){ String testValue = " Klarer Himmel" + "\n"; - Assert.assertEquals(WeatherOutput.SkyCondidtionOutput(10),testValue); + Assert.assertEquals(WeatherOutput.PrintSkyCondidtion(10),testValue); testValue = " Ein paar Wolken" + "\n"; - Assert.assertEquals(WeatherOutput.SkyCondidtionOutput(35),testValue); + Assert.assertEquals(WeatherOutput.PrintSkyCondidtion(35),testValue); testValue = " Wolkig" + "\n"; - Assert.assertEquals(WeatherOutput.SkyCondidtionOutput(70),testValue); + Assert.assertEquals(WeatherOutput.PrintSkyCondidtion(70),testValue); testValue = " Bedeckter Himmel" + "\n"; - Assert.assertEquals(WeatherOutput.SkyCondidtionOutput(99),testValue); + Assert.assertEquals(WeatherOutput.PrintSkyCondidtion(99),testValue); testValue = " Something wrong with the sky - just run" + "\n"; - Assert.assertEquals(WeatherOutput.SkyCondidtionOutput(-666),testValue); - Assert.assertEquals(WeatherOutput.SkyCondidtionOutput(666),testValue); + Assert.assertEquals(WeatherOutput.PrintSkyCondidtion(-666),testValue); + Assert.assertEquals(WeatherOutput.PrintSkyCondidtion(666),testValue); }