Advertisement
Kulas_Code20

TimeBomb

Jun 23rd, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.30 KB | None | 0 0
  1. package projects;
  2. import java.time.Instant;
  3. import java.time.LocalDateTime;
  4. import java.time.ZoneId;
  5. import java.time.format.DateTimeFormatter;
  6. import java.util.Date;
  7. import java.util.Scanner;
  8.  
  9. public class TimeBomb {
  10.     static String[][] questions = {
  11.         {
  12.             "What is the square root of 121?",
  13.             "[A] 10\n[B] 11\n[C]13\n\nYour answer: ",
  14.             "b"
  15.         },
  16.         {
  17.             "What is the decimal of 10100?",
  18.             "[A] 100\n[B] 20\n[C] 18\n\nYour answer: ",
  19.             "b"
  20.         },
  21.         {
  22.             "What is the  binary number of 108?",
  23.             "[A] 1101100\n[B] 0101100\n[C] 11111000\n\nYour answer: ",
  24.             "a"
  25.         },
  26.         {
  27.             "What is the octal number of 4848?",
  28.             "[A] 11360 base 8\n[B] 21450  base 8\n[C] 27D base 16\n\nYour answer: ",
  29.             "a"
  30.         },
  31.         {
  32.             "What is the hexadecimal number of  9000?",
  33.             "[A] 12F0 base 16\n[B] 2328 base 16\n[C] 6C base 16\n\nYour answer: ",
  34.             "b"
  35.         },
  36.     };
  37.     static String[] answers = new String[5];
  38.     static int corrects = 0, wrongs = 0, index = 0;
  39.  
  40.     public static void main(String[] args) {
  41.         Scanner input = new Scanner(System.in);
  42.         String leftAlignFormat = "| %-10d | %-16s | %-16s | %-16s | %-16s |%n";
  43.  
  44.         Date startDate = Date.from(Instant.now()), endDate = null;
  45.  
  46.         String timeStart = formatTime(LocalDateTime.now());
  47.  
  48.         Date start = null;
  49.         Date end = null;
  50.  
  51.         int index = 0;
  52.  
  53.         // loop through all the questions
  54.         while (index < questions.length) {
  55.             // each start of the loop, we get the time (in milliseconds)
  56.             Instant _instant = Instant.now();
  57.             // we convert that time into a date & time format
  58.             LocalDateTime _start = LocalDateTime.ofInstant(_instant, ZoneId.systemDefault());
  59.  
  60.             // this here, records when the user started answering the question.
  61.             start = Date.from(_instant);
  62.  
  63.             // grab the question, options and answer from our 2-dimensional array
  64.             String question = questions[index][0];
  65.             String options = questions[index][1];
  66.             String answer = questions[index][2];
  67.  
  68.             System.out.println("Question #" + (index + 1));
  69.             System.out.println(question);
  70.             System.out.print(options);
  71.  
  72.             String in = input.nextLine();
  73.  
  74.             if ( in .toLowerCase().equals(answer.toLowerCase()))
  75.                 corrects += 1; // if correct, increment our 'corrects' counter
  76.             else
  77.                 wrongs += 1; // if wrong, increment our 'wrongs' counter
  78.  
  79.             end = Date.from(Instant.now());
  80.  
  81.             answers[index] = String.format("%s|%s|%s|%s", in .toUpperCase(), in .toLowerCase().equals(answer.toLowerCase()) ? "Corrrect" : "Wrong",
  82.                 formatTime(_start).split("@")[1],
  83.                 computeDuration(start, end));
  84.  
  85.             index += 1;
  86.             System.out.println();
  87.         }
  88.  
  89.         input.close();
  90.  
  91.         endDate = Date.from(Instant.now());
  92.  
  93.         /*
  94.         After answering all the questions, the user's performance will be displayed in this table:
  95.        
  96.         +----------------------------------------------------------------------------------------+
  97.         | Time bomb game started on June 23, 2021 @04:21 PM                                      |
  98.         +------------+------------------+------------------+------------------+------------------+
  99.         | Question # | Your Answer      | Remarks          | Time             | Duration         |
  100.         +------------+------------------+------------------+------------------+------------------+
  101.         | 1          | A                | Corrrect         | 04:21 PM         | 2 seconds        |
  102.         | 2          | C                | Wrong            | 04:21 PM         | 1 seconds        |
  103.         | 3          | B                | Wrong            | 04:21 PM         | 2 seconds        |
  104.         | 4          | A                | Corrrect         | 04:21 PM         | 1 seconds        |
  105.         | 5          | B                | Wrong            | 04:21 PM         | 1 seconds        |
  106.         +------------+------------------+------------------+------------------+------------------+
  107.  
  108.  
  109.         */
  110.  
  111.         System.out.println("+----------------------------------------------------------------------------------------+");
  112.         System.out.printf("| Time bomb game started on %-60s |\n", timeStart);
  113.         System.out.println("+------------+------------------+------------------+------------------+------------------+");
  114.         System.out.println("| Question # | Your Answer      | Remarks          | Time             | Duration         |");
  115.         System.out.println("+------------+------------------+------------------+------------------+------------------+");
  116.  
  117.         int counter = 1;
  118.  
  119.         for (String answer: answers) {
  120.             // split the user's answers
  121.             String[] _data = answer.split("\\|");
  122.             String _answer = _data[0];
  123.             String _remarks = _data[1];
  124.             String _time = _data[2];
  125.             String _duration = _data[3];
  126.  
  127.             System.out.printf(leftAlignFormat, counter, _answer, _remarks, _time, _duration);
  128.  
  129.             counter += 1;
  130.         }
  131.         System.out.println("+------------+------------------+------------------+------------------+------------------+");
  132.  
  133.         System.out.printf("Number of correct answers: %d\t\tNumber of wrong answers: %d\n", corrects, wrongs);
  134.  
  135.         System.out.printf("Total Duration: %s\n", computeDuration(startDate, endDate));
  136.     }
  137.  
  138.     static String formatTime(LocalDateTime localDateTime) {
  139.         return localDateTime.format(DateTimeFormatter.ofPattern("MMMM dd, yyyy @hh:mm a"));
  140.     }
  141.  
  142.     static String computeDuration(Date start, Date end) {
  143.  
  144.         long diff = end.getTime() - start.getTime();
  145.  
  146.         diff = (diff / 1000) % 60;
  147.  
  148.         return (diff > 60) ? computeDurationMinutes(start, end) : String.format("%d second(s)", diff);
  149.     }
  150.  
  151.     static String computeDurationMinutes(Date start, Date end) {
  152.  
  153.         long diff = end.getTime() - start.getTime();
  154.  
  155.         diff = (diff / (60 * 1000)) % 60;
  156.  
  157.         return String.format("%d minute(s)", diff);
  158.  
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement