Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.39 KB | None | 0 0
  1. import java.io.*;
  2. import java.rmi.Naming;
  3.  
  4. /**
  5. * Created by Nivesh Varma on 19 Mar 17.
  6. */
  7. public class CommandLineTeller {
  8. public static void main(String[] args) throws Exception {
  9. TellerServer server = null;
  10. if (args.length == 0)
  11. {
  12. server = (TellerServer) Naming.lookup("TellerServices");
  13. System.out.println("The client has connected to the server at localhost.");
  14. }
  15. else
  16. {
  17. server = (TellerServer) Naming.lookup("rmi://" + args[0] + "/TellerServices");
  18. System.out.println("The client has connected to the server at " + args[0]);
  19. }
  20. if (server == null) System.out.println("The client can't find the server!");
  21.  
  22. InputStreamReader isr = new InputStreamReader(System.in); //point to keyboard for input
  23. BufferedReader kb = new BufferedReader(isr); //point to isr for input
  24.  
  25. try {
  26. while (true) {
  27. System.out.println("Please enter a command:");
  28. String input = kb.readLine(); // read keyboard input
  29.  
  30. String tempUserInput = input;
  31. String tempUserInput2 = input;
  32. String tempUserInput3 = input;
  33.  
  34. int parameters = tempUserInput.length() - tempUserInput.replace(",", "").length() + 1;
  35. int parentheses = (tempUserInput2.length()
  36. - tempUserInput2.replace("(", "").length())
  37. + (tempUserInput3.length() - tempUserInput3.replace(")", "").length());
  38.  
  39. if (input.contains(" ")) {
  40. System.out.println("ERROR: Whitespace character detected");
  41. }
  42.  
  43. // openNewAccount(String accountType, String customerName)
  44. else if (input.startsWith("openNewAccount(")
  45. && (input.endsWith(")") || input.endsWith(");")))
  46. {
  47. if (parameters!=2 || parentheses!=2) {
  48. System.out.println("ERROR: Incorrect number of parameters or bad format");
  49. } else {
  50. int commaOffset = input.indexOf(",");
  51. String parameter1 = input.substring(15, commaOffset);
  52. if(!parameter1.equalsIgnoreCase("checking")
  53. && !parameter1.equalsIgnoreCase("savings")) {
  54. System.out.println("ERROR: Invalid account type");
  55. } else {
  56. int rightParenOffset = input.indexOf(")");
  57. String parameter2 = input.substring(commaOffset + 1, rightParenOffset);
  58. String serverReply = server.openNewAccount(parameter1, parameter2);
  59. System.out.println(serverReply);
  60. }
  61. }
  62. }
  63.  
  64. // closeOutAccount(int accountNumber, String customerName)
  65. else if (input.startsWith("closeOutAccount(")
  66. && (input.endsWith(")") || input.endsWith(");")))
  67. {
  68. if (parameters!=2 || parentheses!=2) {
  69. System.out.println("ERROR: Incorrect number of parameters or bad format");
  70. } else {
  71. int commaOffset = input.indexOf(",");
  72. String parameter1 = input.substring(16,commaOffset);
  73. int p1_int = Integer.parseInt(parameter1);
  74. if (p1_int <= 0) {
  75. System.out.println("ERROR: Invalid account number");
  76. } else {
  77. int rightParenOffset = input.indexOf(")");
  78. String parameter2 = input.substring(commaOffset+1, rightParenOffset);
  79. String serverReply = server.closeOutAccount(p1_int,parameter2);
  80. System.out.println(serverReply);
  81. }
  82. }
  83. }
  84.  
  85. // processAccount(String processType, int accountNumber, double amount)
  86. else if (input.startsWith("processAccount")
  87. && (input.endsWith(")") || input.endsWith(");")))
  88. {
  89. if (parameters!=3 || parentheses!=2) {
  90. System.out.println("ERROR: Incorrect number of parameters or bad format");
  91. } else {
  92. int commaOffset1 = input.indexOf(",");
  93. String parameter1 = input.substring(15, commaOffset1);
  94. if(!parameter1.equalsIgnoreCase("deposit")
  95. && !parameter1.equalsIgnoreCase("withdraw")) {
  96. System.out.println("ERROR: Invalid account type");
  97. } else {
  98. int commaOffset2 = input.indexOf(",", commaOffset1 + 1);
  99. String parameter2 = input.substring(commaOffset1 + 1, commaOffset2);
  100. int p2_int = Integer.parseInt(parameter2);
  101. if (p2_int <= 0) {
  102. System.out.println("ERROR: Invalid account number");
  103. } else {
  104. int rightParenOffset = input.indexOf(")");
  105. String parameter3 = input.substring(commaOffset2 + 1, rightParenOffset);
  106. double p3_double = Double.parseDouble(parameter3);
  107. if (p2_int <= 0) {
  108. System.out.println("ERROR: Invalid amount");
  109. } else {
  110. String serverReply = server.processAccount(parameter1, p2_int, p3_double);
  111. System.out.println(serverReply);
  112. }
  113. }
  114. }
  115. }
  116. }
  117.  
  118. // showAccount (int accountNumber)
  119. else if (input.startsWith("showAccount(")
  120. && (input.endsWith(")") || input.endsWith(");")))
  121. {
  122. int rightParenOffset = input.indexOf(")");
  123. String parameter1 = input.substring(12,rightParenOffset);
  124. int p1_int = Integer.parseInt(parameter1);
  125. String serverReply = server.showAccount(p1_int);
  126. System.out.println(serverReply);
  127. }
  128.  
  129. // showAccounts (String customerName)
  130. else if (input.startsWith("showAccounts")
  131. && (input.endsWith(")") || input.endsWith(");")))
  132. {
  133. int rightParenOffset = input.indexOf(")");
  134. String parameter1 = input.substring(13,rightParenOffset);
  135. String serverReply = server.showAccounts(parameter1);
  136. System.out.println(serverReply);
  137. }
  138.  
  139. else {
  140. System.out.println("ERROR: Method not recognized!");
  141. }
  142. }
  143. } catch (IOException ioe) {
  144. System.out.println(ioe);
  145. }
  146.  
  147. /*try {
  148. while (true) {
  149. System.out.println("Please enter a command:");
  150. String userInput = kb.readLine();
  151. if (userInput.contains(" ")) {
  152. System.out.println("ERROR: Whitespace character detected");
  153. return;
  154. }
  155. String tempUserInput = userInput;
  156. String tempUserInput2 = userInput;
  157. String tempUserInput3 = userInput;
  158.  
  159. int parameters = tempUserInput.length() - tempUserInput.replace(",", "").length() + 1;
  160. int parentheses = (tempUserInput2.length()
  161. - tempUserInput2.replace("(", "").length())
  162. + (tempUserInput3.length() - tempUserInput3.replace(")", "").length());
  163.  
  164. if (userInput.startsWith("openNewAccount(")
  165. && (userInput.endsWith(")") || userInput.endsWith(");"))) {
  166. // System.out.println("\"openNewAccount(***,***)\" entered");
  167. // System.out.println(parentheses + " parentheses entered");
  168. if (parameters!=2 || parentheses!=2) {
  169. System.out.println("ERROR: Incorrect number of parameters");
  170. } else System.out.println("COUNT CORRECT");
  171. }
  172.  
  173. else if (userInput.startsWith("closeOutAccount(")
  174. && (userInput.endsWith(")") || userInput.endsWith(");"))) {
  175. // System.out.println("\"closeOutAccount\" entered");
  176. if (parameters!=2 || parentheses!=2) {
  177. System.out.println("ERROR: Incorrect number of parameters");
  178. } else System.out.println("COUNT CORRECT");
  179. }
  180.  
  181. else if (userInput.startsWith("processAccount(")
  182. && (userInput.endsWith(")") || userInput.endsWith(");"))) {
  183. // System.out.println("\"processAccount\" entered");
  184. if (parameters!=3 || parentheses!=2) {
  185. System.out.println("ERROR: Incorrect number of parameters");
  186. } else System.out.println("COUNT CORRECT");
  187. }
  188.  
  189. else if (userInput.startsWith("showAccount(")
  190. && (userInput.endsWith(")") || userInput.endsWith(");"))) {
  191. // System.out.println("\"showAccount\" entered");
  192. if (parameters!=1 || parentheses!=2) {
  193. System.out.println("ERROR: Incorrect number of parameters");
  194. } else System.out.println("COUNT CORRECT");
  195. }
  196.  
  197. else if (userInput.startsWith("showAccounts(")
  198. && (userInput.endsWith(")") || userInput.endsWith(");"))) {
  199. // System.out.println("\"showAccounts\" entered");
  200. if (parameters!=1 || parentheses!=2) {
  201. System.out.println("ERROR: Incorrect number of parameters");
  202. } else System.out.println("COUNT CORRECT");
  203. }
  204.  
  205. else {
  206. System.out.println("ERROR: Method not recognized!");
  207. return;
  208. }
  209. }
  210. } catch (IOException ioe) {}*/
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement