Advertisement
Guest User

main

a guest
Jul 29th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. package arrays;
  2. import java.io.*;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. public class main {
  6.  
  7. public static int searchByName(ArrayList<person> personList) {
  8. System.out.println("Enter name(first and last, first letters capitlized):");
  9. String name = System.console().readLine();
  10. for(int i = 0; i < personList.size(); i++){
  11. //System.out.println(name + "=" + personList.get(i).getName());
  12. if (personList.get(i).getName().equals(name)){
  13. return i;
  14. }
  15. }
  16. return -1;
  17. };
  18.  
  19. public static int searchByCreditCard(ArrayList<person> personList) {
  20. System.out.println("Enter name(first and last, first letters capitlized):");
  21. String name = System.console().readLine();
  22. for(int i = 0; i < personList.size(); i++){
  23. //System.out.println(name + "=" + personList.get(i).getcCard());
  24. if (personList.get(i).getcCard().equals(name)){
  25. return i;
  26. }
  27. }
  28. return -1;
  29. };
  30.  
  31. public static int searchByDateOfBirth(ArrayList<person> personList) {
  32. System.out.println("Enter name(first and last, first letters capitlized):");
  33. String name = System.console().readLine();
  34. for(int i = 0; i < personList.size(); i++){
  35. //System.out.println(name + "=" + personList.get(i).getdateOfBirth());
  36. if (personList.get(i).getdateOfBirth().equals(name)){
  37. return i;
  38. }
  39. }
  40. return -1;
  41. };
  42.  
  43. public static int searchBySocialSecurity(ArrayList<person> personList) {
  44. System.out.println("Enter name(first and last, first letters capitlized):");
  45. String name = System.console().readLine();
  46. for(int i = 0; i < personList.size(); i++){
  47. //System.out.println(name + "=" + personList.get(i).getsSec());
  48. if (personList.get(i).getsSec().equals(name)){
  49. return i;
  50. }
  51. }
  52. return -1;
  53. };
  54.  
  55.  
  56. public static void info(person personObject) {
  57. System.out.println('\n' + "Information:");
  58. System.out.println("Name: " + personObject.getName() );
  59. System.out.println("Credit Card Number: " + personObject.getcCard() );
  60. System.out.println("Date Of Birth: " + personObject.getdateOfBirth() );
  61. System.out.println("Social Security Number: " + personObject.getsSec() );
  62. System.out.println('\n');
  63. return;
  64. };
  65.  
  66. public static void search(ArrayList<person> personList) {
  67. System.out.println("Search by?(name, creditcard, dateofbirth, ssn");
  68. String field = System.console().readLine();
  69. int index = -1;
  70. if (field.equals("name")) {
  71. index = searchByName(personList);
  72. info(personList.get(index));
  73. }
  74. else if (field.equals("creditcard")) {
  75. index = searchByCreditCard(personList);
  76. person personObject = personList.get(index);
  77. info(personList.get(index));
  78. }
  79. else if (field.equals("dateofbirth")) {
  80. index = searchByDateOfBirth(personList);
  81. person personObject = personList.get(searchByDateOfBirth(personList));
  82. info(personList.get(index));
  83. }
  84. else if (field.equals("ssn")) {
  85. index = searchBySocialSecurity(personList);
  86. person personObject = personList.get(searchBySocialSecurity(personList));
  87. info(personList.get(index));
  88. }
  89. return;
  90. };
  91.  
  92. public static void main (String[] args) {
  93. FileInputStream fIn;
  94. int state = 1;
  95. String name = "";
  96. boolean acceptingCommands = true;
  97. try{
  98. System.out.println("Get file pls");
  99. String fileName = System.console().readLine();
  100. FileReader fileReader = new FileReader(fileName);
  101. BufferedReader bufferedReader = new BufferedReader(fileReader);
  102. StringBuffer stringBuffer = new StringBuffer();
  103. String line = "";
  104. String cCard = "";
  105. String dateOfBirth = "";
  106. String sSec = "";
  107.  
  108. ArrayList<person> personList = new ArrayList<person>();
  109.  
  110.  
  111.  
  112. fIn = new FileInputStream (fileName);
  113. Scanner scanner = new Scanner(fIn);
  114.  
  115.  
  116. // System.out.flush();
  117. //System.out.println("hey im gay");
  118.  
  119. while(scanner.hasNextLine()){
  120. scanner.nextLine();
  121. line = bufferedReader.readLine();
  122. //System.out.println(line);
  123. person currentPerson = new person();
  124.  
  125. for (int i = 0; i < line.length(); i++){
  126. //NAME
  127. if (state == 1){
  128. if (line.charAt(i) == ','){
  129. state = state + 1;
  130. //System.out.println(name + "\n");
  131. currentPerson.setName(name);
  132. name = "";
  133.  
  134. }
  135. else{
  136. name = name + line.charAt(i);
  137. }
  138. }
  139. //Credit Card
  140. else if (state == 2){
  141. if (line.charAt(i) == ','){
  142. state = state + 1;
  143. //System.out.println(cCard + "\n");
  144. currentPerson.setcCard(cCard);
  145. cCard = "";
  146. }
  147. else{
  148. cCard = cCard + line.charAt(i);
  149. }
  150. }
  151. //Date of Birth
  152. else if (state == 3){
  153. if (line.charAt(i) == ','){
  154. state = state + 1;
  155. //System.out.println(dateOfBirth + "\n");
  156. currentPerson.setdateOfBirth(dateOfBirth);
  157. dateOfBirth = "";
  158. }
  159. else{
  160. dateOfBirth = dateOfBirth + line.charAt(i);
  161. }
  162. }
  163. // Social Security
  164. else if (state == 4){
  165. sSec = sSec + line.charAt(i);
  166.  
  167.  
  168. }
  169. }
  170. state = 1;
  171. //System.out.println(sSec + "\n");
  172. currentPerson.setsSec(sSec);
  173. sSec = "";
  174. personList.add(currentPerson);
  175.  
  176. }
  177. scanner.close();
  178.  
  179. /*for (person thisPerson : personList){
  180. System.out.println("Name: " + thisPerson.getName() );
  181. System.out.println("Credit Card Number: " + thisPerson.getcCard() );
  182. System.out.println("Date Of Birth: " + thisPerson.getdateOfBirth() );
  183. System.out.println("Social Security Number: " + thisPerson.getsSec() );
  184. }*/
  185. while (acceptingCommands == true){
  186. String commandWord;
  187. System.out.println("What's up dude(Enter a command(type help for a list of commands))?");
  188. commandWord = System.console().readLine();
  189. if (commandWord.equals("exit")){
  190. acceptingCommands = false;
  191. System.out.println("Closing");
  192. }
  193. else if (commandWord.equals("help")){
  194. System.out.println("Commands: ");
  195. System.out.println("search, sort, print, edit");
  196. }
  197. else if (commandWord.equals("search")){
  198. search(personList);
  199. }
  200. }
  201. }
  202. catch(IOException ex){
  203.  
  204.  
  205. }
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement