Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. package Project1;
  2.  
  3. public class Date212 implements Comparable {
  4.  
  5. private int year;
  6. private int month;
  7. private int day;
  8.  
  9. // Constructor
  10. public Date212(String a) {
  11. year = Integer.parseInt(a.substring(0, 4));
  12. month = Integer.parseInt(a.substring(4, 6));
  13. day = Integer.parseInt(a.substring(6, 8));
  14. }
  15.  
  16. // Whole slew of setters and getters
  17. public int getYear() {
  18. return year;
  19. }
  20. public void setYear(int year) {
  21. this.year = year;
  22. }
  23. public int getMonth() {
  24. return month;
  25. }
  26. public void setMonth(int month) {
  27. this.month = month;
  28. }
  29. public int getDay() {
  30. return day;
  31. }
  32. public void setDay(int day) {
  33. this.day = day;
  34. }
  35. public void setDate(int y, int m, int d) {
  36. year = y;
  37. month = m;
  38. day = d;
  39. }
  40.  
  41. // Returning fully worded month from number.
  42. private String numToMonth(int a) {
  43. switch (a) {
  44. case 1:
  45. return "January";
  46. case 2:
  47. return "February";
  48. case 3:
  49. return "March";
  50. case 4:
  51. return "April";
  52. case 5:
  53. return "May";
  54. case 6:
  55. return "June";
  56. case 7:
  57. return "July";
  58. case 8:
  59. return "August";
  60. case 9:
  61. return "September";
  62. case 10:
  63. return "October";
  64. case 11:
  65. return "November";
  66. case 12:
  67. return "December";
  68. default:
  69. throw new IllegalArgumentException("There are only twelve months.");
  70. }
  71. }
  72.  
  73. // Overriding the Object class' equals function
  74. @Override
  75. public boolean equals(Object comparison) {
  76. Date212 date = (Date212) comparison;
  77. if (date.getYear() == getYear() && date.getMonth() == getMonth() && date.getDay() == getDay())
  78. return true;
  79. else
  80. return false;
  81. }
  82.  
  83. // Overriding the Object class' compareTo function
  84. @Override
  85. public int compareTo(Object comparison) {
  86. Date212 date = (Date212) comparison;
  87. if (date.year > year)
  88. return 1;
  89. else if (date.year < year)
  90. return -1;
  91. if (date.month > month)
  92. return 1;
  93. else if (date.month < month)
  94. return -1;
  95. if (date.day > day)
  96. return 1;
  97. else if (date.day < day)
  98. return -1;
  99. else
  100. return 0;
  101. }
  102.  
  103. // Used to get date in yyyymmdd format
  104. public String toRawDate() {
  105. return Integer.toString(year) + month + day;
  106. }
  107.  
  108. // Overriding the Object class' toString function
  109. @Override
  110. public String toString() {
  111. return numToMonth(getMonth()) + " " + getDay() + ", " + getYear();
  112. }
  113. }
  114.  
  115. /*
  116. * Create a class called Date212 to represent a date. It will store the year,
  117. * month and day as integers so you will need three private instance variables.
  118. * One constructor should be provided that takes a String representing the date
  119. * in yyyy mm dd format. The constructor should use the substring method of
  120. * class String to pull out the month, day and year, parse them as integers put
  121. * them in the instance variables. The Date212 class should have the usual set
  122. * and get methods, and equals, compareTo and toString methods. The toString
  123. * method should print the date in “mm dd, yyyy” format (for example, 20171002
  124. * would be returned as “October 2, 2017”.
  125. */
  126.  
  127. package Project1;
  128.  
  129. import java.awt.GridLayout;
  130.  
  131. import javax.swing.JLabel;
  132. import javax.swing.JPanel;
  133.  
  134. public class DateGUI extends JPanel {
  135.  
  136. // Constructor
  137. public DateGUI() {
  138. setLayout(new GridLayout(0, 2));
  139. }
  140.  
  141. // Adding the left and right parts of the GUI
  142. public void displayDate(Date212 date) {
  143. add(new JLabel(date.toRawDate()));
  144. add(new JLabel(date.toString()));
  145. }
  146.  
  147. // Basic Selection sort loop
  148. public void selectionSort(Date212[] dates) {
  149. for (int i = 0; i < dates.length - 1; i++) {
  150. int lowestNum = i;
  151. for (int j = i + 1; j < dates.length; j++)
  152. if (dates[j].compareTo(dates[lowestNum]) < 0)
  153. lowestNum = j;
  154. if (dates[lowestNum] != dates[i]) {
  155. Date212 temp = dates[lowestNum];
  156. dates[lowestNum] = dates[i];
  157. dates[i] = temp;
  158. }
  159. }
  160. }
  161.  
  162. // Adding list of sorted dates
  163. public void displayDates(Date212[] dates) {
  164. selectionSort(dates);
  165. for (int i = 0; i < dates.length; i++) {
  166. displayDate(dates[i]);
  167. }
  168. }
  169.  
  170. /*
  171. * Given an input file of dates represented as Strings, read the dates from the
  172. * file and display them in a GUI. The dates will be in the form yyyymmdd (such
  173. * as 20171001 for October 1, 2017). The GUI should have a GridLayout with one
  174. * row and two columns. The left column should display the dates in the format
  175. * read from the file, and the right column should display the dates as Date212
  176. * object in sorted order (using Selection Sort).
  177. */
  178.  
  179. }
  180.  
  181. package Project1;
  182.  
  183. import java.io.BufferedReader;
  184. import java.io.FileReader;
  185. import java.io.IOException;
  186.  
  187. import javax.swing.JFrame;
  188. import javax.swing.JOptionPane;
  189.  
  190. public class Project1 {
  191.  
  192. private static DateGUI dateGUI;
  193.  
  194. public static void main(String[] args) throws IOException {
  195.  
  196. // Create JFrame. Add a DateGUI to said frame.
  197. JFrame main = new JFrame("Date Display");
  198. main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  199. dateGUI = new DateGUI();
  200. main.add(dateGUI);
  201. String fileName = JOptionPane.showInputDialog("Please insert file path for date file: ");
  202. dateGUI.displayDates(readFile(fileName));
  203. main.pack();
  204. main.setVisible(true);
  205.  
  206. // Read file and make array of Date212's. Feed to DateGUI. Cry. ??? Profit.
  207. // Need scanners and stuff. Don't know file name. Doesn't matter???
  208.  
  209. }
  210.  
  211. public static Date212[] readFile(String fileName) throws IOException {
  212. FileReader rawDates = new FileReader(fileName);
  213. BufferedReader textReader = new BufferedReader(rawDates);
  214. String line, bigString = null;
  215. while ((line = textReader.readLine()) != null) {
  216. if (bigString == null) {
  217. bigString = line;
  218. } else {
  219. bigString = bigString + "," + line;
  220. }
  221. }
  222. String[] tempStringHolder = bigString.split(",");
  223. Date212[] dates = new Date212[tempStringHolder.length];
  224. for (int i = 0; i < tempStringHolder.length; i++) {
  225. dates[i] = new Date212(tempStringHolder[i]);
  226. }
  227. textReader.close();
  228. return dates;
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement