Skip to content

Commit

Permalink
Merge pull request #5 from bruch-alex/maven-integration
Browse files Browse the repository at this point in the history
rewrite ui
  • Loading branch information
bruch-alex authored Jul 14, 2024
2 parents a5ae1c1 + 337cd63 commit 8ade4c5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/main/java/Coder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public class Coder {
public static String startCoder(String message) {
public static String start(String message) {
StringBuilder result = new StringBuilder();

for (char c : message.toCharArray()){
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/Decoder.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import org.jline.reader.LineReader;

import java.util.Scanner;

public class Decoder {
public static String startDecoder(String message) {
public static String start(String message) {
boolean withErrors = false;
StringBuilder sb = new StringBuilder(message);
StringBuilder result = new StringBuilder();
Expand All @@ -19,7 +15,7 @@ public static String startDecoder(String message) {
result.append(decode(binaryCharWithParityBits));
}
if (!withErrors){
System.out.println("!!!Received without errors!!!");
System.out.println("\n!!!Received without errors!!!");
}
return result.toString();
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/Helpers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
public class Helpers {

public static StringBuilder replaceParityBits(StringBuilder binaryMessageWithEmptyParityBits) {
for (int i = 1; i <= 12; i *= 2)
{
Expand Down
139 changes: 68 additions & 71 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import org.jline.reader.LineReaderBuilder;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.utils.InfoCmp;

public class Main {
static String result;

public static void main(String[] args) {
try {
Terminal terminal = TerminalBuilder.builder().system(true).build();
Expand All @@ -17,81 +18,77 @@ public static void main(String[] args) {

public static void startUI(LineReader reader, Terminal terminal) {
while (true) {
terminal.puts(InfoCmp.Capability.clear_screen);
terminal.flush();
System.out.println("Hello Human!\nWhat do u want to do now?");
System.out.println("1 - Encode text\t2 - Decode Text\t3 - Exit");
String result;

switch (Integer.parseInt(reader.readLine())) {
case 1:
while (true) {
System.out.println("Enter the message you want to encode: ");
result = Coder.startCoder(reader.readLine());
terminal.puts(InfoCmp.Capability.clear_screen);
terminal.flush();
System.out.println("Your message was encoded successfully.\nChoose a way to see result:");
System.out.println("1 - print it in console\n2 - write to file (Not yet implemented)");
switch (Integer.parseInt(reader.readLine())) {
case 1:
terminal.puts(InfoCmp.Capability.clear_screen);
terminal.flush();
System.out.println("Pls copy your encoded text:\n\n" + result + "\n");
System.out.println("Do u want to decode it now? (y/n)");
switch (reader.readLine()) {
case "y":
while (true) {
terminal.puts(InfoCmp.Capability.clear_screen);
terminal.flush();
System.out.println("Enter your encoded text:");
result = Decoder.startDecoder(reader.readLine());
System.out.println("Your decoded text:\n\n" + result + "\n");
System.out.println("Try decode again? (y/n)");
switch (reader.readLine()) {
case "y":
continue;
case "n":
break;
}
break;
}
break;
case "n":
System.exit(0);
}
break;
case 2:
System.out.println("not yet implemented");
break;
}
System.out.println("Do u want to encode something else? (y/n)");
switch (reader.readLine()) {
case "y":
break;
case "n":
System.exit(0);
break;
}
}
case 2:
while (true) {
System.out.println("Enter the message you want to decode: ");
result = Decoder.startDecoder(reader.readLine());
System.out.println("Your decoded text:\n\n" + result + "\n");
System.out.println("\nTry again? (y/n)");
switch (reader.readLine()) {
case "y":
break;
case "n":
System.exit(0);
}
}
case 3:
printMainMenu(terminal);
switch (reader.readLine()) {
case "1":
encodeSequence(reader, terminal);
break;
case "2":
decodeSequence(reader, terminal);
break;
case "3":
System.exit(0);
default:
System.err.println("Incorrect input, try again");
break;
}
}
}

public static void printMainMenu(Terminal terminal) {
System.out.println("========== Main Menu ==========");
System.out.println(
"1. Encode text with Hamming code\n" +
"2. Decode text\n" +
"3. Exit program");
}

public static void encodeSequence(LineReader reader, Terminal terminal) {
System.out.println("Enter the message you want to encode: ");
result = Coder.start(reader.readLine());

showPrintOptions();
handlePrintOptions(reader);
}

public static void showPrintOptions() {
System.out.println("\nYour message has been processed successfully.\nChoose a way to see result:");
System.out.println("1 - print it in console\n2 - write to file (not yet implemented)\n3. Go back");
}

public static void handlePrintOptions(LineReader reader) {
boolean exit = false;
while (!exit) {
switch (reader.readLine()) {
case "1":
printResultToConsole(result);
exit = true;
break;
case "2": // Plug while writing to a file is not supported
System.err.println("Not implemented");
showPrintOptions();
break;
case "3":
exit = true;
break;
default:
System.err.println("Invalid input. Try again");
showPrintOptions();
break;
}
}
}

public static void printResultToConsole(String result) {
System.out.println("Your message:");
System.out.println("\n" + result + "\n");
}

public static void decodeSequence(LineReader reader, Terminal terminal) {
System.out.println("Enter text you want to decode:");
result = Decoder.start(reader.readLine());

showPrintOptions();
handlePrintOptions(reader);
}
}
5 changes: 5 additions & 0 deletions src/test/java/HelpersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import static org.junit.Assert.*;

public class HelpersTest {

}

0 comments on commit 8ade4c5

Please sign in to comment.