Guest User

Untitled

a guest
Jun 24th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class VehicleTest {
  5. public static void main(String[] args) throws FileNotFoundException {
  6. String vehicle = "vehicle";
  7. String car = "car";
  8. String americanCar = "american car";
  9. String foreignCar = "foreign car";
  10. String truck = "truck";
  11. String bicycle = "bicycle";
  12.  
  13. File file = new File(args[0]);
  14. Scanner input = new Scanner(file);
  15.  
  16. String[] autos = new String[100];
  17. ArrayList allVehicles = new ArrayList();
  18.  
  19.  
  20. for (int i = 0; i < autos.length; i++) {
  21. autos[i] = input.nextLine();
  22. }
  23.  
  24. int j = 0;
  25. int i = 0;
  26.  
  27. while (i++ < autos.length) {
  28. if (vehicle.equalsIgnoreCase(autos[j++])) {
  29. Vehicle v = new Vehicle();
  30. v.setOwnerName(autos[j]);
  31. allVehicles.add(v);
  32. }else if(car.equalsIgnoreCase(autos[j++])){
  33. Car c = new Car();
  34. c.setOwnerName(autos[j]);
  35. allVehicles.add(c);
  36. }
  37. }
  38.  
  39. for(Object a: allVehicles){
  40. System.out.println(a);
  41. }
  42. }
  43. }
  44.  
  45. while i is less than the length of the string array
  46. if you see the word vehicle create a new vehicle object and add it to the arrayList.
  47. if you see the word car create a new car object and add it to the arrayList.
  48. .....
  49.  
  50. foreign car
  51. aMarioy
  52. Mario's house
  53. (777) 777-7777
  54. gmario@mario.com
  55. false
  56. black
  57. Italy
  58. 4415.91
  59.  
  60. truck
  61. aDougy
  62. Doug's house
  63. (123) 456-7890
  64. hdoug@doug.com
  65. 30
  66. 61234.56
  67. 8/10/2003
  68.  
  69. vehicle
  70. aRobby
  71. Rob's house
  72. (987) 654-3210
  73. irob@rob.com
  74.  
  75. bicycle
  76. bTommy
  77. Tom's house
  78. (246) 810-1214
  79. jtom@tom.com
  80. 7
  81.  
  82. truck
  83. bGeorge
  84. George's house
  85. (666) 666-6666
  86. kgeorge@george.com
  87. 25
  88. 51234.56
  89. 12/4/2004
  90.  
  91. vehicle
  92. bTim
  93. Tim's house
  94. (111) 111-1111
  95. tim@tim.com
  96.  
  97. bicycle
  98. bJim
  99. Jim's house
  100. (555) 555-5555
  101. Ajim@jim.com
  102. 5
  103.  
  104. american car
  105. bJohn
  106. John's house
  107. (888) 888-8888
  108. Bjohn@john.com
  109. true
  110. green
  111. false
  112. true
  113.  
  114. car
  115. cKen
  116. Ken's house
  117. (999) 999-9999
  118. Cken@ken.com
  119. false
  120. orange
  121.  
  122. foreign car
  123. cMario
  124. Mario's house
  125. (777) 777-7777
  126. Dmario@mario.com
  127. false
  128. black
  129. Italy
  130. 4415.91
  131.  
  132.  
  133. american car
  134. gSam
  135. Sam's house
  136. (333) 333-3333
  137. Hsam@sam.com
  138. false
  139. blue
  140. true
  141. false
  142.  
  143. while (j < autos.length) {
  144. if (vehicle.equalsIgnoreCase(autos[j])) {
  145. j++;
  146. Vehicle v = new Vehicle();
  147. v.setOwnerName(autos[j++]);
  148. allVehicles.add(v);
  149. } else if(car.equalsIgnoreCase(autos[j])){
  150. j++;
  151. Car c = new Car();
  152. c.setOwnerName(autos[j++]);
  153. allVehicles.add(c);
  154. }
  155. }
  156.  
  157. while (j < autos.length) {
  158. String type = autos[j++];
  159. if (vehicle.equalsIgnoreCase(type)) {
  160. Vehicle v = new Vehicle();
  161. v.setOwnerName(autos[j++]);
  162. allVehicles.add(v);
  163. } else if(car.equalsIgnoreCase(type)){
  164. Car c = new Car();
  165. c.setOwnerName(autos[j++]);
  166. allVehicles.add(c);
  167. }
  168. }
  169.  
  170. while (scanner.hasNext()) {
  171. String type = scanner.nextLine();
  172. if (type.equalsIgnoreCase("vehicle")) {
  173. allVehicles.add(new Vehicle(scanner));
  174. } else if (type.equalsIgnoreCase("car")) {
  175. allVehicles.add(new Car(scanner));
  176. }
  177. // ...
  178. }
  179.  
  180. // Use a base type in real code
  181. private static Object parseNextVehicle(Scanner scanner) {
  182. String type = scanner.nextLine();
  183. if (type.equalsIgnoreCase("vehicle")) {
  184. return new Vehicle(scanner);
  185. } else if (type.equalsIgnoreCase("car")) {
  186. return new Car(scanner);
  187. }
  188. // ... throw an exception indicating an unknown vehicle type
  189. }
  190.  
  191. // ... and then in the main method, use it like this:
  192. while (scanner.hasNextLine()) {
  193. allVehicles.add(parseNextVehicle(scanner));
  194. }
  195.  
  196. for (int i = 0, j = 0; i < autos.length; ++i, ++j) {
  197. if (vehicle.equalsIgnoreCase(autos[j])) {
  198. // ...
Add Comment
Please, Sign In to add comment