0

I`m currently trying to code a calender. I save my Appointments in 2 different ArrayLists within another ArrayList.

Strings ( Subject, Place, People ) go into the first ArrayList within another ArrayList = arStr
Integers( Date and Time ) go into the second ArrayList within another ArrayList = arInt

When I create an Appointment I want to sort it according to the date. So if I want to add a new Appointment, it should be saved above or below the saved ones ( depeding on the time ) in the outter List. The already saved ones should go down in the outter List, if their date is later than the new one. After that is done i want to connect the String Appointment to the Int Appointment.

My Problem is that I cant find a way to sort them in this way, can anyone help me pls :) ?

public class Calender
{
public static ArrayList<ArrayList<Integer>> arInt = new ArrayList<ArrayList<Integer>>();        
public static ArrayList<ArrayList<String>> arStr = new ArrayList<ArrayList<String>>();
public static Scanner read = new Scanner(System.in);
public static int counter = 0;  // counts all made appointments

public static void main (String[]args)
    {
    //Adding Arrylists for space    
    arInt.add(new ArrayList());
    arStr.add(new ArrayList());
    arInt.add(new ArrayList());
    arStr.add(new ArrayList());

    // This is one Appointment ( Subject, Year, Month, Day, Hour )
    // Already saved Appointment
    counter++;
    arStr.get(0).add("3.Liste");
    arInt.get(0).add(2017);
    arInt.get(0).add(2);
    arInt.get(0).add(8);
    arInt.get(0).add(16);

    // new Appointment
    counter++;          
    String betreff = "1. Appointment";
    int year = 2017;
    int month = 2;
    int day = 8;
    int hours = 15;     

    // How to compare these Variables with the first Appointment and save it accordigly ?

    }
}
Coldvirus
  • 45
  • 10

2 Answers2

4

My suggestion is to create the new class Appointment, assign it an LocalDateTime and let the calendar store a list of that type, such as List<Appointment> appointments = new ArrayList<>().

Afterwards, it is easy to sort the appointments using the built-in sort method and your own comparator:

appointments.sort((a1, a2) -> a1.date.isBefore(a2.date));)

In any case, I suggest you do a tutorial on Object-Oriented Design first, such as https://www.tutorialspoint.com/object_oriented_analysis_design/index.htm.

Halmackenreuter
  • 681
  • 5
  • 9
  • So basically i would just have one ArrayList thats made up out of Appointments ? Thx for your help ! – Coldvirus Jan 06 '17 at 14:28
  • Just a suggestion. Of course, an actual full-fledged Calendar implementation could be much more complex; but if your task is to store some appointments, yes, I would do it like that. – Halmackenreuter Jan 06 '17 at 14:32
  • It dont know if that changes anything, but all the operations include removing an appointment, changing an Appointment and, yeah thats it :'D – Coldvirus Jan 06 '17 at 14:36
0

Firstly, you have ArrayLists within other ArrayLists which are completely unnecessary in this case. Removing those massively simplifies your code:

public class Calender
{
    public static ArrayList<Integer> arInt = new ArrayList<>();        
    public static ArrayList<String> arStr = new ArrayList<>();
    public static Scanner read = new Scanner(System.in);
    public static int counter = 0;  // counts all made appointments

    public static void main (String[]args)
    {
        // This is one Appointment ( Subject, Year, Month, Day, Hour )
        // Already saved Appointment
        counter++;
        arStr.add("3.Liste");
        arInt.add(2017);
        arInt.add(2);
        arInt.add(8);
        arInt.add(16);

        // new Appointment
        counter++;          
        String betreff = "1. Appointment";
        int year = 2017;
        int month = 2;
        int day = 8;
        int hours = 15;     

        // How to compare these Variables with the first Appointment and save it accordigly ?
    }
}

As a next step, you will want to unify those two lists. What you currently have is one list of dates and a separate list of appointment names. You want a single list of appointments. To do that you will need to write an Appointment class:

public class Appointment
{
    private String name;
    private Date   date;

    // and so on
}

You will then be able to create an ArrayList of those:

ArrayList<Appointment> appointments = new ArrayList<>();

To be able to sort your list in various ways you can use:

Collections.sort(appointments, aCustomComparator);

You will need to write your own "comparator" to accomplish this. This is basically a function which compares two objects to see which one comes first. Does January 1st come before January 3rd? That kind of thing.

See this answer or Google 'write a custom comparator java' for more information.

Community
  • 1
  • 1
Michael
  • 41,989
  • 11
  • 82
  • 128
  • Thats actually really helpful, thank you. Yet I dont understand one thing, if lets say all dates and times are saved in one ArrayListInt, how will i be able to sort them to the right Appointment ? Thx for your help (Y) – Coldvirus Jan 06 '17 at 14:43
  • There would be no `ArrayListInt`. You would create one `ArrayList` called `appointments`. You would insert into that different `Appointment` objects: `appointments.add(new Appointment("my name", somedate)`. Then sort it however you want. – Michael Jan 06 '17 at 14:50
  • oooohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh, thanks a lot ! – Coldvirus Jan 06 '17 at 14:55