Advertisement
Nktfh100

java console timer

Nov 21st, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.Scanner;
  3.  
  4. public class Timer {
  5.  
  6.     public static String[] getCharLines(char char_) {
  7.         switch (char_) {
  8.         case '0':
  9.             return new String[] { "   ___  ", "  / _ \\ ", " | | | |", " | | | |", " | |_| |", "  \\___/ ", "        ", "        " };
  10.         case '1':
  11.             return new String[] { "  __ ", " /_ |", "  | |", "  | |", "  | |", "  |_|", "", "" };
  12.         case '2':
  13.             return new String[] { "  ___  ", " |__ \\ ", "    ) |", "   / / ", "  / /_ ", " |____|", "       ", "       " };
  14.         case '3':
  15.             return new String[] { "  ____  ", " |___ \\ ", "   __) |", "  |__ < ", "  ___) |", " |____/ ", "        ", "        " };
  16.         case '4':
  17.             return new String[] { "  _  _   ", " | || |  ", " | || |_ ", " |__   _|", "    | |  ", "    |_|  ", "         ", "         " };
  18.         case '5':
  19.             return new String[] { "  _____ ", " | ____|", " | |__  ", " |___ \\ ", "  ___) |", " |____/ ", "        ", "        " };
  20.         case '6':
  21.             return new String[] { "    __  ", "   / /  ", "  / /_  ", " | '_ \\ ", " | (_) |", "  \\___/ ", "        ", "        " };
  22.         case '7':
  23.             return new String[] { "  ______ ", " |____  |", "     / / ", "    / /  ", "   / /   ", "  /_/    ", "         ", "         " };
  24.         case '8':
  25.             return new String[] { "   ___  ", "  / _ \\ ", " | (_) |", "  > _ < ", " | (_) |", "  \\___/ ", "        ", "        " };
  26.         case '9':
  27.             return new String[] { "   ___  ", "  / _ \\ ", " | (_) |", "  \\__, |", "    / / ", "   /_/  ", "        ", "        " };
  28.         case ':':
  29.             return new String[] { "    ", "  _ ", " (_)", "    ", "  _ ", " (_)", "    ", "    ", "    " };
  30.         default:
  31.             return new String[] { "    ", "    ", "    ", "    ", "    ", "    ", "    ", "    ", };
  32.         }
  33.     }
  34.  
  35.     public static String addZero(Integer number) {
  36.         String number_ = String.valueOf(number);
  37.         if (number <= 9) {
  38.             number_ = "0" + String.valueOf(number);
  39.         }
  40.         return number_;
  41.     }
  42.  
  43.     public static void clearTerminal() {
  44.         try { // clear terminal
  45.             if (System.getProperty("os.name").contains("Windows"))
  46.                 new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
  47.             else
  48.                 Runtime.getRuntime().exec("clear");
  49.         } catch (IOException | InterruptedException ex) {
  50.         }
  51.     }
  52.  
  53.     public static void main(String[] args) {
  54.  
  55.         System.out.println("Minutes:");
  56.         Scanner scObj = new Scanner(System.in);
  57.         Integer minutes = scObj.nextInt();
  58.  
  59.         System.out.println("seconds:");
  60.         Integer seconds = scObj.nextInt();
  61.         System.out.println(minutes + ":" + seconds);
  62.         Boolean isFinished = false;
  63.         while (isFinished == false) {
  64.             seconds--;
  65.             if (seconds < 0) {
  66.                 seconds = 59;
  67.                 minutes--;
  68.                 if (minutes < 0) {
  69.                     isFinished = true;
  70.                     break;
  71.                 }
  72.             }
  73.             clearTerminal();
  74.             String minutes_ = addZero(minutes);
  75.             String seconds_ = addZero(seconds);
  76.             String[][] lines = { getCharLines(minutes_.charAt(0)), getCharLines(minutes_.charAt(1)), getCharLines(':'), getCharLines(seconds_.charAt(0)), getCharLines(seconds_.charAt(1)) };
  77.             for (Integer i = 0; i < 7; i++) {
  78.                 System.out.println(lines[0][i] + " " + lines[1][i] + " " + lines[2][i] + " " + lines[3][i] + " " + lines[4][i]);
  79.             }
  80.             try {
  81.                 Thread.sleep(1000);
  82.             } catch (Exception e) {
  83.                 //
  84.             }
  85.         }
  86.         System.out.println("Timer finished!");
  87.         System.out.println("Closing now.");
  88.         try {
  89.             Thread.sleep(5000);
  90.         } catch (Exception e) {
  91.             //
  92.         }
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement