Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.rmi.Naming;
- /**
- * Created by Nivesh Varma on 19 Mar 17.
- */
- public class CommandLineTeller {
- public static void main(String[] args) throws Exception {
- TellerServer server = null;
- if (args.length == 0)
- {
- server = (TellerServer) Naming.lookup("TellerServices");
- System.out.println("The client has connected to the server at localhost.");
- }
- else
- {
- server = (TellerServer) Naming.lookup("rmi://" + args[0] + "/TellerServices");
- System.out.println("The client has connected to the server at " + args[0]);
- }
- if (server == null) System.out.println("The client can't find the server!");
- InputStreamReader isr = new InputStreamReader(System.in); //point to keyboard for input
- BufferedReader kb = new BufferedReader(isr); //point to isr for input
- try {
- while (true) {
- System.out.println("Please enter a command:");
- String input = kb.readLine(); // read keyboard input
- String tempUserInput = input;
- String tempUserInput2 = input;
- String tempUserInput3 = input;
- int parameters = tempUserInput.length() - tempUserInput.replace(",", "").length() + 1;
- int parentheses = (tempUserInput2.length()
- - tempUserInput2.replace("(", "").length())
- + (tempUserInput3.length() - tempUserInput3.replace(")", "").length());
- if (input.contains(" ")) {
- System.out.println("ERROR: Whitespace character detected");
- }
- // openNewAccount(String accountType, String customerName)
- else if (input.startsWith("openNewAccount(")
- && (input.endsWith(")") || input.endsWith(");")))
- {
- if (parameters!=2 || parentheses!=2) {
- System.out.println("ERROR: Incorrect number of parameters or bad format");
- } else {
- int commaOffset = input.indexOf(",");
- String parameter1 = input.substring(15, commaOffset);
- if(!parameter1.equalsIgnoreCase("checking")
- && !parameter1.equalsIgnoreCase("savings")) {
- System.out.println("ERROR: Invalid account type");
- } else {
- int rightParenOffset = input.indexOf(")");
- String parameter2 = input.substring(commaOffset + 1, rightParenOffset);
- String serverReply = server.openNewAccount(parameter1, parameter2);
- System.out.println(serverReply);
- }
- }
- }
- // closeOutAccount(int accountNumber, String customerName)
- else if (input.startsWith("closeOutAccount(")
- && (input.endsWith(")") || input.endsWith(");")))
- {
- if (parameters!=2 || parentheses!=2) {
- System.out.println("ERROR: Incorrect number of parameters or bad format");
- } else {
- int commaOffset = input.indexOf(",");
- String parameter1 = input.substring(16,commaOffset);
- int p1_int = Integer.parseInt(parameter1);
- if (p1_int <= 0) {
- System.out.println("ERROR: Invalid account number");
- } else {
- int rightParenOffset = input.indexOf(")");
- String parameter2 = input.substring(commaOffset+1, rightParenOffset);
- String serverReply = server.closeOutAccount(p1_int,parameter2);
- System.out.println(serverReply);
- }
- }
- }
- // processAccount(String processType, int accountNumber, double amount)
- else if (input.startsWith("processAccount")
- && (input.endsWith(")") || input.endsWith(");")))
- {
- if (parameters!=3 || parentheses!=2) {
- System.out.println("ERROR: Incorrect number of parameters or bad format");
- } else {
- int commaOffset1 = input.indexOf(",");
- String parameter1 = input.substring(15, commaOffset1);
- if(!parameter1.equalsIgnoreCase("deposit")
- && !parameter1.equalsIgnoreCase("withdraw")) {
- System.out.println("ERROR: Invalid account type");
- } else {
- int commaOffset2 = input.indexOf(",", commaOffset1 + 1);
- String parameter2 = input.substring(commaOffset1 + 1, commaOffset2);
- int p2_int = Integer.parseInt(parameter2);
- if (p2_int <= 0) {
- System.out.println("ERROR: Invalid account number");
- } else {
- int rightParenOffset = input.indexOf(")");
- String parameter3 = input.substring(commaOffset2 + 1, rightParenOffset);
- double p3_double = Double.parseDouble(parameter3);
- if (p2_int <= 0) {
- System.out.println("ERROR: Invalid amount");
- } else {
- String serverReply = server.processAccount(parameter1, p2_int, p3_double);
- System.out.println(serverReply);
- }
- }
- }
- }
- }
- // showAccount (int accountNumber)
- else if (input.startsWith("showAccount(")
- && (input.endsWith(")") || input.endsWith(");")))
- {
- int rightParenOffset = input.indexOf(")");
- String parameter1 = input.substring(12,rightParenOffset);
- int p1_int = Integer.parseInt(parameter1);
- String serverReply = server.showAccount(p1_int);
- System.out.println(serverReply);
- }
- // showAccounts (String customerName)
- else if (input.startsWith("showAccounts")
- && (input.endsWith(")") || input.endsWith(");")))
- {
- int rightParenOffset = input.indexOf(")");
- String parameter1 = input.substring(13,rightParenOffset);
- String serverReply = server.showAccounts(parameter1);
- System.out.println(serverReply);
- }
- else {
- System.out.println("ERROR: Method not recognized!");
- }
- }
- } catch (IOException ioe) {
- System.out.println(ioe);
- }
- /*try {
- while (true) {
- System.out.println("Please enter a command:");
- String userInput = kb.readLine();
- if (userInput.contains(" ")) {
- System.out.println("ERROR: Whitespace character detected");
- return;
- }
- String tempUserInput = userInput;
- String tempUserInput2 = userInput;
- String tempUserInput3 = userInput;
- int parameters = tempUserInput.length() - tempUserInput.replace(",", "").length() + 1;
- int parentheses = (tempUserInput2.length()
- - tempUserInput2.replace("(", "").length())
- + (tempUserInput3.length() - tempUserInput3.replace(")", "").length());
- if (userInput.startsWith("openNewAccount(")
- && (userInput.endsWith(")") || userInput.endsWith(");"))) {
- // System.out.println("\"openNewAccount(***,***)\" entered");
- // System.out.println(parentheses + " parentheses entered");
- if (parameters!=2 || parentheses!=2) {
- System.out.println("ERROR: Incorrect number of parameters");
- } else System.out.println("COUNT CORRECT");
- }
- else if (userInput.startsWith("closeOutAccount(")
- && (userInput.endsWith(")") || userInput.endsWith(");"))) {
- // System.out.println("\"closeOutAccount\" entered");
- if (parameters!=2 || parentheses!=2) {
- System.out.println("ERROR: Incorrect number of parameters");
- } else System.out.println("COUNT CORRECT");
- }
- else if (userInput.startsWith("processAccount(")
- && (userInput.endsWith(")") || userInput.endsWith(");"))) {
- // System.out.println("\"processAccount\" entered");
- if (parameters!=3 || parentheses!=2) {
- System.out.println("ERROR: Incorrect number of parameters");
- } else System.out.println("COUNT CORRECT");
- }
- else if (userInput.startsWith("showAccount(")
- && (userInput.endsWith(")") || userInput.endsWith(");"))) {
- // System.out.println("\"showAccount\" entered");
- if (parameters!=1 || parentheses!=2) {
- System.out.println("ERROR: Incorrect number of parameters");
- } else System.out.println("COUNT CORRECT");
- }
- else if (userInput.startsWith("showAccounts(")
- && (userInput.endsWith(")") || userInput.endsWith(");"))) {
- // System.out.println("\"showAccounts\" entered");
- if (parameters!=1 || parentheses!=2) {
- System.out.println("ERROR: Incorrect number of parameters");
- } else System.out.println("COUNT CORRECT");
- }
- else {
- System.out.println("ERROR: Method not recognized!");
- return;
- }
- }
- } catch (IOException ioe) {}*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement