Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Aufgabe3 {
  4.  
  5. /**
  6. * @author S1010237024
  7. */
  8.  
  9. private static void printTime(long Time) {
  10. long hour = (Time % 86400000) / 3600000;
  11. long min = (Time % 3600000) / 60000;
  12. long sec = (Time % 60000) / 600;
  13.  
  14. System.out.format("%02d:%02d:%02d", hour, min, sec);
  15. Out.println();
  16.  
  17. }// printTime
  18.  
  19. public static void main(String[] args) {
  20.  
  21. String userCommand = "";
  22. long curTime = 0;
  23. long secTime = 0;
  24. long partTime = 0;
  25. long totalTime = 0;
  26. Scanner readCommand = new Scanner(System.in);
  27. boolean bRestart = true;
  28. boolean bStarted = false;
  29.  
  30. Out.println("Enter: start/stop/reset/exit:");
  31. Out.println("Other commands will be ignored");
  32.  
  33. do {
  34. userCommand = readCommand.next();
  35.  
  36. if (userCommand.equalsIgnoreCase("start") && bStarted == false) {
  37. curTime = System.currentTimeMillis();
  38. Out.println("Task started.");
  39. bStarted = true;
  40. }// if start
  41.  
  42. else if (userCommand.equalsIgnoreCase("start") && bStarted == true) {
  43. Out.println("Can't start. Task already running.");
  44. }// if start while started
  45.  
  46. if (userCommand.equalsIgnoreCase("stop") && bStarted == true) {
  47. secTime = System.currentTimeMillis();
  48. Out.println("Task stopped.");
  49.  
  50. partTime = (secTime - curTime);
  51. Out.print("Time on task: ");
  52. printTime(partTime);
  53.  
  54. totalTime = totalTime + partTime;
  55. Out.print("Total time on task: ");
  56. printTime(totalTime);// calculate the total time in ms. Method
  57. // printTime to format
  58.  
  59. bStarted = false;
  60. }// if stop
  61.  
  62. else if (userCommand.equalsIgnoreCase("stop") && bStarted == false) {
  63. Out.println("Can't stop. No task running");
  64. }// if stop while stopped
  65.  
  66. if (userCommand.equalsIgnoreCase("reset") && bStarted == false) {
  67. Out.println("Task resetted.");
  68. partTime = 0;
  69. totalTime = 0;
  70. curTime = 0;
  71. secTime = 0;
  72. }// if reset
  73. if (userCommand.equalsIgnoreCase("reset") && bStarted == true) {
  74. Out.println("Reset not possible. Task running.");
  75. }// if reset while task started
  76.  
  77. if (userCommand.equalsIgnoreCase("exit")) {
  78. Out.println("System exit");
  79. Out.print("Time on task: ");
  80. printTime(partTime);
  81. Out.print("Total time on task: ");
  82. printTime(totalTime);
  83. System.exit(0);
  84. bRestart = false;
  85. }// if exit
  86.  
  87. } while (bRestart == true);
  88.  
  89. }// main
  90.  
  91. }// Aufgabe3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement