Stock Control System
Loading...
Searching...
No Matches
Product.c
Go to the documentation of this file.
1/*
2 * Copyright (c) All Rights Reserved
3 * 2025 Oliver Dixon <od641@york.ac.uk>
4 */
5
14#include "Product.h"
15#include <stdio.h>
16
17#include <errno.h>
18#include <stdlib.h>
19#include <string.h>
20
21struct Product
22{
23 unsigned int id;
24 unsigned int quantity;
26};
27
28struct Product *product_create(const unsigned int id, const char *const name, const unsigned int quantity)
29{
30 const size_t name_length = strlen(name);
31
32 if (name_length > MAX_NAME_LENGTH) {
33 fprintf(stderr, "Name of product \"%s\" exceeds maximum character limit.\n", name);
34 return NULL;
35 }
36
37 errno = 0;
38 struct Product *product = malloc(sizeof(struct Product));
39
40 if (product == NULL) {
41 fprintf(stderr, "Could not create product \"%s\": %s\n", name, strerror(errno));
42 return NULL;
43 }
44
45 product->id = id;
46 product->quantity = quantity;
47 strncpy(product->name, name, name_length);
48 product->name[name_length] = '\0';
49
50 return product;
51}
52
53void product_delete(struct Product **this)
54{
55 free(*this);
56 *this = NULL;
57}
58
59int product_report_serialise(const struct Product *const this, FILE *const buffer)
60{
61 return fprintf(buffer, "%-4d | %-" XSTR(MAX_NAME_LENGTH) "s | %-11d\n", this->id, this->name, this->quantity);
62}
63
64int product_file_serialise(const struct Product *const this, FILE *const buffer)
65{
66 return fprintf(buffer, "%s\n%d\n", this->name, this->quantity);
67}
68
69bool product_modify_quantity(struct Product *this, const int modifier)
70{
71 if (modifier < 0 && (unsigned int) -modifier > this->quantity)
72 return false;
73
74 this->quantity += modifier;
75 return true;
76}
77
78int product_ptr_compare(const void *const product_lhs, const void *const product_rhs)
79{
80 const unsigned int lhs_quantity = (*(const struct Product **) product_lhs)->quantity;
81 const unsigned int rhs_quantity = (*(const struct Product **) product_rhs)->quantity;
82
83 if (lhs_quantity < rhs_quantity)
84 return -1;
85
86 if (lhs_quantity > rhs_quantity)
87 return 1;
88
89 return 0;
90}
struct Product * product_create(const unsigned int id, const char *const name, const unsigned int quantity)
Definition Product.c:28
int product_file_serialise(const struct Product *const this, FILE *const buffer)
Definition Product.c:64
void product_delete(struct Product **this)
Definition Product.c:53
int product_ptr_compare(const void *const product_lhs, const void *const product_rhs)
Definition Product.c:78
bool product_modify_quantity(struct Product *this, const int modifier)
Definition Product.c:69
Product class specification.
#define MAX_NAME_LENGTH
Maximum number of bytes for a Product name, excluding the NULL-terminator.
Definition Product.h:26
#define XSTR(VALUE)
Definition Product.h:20
The Product represents a single product with a unique numerical identifier, human-readable name,...
Definition Product.c:22
char name[MAX_NAME_LENGTH+1]
NULL-terminated human-readable name of the Product.
Definition Product.c:25
unsigned int id
Numerical identifier of the Product.
Definition Product.c:23
unsigned int quantity
Non-negative number of Product items in stock.
Definition Product.c:24
int product_report_serialise(const struct Product *this, FILE *buffer)
Serialise the given Product in the report format to the given buffer.
Definition Product.c:59