/* Candidate Guide 1 - Module 201

                                                                                   Written by Christopher Krawczyk

                                                                                   Date of start: 27-12-2002

                                                                                   Date Ended: 28-12-2002

                                                                                */

                               

#include <stdio.h>                                                    /* Standard Libraries */

#include <stdlib.h>

#include <ctype.h>

#include <string.h>

#include <time.h>

 

 

 

#include <conio.h>                                                   /* Non standard library */

 

                                                /* Prototypes of functions */

 

void waitState(void);                                                                               

int chocolateInformation(void);

char choice(void);

int howManyChocolate(int chocolate);

void displayChocolateInfo(int chocolate);

int coinEntry(int chocolate, int quantity);

void chocolateDispense(int chocolate, int quantity);

 

int total = 0;                                            // global variable to store total price of chocolates

 

void waitState(void)

{

                printf("\n\n\n\n\n");

                printf("\t\tWelcome to The Vending Machine\n\n");

                printf("\t\tPlease press ANY key to continue\n\n");

 

                getch();                                                                    // wait for a character to be pressed

 

                system("cls");                                                          // clear the screen after pressing character

 

}

 

int chocolateInformation(void)

{

                auto int chocolate = 5;                                                             /* Chocolate the customer whishes to purchase */

               

                do

                {

 

                                printf("Please select the chocolate that you wish to purchase\n");

                                printf("Do this by pressing the number of the chocoloate\n\n");

 

                                printf("\t1:    All Milk Bar\n");

                                printf("\t2:    Sugar Drops\n");

                                printf("\t3:    Caramel Suprise\n");

                                printf("\t4:    Chunky Milk Bar\n\n");

 

                                printf("\tOr press 5 to quit and restart\n\n");

 

                                printf("Please make your selection now\n\n");

 

                                fflush(stdin);

 

                                scanf("%d", &chocolate);

 

                                if (chocolate != 1 && chocolate != 2 && chocolate != 3 && chocolate != 4)

                                {

                                                fflush(stdout);

                                                system("cls");

                                                printf("You have entered a wrong value, please re-enter\n\n");

                                                continue;

                                }

 

                                if (chocolate == 5 )

                                {

                                                printf("You have choosen to restart \n\n");

                                                system("cls");

                                                waitState();

                                }

 

                } while (chocolate != 1 && chocolate != 2 && chocolate != 3 && chocolate != 4);

 

                return chocolate;

}

 

char choice(void)

{

                                auto char decision;  

 

                                printf("\n\nWould you like to continue with the transaction?\n");

                                printf("Please enter Y to contine or anything else to restart\n\n");

 

                                fflush(stdin);

                                scanf("%c", &decision);

 

                                if (islower(decision))

                                                decision = toupper(decision);

 

                                system("cls");

 

                                return decision;

}

 

void displayChocolateInfo(int chocolate)

{

                printf("You have choosen to purchase ");

 

                switch (chocolate)

                {

                case 1: printf("All Milk Bar          Priced: 25p\n");

                                break;

                case 2: printf("Sugar Drops           Priced: 35p\n");

                                break;

                case 3: printf("Caramel Suprise       Priced: 50p\n");

                                break;

                case 4: printf("Chunky Milk Bar       Priced: 75p\n");

                                break;

                default: printf("An error has occured somewhere\n\n");

                                break;

                }

}

 

 

int howManyChocolate(int chocolate)

{

                auto int quantity;                                                     /* How many chocolates to purchase */

               

                printf("\n\nHow many ");

 

                switch (chocolate)

                {

                    case 1: printf("All Milk Bar");

                                                break;

                case 2: printf("Sugar Drops");

                                                break;

                    case 3: printf("Caramel Suprise");

                                                break;

                case 4: printf("Chunky Milk Bar");

                                                break;

                }

 

                printf(" would you like (max 5) ?\n\n");

 

                do

                {

                                fflush(stdin);

 

                                scanf("%d", &quantity);

 

                                if (quantity != 1 && quantity != 2 && quantity != 3 && quantity != 4 && quantity != 5)

                                {

                                                printf("You have entered an incorrect value, please re-enter\n\n");

                                                continue;

                                }

                                break;

 

                } while (quantity != 1 && quantity != 2 && quantity != 3 && quantity != 4 && quantity != 5);

 

                system("cls");

 

                return quantity;

}

 

int coinEntry(int chocolate, int quantity)

