started task 7
This commit is contained in:
@@ -39,23 +39,43 @@ public class Client {
|
||||
XmlRpcClient client = new XmlRpcClient();
|
||||
client.setConfig(config);
|
||||
|
||||
InputHandler handler = new InputHandler();
|
||||
|
||||
while (true) {
|
||||
// create parameters to be sent to server method in a vector
|
||||
// and populate it
|
||||
Vector params = new Vector();
|
||||
|
||||
int a = validateInput(scanner);
|
||||
int b = validateInput(scanner);
|
||||
// get two integers from user
|
||||
// promptForInt returns null if user wants to exit
|
||||
Integer a = handler.promptForInt(scanner);
|
||||
if (a == null) { // user wants to exit
|
||||
notifyExit();
|
||||
break;
|
||||
}
|
||||
Integer b = handler.promptForInt(scanner);
|
||||
if (b == null) { // user wants to exit
|
||||
notifyExit();
|
||||
break;
|
||||
}
|
||||
|
||||
// get command from user
|
||||
String command = handler.promptForCommand(scanner);
|
||||
if (command.equals("exit")) { // user wants to exit
|
||||
notifyExit();
|
||||
break;
|
||||
}
|
||||
|
||||
// add parameters to vector
|
||||
params.addElement(Integer.valueOf(a));
|
||||
params.addElement(Integer.valueOf(b));
|
||||
// make the remote method call
|
||||
// the result of the remot call must be an Object
|
||||
// and be casted to the appropriate type
|
||||
Object result = client.execute("sample.add", params);
|
||||
Object result = client.execute("calculator." + command, params);
|
||||
// process the result
|
||||
int sum = ((Integer) result).intValue();
|
||||
System.out.println("The sum is: " + sum);
|
||||
System.out.println("The " + handler.mapCommand(command) + " of " + a + " and " + b + " is: " + sum);
|
||||
|
||||
}
|
||||
|
||||
@@ -64,19 +84,8 @@ public class Client {
|
||||
}
|
||||
}
|
||||
|
||||
private static int validateInput(Scanner scanner) {
|
||||
int number;
|
||||
while (true) {
|
||||
System.out.print("Enter an integer: ");
|
||||
if (scanner.hasNextInt()) {
|
||||
number = scanner.nextInt();
|
||||
break;
|
||||
} else {
|
||||
System.out.println("Invalid input. Please enter a valid integer.");
|
||||
scanner.next();
|
||||
}
|
||||
}
|
||||
return number;
|
||||
private static void notifyExit() {
|
||||
System.out.println("Exit command received. Exiting...");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
127
week3_TinsaeGhilay/Task 2/src/client/InputHandler.java
Normal file
127
week3_TinsaeGhilay/Task 2/src/client/InputHandler.java
Normal file
@@ -0,0 +1,127 @@
|
||||
package client;
|
||||
|
||||
/**
|
||||
* InputHandler class
|
||||
*
|
||||
* @author Tinsae Ghilay
|
||||
* for handling user inputs
|
||||
*/
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class InputHandler {
|
||||
|
||||
// list of valid commands
|
||||
private String[] commands = {
|
||||
"add",
|
||||
"subtract",
|
||||
"multiply",
|
||||
"divide",
|
||||
"mod",
|
||||
"power",
|
||||
"root",
|
||||
"exit"
|
||||
};
|
||||
|
||||
/**
|
||||
* method to prompt user for a valid integer
|
||||
*
|
||||
* @param scanner Scanner object for user input
|
||||
* @return valid integer input from user
|
||||
*/
|
||||
public Integer promptForInt(Scanner scanner) {
|
||||
Integer number;
|
||||
while (true) {
|
||||
System.out.print("Enter an integer: ");
|
||||
if (scanner.hasNextInt()) {
|
||||
number = scanner.nextInt();
|
||||
scanner.nextLine(); // consume the newline
|
||||
break;
|
||||
} else {
|
||||
String command = scanner.nextLine();
|
||||
// check if user wants to exit and return null
|
||||
if (command.equalsIgnoreCase("exit")) {
|
||||
return null;
|
||||
}
|
||||
System.out.println("Invalid input. Please enter a valid integer.");
|
||||
}
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
/**
|
||||
* method to prompt user for a valid command
|
||||
*
|
||||
* @param scanner Scanner object for user input
|
||||
* @return valid command input from user
|
||||
*/
|
||||
public String promptForCommand(Scanner scanner) {
|
||||
System.out.print("Enter command (add, subtract, multiply, divide, mod, power, root, exit): ");
|
||||
while (true) {
|
||||
String command = scanner.nextLine().trim().toLowerCase();
|
||||
if (isValidCommand(command)) {
|
||||
return command;
|
||||
} else {
|
||||
System.out.print("Invalid command. Please enter a valid command: ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* helper method to check if command is valid
|
||||
*
|
||||
* @param command
|
||||
* @return
|
||||
*/
|
||||
private boolean isValidCommand(String command) {
|
||||
return Arrays.asList(commands).contains(command.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for commands
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String[] getCommands() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for command at index
|
||||
*
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public String getCommand(int index) {
|
||||
return commands[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* method to map user command to result term
|
||||
*
|
||||
* @param command
|
||||
* @return
|
||||
*/
|
||||
public String mapCommand(String command) {
|
||||
switch (command.toLowerCase()) {
|
||||
case "add":
|
||||
return "sum";
|
||||
case "subtract":
|
||||
return "difference";
|
||||
case "multiply":
|
||||
return "product";
|
||||
case "divide":
|
||||
return "quotient";
|
||||
case "mod":
|
||||
return "mod";
|
||||
case "power":
|
||||
return "power";
|
||||
case "root":
|
||||
return "root";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
91
week3_TinsaeGhilay/Task 2/src/server/Executor.java
Normal file
91
week3_TinsaeGhilay/Task 2/src/server/Executor.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package server;
|
||||
|
||||
/**
|
||||
* Executor class for XML-RPC communication
|
||||
*
|
||||
* @author Tinsae Ghilay
|
||||
* based on tutorial from
|
||||
* https://www.tutorialspoint.com/xml-rpc/xml_rpc_examples.htm
|
||||
*/
|
||||
public class Executor {
|
||||
|
||||
// define methods that can be called remotely
|
||||
|
||||
/**
|
||||
* sum of two integers
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
public Integer add(Integer x, Integer y) {
|
||||
return Integer.valueOf(x + y);
|
||||
}
|
||||
|
||||
/**
|
||||
* difference of two integers x - y
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
public Integer subtract(Integer x, Integer y) {
|
||||
return Integer.valueOf(x - y);
|
||||
}
|
||||
|
||||
/**
|
||||
* product of two integers
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
public Integer multiply(Integer x, Integer y) {
|
||||
return Integer.valueOf(x * y);
|
||||
}
|
||||
|
||||
/**
|
||||
* division of two integers x/y
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
public Double divide(Integer x, Integer y) {
|
||||
return Double.valueOf((double) x / y);
|
||||
}
|
||||
|
||||
/**
|
||||
* modulus of two integers
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
public Integer mod(Integer x, Integer y) {
|
||||
return Integer.valueOf(x % y);
|
||||
}
|
||||
|
||||
/**
|
||||
* power of two integers x^y
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
public Integer power(Integer x, Integer y) {
|
||||
return Integer.valueOf((int) Math.pow(x, y));
|
||||
}
|
||||
|
||||
/**
|
||||
* root of two integers X√Y
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
public Integer root(Integer x, Integer y) {
|
||||
return Integer.valueOf((int) Math.pow(x, 1.0 / y));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,20 +24,24 @@ public class Server {
|
||||
|
||||
// start XML-RPC server at port 8080
|
||||
WebServer server = new WebServer(8080);
|
||||
|
||||
XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
|
||||
|
||||
PropertyHandlerMapping phm = new PropertyHandlerMapping();
|
||||
phm.addHandler("sample", Server.class);
|
||||
|
||||
phm.addHandler("calculator", Executor.class);
|
||||
|
||||
xmlRpcServer.setHandlerMapping(phm);
|
||||
|
||||
server.start();
|
||||
|
||||
System.out.println(TAG + ": Server started successfully.");
|
||||
|
||||
System.out.println("Accepting requests. (Halt program to stop.)");
|
||||
|
||||
} catch (Exception e) { // catch exceptions if problems occur
|
||||
System.err.println(TAG + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public Integer sum(Integer x, Integer y) {
|
||||
return Integer.valueOf(x + y);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user