Skip to content

Commit

Permalink
feat: implement Practice 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed Mar 11, 2024
1 parent cb72a06 commit 7ce43da
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 17 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
Expand Down
126 changes: 109 additions & 17 deletions src/main/java/me/alex4386/gachon/sw14462/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,124 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
public static String currentTarget = "day02";
public static String currentTarget = "day03";

public static void main(String[] args) {
String target = currentTarget;
Class<?> klass = null;
public static Map<String, Class<?>> getAvailableTargetClassNames() {
Map<String, Class<?>> classes = new HashMap<>();

for (int i = 1; i <= 99; i++) {
String className = String.format(Main.class.getPackage().getName() + ".day%02d.Main", i);

try {
Class<?> klass = Class.forName(className);
classes.put(String.format("day%02d", i), klass);
} catch(ClassNotFoundException e) {
continue;
}
}

return classes;
}

public static void launchCurrentTarget(String[] args) {
Map<String, Class<?>> classes = Main.getAvailableTargetClassNames();

if (classes.containsKey(currentTarget)) {
Class<?> klass = classes.get(currentTarget);
if (klass != null) {
Main.launchGivenClass(klass, args);
}
}
}

public static void launchGivenClass(Class<?> klass, String[] args) {
Method mainMethod;

try {
klass = Class.forName(Main.class.getPackage().getName()+"."+target+".Main");
} catch(ClassNotFoundException e) {
System.err.println("Failed to fetch class");
System.exit(1);
mainMethod = klass.getMethod("main", String[].class);
} catch (NoSuchMethodException e) {
System.out.println("Main method not found in the target class.");
return;
}

Method mainMethod = null;
try {
mainMethod = klass.getDeclaredMethod("main", String[].class);
} catch(NoSuchMethodException ignored) {}
mainMethod.invoke(null, (Object) args);
} catch (InvocationTargetException e) {
System.out.println("Failed to invoke main method.");
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
return;
}
}

if (mainMethod != null) {
try {
Object[] invokeArgs = { args };
mainMethod.invoke(klass, invokeArgs);
} catch(IllegalAccessException | InvocationTargetException e) {
System.exit(1);
public static void main(String[] args) {
Class<?> klass;
if (currentTarget.length() > 0) {
Main.launchCurrentTarget(args);
return;
}

if (args.length >= 1) {
Map<String, Class<?>> classes = Main.getAvailableTargetClassNames();
currentTarget = args[0];

if (classes.containsKey(currentTarget)) {
klass = classes.get(currentTarget);
List<String> newArgs = new ArrayList<>();
if (args.length >= 1) {
for (int i = 1; i < args.length; i++) {
newArgs.add(args[i]);
}
}

String[] newArgsArray = (String[]) newArgs.toArray();
Main.launchGivenClass(klass, newArgsArray);
return;
}
}

if (System.console() != null) {
askUserForLaunch(args);
} else {
System.out.println("Console not available for loading specific class. Exiting.");
System.exit(1);
}
}

public static void askUserForLaunch(String[] args) {
Map<String, Class<?>> classes = Main.getAvailableTargetClassNames();

// list all classes and make user select one
System.out.println("Available classes:");
for (String className : classes.keySet()) {
System.out.println("* " + className);
}

while (true) {
System.out.println("");
System.out.print("Class name to launch (Enter \"exit\" to exit): ");
String input = System.console().readLine();

if (input.equalsIgnoreCase("exit")) {
break;
}

if (classes.containsKey(input)) {
System.out.println("Launching " + input + "...");
System.out.println("");

currentTarget = input;
Main.launchCurrentTarget(args);
break;
} else {
System.out.println("Invalid class name.");
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day03/DigitParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.alex4386.gachon.sw14462.day03;

public class DigitParser {
protected int integer;
protected int currentDigitPow;

public DigitParser(int integer) {
this.integer = integer;
this.currentDigitPow = DigitParser.digitPow(integer) - 1;
}

public static int digitPow(int integer) {
return String.valueOf(integer).length();
}

public int nextDigit() {
if (this.currentDigitPow < 0) {
return -1;
}

int digit = (int) ((this.integer / Math.pow(10, this.currentDigitPow)) % 10);
this.currentDigitPow--;
return digit;
}
}
19 changes: 19 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day03/EggBasket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.alex4386.gachon.sw14462.day03;

public class EggBasket {
// At the beginning of the section enclosed by curly braces `{}`
public static void main(String[] args) {
// A variable must be declared before it is used
int numberOfBaskets, eggsPerBasket, totalEggs; // variable declaration

numberOfBaskets = 20; // assignment statement
eggsPerBasket = 18;

totalEggs = numberOfBaskets * eggsPerBasket;

System.out.println("If you have");
System.out.println(eggsPerBasket + " eggs per basket and");
System.out.println(numberOfBaskets + " baskets, then");
System.out.println("the total number of eggs is " + totalEggs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day03;

public class FahrenheitConverter {
public static double toCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
}
40 changes: 40 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day03/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.alex4386.gachon.sw14462.day03;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Exercise 2_1. Read a four-digit integer and display one digit per line");
Main.exercise2_1();

System.out.println("");
System.out.println("Exercise 2_2. Convert Fahrenheit to Celsius");
Main.exercise2_2();
}

public static void exercise2_1() {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a four-digit integer: ");
int integer = scanner.nextInt();

DigitParser parser = new DigitParser(integer);
while (true) {
int digit = parser.nextDigit();
if (digit == -1) {
break;
}
System.out.println(digit);
}
}

public static void exercise2_2() {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a temperature in Fahrenheit: ");
double fahrenheit = scanner.nextDouble();

double celsius = FahrenheitConverter.toCelsius(fahrenheit);
System.out.printf("The temperature in Celsius is %.02f°C.\n", celsius);
}
}

0 comments on commit 7ce43da

Please sign in to comment.