diff --git a/src/main/java/rvc/timecalc/Main.java b/src/main/java/rvc/timecalc/Main.java index 087d9d2..89eba8d 100644 --- a/src/main/java/rvc/timecalc/Main.java +++ b/src/main/java/rvc/timecalc/Main.java @@ -1,13 +1,6 @@ package rvc.timecalc; -import java.io.IOException; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.HashSet; -import java.util.Scanner; -import java.util.Set; +import javax.swing.JOptionPane; /** * @author Robert @@ -15,196 +8,37 @@ import java.util.Set; */ public class Main { - public static final String WALL = "||"; - private static final String NEW_LINE = "\n"; + public static void main(String[] args) { - while(true) {run(args); - if(args.length != 0) { - break; - } - System.out.println(("Do you want to continue (y/n)?")); - Scanner scanner = new Scanner(System.in); - - if(!scanner.nextLine().equals("y")) { - System.out.println("Exiting \"Time Calc\"."); - break; - } - } - - } - public static void run(String[] args) { - System.out.println("*** Time Calc ***"); - System.out.println("-----------------\n"); - int EXPECTED_ARG_COUNT = 2; - if (args.length == 0) { - Scanner scanner = new Scanner(System.in); - args = new String[2]; - System.out.print("Arguments: "); - String returned = scanner.nextLine(); - args[0] = returned.split(" ")[0]; - if (returned.contains(" ")) { - args[1] = returned.split(" ")[1]; - } else { - args[1] = "0:00"; - } - } - if(args.length == 1) { - args = new String[]{args[0], "0:00"}; - } - if (args.length < EXPECTED_ARG_COUNT) { - throw new RuntimeException( - "Expected arg count is " + EXPECTED_ARG_COUNT - + ", but count of found args is: " + args.length); - } - String startTime = args[0]; - String overtimeUsed = args[1]; - int startHour = Integer.valueOf(startTime.split(":")[0]); - int startMinute = Integer.valueOf(startTime.split(":")[1]); - // System.out.println("startHour=" + startHour); - // System.out.println("startMinute=" + startMinute); - int overtimeUsedHour = Integer.valueOf(overtimeUsed.split(":")[0]); - int overtimeUsedMinute = Integer.valueOf(overtimeUsed.split(":")[1]); - // System.out.println("overtimeUsedHour=" + overtimeUsedHour); - // System.out.println("overtimeUsedMinute=" + overtimeUsedMinute); - - int workingHoursLength = 8; - int endHour = startHour + workingHoursLength - overtimeUsedHour; - int endMinute = startMinute + 30 - overtimeUsedMinute; - while (endMinute >= 60) { - endMinute = endMinute - 60; - endHour = endHour + 1; - } - int totalMinutes = (endHour * 60 + endMinute) - (startHour * 60 + startMinute); -// System.out.println("totalMinutes=" + totalMinutes); - // System.out.println("endHour=" + endHour); - // System.out.println("endMinute=" + endMinute); - System.out.println(); - DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm"); - - Set alreadyShownTimes = new HashSet<>(); - NumberFormat formatter = new DecimalFormat("#0.00"); - - while (true) { - LocalDateTime now = LocalDateTime.now(); - String nowString = dtf.format(now); - if (alreadyShownTimes.contains(nowString)) { - //nothing to do - continue; - } else { - alreadyShownTimes.add(nowString); - } - int hourNow = Integer.parseInt(nowString.split(":")[0]); - int minuteNow = Integer.parseInt(nowString.split(":")[1]); - - // System.out.println("hourNow=" + hourNow); - // System.out.println("minuteNow=" + minuteNow); - int hourRemains = endHour - hourNow; - int minuteRemains = endMinute - minuteNow; - if (minuteRemains < 0) { - minuteRemains = minuteRemains + 60; - hourRemains = hourRemains - 1; - } - int hourDone = workingHoursLength - overtimeUsedHour - hourRemains; - int minutesDone = 30 - overtimeUsedMinute - minuteRemains; - int totalMinutesDone = hourDone * 60 + minutesDone; - double done = ((double)totalMinutesDone)/((double)totalMinutes); -// System.out.println("hourDone=" + hourDone); -// System.out.println("minutesDone=" + minutesDone); -// System.out.println("totalMinutesDone=" + totalMinutesDone); - String msg = "Done=" + formatter.format(done * 100) + "% Remains=" + String.format("%02d", hourRemains) + ":" + String - .format("%02d", minuteRemains) + " (" + String.format("%03d", (hourRemains * 60 + minuteRemains)) + " minute" + ((hourRemains * 60 + minuteRemains) > 1 ? "s" : " ") + ")" + " End=" + String - .format("%02d", endHour) + ":" + String - .format("%02d", endMinute); - - if(System.getProperty("progress")!=null) { - printPercentToAscii(done, msg); - } else { - System.out.print(msg); - } - if(hourRemains <= 0 && minuteRemains <= 0) { - System.out.println("Congratulation :-) It is the time to go home.\n\n"); - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - - } - break; - } - + while(true) { + String startTime = (String) JOptionPane.showInputDialog( + null, + "Start Time:", + "Start Time", + JOptionPane.PLAIN_MESSAGE, + null, + null, + "" + ); + String overTime = (String) JOptionPane.showInputDialog( + null, + "Overtime:", + "Overtime", + JOptionPane.PLAIN_MESSAGE, + null, + null, + "0:00" + ); try { - Thread.sleep(5000); - } catch (InterruptedException e) { - + TimeCalc timeCalc = + new TimeCalc(startTime, overTime); + } catch(Exception e) { + JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), e.getMessage(), JOptionPane.ERROR_MESSAGE); } } - } - private static void printPercentToAscii(double percent, - String msg) { - NumberFormat formatter = new DecimalFormat("#00.00"); - String s = formatter.format(percent * 100); - s = s.replace(",",""); - - int percentInt = (int)(percent * 100); -// System.out.println("percentInt=" + percentInt); - int i1 = percentInt > 20 ? 10 : (int) (percentInt / 2); - int i2 = percentInt > 40 ? 10 : (int) ((percentInt - 20) / 2); - int i3 = percentInt > 60 ? 10 : (int) ((percentInt - 40) / 2); - int i4 = percentInt > 80 ? 10 : (int) ((percentInt - 60) / 2); - int i5 = (int) ((percentInt - 80) / 2); - int[] array = new int[]{i1,i2,i3,i4,i5}; - -// System.out.println(i1 +" " + i2 + " " + i3 + " " + i4); - - - int index = 0; - for(int i:array) { - - if(i < 0) { - i = 0; - } - System.out.print(index == 2 ? (msg + createSpaces(9 + (percentInt<10 ? 1: 0) + (percentInt==100 ? -1: 0))) : createSpaces(58)); - for(int j = 1; j <= i; j++) { - System.out.print("#"); - } - for(int j = 10; j > i; j--) { - System.out.print("."); - } - System.out.println(); - index++; - } - - System.out.println(createSpaces(58) + "||======||"); - int spacesTotal = 52; - int spacesDone = (int) (percent * 52); - int spacesTodo = spacesTotal - spacesDone; - System.out.println( - WALL + createSpaces(spacesDone) + " () " + createSpaces(spacesTodo) + WALL + (spacesTodo == 0 ? " GO " :"XXXXXX") + WALL + NEW_LINE + - WALL + createSpaces(spacesDone) + "/||\\" + createSpaces(spacesTodo) + WALL + (spacesTodo == 0 ? " HOME " :"XXXXXX") + WALL + NEW_LINE + - WALL + createSpaces(spacesDone) + " /\\ " + createSpaces(spacesTodo) + WALL + (spacesTodo == 0 ? " !! " :"XXXXXX") + WALL + NEW_LINE + - "====================================================================" - ); - - System.out.println("\n\n"); } - private static final String createSpaces(int spaceCount) { - return create(spaceCount, ' '); - } - private static final String create(int count, char ch) { - StringBuilder sb = new StringBuilder(); - for(int i = 1; i <= count; i++) { - sb.append(ch); - } - return sb.toString(); - } - private static final String createOneZeroes(int count) { - StringBuilder sb = new StringBuilder(); - for(int i = 1; i <= count; i++) { - double random = Math.random(); - sb.append(random > 0.66 ? "1" : (random > 0.33 ? "0" : "1")); - } - return sb.toString(); - } + } diff --git a/src/main/java/rvc/timecalc/TimeCalc.java b/src/main/java/rvc/timecalc/TimeCalc.java new file mode 100644 index 0000000..1e0ffa2 --- /dev/null +++ b/src/main/java/rvc/timecalc/TimeCalc.java @@ -0,0 +1,222 @@ +package rvc.timecalc; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JTextArea; +import javax.swing.JTextPane; +import javax.swing.SwingConstants; +import java.awt.Color; +import java.awt.Font; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashSet; +import java.util.Set; + +/** + * @author Robert + * @since 08.02.2024 + */ +public class TimeCalc { + private static final String DEFAULT_OVERTIME = "0:00"; + private static final int WORKING_HOURS_LENGTH = 8; + private static final int WORKING_MINUTES_LENGTH = 30; + public static final String WALL = "||"; + private static final String NEW_LINE = "\n"; + private final static DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm"); + private final String startTime; + private final String overTime; + private final int startHour; + private final int startMinute; + private final int overtimeHour; + private final int overtimeMinute; + private final int totalMinutes; + private int endHour; + private int endMinute; + private final Set alreadyShownTimes = new HashSet<>(); + private boolean stopBeforeEnd = false; + + public TimeCalc(String startTimeIn, String overTimeIn) { + this.startTime = startTimeIn; + this.overTime = (overTimeIn == null || overTimeIn.isEmpty()) ? DEFAULT_OVERTIME : overTimeIn; + + this.startHour = Integer.valueOf(startTime.split(":")[0]); + this.startMinute = Integer.valueOf(startTime.split(":")[1]); + + this.overtimeHour = Integer.valueOf(overTime.split(":")[0]); + this.overtimeMinute = Integer.valueOf(overTime.split(":")[1]); + + + this.endHour = startHour + WORKING_HOURS_LENGTH - overtimeHour; + this.endMinute = startMinute + WORKING_MINUTES_LENGTH - overtimeMinute; + while (endMinute >= 60) { + endMinute = endMinute - 60; + endHour = endHour + 1; + } + this.totalMinutes = (endHour * 60 + endMinute) - (startHour * 60 + startMinute); + + NumberFormat formatter = new DecimalFormat("#0.00"); + + + + + JFrame window=new JFrame(); + + JButton restartButton=new JButton("Restart"); + JButton exitButton=new JButton("Exit"); + + restartButton.setBounds(280,260,100, 30); + exitButton.setBounds(390,260,100, 30); + + window.add(restartButton); + window.add(exitButton); + JTextPane text = new JTextPane (); + text.setBounds(10,10,540, 250); + text.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); + text.setForeground(Color.GRAY); + text.setBackground(new Color(238,238,238)); + window.add(text); + + window.setSize(550,350); + window.setLayout(null); + window.setVisible(true); + window.setTitle("Time Calc"); + window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + + window.addWindowListener(new java.awt.event.WindowAdapter() { + @Override + public void windowClosing(java.awt.event.WindowEvent e) { + System.exit(0); + } + }); + exitButton.addActionListener(e -> System.exit(0)); + restartButton.addActionListener(e -> {window.setVisible(false); stopBeforeEnd = true;}); + + + StringBuilder sb = null; + while (true) { + if(stopBeforeEnd) { + window.setVisible(false); + window.dispose(); + break; + } + sb = new StringBuilder(); + LocalDateTime now = LocalDateTime.now(); + String nowString = DATE_TIME_FORMATTER.format(now); + if (alreadyShownTimes.contains(nowString)) { + //nothing to do + continue; + } else { + alreadyShownTimes.add(nowString); + } + int hourNow = Integer.parseInt(nowString.split(":")[0]); + int minuteNow = Integer.parseInt(nowString.split(":")[1]); + + int hourRemains = endHour - hourNow; + int minuteRemains = endMinute - minuteNow; + if (minuteRemains < 0) { + minuteRemains = minuteRemains + 60; + hourRemains = hourRemains - 1; + } + int hourDone = WORKING_HOURS_LENGTH - overtimeHour - hourRemains; + int minutesDone = 30 - overtimeMinute - minuteRemains; + int totalMinutesDone = hourDone * 60 + minutesDone; + double done = ((double)totalMinutesDone)/((double)totalMinutes); + + String msg = "Done=" + formatter.format(done * 100) + "% Remains=" + String.format("%02d", hourRemains) + ":" + String + .format("%02d", minuteRemains) + " (" + String.format("%03d", (hourRemains * 60 + minuteRemains)) + " minute" + ((hourRemains * 60 + minuteRemains) > 1 ? "s" : " ") + ")" + " End=" + String + .format("%02d", endHour) + ":" + String + .format("%02d", endMinute); + + //if(System.getProperty("progress")!=null) { + printPercentToAscii(done, msg, sb); +// } else { +// sb.append(msg); +// } + if(hourRemains <= 0 && minuteRemains <= 0) { + sb.append("\nCongratulation :-) It is the time to go home."); + //System.out.println(sb.toString()); + text.setText(sb.toString()); + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + + } + while(!stopBeforeEnd) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + + } + } + } else { + //System.out.println(sb.toString()); + text.setText(sb.toString()); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + + } + } + window.setVisible(false); + window.dispose(); + } + private static void printPercentToAscii(double percent, + String msg, StringBuilder sb) { + NumberFormat formatter = new DecimalFormat("#00.00"); + String s = formatter.format(percent * 100); + s = s.replace(",",""); + + int percentInt = (int)(percent * 100); + int i1 = percentInt > 20 ? 10 : (int) (percentInt / 2); + int i2 = percentInt > 40 ? 10 : (int) ((percentInt - 20) / 2); + int i3 = percentInt > 60 ? 10 : (int) ((percentInt - 40) / 2); + int i4 = percentInt > 80 ? 10 : (int) ((percentInt - 60) / 2); + int i5 = (int) ((percentInt - 80) / 2); + int[] array = new int[]{i1,i2,i3,i4,i5}; + + int index = 0; + for(int i:array) { + + if(i < 0) { + i = 0; + } + sb.append(index == 2 ? (msg + createSpaces(9 + (percentInt<10 ? 1: 0) + (percentInt==100 ? -1: 0))) : createSpaces(58)); + for(int j = 1; j <= i; j++) { + sb.append("#"); + } + for(int j = 10; j > i; j--) { + sb.append("."); + } + sb.append("\n"); + index++; + } + + sb.append(createSpaces(58) + "||======||\n"); + int spacesTotal = 52; + int spacesDone = (int) (percent * 52); + int spacesTodo = spacesTotal - spacesDone; + sb.append( + WALL + createSpaces(spacesDone) + " () " + createSpaces(spacesTodo) + WALL + (spacesTodo == 0 ? " GO " :"XXXXXX") + WALL + NEW_LINE + + WALL + createSpaces(spacesDone) + "/||\\" + createSpaces(spacesTodo) + WALL + (spacesTodo == 0 ? " HOME " :"XXXXXX") + WALL + NEW_LINE + + WALL + createSpaces(spacesDone) + " /\\ " + createSpaces(spacesTodo) + WALL + (spacesTodo == 0 ? " !! " :"XXXXXX") + WALL + NEW_LINE + + "====================================================================" + NEW_LINE + ); + + } + private static final String createSpaces(int spaceCount) { + return create(spaceCount, ' '); + } + private static final String create(int count, char ch) { + StringBuilder sb = new StringBuilder(); + for(int i = 1; i <= count; i++) { + sb.append(ch); + } + return sb.toString(); + } + +}