Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package cal;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- import java.io.*;
- import java.util.*;
- import static java.lang.System.*;
- public class CalculatorClient {
- private CalculatorClient() {}
- public static void main(String[] args) {
- String host = (args.length < 1) ? null : args[0];
- try {
- Registry registry = LocateRegistry.getRegistry(host);
- Calculator stub = (Calculator) registry.lookup("Calculator");
- //this section is going to try open a testing file (if found) and execute it
- try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
- String line;
- String token;
- int value;
- int response;
- boolean empty;
- //reading line by line
- while ((line = br.readLine()) != null) {
- StringTokenizer tokenizer = new StringTokenizer(line);
- while(tokenizer.hasMoreToken()) {
- token = tokenizer.nextToken();
- if(token == "+" ||token == "/" || token == "-" || token == "*") {
- stub.pushOperator(token);
- }
- else if(token == "pop") {
- response = stub.pop();
- System.out.println(response);
- }
- else if(token == "delay") {
- token = tokenizer.nextToken();
- value = Integer.parseInt(token);
- stub.delayPop(value);
- }
- else if(token == "empty") {
- empty = stub.isEmpty();
- System.out.println(empty);
- }
- else {
- try{
- //check if the token is a integer
- int num = Integer.parseInt(token);
- stub.pushValue(num);
- } catch (NumberFormatException error) {
- System.err.println("wrong input");
- }
- }
- }
- //end of line is reached, so pop everything
- response = stub.pop();
- System.out.println(response);
- }
- br.close();
- } catch(IOException error) {
- System.err.println("File not found");
- }
- } catch (Exception error) {
- System.err.println("Client exception: " + error.toString());
- error.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement