0

My program simulates the operation of a lift(elevator) and it creates a line in a text file every time a 'user' uses the lift.

Sample of the text file:
25/03/14_05:09:24 Slccj Azeepc 1 2 25/03/14_05:37:48 Wfvp Dvjhlwvpc 3 5 25/03/14_05:38:27 Mfkk Wtrsejplc 2 1 25/03/14_05:39:00 Qczoz Mlrrtyd 0 4

Each usage creates a line in the file with the timestamp, the (encoded) user's name, the floor which they entered the lift and the floor alighted on.

I've been trying to figure out how to print a report which details how many times each user has used the lift in descending order.

Here is what I have so far:

public void showUseCount2() {

    int[] userCount = new int[4];

    String[] users = new String[4];
    users[0] = "Wfvp Dvjhlwvpc";    
    users[1] = "Qczoz Mlrrtyd";     
    users[2] = "Slccj Azeepc";      
    users[3] = "Mfkk Wtrsejplc";    

    try {   
        String strLine;
        for (int i = 0; i < userCount.length; i++) {
            FileInputStream fis = new FileInputStream("reports.txt");
            DataInputStream dis = new DataInputStream(fis);
            BufferedReader br = new BufferedReader(new InputStreamReader(dis));
            while ((strLine = br.readLine()) != null) {
                int startIndex = strLine.indexOf(users[i]);
                while (startIndex != -1) {
                    userCount[i]++;
                    startIndex = strLine.indexOf(users[i], startIndex + users[i].length());
                }
            }
            dis.close();    
        }    
        Arrays.sort(userCount);
        System.out.println(Arrays.asList(userCount));
    }
    catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
    System.out.println("Wfvp Dvjhlwvpc used the lift: " + userCount[0] + " times");
    System.out.println("Qczoz Mlrrtyd used the lift: " + userCount[1] + " times");
    System.out.println("Slccj Azeepc used the lift: " + userCount[2] + " times");
    System.out.println("Mfkk Wtrsejplc used the lift: " + userCount[3] + " times");
}

This is the output I'm getting at the moment:
[[I@19b04e2] Wfvp Dvjhlwvpc used the lift: 1 times Qczoz Mlrrtyd used the lift: 1 times Slccj Azeepc used the lift: 1 times Mfkk Wtrsejplc used the lift: 2 times

So my for loop successfully fills the userCount array. I've tried using Arrays.sort but it doesn't seem to be working properly for me. So I need a way to go about sorting these numbers in the array whilst maintaining a relationship between the user and their use count. Any help will be appreciated!

1
  • Note: You probably want System.out.println(Arrays.toString(userCount)); Commented Mar 25, 2014 at 20:15

2 Answers 2

2

Instead of sorting an array of ints, you'll want to define a class that holds the value you're sorting with and other information you want associated with it. You will then sort an array of those objects, instead of an array of ints. You can either make the class implement Comparable, or you can define a Comparator that you pass as a second argument to Arrays.sort. Example:

class User implements Comparable<User> {
    private int count;
    public User (String name) {
        this.name = name;
        count = 0;
    }
    public void incrementCount() {
        count++;
    }
    @Override
    public int compareTo(User other) {
        return Integer.compare(count, other.count);
        // Or if you want compareTo to return the reverse result, so that you can sort
        // in descending order:
        // return Integer.compare(other.count, count);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Objectify your log record, and parse the log line to create an Object

class LiftLogRecord {
   private final long userId;
   private final floor;
   private final numberOfTimes;
   private Date dateUsed;
   //rest of the code
}

and then use write custom comparator to sort it out based on required field

Collections.sort(logRecordList, (LiftLogRecord r1, LiftLogRecord r2) -> r1.getNumberUsed()-r2.getNumberUsed()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.