Renamed NetBuilder to InteractiveConsole for interactive net building and update Main to use it
This commit is contained in:
@@ -12,16 +12,16 @@ import net.berack.upo.valpre.sim.ServerNode;
|
||||
* Interactive net builder. This class allows the user to build a net by adding
|
||||
* nodes and connections. The user can also save the net to a file.
|
||||
*/
|
||||
public class NetBuilderInteractive {
|
||||
public class InteractiveConsole {
|
||||
|
||||
private final Net net = new Net();
|
||||
private Net net = new Net();
|
||||
private final PrintStream out;
|
||||
private final Scanner scanner;
|
||||
|
||||
/**
|
||||
* Create a new interactive net builder. Uses System.in and System.out.
|
||||
*/
|
||||
public NetBuilderInteractive() {
|
||||
public InteractiveConsole() {
|
||||
this(System.out, System.in);
|
||||
}
|
||||
|
||||
@@ -31,21 +31,20 @@ public class NetBuilderInteractive {
|
||||
* @param out the output stream
|
||||
* @param in the input stream
|
||||
*/
|
||||
public NetBuilderInteractive(PrintStream out, InputStream in) {
|
||||
public InteractiveConsole(PrintStream out, InputStream in) {
|
||||
this.out = out;
|
||||
this.scanner = new Scanner(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the interactive net builder.
|
||||
*
|
||||
* @param args the arguments
|
||||
*/
|
||||
public Net run() {
|
||||
public Net runNetBuilder() {
|
||||
while (true) {
|
||||
try {
|
||||
var choice = choose("Choose the next step to do:",
|
||||
"Add a node", "Add a connection", "Print Nodes", "Save the net", "Exit");
|
||||
"Add a node", "Add a connection", "Print Nodes", "Save the net", "Load net", "Clear",
|
||||
"Exit");
|
||||
switch (choice) {
|
||||
case 1 -> {
|
||||
var node = this.buildNode();
|
||||
@@ -31,10 +31,7 @@ public class Main {
|
||||
var plot = new Plot(csv);
|
||||
plot.show();
|
||||
}
|
||||
case "net" -> {
|
||||
var net = new NetBuilderInteractive();
|
||||
net.run();
|
||||
}
|
||||
case "net" -> new InteractiveConsole().runNetBuilder();
|
||||
default -> exit("Invalid program!");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.berack.upo.valpre.NetBuilderInteractive;
|
||||
import net.berack.upo.valpre.InteractiveConsole;
|
||||
import net.berack.upo.valpre.rand.Distribution;
|
||||
|
||||
public class TestInteractions {
|
||||
@@ -104,31 +104,31 @@ public class TestInteractions {
|
||||
var inputs = List.of("5");
|
||||
var bytes = String.join("\n", inputs).getBytes();
|
||||
var in = new ByteArrayInputStream(bytes);
|
||||
var net = new NetBuilderInteractive(out, in).run();
|
||||
var net = new InteractiveConsole(out, in).runNetBuilder();
|
||||
assertEquals("", net.toString());
|
||||
|
||||
inputs = List.of("1", "1", "Source", "1", "1.0", "5");
|
||||
inputs = List.of("1", "1", "Source", "1", "1.0", "7");
|
||||
bytes = String.join("\n", inputs).getBytes();
|
||||
in = new ByteArrayInputStream("1\n1\nSource\n1\n1.0\n1000\n5\n".getBytes());
|
||||
net = new NetBuilderInteractive(out, in).run();
|
||||
net = new InteractiveConsole(out, in).runNetBuilder();
|
||||
assertEquals("Source[servers:1, queue:100, spawn:-1, Exponential(1.0)] -\n", net.toString());
|
||||
|
||||
inputs = List.of("1", "2", "Terminal", "1", "2.0", "500",
|
||||
"1", "3", "Queue", "5", "3.2", "0.6", "1",
|
||||
"5");
|
||||
"7");
|
||||
bytes = String.join("\n", inputs).getBytes();
|
||||
in = new ByteArrayInputStream(bytes);
|
||||
net = new NetBuilderInteractive(out, in).run();
|
||||
net = new InteractiveConsole(out, in).runNetBuilder();
|
||||
assertEquals("Terminal[servers:1, queue:100, spawn:500, Exponential(2.0)] -\n"
|
||||
+ "Queue[servers:1, queue:100, spawn:0, Normal(3.2, 0.6)] -\n", net.toString());
|
||||
|
||||
inputs = List.of("1", "1", "Source", "1", "2.0",
|
||||
"1", "3", "Queue", "5", "3.2", "0.6", "1",
|
||||
"2", "Source", "Queue", "1.0",
|
||||
"5");
|
||||
"7");
|
||||
bytes = String.join("\n", inputs).getBytes();
|
||||
in = new ByteArrayInputStream(bytes);
|
||||
net = new NetBuilderInteractive(out, in).run();
|
||||
net = new InteractiveConsole(out, in).runNetBuilder();
|
||||
assertEquals("Source[servers:1, queue:100, spawn:-1, Exponential(2.0)] -> Queue(1.0)\n"
|
||||
+ "Queue[servers:1, queue:100, spawn:0, Normal(3.2, 0.6)] -\n", net.toString());
|
||||
}
|
||||
@@ -156,6 +156,8 @@ public class TestInteractions {
|
||||
* - Enter the weight: 1.0
|
||||
* 3. Print Nodes
|
||||
* 4. Save the net
|
||||
* 5. Exit
|
||||
* 5. Load net
|
||||
* 6. Clear
|
||||
* 7. Exit
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user