icp/oer/courses/c-newcomers/sections/04-supermarket/05-customers/program.c

82 lines
1.9 KiB
C

#include <stdio.h>
#include <stdbool.h>
#include "supermarket.h"
#define CUSTOMER_ITEM_COUNT 5
typedef struct
{
item Items[CUSTOMER_ITEM_COUNT];
int PackingSpeed;
}customer;
int main()
{
bool CustomersLeft = true;
int TimePassed = 0;
// NOTE: the {...} syntax is an easy way to initialize structs
customer CustomerQueue[3] =
{
{
{tissues, pasta, tomato_sauce, bread, butter},
2
},
{
{toothbrush, batteries, bread, bread, butter},
3
},
{
{bread, batteries, tomato_sauce, pasta, tissues},
9
}
};
int NextQueueIndex = 0;
initTimeTable();
int ItemsLeft = CUSTOMER_ITEM_COUNT;
int TimeToScan = 0;
state CurrentState = STATE_ready_for_customer;
int CurrentCustomer = 0;
int CurrentItemIndex;
while(CustomersLeft)
{
if(CurrentState == STATE_ready_for_item)
{
// TODO: check if there are items left
// if not, change to the STATE_ready_for_customer state
// and don't forget to reset ItemsLeft
CurrentItemIndex = CUSTOMER_ITEM_COUNT - ItemsLeft;
item CurrentItem = CustomerQueue[CurrentCustomer].Items[CurrentItemIndex];
TimeToScan = Time[CurrentItem] + CustomerQueue[CurrentCustomer].PackingSpeed;
CurrentState = STATE_scanning;
}
else if (CurrentState == STATE_scanning)
{
TimePassed ++;
TimeToScan --;
if (TimeToScan == 0)
{
ItemsLeft--;
CurrentState = STATE_ready_for_item;
// NOTE: we moved the printf inside so the console doesn't get spammed with messages
printf("Time since start: %ds. Scanned product %d. %d to go.\n", TimePassed, CurrentItemIndex, ItemsLeft);
}
}
else // NOTE: we added the state STATE_ready_for_customer
{
// TODO: implement this.
// don't forget to check if there are any customers left and to change
// the state
printf("Currently serving customer %d\n", CurrentCustomer);
CustomersLeft = false;
}
}
}