Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<io.h>
- #include<fcntl.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #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<max; x++)
- {
- if (isdigit(test[x]))
- {
- return FALSE;
- }
- if (x==max)
- {
- return TRUE;
- }
- x++;
- }
- return TRUE;
- }
- int fileSize()
- {
- int lengthOfFile;
- int file;
- file = open("Customers.dat",O_RDONLY,0);
- lengthOfFile = lseek (file,0, SEEK_END);
- return lengthOfFile;
- }
- int getNoOfRecords()
- {
- return (fileSize()/(STRUCTSIZE));
- }
- /**
- * This method will compare the ID passed from the ID of the customer to check
- * whether it is exists or not. If it exists it will output 1 otherwise it
- * will output -1. This will make sure that the Person's ID is unique
- *
- */
- int searchID (int cID)
- {
- // for the while loop
- int index;
- index = 0;
- //gets the number of records currently held in the file.
- int records;
- records = getNoOfRecords();
- //User will input the ID into this variable and it will be checked
- //whether it exists or not
- int IDstatus;
- IDstatus = 0;
- while (index != records)
- {
- fread (&c,STRUCTSIZE,1,cfp);
- if (c.ID == cID)
- {
- IDstatus = 1;
- }
- else
- {
- IDstatus = -1;
- }
- }
- return IDstatus;
- }
- while (index != records)
- {
- fread (&c,STRUCTSIZE,1,cfp);
- if (c.ID == cID)
- {
- IDstatus = 1;
- break; // <<<< otherwise IDStatus will be overwritten by next iteration
- }
- else
- {
- IDstatus = -1;
- }
- index++; // <<< otherwise endless loop
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement