Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2004 SOLVED QUESTION 3 : LOGIN TIME

Thursday, March 28, 2019

ISC COMPUTER SCIENCE PRACTICAL 2004 SOLVED QUESTION 3 : LOGIN TIME


The manager of a company wants to analyse 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 log off 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 hour machine hours and minutes.
Design a program:
(a)    To find the duration for which each user has 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 values and some more random data.
SAMPLE DATA:
INPUT:
Number of users: 3
USER                                         LOGIN                                LOGOUT
IDENTIFICATION                      TIME & DATE                    TIME & DATE

149                                          20:10   20-12                           2:50     21-12

173                                          12:30   20-12                           12:30   21-12

142                                          16:20   20-12                           16:30   20-12

OUTPUT:
USER                             LOGIN                    LOGOUT                   DURATION
IDENTIFICATION          TIME & DATE        TIME & DATE             HOURS : MINS

149                              20:10   20-12               2:50     21-12                  6 : 40

173                              12:30   20-12               12:30   21-12               24 : 00

142                              16:20   20-12               16:30   20-12               00 : 10

THE USER WHO LOGGED IN FOR THE LONGEST DURATION

173                              12:30   20-12               12:30   21-12               24 : 00

In this program I have taken the inputs as HH:MM and DD/MM format and have extracted
the data. If you like you can take the inputs for hours, minutes, day and month separately.
Now coming to the main logic of the program take example for one user who
logged in at 10:30 on 25/1 and logged out on 2/2 on 16:00.
First we will calculate the number of days in between those days (excluding 25th and 2nd)
Folowing is the code:
if (in_month==out_month) {
                     if(in_day==out_day)
                           days=out_day-in_day;
                     else
                     days=out_day-in_day-1;
                     }
              else
                     days=(month_days[in_month-1]-in_day)+(out_day-1);
Now just multiply 24 to the number of days. For the remaining two days 25th and 2nd
just calculate the hours and minutes and add accordingly to get the total number of
hours and minutes.
For example on 25th the user has worked for
24-10-1 hrs and 30 mins and on the last day the user has worked for 16 hrs 00 mins.
Note that if the login day and logout date are same, then the number of hours worked
can be calculated simply by taking the difference of logout time and login time (hrs and minutes to calculated separately)



import java.util.*;
public class ISC2004Q3 {
       public static void main(String[] args) {
              int N,i,max=0,pos=0;
              Scanner in =new Scanner(System.in);
              System.out.println("Enter the number of users");
              N =in.nextInt();
              Work[] employee = new Work[N];
              for(i=0;i<N;i++) {
                     employee[i] = new Work();
                     employee[i].inputTimeAndDate();
                     employee[i].extraction();
                     }
              System.out.println("Employee ID     "+"Login Time & Date      "+"Logout Time & Date     "+"Duration");
              for(i=0;i<N;i++) {
                     employee[i].loggedinDuration();
                     employee[i].display();
                     if(max<Integer.parseInt(employee[i].loggedinDuration())) {
                           max=Integer.parseInt(employee[i].loggedinDuration());
                           pos=i;
                           }
                     }
              System.out.println("Employee with longest duration:");
              employee[pos].display();
              in.close();
       }
}
class Work{
       public void display() {
              String h,m;
              m=logged_hour.substring(logged_hour.length()-2,logged_hour.length());
              h=logged_hour.substring(0,logged_hour.length()-2)+":";
              System.out.println(identification_number+"              "+login_time+"  "+login_date+"                "+logout_time+"   "+logout_date+"      "+h+m); 
       }
       public String loggedinDuration() {
              int days=0,hr=0,min=0;
              logged_hour="";
              if (in_month==out_month) {
                     if(in_day==out_day)
                           days=out_day-in_day;
                     else
                     days=out_day-in_day-1;
                     }
              else
                     days=(month_days[in_month-1]-in_day)+(out_day-1);
              hr = days*24;
              if(days==0 && in_day==out_day) {
                     if(in_min>0)
                           hr += out_hr-in_hr-1;
                     else
                           hr += out_hr-in_hr;
                     }
              else {
                     if(in_min>0)
                           hr += 24-in_hr-1+out_hr;
                     else
                           hr += 24-in_hr+out_hr;
                     }
              min = (60-in_min)+out_min;
              if(min>=60  ) {
                     min = min%60;
                     if(in_min !=0 || out_min!=0)
                           hr +=1;
                     }
              if(hr<10)
                     logged_hour += "0"+hr;
              else
                     logged_hour += hr;
              if(min<10)
                     logged_hour += "0"+min;
              else
                     logged_hour += min;
              return logged_hour;
       }
       public void extraction() {
       String hr="",min="",day="",month="";    
       int i,counter=0;
       while(login_time.charAt(counter) != ':') {
              hr += login_time.charAt(counter);
              counter++;
              }
       in_hr=Integer.parseInt(hr);
       for(i=counter+1;i<login_time.length();i++)
              min += login_time.charAt(i);
      
       in_min=Integer.parseInt(min);
       counter = 0;
       while(login_date.charAt(counter) != '/') {
              day += login_date.charAt(counter);
              counter++;
              }
       in_day=Integer.parseInt(day);
       for(i=counter+1;i<login_date.length();i++)
              month += login_date.charAt(i);
      
       in_month=Integer.parseInt(month);
      
       counter =0;
       hr=min=day=month="";
       while(logout_time.charAt(counter) != ':')       {
              hr += logout_time.charAt(counter);
              counter++;
              }
       out_hr=Integer.parseInt(hr);
      
       for(i=counter+1;i<logout_time.length();i++)
              min += logout_time.charAt(i);
      
       out_min=Integer.parseInt(min);
       counter = 0;
       while(logout_date.charAt(counter) != '/')       {
              day += logout_date.charAt(counter);
              counter++;
              }
       out_day=Integer.parseInt(day);
      
       for(i=counter+1;i<logout_date.length();i++)
              month += logout_date.charAt(i);
      
       out_month=Integer.parseInt(month);
       }
      
       public void inputTimeAndDate() {
              Scanner in =new Scanner(System.in);
              System.out.println("Enter the employee ID");
              identification_number =in.nextLine();
              System.out.println("Enter the login time in HH:MM format");
              login_time =in.nextLine();
              System.out.println("Enter the login date in DD/MM format");
              login_date =in.nextLine();
              System.out.println("Enter the logout time in HH:MM format");
              logout_time =in.nextLine();
              System.out.println("Enter the logout date in DD/MM format");
              logout_date =in.nextLine();
             
       }
       private String logged_hour;
       private int[] month_days = {31,28,31,30,31,30,31,31,30,31,30,31};
       private String identification_number;
       private String login_time;
       private String login_date;
       private String logout_time;
       private String logout_date;
       private int in_hr;
       private int in_min;
       private int in_day;
       private int in_month;
       private int out_hr;
       private int out_min;
       private int out_day;
       private int out_month;
}

No comments:

Post a Comment