diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9fb18b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +out diff --git a/LearnJava.iml b/LearnJava.iml new file mode 100644 index 0000000..b13ff2f --- /dev/null +++ b/LearnJava.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/enigma-console.jar b/lib/enigma-console.jar new file mode 100644 index 0000000..0bd155e Binary files /dev/null and b/lib/enigma-console.jar differ diff --git a/src/jkord/lesson1/ConsoleApp.java b/src/jkord/lesson1/ConsoleApp.java new file mode 100644 index 0000000..d90b96e --- /dev/null +++ b/src/jkord/lesson1/ConsoleApp.java @@ -0,0 +1,129 @@ +package jkord.lesson1; + +import enigma.console.Console; +import enigma.core.Enigma; +import java.util.*; + +import jkord.lesson1.handler.Command; + +public class ConsoleApp +{ + private Console sConsole; + private String title; + + private ArrayList commands; + private int maxStrIndexCommand; + + public ConsoleApp(String title) { + sConsole = Enigma.getConsole(title); + + this.title = title; + this.commands = new ArrayList<>(); + + //colorText = new TextAttributes(Color.BLUE, Color.WHITE); + //colorRestText = new TextAttributes(Color.WHITE, Color.BLACK); + } + + public void addSubApp(List items) { + commands.addAll(items); + commands.add(new Command("Exit (Esc)", () -> close())); + + maxStrIndexCommand = maxStrList(); + } + + public int maxStrList() { + int max = 0; + for (int i = 0; i < commands.size(); i++) + if (commands.get(max).getName().length() < commands.get(i).getName().length()) + max = i; + + return commands.get(max).getName().length(); + } + + public void print(String text) { + System.out.print(text); + } + public void printLn(String text) { + System.out.println(text); + } + public void enter() { + System.out.println(); + } + public int read() { + while (true) { + try { + return Integer.parseInt(sConsole.readLine()); + } catch (Exception e) { + System.out.println("Invalid input! Try again"); + } + } + } + + private void clearFirstStep() { + for (int i = 0; i < 20; i++) + enter(); + } + + private void clearSecondStep() { + for (int i = 0; i < 20; i++) + printLn("."); + } + + public void drawPart() { + print(" "); + for (int i = 0; i < maxStrIndexCommand + 4; i++) + print("-"); + enter(); + } + + public void drawLines() { + clearFirstStep(); + + for (int j = 0; j <= (maxStrIndexCommand + 4 - title.length()) / 2; j++) + print(" "); + print(title); + + enter(); + drawPart(); + + int i = 0, index = 1; + for (Command command : commands) + { + //if (i == selectIndex) + // sConsole.setTextAttributes(colorText); + + if (command.getName() != "-") { + print("| " + index +" "); + print(command.getName()); + + for (int j = 0; j < maxStrIndexCommand - command.getName().length(); j++) + print(" "); + printLn(" |"); + index++; + } else { + drawPart(); + } + + //sConsole.setTextAttributes(colorRestText); + i++; + } + + drawPart(); + } + + public void processing() { + while (true) { + drawLines(); + print("Choose: "); + int index = read() - 1; + if (index > 0 && index < commands.size()) + commands.get(index).run(); + else + printLn("Command not found!"); + } + } + + public void close() { + System.exit(0); + } +} \ No newline at end of file diff --git a/src/jkord/lesson1/Main.java b/src/jkord/lesson1/Main.java new file mode 100644 index 0000000..c5224e1 --- /dev/null +++ b/src/jkord/lesson1/Main.java @@ -0,0 +1,47 @@ +package jkord.lesson1; + +import java.util.*; + +import jkord.lesson1.handler.Command; +import jkord.lesson1.util.*; + +public class Main +{ + static ConsoleApp app = new ConsoleApp("Lesson 1"); + + public void addCommands() + { + List commands = new ArrayList<>(); + + commands.add(new Command("Factorial", () -> factorialApp())); + commands.add(new Command("Fibonacci sequence", () -> sequenceFibonacci())); + commands.add(new Command("Numbers in words", () -> verbalNumber())); + + app.addSubApp(commands); + } + + private void factorialApp() { + app.printLn(" Factorial"); + app.printLn("Enter n: "); + app.printLn("Result: " + ExtraMath.factorial(app.read())); + } + + private void sequenceFibonacci() { + app.printLn(" Sequence Fibonacci"); + app.printLn("Enter n: "); + app.printLn("Result: " + Arrays.toString(ExtraMath.sequenceFibonacci(app.read()))); + } + + private void verbalNumber() { + app.printLn(" Numbers in words"); + app.printLn("Enter Number: "); + + app.printLn("Result: " + new VerbalNumber().toString(app.read())); + } + + public static void main(String[] args) + { + new Main().addCommands(); + app.processing(); + } +} \ No newline at end of file diff --git a/src/jkord/lesson1/handler/Action.java b/src/jkord/lesson1/handler/Action.java new file mode 100644 index 0000000..490fbe9 --- /dev/null +++ b/src/jkord/lesson1/handler/Action.java @@ -0,0 +1,6 @@ +package jkord.lesson1.handler; + +@FunctionalInterface +public interface Action { + public abstract void run(); +} diff --git a/src/jkord/lesson1/handler/Command.java b/src/jkord/lesson1/handler/Command.java new file mode 100644 index 0000000..e80600e --- /dev/null +++ b/src/jkord/lesson1/handler/Command.java @@ -0,0 +1,29 @@ +package jkord.lesson1.handler; + +public class Command +{ + private String name; + private Action action; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Command(String name) { + this.name = name; + } + + public Command(String name, Action action) { + this.name = name; + this.action = action; + } + + public void run() { + if (action != null) + action.run(); + } +} \ No newline at end of file diff --git a/src/jkord/lesson1/util/ExtraMath.java b/src/jkord/lesson1/util/ExtraMath.java new file mode 100644 index 0000000..fb423de --- /dev/null +++ b/src/jkord/lesson1/util/ExtraMath.java @@ -0,0 +1,32 @@ +package jkord.lesson1.util; + +import java.math.BigInteger; + +public class ExtraMath +{ + private static long factorial(int n, long acc) + { + return (n == 0)? acc : factorial(n - 1, acc * n); + } + + public static long factorial(int n) + { + return (n < 1)? 1 : factorial(n, 1); + } + + public static long[] sequenceFibonacci(int n) + { + if (n < 3) + return new long[] {1}; + + long sequence[] = new long[n]; + sequence[0] = 1; + sequence[1] = 1; + + for (int i = 2; i < n; i++) { + sequence[i] = sequence[i - 1] + sequence[i - 2]; + } + + return sequence; + } +} \ No newline at end of file diff --git a/src/jkord/lesson1/util/VerbalNumber.java b/src/jkord/lesson1/util/VerbalNumber.java new file mode 100644 index 0000000..fabca45 --- /dev/null +++ b/src/jkord/lesson1/util/VerbalNumber.java @@ -0,0 +1,82 @@ +package jkord.lesson1.util; + + +public class VerbalNumber +{ + final String[] str1 = {"zero","one","two","three","four","five","six","seven","eight","nine"}; + final String[] str11 = {"", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"}; + final String[] str10 = {"", "ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; + final String[] unit = {"thousands", "million", "billion", "trillion"}; + final String pref = "negative"; + + protected String getS1(int n) { + return str1[n]; + } + protected String getS11(int n) { + return str11[n]; + } + protected String getS10(int n) { + return str10[n]; + } + protected String getS100(int n) { + return str1[n] + " hundred"; + } + + protected String getUnit(long cnt) { + int cn = String.valueOf(cnt).length(); + if (cn < 4) + return ""; + + switch (cn) { + case 4: return unit[0]; + case 5: return unit[1]; + case 6: return unit[2]; + case 7: return unit[3]; + default: return "infinity"; + } + } + + protected String triadToString(int n) { + StringBuilder sb = new StringBuilder(); + + if (n % 1000 > 99) { + sb.append(getS100(n % 1000 / 100)); + sb.append(" "); + } + if (n % 100 > 10 && n % 100 < 20) { + sb.append(getS11(n % 10)); + sb.append(" "); + + return sb.toString(); + } + if (n % 100 > 9) { + sb.append(getS10(n % 100 / 10)); + sb.append(" "); + } + if (n % 10 > 0) { + sb.append(getS1(n % 10)); + sb.append(" "); + } + + return sb.toString(); + } + + public String toString(Number num) { + String str = ""; + if (num.longValue() == 0) + str = getS1(0); + + if (num.longValue() < 0) { + num = - num.longValue(); + str = pref; + } + + while (num.longValue() > 0) { + String triad = triadToString((int)(num.longValue() % 1000)); + str = triad + getUnit(num.longValue()) + " " + str; + num = num.longValue() / 1000; + } + + return str; + } +}