Assignment 5 Files, Linked Lists, and Binary Search Trees Technical details We advise you to look carefully at the example input and output files that we supply. Many questions will be answered by simply reviewing these files and the instructions. Carefully read the next sections and follow the directions meticulously. Strictly follow the input/output instructions! As in the previous assignments, keep in mind that the code you submit is tested by automated tools. Failing to produce the exact same output as our published executable will negatively impact your grade. Introduction You were just hired for your first job as the programmer for a large logistics company. The only problem is that the company s entire inventory system just crashed! The only information left are the following files: 1. An old inventory file which is one year old. 2. A file for every customer of the company, detailing all purchases from the past year. 3. A file containing the minimal number of items that must be in stock for each product. Your mission is to recreate the inventory and restore order in the company. Part 1 Reading the old inventory file and recreating the old inventory using a binary search tree. This will be done by implementing the function: product_tree *read_initial_stock_file(file *initial_stock_file); The initial inventory file contains the ID and name of the product and the quantity of this product in stock one year ago. For every product there will be exactly one line in the file: - Product ID: 9 digits exactly. Can begin with a 0. - One space character ( ). - Product name: 10 chars exactly. - Quantity: a number between 0-9999. An example of such a file is included in the assignment web site: InitialInventory.txt.
Each product will be stored in memory as a node of a binary search tree. Every node in the tree will be as follows: typedef struct product_tree { char *id; //Product ID number. This is the key of the search tree. char *product_name; //Name of the product. int current_quantity; //How many items are there in stock, currently. double current_average_rating; //The current average rating of the //product. Each customer's rating is //given the same weight. This is //initially 0. sales_info *sales; //This is initially empty. struct product_tree *left, *right; } product_tree; The product tree will be sorted by the product s ID number. This means that for every node in the tree, all products in the left subtree must have lower IDs than the node, and all products in the right subtree must have greater IDs than the node! The initial inventory file will be read line by line, adding a node to the product tree for every line. The current_average_rating will be given 0, and sales will remain empty for now. After reading the initial inventory, you will procede to reading all the customer files. This will be done by implementing the function: void read_customer_sales(file *customer_file, product_tree *root_product); Every customer file will be of the following structure: - The first line will contain the ID of the customer: 9 digits exactly. Can begin with a 0. - All other lines will represent purchases: Product ID (9 digits exactly), one space character ( ), number of items purchased (integer between 1-9999), one space character ( ), rating of the product (integer between 1-100). You can assume that a customer will appear at most once per product. An example of such a file is included in the assignment web site, Customer1.txt. Each purchase will be stored in memory as a node in a linked list, named sales. This list is defined as follows: typedef struct sales_info { char *customer_id; //ID number of the customer. int rating; //Rating that the customer gave the product. Number //between 1 and 100. int quantity; //How many items did the customer buy. struct sales_info *next; } sales_info;
When you read a purchase line from the customer file, you must insert the sales_info node (that has the customer_id, rating, and quantity) into the end of the sales list of the relevant product. When inserting a sales_info node, you must do two additional things: 1. Update the field current_quantity in the product_tree node. This is done by subtracting the quantity of the sale from current_quantity. 2. Update the field current_average_rating in the product_tree node. This is done by computing the average rating given by the customers that bought the product. For example, if a product has two sales with ratings 40 and 71, its current_average_rating will be (40+71)/2=55.5. If another sale is added, the current_average_rating is re-computed. When you are finished reading all customer files, you are expected to have the product tree, where the sales list for every product contains all sales made by all customers, and the current_quantity and current_average_rating of all the products are updated.
Part 2 Now that you have the current inventory, you must generate some reports for your boss. The first report will be concerning the products. You will implement the following function: void create_product_report(product_tree *root_product, char *product_report_file_name); This function will create a file named product_report_file_name, which will contain the following: 1. The first part of the report will be a list of the products, ordered by their IDs from low to high. Every product will be in a separate line, with the following structure: Product ID (9 digits exactly), one space character ( ), current_quantity (integer 0-9999), one space character ( ), current_average_rating (double 1-100, one digit after the decimal point; examples: 97.4, 100.0). 2. An empty line. 3. The second part of the report will be a list of the products, ordered by the popularity from lowest to highest. The line structure here is the same as in the first part. An example of such a report is included in the assignment web site, ProductReport.txt. The seconed report will be concerning orders that need to be done to match a certain minimum inventory. You will implement the following function: void create_order_report(product_tree *root_product, FILE *minimal_stock_file, Char *order_report_file_name); For this report you will need to read the minimal_stock_file. Every line in this file represents a product and has the following structure: - Product ID: 9 digits exactly. - Minimal quantity that must be in stock: Integer 1-9999.
This report must have one line for each product for which the current stock is lower than the minimal quantity that must be in stock (taken from minimal_stock_file). There will be one line per product with the following structure: - Product ID: 9 digits exactly. - The quantity we need to order so that we have the minimal quantity in stock. For example, if product no. 123456789 has a current stock of 15, and its minimal quantity is 105, then we would output the line: 123456789 90 An example of such a report is included in the assignment web site: OrderReport.txt. Some (important) clarifications: 1. You may assume the input is correct! For example, IDs of products and customers, as well as average ratings of products, will be unique, and quantities will never drop below zero. 2. You are provided with a skeleton file for the code (assignment5.c). You must use (and submit) this file! 3. You should not change any of the functions given to you. Changing them will result in a 0 grade! 4. You should not change the main function. All you need to do is implement the additional functions (according to the declared prototypes). You can of course add more functions, but think carefully before you do.