Friday 25 October 2013

Login and Logout time

/* Login and Logout 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 the 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 most. The time format is 24 hour machine hours and minutes.
Design a program to find:
• To find the duration for which each user has logged.
• Output all records along with the duration. You may assume no user will login for more than 48 hours.

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

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:0

The user who logged in for the longest duration:-
173 12:30 20-12 12:30 21-12 24:00

Answer 3.*/
import java.io.*;
class Date1
{
 //Data members
 public int day;
 public int month;
 //Constructor
 public Date1()
 {
     day=0;
     month=0;
 }
 //Function to input date
 public void Input()
 throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter day ::");
       day=Integer.parseInt(br.readLine());
       System.out.println("Enter month ::");
       month=Integer.parseInt(br.readLine());
    }
  //function to display date
  public void display()
  {
      System.out.print(day+"-"+month);
   }
}
class Time
{
 //Data members
 public int hour;
 public int minute;
 //Constructor
 public Time()
 {
     hour=0;
     minute=0;
 }
 //Function to input time
 public void Input()
 throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter hour ::");
       hour=Integer.parseInt(br.readLine());
       System.out.println("Enter minute ::");
       minute=Integer.parseInt(br.readLine());
    }
  //function to display time
  public void display()
  {
      System.out.print(hour+":"+minute);
   }
}
class Q3_2004
{
 //Data members
 public Date1 logindt;
 public Date1 logoutdt;
 public Time logintm;
 public Time logouttm;
 private int userid;
 //Constructor
 Q3_2004()
 {
     userid=0;
     logindt=new Date1();
     logoutdt=new Date1();
     logintm=new Time();
     logouttm=new Time();
  }
  //Function to input data
  public void Input()
  throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter user identification number ::");
       userid=Integer.parseInt(br.readLine());
       System.out.println("Enter login time::");
       logintm.Input();
       System.out.println("Enter login date::");
       logindt.Input();
       System.out.println("Enter logout time::");
       logouttm.Input();
       System.out.println("Enter logout date::");
       logoutdt.Input();      
    }
   //Function to find duration
   public Time Duration()
   {
       Time duration=new Time();
       if(logintm.minute > logouttm.minute)
         duration.minute=(60-logintm.minute)+logouttm.minute;
       else
         duration.minute = logouttm.minute-logintm.minute;
       duration.hour=(24-logintm.hour)+logouttm.hour;
       return duration;
    }
  //Function to display
  public void Display()
  {
      System.out.print(userid+"\t\t"+logintm.hour+":"+logintm.minute+"\t"+logindt.day+"-"+logindt.month+"\t\t"+logouttm.hour+":"+logouttm.minute+"\t"+logoutdt.day+"-"+logoutdt.month);
  }
   
  //Function to find duration
  public void findduration()
  throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       int n,i,longhour=0;
      
       Q3_2004 longobj=new Q3_2004();
           
       System.out.println("Number of users :;");
       n=Integer.parseInt(br.readLine());
       Time longdur =new Time();
       for(i=1;i<=n;i++)
       {
           System.out.println("\nEnter details for user "+i);
           Q3_2004 obj=new Q3_2004();
           obj.Input();
           obj.Display();
           Time dur=new Time();
           dur=obj.Duration();
           System.out.print("\t\t");
           dur.display();
           System.out.println("\n");
          
           if(dur.hour>longhour)
           {
               longhour=dur.hour;
               longobj=obj;
               longdur=dur;
            }
        }
      
        System.out.println("\nThe user logged in for the longest duration ::");
        longobj.Display();
        System.out.print("\t\t");
        longdur.display();
    }
}
class Q3_2004main
{
  public static void main(String args[])
  throws IOException
  {
      Q3_2004 obj=new Q3_2004();
      obj.findduration();
    }
}

No comments:

Post a Comment