Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
out
13 changes: 13 additions & 0 deletions LearnJava.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="enigma-console" level="project" />
<orderEntry type="library" name="enigma-shell" level="project" />
</component>
</module>
Binary file added lib/enigma-console.jar
Binary file not shown.
129 changes: 129 additions & 0 deletions src/jkord/lesson1/ConsoleApp.java
Original file line number Diff line number Diff line change
@@ -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<Command> 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<Command> 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);
}
}
47 changes: 47 additions & 0 deletions src/jkord/lesson1/Main.java
Original file line number Diff line number Diff line change
@@ -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<Command> 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();
}
}
6 changes: 6 additions & 0 deletions src/jkord/lesson1/handler/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package jkord.lesson1.handler;

@FunctionalInterface
public interface Action {
public abstract void run();
}
29 changes: 29 additions & 0 deletions src/jkord/lesson1/handler/Command.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
32 changes: 32 additions & 0 deletions src/jkord/lesson1/util/ExtraMath.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
82 changes: 82 additions & 0 deletions src/jkord/lesson1/util/VerbalNumber.java
Original file line number Diff line number Diff line change
@@ -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;
}
}