KnowledgeBoat Logo

Computer Science

The manager of a company wants to analyze the machine usage from the records to find the utilization of the machine. He wants to know how long each user used the machine. When the user wants to use the machine, he must login to the machine and after finishing the work, he must logoff the machine.

Each log record consists of:

User Identification number
Login time and date
Logout time and date

Time consists of:

Hours
Minutes

Date consists of:

Day
Month

You may assume all logins and logouts are in the same year and there are 100 users at the most. The time format is 24 hours.

Design a program:

(a) To find the duration for which each user logged. Output all records along with the duration in hours (format hours: minutes).

(b) Output the record of the user who logged for the longest duration. You may assume that no user will login for more than 48 hours.

Test your program for the following data and some random data.

Sample Data

Input:
Number of users: 3
User Identification

Login Time and DateLogout Time and Date
20:1020-122:5021-12
12:3020-1212:3021-12
16:2020-1216:3020-12

Output:

User IdentificationLogin Time and DateLogout Time and DateDuration
Hours:Minutes
14920:1020-122:5021-126:40
17312:3020-1212:3021-1224:00
14216:2020-1216:3020-1200:10

The user who logged in for longest duration:

17312:3020-1212:3021-1224:00

Java

Java Arrays

8 Likes

Answer

import java.util.Scanner;

public class KboatMachineUtilization
{
    public static void main(String args[]) {
        final int MINS_IN_DAY = 1440;
        final int MINS_IN_HOUR = 60;
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Number of Users: ");
        int n = in.nextInt();
        in.nextLine();

        if (n > 100 || n < 1) {
            System.out.println("Invalid Input!");
            System.out.println("No of users must be between 1 and 100");
            return;
        }

        String records[][] = new String[n][6];

        int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        for (int i = 0; i < n; i++) {
            System.out.println("Enter record of user " + (i+1) + ": ");
            System.out.print("Enter User Identification: ");
            records[i][0] = in.nextLine();
            System.out.print("Enter Login Time(hh:mm): ");
            records[i][1] = in.nextLine();
            System.out.print("Enter Login Date(dd-mm): ");
            records[i][2] = in.nextLine();
            System.out.print("Enter Logout Time(hh:mm): ");
            records[i][3] = in.nextLine();
            System.out.print("Enter Logout Date(dd-mm): ");
            records[i][4] = in.nextLine();
        }
        System.out.println();

        int longIdx = 0;
        int longDuration = 0;
        for (int i = 0; i < n; i++) {
            int duration = 0;
            int tempIdx = records[i][1].indexOf(':');
            int loginHr = Integer.parseInt(records[i][1].substring(0, tempIdx));
            int loginMin = Integer.parseInt(records[i][1].substring(tempIdx + 1));
            tempIdx = records[i][3].indexOf(':');
            int logoutHr = Integer.parseInt(records[i][3].substring(0, tempIdx));
            int logoutMin = Integer.parseInt(records[i][3].substring(tempIdx + 1));
            int m1 = loginHr * MINS_IN_HOUR + loginMin;
            int m2 = logoutHr * MINS_IN_HOUR + logoutMin;

            //If login & logout is on the same day
            if (records[i][2].equals(records[i][4])) {
                duration = m2 - m1;
            }
            else {
                int daysDiff = 0;
                tempIdx = records[i][2].indexOf('-');
                int loginDay = Integer.parseInt(records[i][2].substring(0, tempIdx));
                int loginMonth = Integer.parseInt(records[i][2].substring(tempIdx + 1));
                tempIdx = records[i][4].indexOf('-');
                int logoutDay = Integer.parseInt(records[i][4].substring(0, tempIdx));
                int logoutMonth = Integer.parseInt(records[i][4].substring(tempIdx + 1));
                //If login & logout is in the same month
                if (loginMonth == logoutMonth) {
                    daysDiff = logoutDay - loginDay - 1;
                }
                else {
                    daysDiff = monthDays[loginMonth - 1] - loginDay + logoutDay - 1;
                }

                duration = (MINS_IN_DAY - m1) + m2 + daysDiff * MINS_IN_DAY;
            }

            if (duration > longDuration) {
                longDuration = duration;
                longIdx = i;
            }

            int durHr = duration / 60;
            int durMin = duration % 60;
            records[i][5] = (durHr == 0 ? "00" : durHr)  
            + ":" 
            + (durMin == 0 ? "00" : durMin);
        }

        System.out.println("User\t\tLogin\t\tLogout\t\tDuration");
        System.out.println("Identification\tTime & Date\tTime & Date\tHours:Minutes");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 6; j++) {
                System.out.print(records[i][j] + "\t");
            }
            System.out.println();
        }

        System.out.println();
        System.out.println("The user who logged in for longest duration:");
        for (int j = 0; j < 6; j++) {
            System.out.print(records[longIdx][j] + "\t");
        }
    }
}

Output

BlueJ output of The manager of a company wants to analyze the machine usage from the records to find the utilization of the machine. He wants to know how long each user used the machine. When the user wants to use the machine, he must login to the machine and after finishing the work, he must logoff the machine. Each log record consists of: User Identification number Login time and date Logout time and date Time consists of: Hours Minutes Date consists of: Day Month You may assume all logins and logouts are in the same year and there are 100 users at the most. The time format is 24 hours. Design a program: (a) To find the duration for which each user logged. Output all records along with the duration in hours (format hours: minutes). (b) Output the record of the user who logged for the longest duration. You may assume that no user will login for more than 48 hours. Test your program for the following data and some random data. Sample Data Input: Number of users: 3 User Identification Output: The user who logged in for longest duration:

Answered By

3 Likes


Related Questions