252 lines
No EOL
6.8 KiB
C
252 lines
No EOL
6.8 KiB
C
/*----------------------------------------------------------
|
|
* HTBLA-Leonding, 2. JG
|
|
* Marc Tismonar
|
|
* ---------------------------------------------------------
|
|
* Description:
|
|
* Implementation of customer_list.h.
|
|
* ----------------------------------------------------------
|
|
*/
|
|
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
#include "customer_list.h"
|
|
|
|
//redefine NULL, bc Mr. Schraml said that its not safe
|
|
#ifdef NULL
|
|
#undef NULL
|
|
#define NULL 0
|
|
#endif
|
|
|
|
struct CustomerListEntryData {
|
|
int id;
|
|
const char* name;
|
|
int revenue;
|
|
};
|
|
|
|
struct CustomerListData {
|
|
int length;
|
|
CustomerListEntry customers[MAX_LIST_ENTRIES];
|
|
};
|
|
|
|
static CustomerList staticCl;
|
|
|
|
|
|
CustomerList init_customer_list() {
|
|
staticCl = (CustomerList)calloc(1, sizeof(struct CustomerListData)); //using calloc :) it initializes the memory with 0
|
|
return staticCl;
|
|
}
|
|
|
|
int get_length(const CustomerList cl) {
|
|
if (cl == 0) {
|
|
return -1;
|
|
}
|
|
return cl->length;
|
|
}
|
|
|
|
CustomerListEntry create_customer(int id, const char* customer_name, int revenue) {
|
|
if (id < 0 || customer_name == 0 || revenue < 0) {
|
|
return 0;
|
|
}
|
|
|
|
CustomerListEntry new_customer = (CustomerListEntry)calloc(1, sizeof(struct CustomerListEntryData));
|
|
if (new_customer == 0) {
|
|
return 0;
|
|
}
|
|
new_customer->id = id;
|
|
new_customer->name = customer_name;
|
|
new_customer->revenue = revenue;
|
|
return new_customer;
|
|
}
|
|
|
|
bool add_customer(CustomerList cl, int id, const char* customer_name, int revenue) {
|
|
if (cl == 0 || id < 0 || customer_name == 0 || revenue < 0) {
|
|
return false;
|
|
}
|
|
|
|
//check if the customer list is full
|
|
if (cl->length >= MAX_LIST_ENTRIES) {
|
|
return false;
|
|
}
|
|
|
|
//check if its already in the list
|
|
for (int i = 0; i < cl->length; i++) {
|
|
if (cl->customers[i]->id == id) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
CustomerListEntry new_customer = create_customer(id, customer_name, revenue);
|
|
if (new_customer == 0) {
|
|
return false;
|
|
}
|
|
cl->customers[cl->length] = new_customer;
|
|
cl->length++;
|
|
return true;
|
|
}
|
|
|
|
bool add_customers(CustomerList cl, CustomerListEntry e[], int n) {
|
|
if (cl == 0 || e == 0 || n < 0) {
|
|
return false;
|
|
}
|
|
|
|
//check if customer list is full
|
|
if (cl->length + n > MAX_LIST_ENTRIES) {
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
// null check
|
|
if (e[i] == 0) {
|
|
return false;
|
|
}
|
|
|
|
//duplication check
|
|
for (int j = 0; j < cl->length; j++) {
|
|
if (cl->customers[j]->id == e[i]->id) {
|
|
return false;
|
|
}
|
|
}
|
|
cl->customers[cl->length] = e[i];
|
|
cl->length++;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
CustomerListEntry get_customer_with_index(const CustomerList cl, int index) {
|
|
if (cl == 0 || index < 0 || index >= cl->length) {
|
|
return 0;
|
|
}
|
|
return cl->customers[index];
|
|
}
|
|
|
|
CustomerListEntry get_customer_with_id(const CustomerList cl, int id) {
|
|
if (cl == 0 || id < 0) {
|
|
return 0;
|
|
}
|
|
|
|
for (int i = 0; i < cl->length; i++) {
|
|
if (cl->customers[i]->id == id) {
|
|
return cl->customers[i];
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int get_customers_with_revenue(const CustomerList cl, int lower_bound, int upper_bound, CustomerListEntry result_list[], int result_len) {
|
|
if (cl == 0 || lower_bound < 0 || upper_bound < 0 || lower_bound > upper_bound || result_list == 0 || result_len < 0) {
|
|
return 0;
|
|
}
|
|
int result_count = 0;
|
|
|
|
//go through customer list & check if revenue is in bounds
|
|
for (int i = 0; i < cl->length; i++) {
|
|
if (cl->customers[i]->revenue >= lower_bound && cl->customers[i]->revenue <= upper_bound) {
|
|
if (result_count < result_len) {
|
|
result_list[result_count] = cl->customers[i];
|
|
}
|
|
result_count++;
|
|
}
|
|
}
|
|
return result_count;
|
|
}
|
|
|
|
CustomerListEntry get_customer_with_highest_revenue(const CustomerList cl) {
|
|
if (cl == 0) {
|
|
return 0;
|
|
}
|
|
CustomerListEntry highest_revenue_customer = cl->customers[0];
|
|
|
|
//go through customer list & return with highest revenue
|
|
for (int i = 1; i < cl->length; i++) {
|
|
if (cl->customers[i]->revenue > highest_revenue_customer->revenue) {
|
|
highest_revenue_customer = cl->customers[i];
|
|
}
|
|
}
|
|
return highest_revenue_customer;
|
|
}
|
|
|
|
CustomerListEntry get_customer_with_lowest_revenue(const CustomerList cl) {
|
|
if (cl == 0) {
|
|
return 0;
|
|
}
|
|
CustomerListEntry lowest_revenue_customer = cl->customers[0];
|
|
|
|
//go through customer list & return with lowest revenue
|
|
for (int i = 1; i < cl->length; i++) {
|
|
if (cl->customers[i]->revenue < lowest_revenue_customer->revenue) {
|
|
lowest_revenue_customer = cl->customers[i];
|
|
}
|
|
}
|
|
return lowest_revenue_customer;
|
|
}
|
|
|
|
int get_top_n_customers_revenue(const CustomerList cl, int n, CustomerListEntry result_list[]) {
|
|
if (cl == 0 || n <= 0 || result_list == 0) {
|
|
return 0;
|
|
}
|
|
|
|
// make a copy of customer list
|
|
CustomerListEntry temp_list[MAX_LIST_ENTRIES];
|
|
for (int i = 0; i < cl->length; i++) {
|
|
temp_list[i] = cl->customers[i];
|
|
}
|
|
|
|
// sort the copy of customer list descending
|
|
for (int i = 0; i < cl->length - 1; i++) {
|
|
for (int j = i + 1; j < cl->length; j++) {
|
|
if (temp_list[i]->revenue < temp_list[j]->revenue) {
|
|
CustomerListEntry temp = temp_list[i];
|
|
temp_list[i] = temp_list[j];
|
|
temp_list[j] = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
// copy the n top customers to result_list
|
|
int result_count = n < cl->length ? n : cl->length;
|
|
for (int i = 0; i < result_count; i++) {
|
|
result_list[i] = temp_list[i];
|
|
}
|
|
|
|
return result_count;
|
|
}
|
|
|
|
int get_bottom_n_customers_revenue(const CustomerList cl, int n, CustomerListEntry result_list[]) {
|
|
if (cl == 0 || n <= 0 || result_list == 0) {
|
|
return 0;
|
|
}
|
|
|
|
// make a copy of customer list
|
|
CustomerListEntry temp_list[MAX_LIST_ENTRIES];
|
|
for (int i = 0; i < cl->length; i++) {
|
|
temp_list[i] = cl->customers[i];
|
|
}
|
|
|
|
// sort the copy of customer list ascending
|
|
for (int i = 0; i < cl->length - 1; i++) {
|
|
for (int j = i + 1; j < cl->length; j++) {
|
|
if (temp_list[i]->revenue > temp_list[j]->revenue) {
|
|
CustomerListEntry temp = temp_list[i];
|
|
temp_list[i] = temp_list[j];
|
|
temp_list[j] = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
// copy the n bottom customers to result_list
|
|
int result_count = n < cl->length ? n : cl->length;
|
|
for (int i = 0; i < result_count; i++) {
|
|
result_list[i] = temp_list[i];
|
|
}
|
|
|
|
return result_count;
|
|
}
|
|
|
|
int get_customer_id(const CustomerListEntry customer) {
|
|
if (customer == 0) {
|
|
return -1;
|
|
}
|
|
return customer->id;
|
|
} |