#include #include #include #include #include #include #include "..Headerscommon.h" #include "..HeaderscustomerManagement.h" static FILE *cfp; static customer c; #define STRUCTSIZE sizeof (customer) /** This is the Customers's Main Menu in which the various sections can be * accessed from here */ boolean customerMainMenu() { int optionC; clrscr(); copyright(); printf ("nnnntt ************* Customer's Main Menu *************n n n"); printf ("Press [1] to add a new Customern"); printf ("Press [2] to edit a Customern"); printf ("Press [3] to list all Customersn"); printf ("Press [4] to Show a Customer's last Ordern"); printf ("Press [5] to go back to Main Menunnn"); if (scanf ("%d",&optionC) == 1) { switch (optionC) { case 1: { clrscr(); getchar(); addCustomer(); break; } case 2: { printf ("Edit a Customern"); break; } case 3: { clrscr(); listCustomers(); getchar(); while (getchar()!='n') { } break; } case 4: { printf ("Customer's Last Ordern"); break; } case 5: { system ("PAUSE"); break; } default: { if (optionC != 1 || optionC != 2 || optionC != 3 || optionC != 4 || optionC !=5) { clrscr(); printf ("Invalid option!n"); system ("PAUSE"); customerMainMenu(); } break; } } } return TRUE; } /** * This following method will append a customer to the * database at the end of the file * * */ void addCustomer () { char ch; copyright(); printf ("nnnntt ************* Add Client **********n n n"); if ((cfp = fopen ("customers.dat","a+b")) == NULL) { fputs("Can't open customers.dat filen",stderr); } printf ("tThis will add another customer to the the databasen"); printf ("tPress 'Y' to confirm or 'N' to return to the Client Main MenuntWITHOUT adding a customern"); ch = getchar(); if (ch == 'n' || ch == 'N') { customerMainMenu(); } else if (ch == 'y' || ch == 'Y') { clrscr(); printf ("nnnntt ************* Add Client **********n n n"); printf ("Please enter Name:n"); while (scanf ("%s", c.name) == 0 || cCheck(c.name,100) == FALSE); { } printf ("Please Enter Surname: n"); while (scanf ("%s",c.surname) == 0 && cCheck (c.surname,100) == FALSE); { } printf ("Please Enter ID Card, [NOTE! Only numbers are allowed!]n"); int cID; cID = 0; while (scanf ("%d",&cID)==0) { printf ("Only Numbers are allowed!n"); while (getchar() != 'n') { } } if (searchID(cID) == 1) { printf ("This ID already exists. Client already exists!n"); printf ("Do you want to input another ID or return to Main Menu?n"); printf ("Press 'Y' if you enter another ID, press any other key to return to Main Menun:"); ch = getchar(); if (ch == 'y' || ch == 'Y') { printf ("Enter another ID:n"); while (scanf ("%d",&cID)==0) { printf ("Only Numbers are allowed!n"); while (getchar() != 'n') { } } searchID(cID); } else if (searchID(cID) == -1) { cID = c.ID; getchar(); } } while (getchar()!='n') { } printf ("Please Enter Address:n"); gets(c.address); fwrite (&c,STRUCTSIZE, 1, cfp); printf ("For Testing purposes:n"); printf (" %sn %sn %sn %dn", c.name, c.surname, c.address, c.ID); askAnother(); } else { printf ("nInvalid choice! Either Y or N is acceptedn"); system ("PAUSE"); getchar(); addCustomer(); } } void listCustomers() { if ((cfp = fopen ("customers.dat","rb")) == NULL) { fputs("Can't open customers.dat filen",stderr); printf ("Returning to Customer Main Menu"); system ("PAUSE"); customerMainMenu(); } rewind (cfp); while (fread (&c,STRUCTSIZE,1,cfp)==1) { printf ("Customer: %s %s ID: %dn", c.surname, c.name, c.ID); } fclose (cfp); // system ("PAUSE"); } void askAnother() { printf ("Do you want to add another Customer?n"); printf ("Enter 'Y' for yes and 'N' to return to the Main Menun"); char input; input = getchar(); if (input == 'Y' || input == 'y') { getchar(); addCustomer(); } else if (input == 'N'|| input == 'n') { fclose (cfp); customerMainMenu(); } else { printf ("Invalid Option! Only Y or N are allowedn"); system ("PAUSE"); askAnother(); } } boolean cCheck(char *test, int max) { int x; for (x =0; x