I am doing a project where I want to emulate a vending machine but the code is needed to be in C language, a language I am not too familiar with. This project must meet these certain specifications.
This is a program that is needed to simulate the Drink Machine. There will be 1 or more drink types in the machine. Your program will read the number of drink types and the drink information from a file. You must then dynamically create an array of structures that will hold the drink information. You may assume that there are no errors within the input file. The Drink Machine Input File Format. Here is a sample file that contains the information for one such Drink Machine:
Cola 1.25 25
Root-beer 1.25 20
Lemon-lime 1.25 25
Water 1.00 40
Orange 1.25 5
Iced-tea 1.25 35
Grape 1.30 15
Iced-Coffee 2.00 35
The first item is an integer that describes how many drinks are described in the input file. Following that are the drink descriptions. The drink name is first, the cost of the drink is second and the number of drinks in the machine at start up is third. Note that the drink names do not include any spaces in the text. You can use the C stream function fscanf() to read in the values from the file. Code You will be creating your Drink Machine code as a C style program. Your source will be in a file called drinkmachine.c. The structure definitions and function prototypes will be put into a file called drinkmachine.h. The program will need a couple of structures.
One structure is for a DrinkItem and contains the following information:
1)id The drink id (assigned by the program) of type int name
2)Drink name (type of drink – read in from a file) price
3)Drink cost (the retail cost of one drink). This is the price the customer will pay for one drink of this type. This is also read in from a file. Type is double.
4)number of drinks Number of drinks of this type in the machine. Initial value is read in from a file. This is also updated by the program as people purchase drinks. Type int. Give this a good name.
5)Drinks purchased Initially 0. Updated whenever a drink is purchased. Type is int. Give this a good name.
The other structure, for the DrinkMachine, will contain the following information:
1)An int that contains a version number. For this assignment this will have a value of 1.
2)An int that contains the number of DrinkItem structures.
3)An array of Drink Items. Each element of the array will be a DrinkItem structure. You will dynamically create this array based on the contents of an input file you will read in.
4)An int that contains the current location in the array of DrinkItem structures. This is used internally by the drink machine part of your code.
You will choose the names for the structure items. Functions Your code will need to implement the following functions:
Function: create() The first function you need to write for your Drink Machine is the create function. This function takes no input parameters and returns back a pointer to your DrinkMachine structure.
First you need to dynamically create a DrinkMachine structure. You will return the address of this structure when you return from this create function. You need to set the version number in the DrinkMachine structure to 1.
You also should create a global const int in your drinkmachine.c file. The name should be INVALID_INDEX and it should be given a value of -1. Initialize the current location value of the DrinkMachine structure to INVALID_INDEX. Next you need to open up your input file. The input file is "[url removed, login to view]". If the file does not exist you need to delete the DrinkMachine structure and return a null pointer back to the caller.
You need to read in the first number (8 in the example above) and create your DrinkMachine structure. You need to set the number of Drink Items to the number you just read in from the file (8 in the above example). NOTE: Your program must be able to handle any number of drinks. You cannot assume 8. You cannot assume any fixed number. Next you need to create an array of DrinkItem structures. This array must be dynamically allocated using the number of Drink Items you read in above . Again, you must be able to support any number of Drink Items. You must use dynamic memory allocation. Do not rely on C99 style variable length arrays.
You will need a loop that will execute once for every Drink Item. Inside the loop you will read in the information for a Drink Item, and put that information into your DrinkItem structure. You also need to set the drink id in the DrinkItem structure. It should have a value of 1 for the first drink, a value of 2 for the 2nd drink, etc.
Make sure your create function closes the input file once it has finished reading in the Drink Item information. You function returns the address of the DrinkMachine structure to the calling function.
Function: destroy() The destroy function has one input parameter, a pointer to a DrinkMachine structure. Your destroy function should delete the array of DrinkItem structures you created. Finally, it should delete the DrinkMachine structure. You must ensure that you are freeing up all memory you allocated in the create function.
Function: firstDrink() The address of the DrinkMachine structure is passed to firstDrink. The firstDrink function returns back the address of a DrinkItem structure (or null pointer).
The firstDrink function needs to return back the address of the first of the DrinkItem structures in the array. If there are no array entries firstDrink should set the current location in the DrinkMachine
structure to INVALID_INDEX and return back null pointer. The current location in the DrinkMachine structure should be set to 0, the index of the first DrinkItem structure in the array. The current location is updated by the nextDrink function and is needed for it to work properly.
Function: nextDrink() The address of the DrinkMachine structure is passed to nextDrink. The nextDrink function returns back the address of a DrinkItem structure (or null pointer). The nextDrink function should get the next DrinkItem structure in the array, if there are any. The logic for nextDrink is as follows:
· If the current location in the DrinkMachine structure is INVALID_INDEX the function should return a nullptr to indicate that there are no more valid entries in the array. Note, this condition should only happen if nextDrink is called after all of the items in the array have been processed or if nextDrink is called and no firstDrink call has been made. This is a logic error in the programming using the firstDrink and nextDrink functions.
· Otherwise, the current location in the DrinkMachine structure should be incremented by 1. If it is less than the DrinkMachine structure’s size value you still have a valid entry left and your program should return back the address of the DrinkItem structure at the new current location.
· If, after you incremented the current location in the previous step, the current location is now greater than or equal to the size in the DrinkMachine structure then you have a condition where there is no next DrinkItem structure in the array. You should set the current location in the DrinkMachine structure to INVALID_INDEX and return a null pointer to indicate that there are no more valid entries in the array.
Stop and Test firstDrink() and nextDrink(): Update your main function you used for earlier testing and test the new firstDrink and nextDrink functions. You can use the following logic (pseudo code):
Create DrinkItem structure pointer pDrink For (pDrink = firstDrink(DrinkMachine structure); pDrink != nullptr; pDrink = nextDrink(DrinkMachine structure)
// Your processing goes here End For Make sure in your tests that all of the entries are being displayed properly.
Also, try calling nextDrink without calling firstDrink and try calling nextDrink after the last entry has been retrieved and make sure your error handling works.
Function: items() The items function has one parameter, the pointer to the DrinkMachine structure. It will return an int, the number of DrinkItem structures in the Drink Machine. For the current implementation this is the size of the array.
For the next 3 functions you need the drink id which is part of the DrinkItem structure.
Function: available() The available function takes two input parameters. The first parameter is a pointer to the DrinkMachine structure. The second parameter is the drink id of a DrinkItem structure. The function checks to see if there if that drink is available (the quantity is 1 or more). The function returns back a bool. The value will be true if the drink is available and false if it is not available. The function will also return a false if the drink id is invalid.
Function: cost() The cost function takes two input parameters. The first parameter is a pointer to the DrinkMachine structure. The second parameter is the drink id of a DrinkItem structure. The function will return back a value of type double. This will be the cost of that Drink Item. The return value will be negative if the drink id is invalid.
Function: purchase() The purchase function takes four parameters and returns an enum class. The first parameter is a pointer to the DrinkMachine structure. The second parameter is the drink id of a DrinkItem structure. The third parameter is the amount of money the customer is using to purchase the drink. This is of type double. The fourth parameter needs to be passed by reference. This will be the amount of change, if any, to be returned to the customer. The fourth parameter is of type double.
The return value is an enum class. The name of the enum class should be Purchase with the values PURCHASED, INVALID, NOT_AVAILABLE, and INSUFFICIENT_FUNDS.
If the drink can be purchased the fourth parameter will contain the amount of change (0.0 if there isn’t any change) and the function will return back Purchase::PURCHASED. The function also needs to decrement the quantity of the DrinkItem structure in the array. The function also needs to increment the purchased count for this item.
If the drink ID is not valid the function will return Purchase::INVALID. If the drink id if valid but there aren’t any drinks of that type left the function will return back Purchase::NOT_AVAILABLE.
Finally, if the drink id is valid and there are drinks of that type available, but the amount is insufficient to purchase the drink the function will return back a value of Purchase::INSUFFICIENT_FUNDS. The value of the fourth parameter will contain the cost of the drink (the same value returned from the cost function).
Function: dumpDrinkMachine() The dumpDrinkMachine function takes a pointer to a DrinkMachine structure as the only parameter and does not return a value. The function displays the contents of the drinks in the drink machine. The output should be similar to the following:
Id Name Price Qty Sold
1 Cola 1.25 25 0
2 Root-beer 1.25 20 0
3 Lemon-lime 1.25 25 0
4 Water 1.00 40 0
5 Orange 1.25 5 0
6 Iced-tea 1.25 35 0
7 Grape 1.30 15 0
8 Iced-Coffee 2.00 35 0
7 freelancers are bidding on average $32 for this job
I will create a C program simulating a drink machine according to your posted requirements. Relevant Skills and Experience I have 30 years experience programming in C. Proposed Milestones $91 USD - C Programming
have more than 10+ years of C application development experience in some of the worlds topmost software companies. Stay tuned, I'm still working on this proposal.