databasePTR init_database(char *file_path) { size_t len; ssize_t chars; FILE *customerFile; char *token; char *linebuffer; char* name; float credit; int c_id; databasePTR db = createDatabase(); //make sure filepath is referencing a valid file if (!checkFile(file_path)) { printf("Error with filepath \n", file_path); exit(EXIT_FAILURE); } customerFile = fopen(file_path, "r"); len = 0; linebuffer = NULL; while ( (chars = getline(&linebuffer, &len, customerFile) != -1) ) // tokenize for each line in input file { token = strtok(linebuffer, "|"); // name is first if (!token) {printf("Invalid customer file format \n"); exit(1);} name = strdup(token); printf("name: %s \n", name); token = strtok(NULL, "|"); // then ID if (!token) {printf("Invalid customer file format \n"); exit(1);} c_id = atoi(token); printf("cid: %d \n", c_id); token = strtok(NULL, "|"); // then credit limit if (!token) {printf("Invalid customer file format \n"); exit(1);} credit = atof(token); customerPTR newCustomer = createCustomer(name, c_id, credit); addCustomer(db, newCustomer); } free(linebuffer); fclose(customerFile); return db; }
/* Reads and parses input from the input file. * Also adds input to the two lists. */ void readInput(FILE* inputFile, CustomerList* cList, OrderList* oList) { int id; char last_name[25]; char first_name[25]; char item_name[20]; float item_price; int item_quantity; while (1) { if( fscanf(inputFile, "%d %s %s %s $%f %d", &id, last_name, first_name, item_name, &item_price, &item_quantity) == EOF){ printf("End of file\n"); break; } /*printf("%d %s %s %s $%.2f %d\n", id, last_name, first_name, item_name, item_price, item_quantity);*/ //check if customer is already in CustomerList. if (containsCust(cList, id)) { addToCust(cList, id, item_price, item_quantity); //Add to list of orders Order* ord = createOrder(id, item_name, item_price, item_quantity); pushOList(oList, ord); } else { char combine_name[50]; strcpy(combine_name, last_name); strcat(combine_name, " "); strcat(combine_name, first_name); //Add to list of customers Customer* cust = createCustomer(id, combine_name, item_price, item_quantity); pushCList(cList, cust); //Add to list of orders Order* ord = createOrder(id, item_name, item_price, item_quantity); pushOList(oList, ord); } } }
static CustomerPtr createBronzeCustomer(const char* name, const Address* address) { return createCustomer(name, address, bronzePriceStrategy); }
static CustomerPtr createGoldCustomer(const char* name, const Address* address) { return createCustomer(name, address, goldPriceStrategy); }
static CustomerPtr createSilverCustomer(const char* name, const Address* address) { return createCustomer(name, address, silverPriceStrategy); }