1. #include<io.h>
  2. #include<fcntl.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include "..Headerscommon.h"
  8. #include "..HeaderscustomerManagement.h"
  9.  
  10. static FILE *cfp;
  11. static customer c;
  12. #define STRUCTSIZE sizeof (customer)
  13.  
  14.  
  15. /** This is the Customers's Main Menu in which the various sections can be
  16. * accessed from here
  17. */
  18. boolean customerMainMenu()
  19. {
  20.  
  21.  
  22.  
  23. int optionC;
  24. clrscr();
  25.  
  26. copyright();
  27.  
  28.  
  29. printf ("nnnntt ************* Customer's Main Menu *************n n n");
  30.  
  31. printf ("Press [1] to add a new Customern");
  32. printf ("Press [2] to edit a Customern");
  33. printf ("Press [3] to list all Customersn");
  34. printf ("Press [4] to Show a Customer's last Ordern");
  35. printf ("Press [5] to go back to Main Menunnn");
  36.  
  37.  
  38. if (scanf ("%d",&optionC) == 1)
  39. {
  40. switch (optionC)
  41. {
  42.  
  43. case 1:
  44. {
  45. clrscr();
  46. getchar();
  47. addCustomer();
  48. break;
  49. }
  50. case 2:
  51. {
  52. printf ("Edit a Customern");
  53. break;
  54. }
  55.  
  56. case 3:
  57. {
  58. clrscr();
  59. listCustomers();
  60. getchar();
  61. while (getchar()!='n')
  62. {
  63.  
  64. }
  65. break;
  66. }
  67. case 4:
  68. {
  69. printf ("Customer's Last Ordern");
  70. break;
  71. }
  72. case 5:
  73. {
  74. system ("PAUSE");
  75. break;
  76. }
  77. default:
  78. {
  79. if (optionC != 1 || optionC != 2 || optionC != 3 || optionC != 4 || optionC !=5)
  80. {
  81. clrscr();
  82. printf ("Invalid option!n");
  83. system ("PAUSE");
  84. customerMainMenu();
  85. }
  86. break;
  87. }
  88. }
  89. }
  90. return TRUE;
  91.  
  92. }
  93.  
  94. /**
  95. * This following method will append a customer to the
  96. * database at the end of the file
  97. *
  98. * */
  99.  
  100. void addCustomer ()
  101. {
  102. char ch;
  103. copyright();
  104.  
  105. printf ("nnnntt ************* Add Client **********n n n");
  106.  
  107. if ((cfp = fopen ("customers.dat","a+b")) == NULL)
  108. {
  109. fputs("Can't open customers.dat filen",stderr);
  110. }
  111.  
  112.  
  113.  
  114. printf ("tThis will add another customer to the the databasen");
  115. printf ("tPress 'Y' to confirm or 'N' to return to the Client Main MenuntWITHOUT adding a customern");
  116. ch = getchar();
  117.  
  118. if (ch == 'n' || ch == 'N')
  119. {
  120. customerMainMenu();
  121. }
  122. else if (ch == 'y' || ch == 'Y')
  123. {
  124.  
  125. clrscr();
  126. printf ("nnnntt ************* Add Client **********n n n");
  127. printf ("Please enter Name:n");
  128. while (scanf ("%s", c.name) == 0 || cCheck(c.name,100) == FALSE);
  129. {
  130.  
  131. }
  132.  
  133.  
  134. printf ("Please Enter Surname: n");
  135. while (scanf ("%s",c.surname) == 0 && cCheck (c.surname,100) == FALSE);
  136. {
  137.  
  138. }
  139.  
  140. printf ("Please Enter ID Card, [NOTE! Only numbers are allowed!]n");
  141. int cID;
  142. cID = 0;
  143. while (scanf ("%d",&cID)==0)
  144. {
  145. printf ("Only Numbers are allowed!n");
  146. while (getchar() != 'n')
  147. {
  148. }
  149. }
  150.  
  151. if (searchID(cID) == 1)
  152. {
  153. printf ("This ID already exists. Client already exists!n");
  154. printf ("Do you want to input another ID or return to Main Menu?n");
  155. printf ("Press 'Y' if you enter another ID, press any other key to return to Main Menun:");
  156.  
  157. ch = getchar();
  158. if (ch == 'y' || ch == 'Y')
  159. {
  160. printf ("Enter another ID:n");
  161. while (scanf ("%d",&cID)==0)
  162. {
  163. printf ("Only Numbers are allowed!n");
  164. while (getchar() != 'n')
  165. {
  166. }
  167. }
  168. searchID(cID);
  169. }
  170. else if (searchID(cID) == -1)
  171. {
  172. cID = c.ID;
  173. getchar();
  174. }
  175.  
  176. }
  177.  
  178.  
  179.  
  180. while (getchar()!='n')
  181. {
  182.  
  183. }
  184.  
  185. printf ("Please Enter Address:n");
  186. gets(c.address);
  187.  
  188.  
  189. fwrite (&c,STRUCTSIZE, 1, cfp);
  190.  
  191. printf ("For Testing purposes:n");
  192. printf (" %sn %sn %sn %dn", c.name, c.surname, c.address, c.ID);
  193. askAnother();
  194.  
  195.  
  196. }
  197. else
  198. {
  199. printf ("nInvalid choice! Either Y or N is acceptedn");
  200. system ("PAUSE");
  201. getchar();
  202. addCustomer();
  203. }
  204. }
  205.  
  206. void listCustomers()
  207. {
  208.  
  209. if ((cfp = fopen ("customers.dat","rb")) == NULL)
  210. {
  211. fputs("Can't open customers.dat filen",stderr);
  212. printf ("Returning to Customer Main Menu");
  213. system ("PAUSE");
  214. customerMainMenu();
  215. }
  216.  
  217. rewind (cfp);
  218.  
  219. while (fread (&c,STRUCTSIZE,1,cfp)==1)
  220. {
  221. printf ("Customer: %s %s ID: %dn", c.surname, c.name, c.ID);
  222. }
  223. fclose (cfp);
  224. // system ("PAUSE");
  225.  
  226. }
  227.  
  228.  
  229. void askAnother()
  230. {
  231. printf ("Do you want to add another Customer?n");
  232. printf ("Enter 'Y' for yes and 'N' to return to the Main Menun");
  233.  
  234. char input;
  235. input = getchar();
  236.  
  237. if (input == 'Y' || input == 'y')
  238. {
  239. getchar();
  240. addCustomer();
  241. }
  242. else if (input == 'N'|| input == 'n')
  243. {
  244.  
  245. fclose (cfp);
  246. customerMainMenu();
  247.  
  248. }
  249. else
  250. {
  251. printf ("Invalid Option! Only Y or N are allowedn");
  252. system ("PAUSE");
  253. askAnother();
  254.  
  255. }
  256.  
  257. }
  258.  
  259.  
  260. boolean cCheck(char *test, int max)
  261. {
  262. int x;
  263. for (x =0; x<max; x++)
  264. {
  265. if (isdigit(test[x]))
  266. {
  267. return FALSE;
  268. }
  269. if (x==max)
  270. {
  271. return TRUE;
  272. }
  273. x++;
  274.  
  275. }
  276. return TRUE;
  277. }
  278.  
  279. int fileSize()
  280. {
  281. int lengthOfFile;
  282. int file;
  283.  
  284. file = open("Customers.dat",O_RDONLY,0);
  285. lengthOfFile = lseek (file,0, SEEK_END);
  286.  
  287. return lengthOfFile;
  288. }
  289.  
  290. int getNoOfRecords()
  291. {
  292. return (fileSize()/(STRUCTSIZE));
  293. }
  294.  
  295. /**
  296. * This method will compare the ID passed from the ID of the customer to check
  297. * whether it is exists or not. If it exists it will output 1 otherwise it
  298. * will output -1. This will make sure that the Person's ID is unique
  299. *
  300. */
  301.  
  302. int searchID (int cID)
  303. {
  304. // for the while loop
  305. int index;
  306. index = 0;
  307. //gets the number of records currently held in the file.
  308. int records;
  309. records = getNoOfRecords();
  310.  
  311. //User will input the ID into this variable and it will be checked
  312. //whether it exists or not
  313.  
  314.  
  315. int IDstatus;
  316. IDstatus = 0;
  317.  
  318. while (index != records)
  319. {
  320. fread (&c,STRUCTSIZE,1,cfp);
  321. if (c.ID == cID)
  322. {
  323. IDstatus = 1;
  324. }
  325. else
  326. {
  327. IDstatus = -1;
  328. }
  329. }
  330.  
  331. return IDstatus;
  332.  
  333. }
  334.  
  335. while (index != records)
  336. {
  337. fread (&c,STRUCTSIZE,1,cfp);
  338. if (c.ID == cID)
  339. {
  340. IDstatus = 1;
  341. break; // <<<< otherwise IDStatus will be overwritten by next iteration
  342. }
  343. else
  344. {
  345. IDstatus = -1;
  346. }
  347. index++; // <<< otherwise endless loop
  348. }