Implement in C Programing 7.10.1: LAB: Product struct Given main(), build a struct called Product that will manage product inventory. Product struct has three data members: a product code (string), the product's price (double), and the number count of product in inventory (int). Assume product code has a maximum length of 20. Implement the Product struct and related function declarations in Product.h, and implement the related function definitions in Product.c as listed below: Product InitProduct(char *code, double price, int count) - set the data members using the three parameters Product SetCode(char *code, Product product) - set the product code (i.e. SKU234) to parameter code void GetCode(char *productCode, Product product) - return the product code in productCode Product SetPrice(double price, Product product) - set the price to parameter product double GetPrice(Product product) - return the price Product SetCount(int count, Product product) - set the number of items in inventory to parameter num int GetCount(Product product) - return the count Product AddInventory(int amt, Product product) - increase inventory by parameter amt Product SellInventory(int amt, Product product) - decrease inventory by parameter amt Ex. If a new Product struct is created with code set to "Apple", price set to 0.40, and the count set to 3, the output is: Name: Apple Price: 0.40 Count: 3 Ex. If 10 apples are added to the Product struct's inventory, but then 5 are sold, the output is: Name: Apple Price: 0.40 Count: 8 Ex. If the Product struct's code is set to "Golden Delicious", price is set to 0.55, and count is set to 4, the output is: Name: Golden Delicious Price: 0.55 Count: 4 Product.c #include #include #include "Product.h" /* Type your code here */ Product.h #ifndef PRODUCT_H_ #define PRODUCT_H_ //TODO: Create Product struct /* Type your code here */ #endif main.c #include #include #include "Product.h" int main() { char code[20]; char productCode[20]; strcpy(code, "Apple"); double price = 0.40; int num = 3; Product product = InitProduct(code, price, num); // Test 1 - Are instance variables set/returned properly? GetCode(productCode, product); printf("Name: %s\n", productCode); printf("Price: %.2lf\n", GetPrice(product)); printf("Count: %d\n\n", GetCount(product)); // Test 2 - Are instance variables set/returned properly after adding and selling? num = 10; product = AddInventory(num, product); num = 5; product = SellInventory(num, product); GetCode(productCode, product); printf("Name: %s\n", productCode); printf("Price: %.2lf\n", GetPrice(product)); printf("Count: %d\n\n", GetCount(product)); // Test 3 - Do setters work properly? strcpy(code, "Golden Delicious"); product = SetCode(code, product); price = 0.55; product = SetPrice(price, product); num = 4; product = SetCount(num, product); GetCode(productCode, product); printf("Name: %s\n", productCode); printf("Price: %.2lf\n", GetPrice(product)); printf("Count: %d\n", GetCount(product)); return 0; }
Implement in C Programing
7.10.1: LAB: Product struct
Given main(), build a struct called Product that will manage product inventory. Product struct has three data members: a product code (string), the product's price (double), and the number count of product in inventory (int). Assume product code has a maximum length of 20.
Implement the Product struct and related function declarations in Product.h, and implement the related function definitions in Product.c as listed below:
- Product InitProduct(char *code, double price, int count) - set the data members using the three parameters
- Product SetCode(char *code, Product product) - set the product code (i.e. SKU234) to parameter code
- void GetCode(char *productCode, Product product) - return the product code in productCode
- Product SetPrice(double price, Product product) - set the price to parameter product
- double GetPrice(Product product) - return the price
- Product SetCount(int count, Product product) - set the number of items in inventory to parameter num
- int GetCount(Product product) - return the count
- Product AddInventory(int amt, Product product) - increase inventory by parameter amt
- Product SellInventory(int amt, Product product) - decrease inventory by parameter amt
Ex. If a new Product struct is created with code set to "Apple", price set to 0.40, and the count set to 3, the output is:
Name: Apple Price: 0.40 Count: 3
Ex. If 10 apples are added to the Product struct's inventory, but then 5 are sold, the output is:
Name: Apple Price: 0.40 Count: 8
Ex. If the Product struct's code is set to "Golden Delicious", price is set to 0.55, and count is set to 4, the output is:
Name: Golden Delicious Price: 0.55 Count: 4
Product.c
#include <stdio.h>
#include <string.h>
#include "Product.h"
/* Type your code here */
Product.h
#ifndef PRODUCT_H_
#define PRODUCT_H_
//TODO: Create Product struct
/* Type your code here */
#endif
main.c
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images