Guest User

Untitled

a guest
Nov 28th, 2018
456
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. package controllers;
  4.  
  5.  
  6. import helpers.InputHelper;
  7. import java.util.List;
  8. import model.City;
  9. import model.YearData;
  10. import repositories.Repository;
  11. import daos.DAOTextImpl;
  12. import java.text.SimpleDateFormat;
  13. import java.util.ArrayList;
  14. import java.util.Calendar;
  15. import java.util.HashSet;
  16. import java.util.LinkedList;
  17. import java.util.Set;
  18. import java.util.TreeSet;
  19. import model.YearData;
  20.  
  21.  
  22.  
  23.  
  24. public class WeatherDataController {
  25. private final Repository repository;
  26. List<City> cities;
  27. City city=new City();
  28.  
  29.  
  30. public WeatherDataController() {
  31. InputHelper inputHelper = new InputHelper();
  32. cities = new LinkedList<>();
  33.  
  34.  
  35. char c = inputHelper.readCharacter("Load an existing File (Y/N)?");
  36. if (c == 'Y' || c == 'y') {
  37.  
  38. String fileName = inputHelper.readString("Enter filename")+".txt";
  39. this.repository = new Repository(fileName);
  40. }
  41. else {
  42. this.repository = new Repository();
  43. }
  44.  
  45. }
  46.  
  47. public void run() {
  48. boolean finished = false;
  49.  
  50. do {
  51. // System.out.println(repository); //if no filename is entered, then repository will return []
  52. char choice = displayMenu();
  53. switch (choice) {
  54. case 'A':
  55. addCity();
  56. break;
  57.  
  58.  
  59.  
  60. case 'B':
  61. addYearData();
  62. break;
  63.  
  64. case 'C':
  65. listCountryCityWeatherDataInCityNameOrder();
  66.  
  67. break;
  68. case 'D':
  69. listCityAverages();
  70. break;
  71.  
  72. case 'E':
  73. listCityWeatherDataInCityIdOrder();
  74. break;
  75.  
  76. case 'Q':
  77. InputHelper inputHelper = new InputHelper();
  78. String fileName = inputHelper.readString("Enter filename");
  79. fileName+=".txt";
  80.  
  81. repository.store(fileName); //used to store/save the file
  82. finished = true;
  83.  
  84. }
  85. } while (!finished);
  86. }
  87.  
  88. private char displayMenu() {
  89.  
  90. InputHelper inputHelper = new InputHelper();
  91. System.out.print("\nA. Add City");
  92. System.out.print("\tB. Add new year data");
  93. System.out.print("\tC. List Country Data In City Name Order");
  94. System.out.print("\tD. List City Average Data");
  95. System.out.print("\tE. List City Weather Data In City ID Order");
  96. System.out.print("\tQ. Quit\n");
  97. return inputHelper.readCharacter("Enter choice", "ABCDEQ");
  98. }
  99.  
  100. private void addCity() {
  101. InputHelper inputHelper = new InputHelper();
  102. boolean validCityId = false;
  103. City requiredCity=null;
  104. char c=inputHelper.readCharacter("Do you wish to add a new city? y to continue, t to terminate:");
  105.  
  106. if (c=='y'||c=='Y')
  107. {
  108. int newCityId=inputHelper.readInt("Enter New City Id:");
  109. String newCityName=inputHelper.readString("Enter new city name:");
  110. String newCityCountry=inputHelper.readString("enter country in which the city is based:");
  111. City newCity= new City(newCityId, newCityName, newCityCountry);
  112. cities.add(new City(newCityId, newCityName, newCityCountry));
  113. repository.add(newCity);
  114. }
  115.  
  116. else if (c=='T'||c=='t')
  117. {
  118. System.exit(0);
  119. }
  120.  
  121.  
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. private void addYearData() {
  130. System.out.format("\033[31m%s\033[0m%n", "Add Year Data");
  131. System.out.format("\033[31m%s\033[0m%n", "=============");
  132. InputHelper inputHelper = new InputHelper();
  133.  
  134. char c=inputHelper.readCharacter("Do you wish to add new year data for a city? y to continue, t to terminate:");
  135. if (c=='y'||c=='Y')
  136. {
  137.  
  138. String year=inputHelper.readString("Enter year:");
  139. float precip=inputHelper.readFloat("enter precipitation value:");
  140. int max=inputHelper.readInt("enter max temp");
  141. int min=inputHelper.readInt("enter min temp");
  142. int speed=inputHelper.readInt("enter wind speed");
  143. String direction=inputHelper.readString("enter wind direction");
  144.  
  145.  
  146.  
  147. City newCity= new City();
  148. YearData yd= new YearData(year, precip, max, min, speed, direction);
  149.  
  150. char cz = inputHelper.readCharacter("Do you wish to add to an existing city?");
  151.  
  152.  
  153. int id =inputHelper.readInt("Please enter the ID of the city you wish to add the yeardata to");
  154. city.getId();
  155. city.getyearDataCollection();
  156. city.addYearData(yd);
  157.  
  158.  
  159.  
  160. repository.add(city);
  161.  
  162. }
  163. else if (c=='T'||c=='t')
  164. {
  165. System.exit(0);
  166. }
  167.  
  168. }
  169.  
  170.  
  171.  
  172. private void listCountryCityWeatherDataInCityNameOrder() {
  173. InputHelper inputHelper = new InputHelper();
  174. System.out.format("\033[31m%s\033[0m%n", "City Name Order");
  175. System.out.format("\033[31m%s\033[0m%n", "===============");
  176. System.out.println("Sort By Name");
  177. System.out.println("------------");
  178. Set<City> newSet = new TreeSet(City.CityNameComparator);
  179. newSet.addAll(cities);
  180.  
  181. System.out.println(newSet);
  182.  
  183. }
  184.  
  185. private void listCityAverages() {
  186. InputHelper inputHelper = new InputHelper();
  187. System.out.format("\033[31m%s\033[0m%n", "City Average Data");
  188. System.out.format("\033[31m%s\033[0m%n", "=================");
  189. Float cityAverages=inputHelper.readFloat("Please enter city average here:");
  190. YearData yeardata=new YearData();
  191. int a= yeardata.getMaxTemp();
  192. int b=yeardata.getMinTemp();
  193.  
  194. }
  195.  
  196. private void listCityWeatherDataInCityIdOrder() {
  197. InputHelper inputHelper = new InputHelper();
  198. System.out.format("\033[31m%s\033[0m%n", "City Id Order");
  199. System.out.format("\033[31m%s\033[0m%n", "=============");
  200. char cityID=inputHelper.readCharacter("Please enter city ID value here:");
  201.  
  202.  
  203.  
  204. }
  205.  
  206.  
  207. }
RAW Paste Data