Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- public class MedListRefBased implements List<Medication> {
- private MedicationNode head;
- private MedicationNode tail;
- private int count;
- public MedListRefBased() {
- head = null;//head
- tail = null;//tail
- count = 0;//initialize count
- }
- public void add(Medication k,int index) {
- if(index < 0 || index > count){
- throw new IndexOutOfBoundsException("Index is not in bounds");
- }//exception handling
- MedicationNode add=new MedicationNode(k);
- if(isEmpty()){
- head=add;
- tail=head;//add the new medication as the head and tail
- }else{
- MedicationNode cur=head;
- for(int i=0;i<index-1;i++){
- cur=cur.next;
- }//for
- add = new MedicationNode(k, cur.prev, cur);
- cur.next=add;
- add.prev=cur;
- add.next=cur.next.next;//add the new medication
- }
- count++;
- }//add
- public Medication get(int index) {
- if(index < 0 || index > count){
- throw new IndexOutOfBoundsException("Index is not in bounds");
- }//exception handling
- MedicationNode cur = head;
- for(int i =0; i<index; i++){
- cur = cur.next;
- }//for
- return cur.item;
- }//get
- public boolean isEmpty() {
- if(tail == null && head == null){
- return true;
- }//if
- return false;
- }//isEmpty
- public int size() {
- return count;
- }//size
- public int indexOf(Medication item) {
- int index=0;
- MedicationNode cur=head;
- while(cur!=null){
- if(cur.item==item){
- break;
- }
- cur=cur.next;
- index++;
- }
- return index;
- }//indexOf
- public void remove(int index) {
- if(index < 0 || index > count){
- throw new IndexOutOfBoundsException("Index is not in bounds");
- }//exception handling
- if(index ==0){
- removeFront();
- }else {
- MedicationNode cur=head;
- for(int i=0;i<index-1;i++){
- cur=cur.next;
- }
- cur.next=cur.next.next;
- }
- count--;
- }//remove
- public void remove(Medication item) {
- MedicationNode cur=head;
- if(head.item.equals(item)){
- removeFront();
- }else{
- for(int i=0;i<count;i++){
- if(cur.item.equals(item)){
- cur.prev.next=cur.next;
- }//if
- cur=cur.next;
- }
- }
- count--;
- }//remove
- public void removeAll() {
- head=null;
- tail=null;
- count=0;
- }//removeAll
- public void removeFront(){
- head = head.next;
- head.prev=null;
- }//removeFront
- public String toString() {
- String details="";
- MedicationNode cur=head;
- while(cur!=null){
- details=details + "," + cur.item;
- cur =cur.next;
- }
- return details;
- }//toString
- public static void main(String[] args) {
- Medication a = new Medication("tylenol",500);
- Medication b = new Medication("ibuprofen",100);
- Medication c = new Medication("meperidol",200);
- Medication d = new Medication("cimetidine",300);
- MedListRefBased test = new MedListRefBased();
- test.add(a,0);
- System.out.println(test.toString());
- test.add(b,1);
- System.out.println(test.toString());
- test.add(c,2);
- System.out.println(test.toString());
- test.add(d,0);
- System.out.println(test.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement