Skip to content

Commit

Permalink
refactor: replace BufferedReader with Keyboard utility for input hand…
Browse files Browse the repository at this point in the history
…ling in DFAController and Menu
  • Loading branch information
Tony0380 committed Nov 2, 2024
1 parent e46de60 commit c8a7b5f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 89 deletions.
102 changes: 21 additions & 81 deletions app/src/main/java/computability/controller/DFAController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package computability.controller;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

Expand All @@ -18,31 +16,25 @@
public class DFAController {
private String input;
private DFA dfa;
private BufferedReader reader;

/**
* Wait for the user to press a key to continue.
*/
private void pressToContinue() {
System.out.println("Press any key to continue...");
try {
System.in.read();
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
}

public DFAController() {
reader = new BufferedReader(new InputStreamReader(System.in));
dfa = new DFA();
}

/**
* Create a new DFA menu for the given DFA.
* @param dfa The DFA to create the menu for.
*/
public DFAController(DFA dfa) {
this.dfa = dfa;
reader = new BufferedReader(new InputStreamReader(System.in));
}

/**
* Wait for the user to press a key to continue.
*/
private void pressToContinue() {
System.out.println("Press any key to continue...");
Keyboard.readChar();
}

/**
Expand All @@ -59,80 +51,54 @@ public int displayMenu() {
System.out.println("7. Load DFA");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
int choice = 0;
try {
choice = Integer.parseInt(reader.readLine());
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
return choice;
return Keyboard.readInt();
}

/**
* Check if a string is accepted by the DFA.
*/
public void checkString() {
System.out.print("Enter a string to check: ");
try {
input = reader.readLine();
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
input = Keyboard.readString();
if (dfa.accepts(input)) {
System.out.println("The string is accepted by the DFA.");
} else {
System.out.println("The string is not accepted by the DFA.");
}
pressToContinue();
}

/**
* Check the start state of the DFA.
*/
public void checkStartState() {
System.out.println("The start state of the DFA is: " + dfa.getStartState().getName());
pressToContinue();
}

/**
* Add a new state to the DFA.
*/
public void addState() {
System.out.print("Enter the name of the new state: ");
String name = "";
try {
name = reader.readLine();
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
String name = Keyboard.readString();
if (dfa.getState(name) != null) {
System.out.println("State with the same name already exists.");
return;
}
dfa.addState(name);
System.out.println("Is the state accepting? (y/n)");
String accepting = "";
try {
accepting = reader.readLine();
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
String accepting = Keyboard.readString();
if(dfa.getStartState() == null) {
System.out.println("Is the state the start state? (y/n)");
String start = "";
try {
start = reader.readLine();
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
String start = Keyboard.readString();
if (start.equals("y")) {
dfa.setStartState(dfa.getState(name));
}
}
if (accepting.equals("y")) {
dfa.setAccepting(name, true);
}
if (accepting.equals("y")) {
dfa.setAccepting(name, true);
}
System.out.println("State added correctly.");
pressToContinue();
}
Expand All @@ -142,28 +108,13 @@ public void addState() {
*/
public void addTransiction() {
System.out.print("Enter the name of the start state: ");
String start = "";
try {
start = reader.readLine();
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
String start = Keyboard.readString();
State get = dfa.getState(start);
System.out.print("Enter the name of the end state: ");
String end = "";
try {
end = reader.readLine();
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
String end = Keyboard.readString();
State endState = dfa.getState(end);
System.out.print("Enter the symbol of the transiction: ");
char symbol = ' ';
try {
symbol = reader.readLine().charAt(0);
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
char symbol = Keyboard.readChar();
if (get != null && endState != null) {
try {
dfa.addTransiction(get, endState, symbol);
Expand All @@ -189,18 +140,12 @@ public void printDFA() {
pressToContinue();
}


/**
* Save the DFA to a file.
*/
public void saveDFA() {
System.out.print("Enter the name of the file to save the DFA: ");
String filename = "";
try {
filename = reader.readLine();
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
String filename = Keyboard.readString();
FileOutputStream outFile;
try {
outFile = new FileOutputStream(filename);
Expand All @@ -217,12 +162,7 @@ public void saveDFA() {

public void loadDFA() {
System.out.print("Enter the name of the file to load the DFA: ");
String filename = "";
try {
filename = reader.readLine();
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
String filename = Keyboard.readString();
try {
FileInputStream inFile = new FileInputStream(filename);
ObjectInputStream inStream = new ObjectInputStream(inFile);
Expand Down
56 changes: 56 additions & 0 deletions app/src/main/java/computability/controller/Keyboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package computability.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Keyboard {

private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

public static String readString() {
try {
return reader.readLine();
} catch (Exception e) {
System.err.println("Error reading string: " + e.getMessage());
return "";
}
}

public static int readInt() {
try {
return Integer.parseInt(readString());
} catch (NumberFormatException e) {
System.err.println("Error parsing integer: " + e.getMessage());
return 0;
}
}

public static double readDouble() {
try {
return Double.parseDouble(readString());
} catch (NumberFormatException e) {
System.err.println("Error parsing double: " + e.getMessage());
return 0.0;
}
}

public static char readChar() {
String input = readString();
if (input.length() > 0) {
return input.charAt(0);
} else {
System.err.println("Error reading char: input is empty");
return '\0';
}
}

public static boolean readBoolean() {
try {
return Boolean.parseBoolean(readString());
} catch (Exception e) {
System.err.println("Error parsing boolean: " + e.getMessage());
return false;
}
}

}
11 changes: 3 additions & 8 deletions app/src/main/java/computability/view/Menu.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package computability.view;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import computability.controller.DFAController;
import computability.controller.Keyboard;

public class Menu {

private String choice;
Expand All @@ -26,12 +26,7 @@ private void welcome() {

private boolean selectOption() {
boolean exit = false;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
choice = reader.readLine();
} catch (Exception e) {
System.out.println("An error occurred. Please try again.");
}
choice = Keyboard.readString();
switch (choice) {
case "1":
DFAController dfaMenu = new DFAController();
Expand Down

0 comments on commit c8a7b5f

Please sign in to comment.