Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. /**
  2. *
  3. * @author Casey Jackson
  4. */
  5. public class HydraList{
  6.  
  7. private letterNode h; //list Header
  8. private dataNode g; //dataList header
  9. public HydraList()
  10. {
  11. h = new letterNode();
  12. //g = new dataNode();
  13. //g.next = null;
  14. //g.data = null;
  15. //g.partNum = null;
  16. h.letter = ' ';
  17. h.next = null;
  18. h.data = null;
  19. }
  20.  
  21. public void insert(AutoPart newPart)
  22. {
  23. letterNode p = h.next;
  24. letterNode q = h;
  25. char x = newPart.getPartNumber().charAt(0);
  26. while(p != null && p.letter != x)
  27. {
  28. q = p;
  29. p = p.next;
  30. }
  31. if (p != null)
  32. {
  33. dataNode b = q.data;
  34. if(b != null)
  35. {
  36. dataNode a = b.next;
  37. while(q.data != null)
  38. {
  39. b = a;
  40. a = a.next;
  41. }
  42. if (a != null)
  43. {
  44. dataNode z = new dataNode();
  45. z = a.next;
  46. a.next = z;
  47. z.data = newPart.deepCopy();
  48. z.partNum = newPart.getPartNumber();
  49. }
  50. else
  51. {
  52. dataNode z = new dataNode();
  53. z = a.next; // this line throws the null pointer exception, because a==null (ref: if-condition)
  54. a.next = z;
  55. z.data = newPart.deepCopy();
  56. z.partNum = newPart.getPartNumber();
  57. }
  58. }
  59. }
  60. else
  61. {
  62. p = h.next; // what is this for?
  63. letterNode n = new letterNode();
  64. n.next = q.next; // will not sort. just building a list
  65. q.next = n; // at least it's a 'pure' insert (always at beginning)
  66. n.letter = x;
  67. dataNode z = new dataNode();
  68. z.next = n.data; // also will not sort...
  69. z.data = newPart.deepCopy();
  70. z.partNum = newPart.getPartNumber();
  71. n.data = z; // above commented line + this one will always insert at beginning
  72. }
  73. }
  74.  
  75. //
  76.  
  77. // public void insert(AutoPart newPart)
  78. // {
  79. // letterNode p = new letterNode();
  80. // letterNode q = new letterNode();
  81. // q = h;
  82. // p = h.next;
  83. // dataNode o = new dataNode();
  84. // char x = newPart.getPartNumber().charAt(0);
  85. // if(letterIsEmpty())
  86. // {
  87. // letterNode n = new letterNode();
  88. // n.next = h.next;
  89. // h.next = n;
  90. // n.letter = newPart.getPartNumber().charAt(0);
  91. // o.next = n.data;
  92. // n.data = o;
  93. // o.partNum = newPart.getPartNumber();
  94. // o.data = newPart.deepCopy();
  95. // }
  96. // else
  97. // {
  98. // while(p != null && p.letter != x)
  99. // {
  100. // q = p;
  101. // p = p.next;
  102. // }
  103. // if(p != null)
  104. // {
  105. // if(p.data == null)
  106. // {
  107. // o.next = p.data;
  108. // p.data = o;
  109. // o.data = newPart.deepCopy();
  110. // o.partNum = newPart.getPartNumber();
  111. // }
  112. // else
  113. // {
  114. // //dataNode z = new dataNode();
  115. // dataNode z = q.data;
  116. // while(g!= null && !(g.partNum.compareTo
  117. // (newPart.getPartNumber()) == 0))
  118. // {
  119. // g = g.next;
  120. // }
  121. // if(g != null)
  122. // {
  123. // o.next = g.next;
  124. // g.next = o;
  125. // o.data = newPart.deepCopy();
  126. // o.partNum = newPart.getPartNumber();
  127. // }
  128. // }
  129. // }
  130. // }
  131. // }
  132.  
  133.  
  134.  
  135. public AutoPart fetch(String targetPart)
  136. {
  137. letterNode a = h.next;
  138. while(a != null && a.letter != targetPart.charAt(0))
  139. {
  140. a = a.next;
  141. }
  142. if (a != null)
  143. {
  144. dataNode b = h.data;
  145. while(b != null && !(b.partNum.compareTo(targetPart) == 0))
  146. {
  147. b = b.next;
  148. }
  149. if (b != null)
  150. {
  151. return b.data.deepCopy();
  152. }
  153. else
  154. return null;
  155. }
  156. else
  157. {
  158. return null;
  159. }
  160. }
  161.  
  162. public boolean delete(String targetPart)
  163. {
  164. letterNode b = h;
  165. letterNode a = h.next;
  166. while( a != null && a.letter != targetPart.charAt(0))
  167. {
  168. b = a;
  169. a = a.next;
  170. }
  171. if( a != null)
  172. {
  173. dataNode d = a.data;
  174. dataNode c = d.next;
  175. while(c != null && !(c.partNum.compareTo(targetPart) == 0))
  176. {
  177. d = c;
  178. c = c.next;
  179. }
  180. if (c != null)
  181. {
  182. d.next = c.next;
  183. if (a.data == null)
  184. {
  185. b.next = a.next;
  186. }
  187. return true;
  188. }
  189. else
  190. return false;
  191. }
  192. else
  193. return false;
  194. }
  195.  
  196. public boolean update(String targetPart, float newPrice, int inStock)
  197. {
  198. AutoPart x = fetch(targetPart);
  199. if(x == null)
  200. {
  201. return false;
  202. }
  203. else
  204. {
  205. x.setPrice(newPrice);
  206. x.setNumInStock(inStock);
  207. return true;
  208. }
  209. }
  210.  
  211.  
  212. // public void print()
  213. // {
  214. // letterNode p = h.next;
  215. // dataNode q = p.data;
  216. // while(p != null)
  217. // {
  218. // while(q != null)
  219. // {
  220. // System.out.println(q.data);
  221. // q = q.next;
  222. // }
  223. // p = p.next;
  224. // }
  225. // }
  226.  
  227.  
  228. public void print()
  229. {
  230. letterNode q = h;
  231. letterNode p = h.next;
  232. //dataNode a = p.data;
  233. while(p != null)
  234. {
  235. dataNode a = p.data;
  236. while(q != null)
  237. {
  238. System.out.println(q.data);
  239. q = q.next;
  240. }
  241. p = p.next;
  242. }
  243. }
  244.  
  245.  
  246. public boolean letterIsEmpty()
  247. {
  248. boolean empty = false;
  249. if(h.next == null){
  250. empty = true;
  251. }
  252. return empty;
  253. }
  254.  
  255. public class letterNode{
  256. private char letter;
  257. private letterNode next;
  258. private dataNode data;
  259. public letterNode(){
  260.  
  261. }
  262.  
  263. }
  264.  
  265. public class dataNode{
  266. private AutoPart data;
  267. private String partNum;
  268. private dataNode next;
  269. public dataNode()
  270. {
  271. }
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement