├── README.md └── myclock.dart /README.md: -------------------------------------------------------------------------------- 1 | # clock 2 | Dart console application for a company clock-in system. 3 | This program allows employees to clock in and out, view their total working hours for the day, and view their total working hours for the week. 4 | 5 | 6 | This Dart application provides a menu-driven interface where employees can clock in, clock out, and view their total working hours for the day or week. Employee data is stored in a map with the employee's name as the key and an Employee object as the value. Each Employee object contains lists of clock-in and clock-out timestamps. 7 | -------------------------------------------------------------------------------- /myclock.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | class Employee { 4 | String name; 5 | List clockIns = []; 6 | List clockOuts = []; 7 | 8 | Employee(this.name); 9 | 10 | void clockIn() { 11 | clockIns.add(DateTime.now()); 12 | print('Clocked in at ${clockIns.last}'); 13 | } 14 | 15 | void clockOut() { 16 | clockOuts.add(DateTime.now()); 17 | print('Clocked out at ${clockOuts.last}'); 18 | } 19 | 20 | Duration getTodayTotalHours() { 21 | var today = DateTime.now(); 22 | var todayClockIns = clockIns.where((dateTime) => 23 | dateTime.year == today.year && 24 | dateTime.month == today.month && 25 | dateTime.day == today.day); 26 | var todayClockOuts = clockOuts.where((dateTime) => 27 | dateTime.year == today.year && 28 | dateTime.month == today.month && 29 | dateTime.day == today.day); 30 | 31 | var total = Duration.zero; 32 | for (var i = 0; i < todayClockIns.length; i++) { 33 | var clockIn = todayClockIns.elementAt(i); 34 | var clockOut = todayClockOuts.elementAt(i); 35 | total += clockOut.difference(clockIn); 36 | } 37 | return total; 38 | } 39 | 40 | Duration getWeeklyTotalHours() { 41 | var today = DateTime.now(); 42 | var startOfWeek = today.subtract(Duration(days: today.weekday - 1)); 43 | var endOfWeek = startOfWeek.add(Duration(days: 6)); 44 | 45 | var weeklyClockIns = clockIns.where((dateTime) => 46 | dateTime.isAfter(startOfWeek.subtract(Duration(seconds: 1))) && 47 | dateTime.isBefore(endOfWeek.add(Duration(days: 1)))); 48 | var weeklyClockOuts = clockOuts.where((dateTime) => 49 | dateTime.isAfter(startOfWeek.subtract(Duration(seconds: 1))) && 50 | dateTime.isBefore(endOfWeek.add(Duration(days: 1)))); 51 | 52 | var total = Duration.zero; 53 | for (var i = 0; i < weeklyClockIns.length; i++) { 54 | var clockIn = weeklyClockIns.elementAt(i); 55 | var clockOut = weeklyClockOuts.elementAt(i); 56 | total += clockOut.difference(clockIn); 57 | } 58 | return total; 59 | } 60 | } 61 | 62 | void main() { 63 | var employees = {}; 64 | 65 | while (true) { 66 | print('1. Clock In'); 67 | print('2. Clock Out'); 68 | print('3. View Today\'s Total Hours'); 69 | print('4. View Weekly Total Hours'); 70 | print('5. Exit'); 71 | stdout.write('Select an option: '); 72 | var choice = int.tryParse(stdin.readLineSync() ?? ''); 73 | 74 | switch (choice) { 75 | case 1: 76 | clockIn(employees); 77 | break; 78 | case 2: 79 | clockOut(employees); 80 | break; 81 | case 3: 82 | viewTodayTotalHours(employees); 83 | break; 84 | case 4: 85 | viewWeeklyTotalHours(employees); 86 | break; 87 | case 5: 88 | print('Exiting...'); 89 | return; 90 | default: 91 | print('Invalid option. Please try again.'); 92 | } 93 | } 94 | } 95 | 96 | void clockIn(Map employees) { 97 | stdout.write('Enter employee name: '); 98 | var name = stdin.readLineSync() ?? ''; 99 | var employee = employees.putIfAbsent(name, () => Employee(name)); 100 | employee.clockIn(); 101 | } 102 | 103 | void clockOut(Map employees) { 104 | stdout.write('Enter employee name: '); 105 | var name = stdin.readLineSync() ?? ''; 106 | var employee = employees[name]; 107 | if (employee != null) { 108 | employee.clockOut(); 109 | } else { 110 | print('Employee not found.'); 111 | } 112 | } 113 | 114 | void viewTodayTotalHours(Map employees) { 115 | stdout.write('Enter employee name: '); 116 | var name = stdin.readLineSync() ?? ''; 117 | var employee = employees[name]; 118 | if (employee != null) { 119 | var totalHours = employee.getTodayTotalHours(); 120 | print('Total working hours today: ${totalHours.inHours} hours ${totalHours.inMinutes % 60} minutes'); 121 | } else { 122 | print('Employee not found.'); 123 | } 124 | } 125 | 126 | void viewWeeklyTotalHours(Map employees) { 127 | stdout.write('Enter employee name: '); 128 | var name = stdin.readLineSync() ?? ''; 129 | var employee = employees[name]; 130 | if (employee != null) { 131 | var totalHours = employee.getWeeklyTotalHours(); 132 | print('Total working hours this week: ${totalHours.inHours} hours ${totalHours.inMinutes % 60} minutes'); 133 | } else { 134 | print('Employee not found.'); 135 | } 136 | } 137 | --------------------------------------------------------------------------------