{

                auto int coin = 0, price = 0, remaining = 0;

               

                displayChocolateInfo(chocolate);

                printf("\n\n");

 

                switch(chocolate)

                {

                case 1: price = 25 * quantity;

                                break;

                case 2: price = 35 * quantity;

                                break;

                case 3: price = 50 * quantity;

                                break;

                case 4: price = 75 * quantity;

                                break;

                default: printf("An error has occured somewhere\n\n");

                                break;

                }

               

                printf("PLEASE ENTER %d PENCE IN COINS\n\n\n", price);

 

                printf("Coins shall be entered, by typing the value of the coin and pressing ENTER\n");

                printf("Only the following coins will be accepted:   5p  10p  20p  50p  100p  \n\n");

                printf("If you want to quit, enter 0 and press ENTER\n\n");

                printf("Please enter you coin and press ENTER\n\n\n");

 

                do

                {

                                fflush(stdin);

                                scanf("%d", &coin);                                                 /* Value of coin enterd here*/

 

                                if (coin != 0 && coin != 5 && coin != 10 && coin != 20 && coin != 50 && coin != 100)

                                {

                                                printf("You have entered a wrong value, please re-enter\n\n");

                                                continue;                                  // jump back to beginning of loop

                                }

 

                                switch (coin)

                                {

                                case 0: break;

                                case 5: total = total + 5;

                                                                break;

                                case 10: total = total + 10;

                                                                break;

                                case 20: total = total + 20;

                                                                break;

                                case 50: total = total + 50;

                                                                break;

                                case 100: total = total + 100;

                                                                break;

                                default: printf("You have entered an incorrect value, please re-enter\n\n");

                                }

 

                                if (coin == 0)                           /*if coin == 0 - exit loop && return 0 to main(void) */

                                                break;

 

                                remaining = price - total;

 

                                printf("So far you have entered %d pence\n\n", total);

                               

                                if (total < price)

                                {

                                                printf("You have %d pence remaining to pay in\n\n", remaining);

                                                printf("Please enter your next coin\n\n");

                                }

                                if (total == price)

                                {

                                                printf("You have now paid in enough money\n\n");

                                }

                                if (total > price)

                                {

                                                printf("You have paid in more than required\n");

                                                printf("you will be returned %d pence change later on\n\n", remaining * -1);

                                }

 

 

                } while ((price > total) && (coin != 0));

 

                return coin;

}

 

void chocolateDispense(int chocolate, int quantity)

{

 

                auto int i = 1;                                           /* Needed for a 'for' loop */

                auto int z =0;                                           /* Used to create a delay */

                auto char delay[50];                 /* Used to copy string to - creates delay */

                auto int remaining = 0;

 

                remaining = total;

 

                printf("PLEASE HOLD ON\n\n");

               

                for (z=0; z <= 123456789; z++)                                                               /*Keeps prcossor busy - creates a delay */

                {              strcpy(delay, "Hello World"); }

 

 

                printf("\n\nYour %d  ", quantity);

 

                switch (chocolate)

                {

                    case 1: printf("All Milk Bar");

                                                break;

                case 2: printf("Sugar Drops");

                                                break;

                    case 3: printf("Caramel Suprise");

                                                break;

                case 4: printf("Chunky Milk Bar");

                                                break;

                }

 

                printf(" is or are being dispensed one at a time - please hold on\n\n\n\n");

               

 

                for (i=1; i<=quantity; i++)

                {

 

 

                                switch (chocolate)

                                {

                                 case 1: printf("All Milk Bar");

                                                                break;

                                 case 2: printf("Sugar Drops");

                                                                break;

                                 case 3: printf("Caramel Suprise");

                                                                break;

                                 case 4: printf("Chunky Milk Bar");

                                                                break;

                                }

 

                                printf(" number: %d is being dispensed\n", i);

 

                                printf("Remaining money: ");

 

                                switch(chocolate)

                                {

                                case 1: remaining = remaining - 25;

                                                break;

                                case 2: remaining = remaining - 35;

                                                break;

                                case 3: remaining = remaining - 50;

                                                break;

                                case 4: remaining = remaining - 75;

                                                break;

                                }

 

                                printf("%d pence after this dispense\n\n\n", remaining);

 

                                for (z=0; z <= 123456789; z++)                                               /*Keeps prcossor busy - creates a delay */

                                {              strcpy(delay, "Hello World"); }

 

                }  /* for loop */

 

                                system("cls");

                                printf("\n\nThank you for using our Machine\n\n\n");

 

 

                                if (remaining > 0)

                                                printf("You have %d pence change to collect\n\n", remaining);

 

                                for (z=0; z <= 123456789; z++)                               /*Keeps prcossor busy - creates a delay */

                                {              strcpy(delay, "Hello World"); }

 

}

 

int main(void)

{

               

                auto int chocolate = 0;                             /* Stores the choosen chocoalte */

                auto char decision;                                   /* Quit and restart or continue */

                auto int quantity;                     /* How many chocoaltes to be purchased */

                auto coin = 1;                                           /* will restart program if user enters 0 when entering coins */

 

                while (decision)

                {

 

                                waitState();                                                              /* Display welcome screen */

               

                                chocolate = chocolateInformation();                                       /* Display info + get selection */

 

                                displayChocolateInfo(chocolate);

 

                                decision = choice();                                                                                  /* continue or quit */

 

                                if (decision != 'Y')

                                                {

                                                                system("cls");                                                          /* clear screen */

                                                                continue;                                  /* Jump bact to beginning of loop */

                                                }

 

                                quantity = howManyChocolate(chocolate);                              /* How many chocolates to purchase */

               

                                coin = coinEntry(chocolate, quantity);                                                    /* Enter coins */

                               

                                if (coin == 0)  {

                                                printf("Your money is now being returned - press Any key to continue\n\n");

                                                getch();

                                                system("cls");

                                                continue; 

                                }                                                                                              /* if coin 0 - restart program */

 

                                chocolateDispense(chocolate, quantity);

 

                                system("cls");                                          /* clears the screen */

 

                } /* while loop */

 

                return 0;

}

back to main         home