Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Project1;
- public class Date212 implements Comparable {
- private int year;
- private int month;
- private int day;
- // Constructor
- public Date212(String a) {
- year = Integer.parseInt(a.substring(0, 4));
- month = Integer.parseInt(a.substring(4, 6));
- day = Integer.parseInt(a.substring(6, 8));
- }
- // Whole slew of setters and getters
- public int getYear() {
- return year;
- }
- public void setYear(int year) {
- this.year = year;
- }
- public int getMonth() {
- return month;
- }
- public void setMonth(int month) {
- this.month = month;
- }
- public int getDay() {
- return day;
- }
- public void setDay(int day) {
- this.day = day;
- }
- public void setDate(int y, int m, int d) {
- year = y;
- month = m;
- day = d;
- }
- // Returning fully worded month from number.
- private String numToMonth(int a) {
- switch (a) {
- case 1:
- return "January";
- case 2:
- return "February";
- case 3:
- return "March";
- case 4:
- return "April";
- case 5:
- return "May";
- case 6:
- return "June";
- case 7:
- return "July";
- case 8:
- return "August";
- case 9:
- return "September";
- case 10:
- return "October";
- case 11:
- return "November";
- case 12:
- return "December";
- default:
- throw new IllegalArgumentException("There are only twelve months.");
- }
- }
- // Overriding the Object class' equals function
- @Override
- public boolean equals(Object comparison) {
- Date212 date = (Date212) comparison;
- if (date.getYear() == getYear() && date.getMonth() == getMonth() && date.getDay() == getDay())
- return true;
- else
- return false;
- }
- // Overriding the Object class' compareTo function
- @Override
- public int compareTo(Object comparison) {
- Date212 date = (Date212) comparison;
- if (date.year > year)
- return 1;
- else if (date.year < year)
- return -1;
- if (date.month > month)
- return 1;
- else if (date.month < month)
- return -1;
- if (date.day > day)
- return 1;
- else if (date.day < day)
- return -1;
- else
- return 0;
- }
- // Used to get date in yyyymmdd format
- public String toRawDate() {
- return Integer.toString(year) + month + day;
- }
- // Overriding the Object class' toString function
- @Override
- public String toString() {
- return numToMonth(getMonth()) + " " + getDay() + ", " + getYear();
- }
- }
- /*
- * Create a class called Date212 to represent a date. It will store the year,
- * month and day as integers so you will need three private instance variables.
- * One constructor should be provided that takes a String representing the date
- * in yyyy mm dd format. The constructor should use the substring method of
- * class String to pull out the month, day and year, parse them as integers put
- * them in the instance variables. The Date212 class should have the usual set
- * and get methods, and equals, compareTo and toString methods. The toString
- * method should print the date in “mm dd, yyyy” format (for example, 20171002
- * would be returned as “October 2, 2017”.
- */
- package Project1;
- import java.awt.GridLayout;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- public class DateGUI extends JPanel {
- // Constructor
- public DateGUI() {
- setLayout(new GridLayout(0, 2));
- }
- // Adding the left and right parts of the GUI
- public void displayDate(Date212 date) {
- add(new JLabel(date.toRawDate()));
- add(new JLabel(date.toString()));
- }
- // Basic Selection sort loop
- public void selectionSort(Date212[] dates) {
- for (int i = 0; i < dates.length - 1; i++) {
- int lowestNum = i;
- for (int j = i + 1; j < dates.length; j++)
- if (dates[j].compareTo(dates[lowestNum]) < 0)
- lowestNum = j;
- if (dates[lowestNum] != dates[i]) {
- Date212 temp = dates[lowestNum];
- dates[lowestNum] = dates[i];
- dates[i] = temp;
- }
- }
- }
- // Adding list of sorted dates
- public void displayDates(Date212[] dates) {
- selectionSort(dates);
- for (int i = 0; i < dates.length; i++) {
- displayDate(dates[i]);
- }
- }
- /*
- * Given an input file of dates represented as Strings, read the dates from the
- * file and display them in a GUI. The dates will be in the form yyyymmdd (such
- * as 20171001 for October 1, 2017). The GUI should have a GridLayout with one
- * row and two columns. The left column should display the dates in the format
- * read from the file, and the right column should display the dates as Date212
- * object in sorted order (using Selection Sort).
- */
- }
- package Project1;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- public class Project1 {
- private static DateGUI dateGUI;
- public static void main(String[] args) throws IOException {
- // Create JFrame. Add a DateGUI to said frame.
- JFrame main = new JFrame("Date Display");
- main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- dateGUI = new DateGUI();
- main.add(dateGUI);
- String fileName = JOptionPane.showInputDialog("Please insert file path for date file: ");
- dateGUI.displayDates(readFile(fileName));
- main.pack();
- main.setVisible(true);
- // Read file and make array of Date212's. Feed to DateGUI. Cry. ??? Profit.
- // Need scanners and stuff. Don't know file name. Doesn't matter???
- }
- public static Date212[] readFile(String fileName) throws IOException {
- FileReader rawDates = new FileReader(fileName);
- BufferedReader textReader = new BufferedReader(rawDates);
- String line, bigString = null;
- while ((line = textReader.readLine()) != null) {
- if (bigString == null) {
- bigString = line;
- } else {
- bigString = bigString + "," + line;
- }
- }
- String[] tempStringHolder = bigString.split(",");
- Date212[] dates = new Date212[tempStringHolder.length];
- for (int i = 0; i < tempStringHolder.length; i++) {
- dates[i] = new Date212(tempStringHolder[i]);
- }
- textReader.close();
- return dates;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement