/* list03.cpp                                                          */
/* First created 24 Oct 1996                                           */
/* Updated: 11 Nov 2003 AHD                                            */
/* Author:  Anne Dawson                                                */
/* This program is the third version in a series of programs           */
/* resulting in the final list.cpp program, which is the solution to   */
/* an Inventory Tracking project of the Pointers and linked lists chapter*/
/*                                                                     */
/* list01.cpp displays the Main Menu, allows the user                  */
/* to enter an integer choice, which triggers the call to a function.  */
/* The functions called from the Main Menu are empty stubs, which when */
/* executed, simply display the function's name.                       */
/*                                                                     */
/* Version list02.cpp                                                  */
/* is a direct copy of list01.cpp, with the the code for the menu 1    */
/* option completed.                                                   */
/*                                                                     */
/* list03.cpp is a direct copy of list02.cpp with                      */
/* code for display_all_inventory (option 2) working                   */
/* Subsequent versions will have subsequent menu                       */
/* options working. The final version of the program will be called    */
/* list.cpp, and will be the fully functional version.                 */

/* Status: COMPLETE                                                    */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h >  /* for getch() */
#include <iostream.h>


int get_choice(void);
int insert_new_product_type(int upc);
int add_new_node(int upc);
int display_all_inventory(void);
int display_one_of_inventory(void);
int display_individual_inventory(int upc);
int print_individual(void);
int change_price(int upc);
int transfer_in(int upc);
int transfer_out(int upc);

struct node
{
	int upc;
	char description[20];
	float price;
	int quantity;
	node *link;
};

 node *start_pointer = NULL;
 node *current_pointer = NULL;


/**********************************************************************/

main()
{
	int upc, choice;

	choice = get_choice();

	while (choice != 9)
	{
		switch(choice)
		{
			case 1:
				cout << "\nPlease enter the UPC code:  ";
				cin >> upc;
				insert_new_product_type(upc);
				break;
			case 2:
				display_all_inventory();
				break;
			case 3:
				cout << "\nPlease enter the UPC code:  ";
				cin >> upc;
				display_individual_inventory(upc);
				break;
			case 4:
				cout << "\nPlease enter the UPC code:  ";
				cin >> upc;
				change_price(upc);
				break;
			case 5:
				cout << "\nPlease enter the UPC code:  ";
				cin >> upc;
				transfer_in(upc);
				break;
			case 6:
				cout << "\nPlease enter the UPC code:  ";
				cin >> upc;
				transfer_out(upc);
				break;
			case 9:
				exit(0);
			default:
				cout << "\nChoice not in list\n  ";
				cout << "\nPlease try again....\n  ";
		} /* end switch */

		choice = get_choice();

	} /* end while */

	return 0;

}   /* end main */


/**********************************************************************/


int get_choice(void)
{
	int choice;

	cout << "\n\nMain Menu\n\n";
	cout << "1)  Insert new product type\n";
	cout << "2)  Display all inventory\n";
	cout << "3)  Display individual inventory\n";
	cout << "4)  Change price\n";
	cout << "5)  Transfer in\n";
	cout << "6)  Transfer out\n";
	cout << "9)  Quit\n";
	cout << "  \nPlease enter your choice: \n";

	choice = 0;

	cin >> choice;

	return choice;

}   /* end of get_choice */


/**********************************************************************/

int insert_new_product_type(int upc)
{

	cout << "\n\nNow in the insert_new_product_type function\n";
	cout << "UPC = " << upc << endl;
	

	if (start_pointer == NULL) // there are no nodes in the list
	{
		add_new_node(upc);  // add the first node
		return 0;           // then leave this function
	}

	// if the execution of the code reaches here, then there were
	// already nodes in the list

	current_pointer = start_pointer; // here we set the current_pointer
												// to point to where the start_pointer
												// points to, i.e. they contain the
												// same address

	if (current_pointer->upc == upc) // here I check to see if the upc
												// (the universal product code)
												// already exists.
	{
		cout << "That user product code already exists\n\n";
		return 1;  // if the upc already exists, we cannot have duplicate
		// upc codes in the list, so a message is output to the screen
		// and execution returns to the calling function at this return
		// statement
	}

	// if execution of the code reaches here, the upc code does not
	// match that of the first node in the list (pointed at by start_pointer)
	// so now we have to check the remaining nodes in the list to make sure
	// that they also do not have a upc code which matches the one just
	// entered...
	while (current_pointer->link != NULL)
	{
		current_pointer = current_pointer->link;
		if (current_pointer->upc == upc)
		{
			cout << "That user product code already exists\n\n" << endl;
			return 0;
		}
	}
	//  at this point current_pointer link is equal to NULL
	//  i.e. we are at the end of the linked list

	// also, at this point, we know that the upc code is not contained in the
	// entire list, so now we can add the product with the new upc code to the
	// linked list...

	add_new_node(upc);

	return 0; // and then return to the calling function
}



/**********************************************************************/

int add_new_node(int upc)
{

	node *temp_pointer = NULL;  // create a temporary pointer called
										 // temp_pointer, and assign NULL to it
										 // i.e. it is not pointing anywhere as it
										 // does not contain an address

	temp_pointer = new node;    // an address of a new node is assigned

	if (temp_pointer == NULL)   // if there was not enough memory on the heap
										 // then NULL would have been assigned to
										 // the pointer instead, so this is checked for
										 // here
	{
		cout << "\nNot enough memory\n";
		exit (1); // leave the program immediately as there is no memory left
					 // to be able to run the program
	}

	// at this point we know that temp_pointer is pointing to a valid
	// area of memory, so now we can place data into the memory location
	// using the arrow operator ->  ( for details of -> see bottom of page 780)

	cout << "\nInserting a new item\n\n";
	temp_pointer->upc = upc;

	cout << "\nPlease enter a description: \n" << endl;
	gets(temp_pointer->description); // gets is the get string function

	cout << "\nThe description entered: " << temp_pointer->description;
	cout << endl;

	cout << "\nPlease enter a price per unit:\n";
	cin >> temp_pointer->price;

	cout << "\nPlease enter quantity in stock:\n";
	cin >> temp_pointer->quantity;

	if (start_pointer == NULL)
		start_pointer = temp_pointer;
	else
		current_pointer->link = temp_pointer;

	temp_pointer->link = NULL;

	return 0;
}

/**********************************************************************/
/**********************************************************************/


int display_all_inventory(void)
{
	cout << "\n\nNow in display_all_inventory\n";
	cout << "Press any key to continue\n";
	getch();

	current_pointer = start_pointer;

	cout << "\nInventory\n\n";

	if (start_pointer == NULL)
	{
		cout << "No stock\n\n";
		return 1;
	}

	while (current_pointer != NULL)
	{
		display_one_of_inventory();
		current_pointer = current_pointer->link;
	}

	return 0;
}

/**********************************************************************/

int display_one_of_inventory(void)
{
	cout << "UPC code         : " << current_pointer->upc << endl;
	cout << "Product          : " << current_pointer->description << endl;
	cout << "Price per unit   : " << current_pointer->price << endl;
	cout << "Quantity in stock: " <<  current_pointer->quantity << endl << endl;

	return 0;
}

/**********************************************************************/

/**********************************************************************/

int display_individual_inventory(int upc)
{
	cout << "\n\nNow in display_individual_inventory function\n";
	cout << "UPC = " << upc << endl;
	cout << "\nPress any key to continue\n";
	getch();
	return 0;
}

/**********************************************************************/

int change_price(int upc)
{
	cout << "\n\nNow in change_price function\n";
	cout << "UPC = " << upc << endl;
	cout << "\nPress any key to continue\n";
	getch();
	return 0;
}

/**********************************************************************/

int transfer_in(int upc)
{
	cout << "\n\nNow in transfer_in function\n";
	cout << "UPC = " << upc << endl;
	cout << "\nPress any key to continue\n";
	getch();
	return 0;
}

/**********************************************************************/

int transfer_out(int upc)
{
	cout << "\n\nNow in transfer_out function\n";
	cout << "UPC = " <<  upc << endl;
	cout << "\nPress any key to continue\n";
	getch();
	return 0;
}

/**********************************************************************/

