-3

I'm trying to get the date (dateTime) from an array list that is inputted by the user and then sort it by when the soonest date is, I've heard about the SimpleDateFormat utility/text from java but i don't know how to utilize it. But im willing to use any methods that you guys can help me with, and an explanation would be great!

package ticketSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class Ticket extends User {

    Scanner sc = new Scanner(System.in);
    static ArrayList<Ticket> ticketList = new ArrayList<Ticket>();
    String pri = "";
    String des = "";
    int rm = 0;
    private String dateTime;

    public Ticket(String userName) {
        super(userName);

    }

    public String getDateTime() {
        return dateTime;
    }

    public void menu2() {
        typesOfTicks();
    }

    public void typesOfTicks() {
        System.out.println("\nWhat type of ticket would you like to create?");
        System.out.println("1: Room Ticket. ");
        System.out.println("2: Common Area. ");
    }

    public void addTicket(Ticket ticket) {
        ticketList.add(ticket);
    }

    public void createRoomTicket() {
        System.out.println("You have created a new room ticket!");
        System.out.println("Date for maintence: ");
        dateTime = sc.nextLine();
        System.out.println("What is the room number: ");
        rm = Integer.parseInt(sc.nextLine());
        System.out.println("What is its priority (1 being the lowest, and 5 the highest)?");
        pri = sc.nextLine();
        System.out.println("Description: ");
        des = sc.nextLine();
        Ticket ticket = new Ticket("Date for maintence(12/34/56): " + dateTime + "\nRoom Number: " + rm + "\nPriority: " + pri + "\nDecription:  " + des + "\n");
        addTicket(ticket);
    }

    public void createAreaTicket() {
        String area = "";
        System.out.println("Date for maintence: ");
        dateTime = sc.nextLine();
        System.out.println("What area is the ticket for? (N, S, W, E)");

        boolean valid = false;
        while (!valid) {
            String s = sc.nextLine();
            if (s.equals("N")) {
                area = "North";
                valid = true;
            } else if (s.equals("S")) {
                area = "South";
                valid = true;
            } else if (s.equals("W")) {
                area = "West";
                valid = true;
            } else if (s.equals("E")) {
                area = "East";
                valid = true;
            } else {
                System.out.println("Not a valid Area. \nTry again: ");
            }

        }

        System.out.println("What's the priority? ");
        pri = sc.nextLine();
        System.out.println("Description: ");
        des = sc.nextLine();
        Ticket ticket = new Ticket("Date for maintence: " + dateTime + "\nArea: " + area + "\nPriority: " + pri + "\nDecription:  " + des + "\n");
        addTicket(ticket);
    }

    public int getTicket() {
        return ticketList.size();
    }


    void viewTicks() {
        if (ticketList.size() <= 0) {
            System.out.println("There are no tickets. ");
        }
        for (int i = 0; i < ticketList.size(); i++) {
            System.out.println(ticketList.get(i));
        }
    }
}
DimaSan
  • 12,264
  • 11
  • 65
  • 75

1 Answers1

0

You have a few options, but I'll give you two. In both cases, I'd use a Date (or similar) object to represent a date rather than a String.

  1. Make Ticket implement the Comparable<T> interface and use Collections.sort(List<T> list).
  2. Call Collections.sort(List<T> list, Comparator<? super T> c) on your List, by implementing Comparator<T>.

Option 1:

Make Ticket implement Comparable<Ticket>, this will allow the List to be sorted by calling Collections.sort(nameOfYourList).

Example Ticket class for Option 1:

package ticketsystem;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Ticket implements Comparable<Ticket> {

    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    private Date date;

    public Ticket(String d) throws ParseException {
        date = DATE_FORMAT.parse(d);
    }

    public Date getDate() {
        return date;
    }

    public String getDateString() {
        return DATE_FORMAT.format(date);
    }

    @Override
    public int compareTo(Ticket t) {
        // Compare the parameter t's Date to this instances Date (reversed to order by soonest first).
        return t.date.compareTo(date);
    }

}

Example usage for Option 1

package ticketsystem;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Example {

    public static void main(String[] args) throws ParseException {

        List<Ticket> tickets = new ArrayList<>();

        tickets.add(new Ticket("1900-05-06"));
        tickets.add(new Ticket("2016-11-02"));
        tickets.add(new Ticket("1976-12-01"));

        Collections.sort(tickets);

        for (Ticket t : tickets) {
            System.out.println(t.getDateString());
        }
    }
}

Option 2: Specify a Comparator to use at the time of calling Collections.sort(List<T> list, Comparator<? super E> c) (or myListName.sort(Comparator<? super E> c))

Example Ticket class for Option 2:

No need to implement Comparable in this case.

package ticketsystem;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Ticket {

    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    private Date date;

    public Ticket(String d) throws ParseException {
        date = DATE_FORMAT.parse(d);
    }

    public Date getDate() {
        return date;
    }

    public String getDateString() {
        return DATE_FORMAT.format(date);
    }

}

Example usage of Option 2:

package ticketsystem;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Example {

    public static void main(String[] args) throws ParseException {

        List<Ticket> tickets = new ArrayList<>();

        tickets.add(new Ticket("1900-05-06"));
        tickets.add(new Ticket("2016-11-02"));
        tickets.add(new Ticket("1976-12-01"));

        Collections.sort(tickets, new Comparator<Ticket>() {    
            @Override
            public int compare(Ticket t1, Ticket t2) {
                return t2.getDate().compareTo(t1.getDate());
            }
        });

        for (Ticket t : tickets) {
            System.out.println(t.getDateString());
        }

    }
}
d.j.brown
  • 1,822
  • 1
  • 12
  • 14
  • Im getting the dates from a user input,so how would i set that up so it can get those dates? – Ricardo96MC Nov 06 '16 at 22:21
  • @Ricardo96MC you'd just have to ensure the user enters if in the desired format, e.g. yyyy-MM-dd in this case, and pass then pass that String. e.g. using `Scanner sc = new Scanner(System.in)`, it would be `Ticket t = new Ticket(sc.next());` – d.j.brown Nov 06 '16 at 22:42