/* File: structitem.C

   struct operations needed for ListNode and List Classes.
*/

#include <stdio.h>
#include <stdlib.h>
#include "structitem.h"

ListItem::ListItem() {		/* default constructor */

   item = NULL ;
}


ListItem::ListItem(data x) {	/* alternate constructor */

   item = new book_t ;
   if (item == NULL) {
      fprintf(stderr, "Could not copy book structure\n") ;
      exit(1) ;
   }
   *item = *x ;
}


ListItem::~ListItem() {		/* destructor */

   if (item != NULL) delete item ;
}


data ListItem::copy() {		/* make a copy */
   book_t *temp ;

   temp = new book_t ;
   if (temp == NULL) {
      fprintf(stderr, "Could not copy book structure\n") ;
      exit(1) ;
   }
   *temp = *item ;
   return temp ;
}


/* Compare data in host object with given data.
   <0 is smaller, =0 is equal, >0 is bigger 
*/
int ListItem::compare(data x) {      

   if (item == NULL && x == NULL) return 0 ;
   if (item == NULL) return -1 ;
   if (x == NULL) return 1 ;

   if (item->price < x->price) return -1 ;
   if (item->price > x->price) return  1 ;
   return 0 ;
}


void ListItem::print() {       /* print to stdout */
      char *kind ;

      printf("\n") ;
      printf("   Title  = \"%s\"\n", item->title ) ;
      printf("   Author = \"%s\"\n", item->author ) ;
      switch(item->kind) {
	 case 1  : kind = "Paperback" ; break ;
	 case 2  : kind = "Hardcover" ; break ;
	 case 3  : kind = "Audio"     ; break ;
	 default : kind = "Unknown" ;
      }
      printf("   Kind = %s, Year = %d, Price = %f\n", 
	 kind, item->year, item->price ) ;
}
