Advertisement
Guest User

Untitled

a guest
May 29th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. import java.util.NoSuchElementException;
  2.  
  3. public class MedListRefBased implements List<Medication> {
  4.  
  5. private MedicationNode head;
  6. private MedicationNode tail;
  7. private int count;
  8.  
  9. public MedListRefBased() {
  10. head = null;//head
  11. tail = null;//tail
  12. count = 0;//initialize count
  13. }
  14.  
  15. public void add(Medication k,int index) {
  16. if(index < 0 || index > count){
  17. throw new IndexOutOfBoundsException("Index is not in bounds");
  18. }//exception handling
  19. MedicationNode add=new MedicationNode(k);
  20. if(isEmpty()){
  21. head=add;
  22. tail=head;//add the new medication as the head and tail
  23. }else{
  24. MedicationNode cur=head;
  25. for(int i=0;i<index-1;i++){
  26. cur=cur.next;
  27. }//for
  28. add = new MedicationNode(k, cur.prev, cur);
  29. cur.next=add;
  30. add.prev=cur;
  31. add.next=cur.next.next;//add the new medication
  32. }
  33. count++;
  34. }//add
  35.  
  36. public Medication get(int index) {
  37. if(index < 0 || index > count){
  38. throw new IndexOutOfBoundsException("Index is not in bounds");
  39. }//exception handling
  40. MedicationNode cur = head;
  41. for(int i =0; i<index; i++){
  42. cur = cur.next;
  43. }//for
  44. return cur.item;
  45. }//get
  46.  
  47. public boolean isEmpty() {
  48. if(tail == null && head == null){
  49. return true;
  50. }//if
  51. return false;
  52. }//isEmpty
  53.  
  54. public int size() {
  55. return count;
  56. }//size
  57.  
  58. public int indexOf(Medication item) {
  59. int index=0;
  60. MedicationNode cur=head;
  61. while(cur!=null){
  62. if(cur.item==item){
  63. break;
  64. }
  65. cur=cur.next;
  66. index++;
  67. }
  68. return index;
  69. }//indexOf
  70.  
  71. public void remove(int index) {
  72. if(index < 0 || index > count){
  73. throw new IndexOutOfBoundsException("Index is not in bounds");
  74. }//exception handling
  75. if(index ==0){
  76. removeFront();
  77. }else {
  78. MedicationNode cur=head;
  79. for(int i=0;i<index-1;i++){
  80. cur=cur.next;
  81. }
  82. cur.next=cur.next.next;
  83. }
  84.  
  85. count--;
  86. }//remove
  87.  
  88. public void remove(Medication item) {
  89. MedicationNode cur=head;
  90. if(head.item.equals(item)){
  91. removeFront();
  92. }else{
  93. for(int i=0;i<count;i++){
  94. if(cur.item.equals(item)){
  95. cur.prev.next=cur.next;
  96. }//if
  97. cur=cur.next;
  98. }
  99.  
  100. }
  101. count--;
  102. }//remove
  103.  
  104. public void removeAll() {
  105. head=null;
  106. tail=null;
  107. count=0;
  108. }//removeAll
  109.  
  110. public void removeFront(){
  111. head = head.next;
  112. head.prev=null;
  113. }//removeFront
  114.  
  115. public String toString() {
  116. String details="";
  117. MedicationNode cur=head;
  118. while(cur!=null){
  119. details=details + "," + cur.item;
  120. cur =cur.next;
  121. }
  122. return details;
  123. }//toString
  124.  
  125.  
  126.  
  127. public static void main(String[] args) {
  128. Medication a = new Medication("tylenol",500);
  129. Medication b = new Medication("ibuprofen",100);
  130. Medication c = new Medication("meperidol",200);
  131. Medication d = new Medication("cimetidine",300);
  132.  
  133.  
  134. MedListRefBased test = new MedListRefBased();
  135.  
  136. test.add(a,0);
  137. System.out.println(test.toString());
  138. test.add(b,1);
  139. System.out.println(test.toString());
  140. test.add(c,2);
  141. System.out.println(test.toString());
  142. test.add(d,0);
  143. System.out.println(test.toString());
  144.  
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement