Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.10 KB | None | 0 0
  1. #ifndef MAIN_H_INCLUDED
  2. #define MAIN_H_INCLUDED
  3.  
  4. void clearInput();
  5. void addNewAccount();
  6. char* myGets();
  7. void listAll();
  8. int promptForDelete();
  9. void loadRecordsFromFile();
  10. void saveRecordsToFile();
  11. void promptAndStoreRecordInMemory();
  12.  
  13. struct account {
  14. int number;
  15.  
  16. char lastname[15];
  17. char firstname[15];
  18.  
  19. float balance;
  20. struct account *next; /*This is the "Link" to the next list record/node*/
  21. };
  22.  
  23. struct account *firsta, *currenta, *newa;
  24.  
  25.  
  26. #endif // MAIN_H_INCLUDED
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include "deleteARecord.h"
  33. #include "modifyARecord.h"
  34. #include "main.h"
  35. int anum = 0;
  36.  
  37. /*PROGRAM ENTRY POINT*/
  38. int main() {
  39. char ch;
  40. firsta = NULL;
  41. loadRecordsFromFile();
  42.  
  43.  
  44. do {
  45. clearInput();
  46. puts("A - Add a new account");
  47. puts("D - Delete a record");
  48. puts("M - Modify a Record Account #");
  49. puts("L - List all records");
  50. puts("Q - Save Records and Quit this programn");
  51. printf("tYour choice:");
  52. ch = getchar();
  53. clearInput();
  54. ch = toupper(ch);
  55. switch (ch) {
  56. case 'A':
  57. puts("Add new accountn");
  58. printf("Original Input: %c", ch);
  59. addNewAccount();
  60. break;
  61. case 'D':
  62. puts("Delete a recordn");
  63. deleteARecord();
  64. break;
  65. case 'L':
  66. puts("List All Accountsn");
  67. listAll();
  68. break;
  69. case 'M':
  70. puts("Modify a Recordn");
  71. modifyARecord();
  72. break;
  73. case 'Q':
  74. puts("Quitn");
  75. break;
  76. case 'n':
  77. ch = getchar();
  78. default:
  79. break;
  80. }
  81. } while (ch != 'Q');
  82.  
  83. saveRecordsToFile();
  84. if (newa != NULL){
  85. free(newa);
  86. printf("n Memory freed!");
  87. }
  88. return(0);
  89. }
  90.  
  91. void saveRecordsToFile(){
  92. FILE *f;
  93. currenta = firsta;
  94. if(currenta == NULL)
  95. return;
  96.  
  97. f = fopen("record.tdb","w");
  98. if(f == NULL)
  99. {
  100. printf("Error writing to file!n");
  101. return;
  102. }
  103.  
  104. while(currenta != NULL)
  105. {
  106. fwrite(currenta,sizeof(struct account),1,f);
  107. currenta = currenta->next;
  108. }
  109. fclose(f);
  110. printf("Data saved to disk!n");
  111. return;
  112.  
  113. }
  114.  
  115.  
  116. void loadRecordsFromFile(){
  117. FILE *f;
  118. f = fopen("record.tdb","r");
  119. if(f == NULL){
  120. printf("Error opening file or file does not exist yet...");
  121. return;
  122. }
  123. firsta = malloc(sizeof(struct account));
  124. currenta = firsta;
  125.  
  126. while(1){
  127. newa = (struct account *)malloc(sizeof(struct account));
  128. /*This line reads each struct from the file and stores in currenta*/
  129. fread(currenta,sizeof(struct account),1,f);
  130. /*If we're at the last record, break out of the loop.*/
  131. if(currenta->next == NULL)
  132. break;
  133.  
  134. /*Otherwise, continue to the next iteration/link in file*/
  135. currenta->next = newa;
  136. currenta = newa;
  137. }
  138. fclose(f);
  139. anum = currenta->number;
  140. }
  141. void clearInput(void)
  142. {
  143. fflush(stdin);
  144. }
  145.  
  146. void listAll(void){
  147.  
  148. unsigned int counter = 0;
  149. if(firsta != NULL)
  150. currenta = firsta;
  151. else{
  152. printf("There are no items in the list.n");
  153. return;
  154. }
  155. while(currenta != NULL){
  156. printf("----------------------------n");
  157. printf("ID#:%dn",currenta->number);
  158. printf("Last Name: %sn",currenta->lastname);
  159. printf("First Name: %sn", currenta->firstname);
  160. printf("Balance: %.2fn", currenta->balance);
  161. counter++;
  162. currenta = currenta->next;
  163. }
  164.  
  165. }
  166.  
  167. //////////////////////////////////////////////////////////////
  168. void addNewAccount(void) {
  169. newa = malloc(sizeof(struct account));
  170.  
  171. /*If this is the first record, initialize all pointers to this record*/
  172.  
  173. if (firsta == NULL)
  174. firsta = currenta = newa;
  175. /*Otherwise, find the end (NULL ptr) and insert there*/
  176. else {
  177. currenta = firsta; /*Select first record then loop thru all records til end*/
  178.  
  179. while (currenta->next != NULL)
  180. currenta = currenta->next;
  181. /*Last record found*/
  182. currenta->next = newa; /*assign the last record's NEXT ptr to our new record to be inserted*/
  183. currenta = newa; /*switch to the newly inserted record and make it currently selected*/
  184. }
  185.  
  186. /*Fill in the new record with data*/
  187. anum++;
  188. promptAndStoreRecordInMemory();
  189.  
  190. }
  191. //////////////////////////////////////////////////////////////
  192. void promptAndStoreRecordInMemory()
  193. {
  194. printf("%27s: %5in", "Account number", anum);
  195. currenta->number = anum;
  196. clearInput();
  197. printf("%27s: ", "Enter customer's last name");
  198. gets(currenta->lastname);
  199. //getchar(); //Clear buffer
  200. printf("The lastname is: "%s"", currenta->lastname);
  201. printf("n");
  202. printf("%27s ", "Enter customer's first name");
  203. gets(currenta->firstname);
  204. //getchar(); //Clear buffer
  205. printf("n");
  206. printf("%27s: $", "Enter account balance");
  207. scanf("%f", &currenta->balance);
  208. currenta->next = NULL;
  209. return;
  210. }
  211. //////////////////////////////////////////////////////////////
  212. int promptForDelete(void){
  213. int numberToDelete;
  214. char shouldContinue;
  215. do{
  216. printf("Enter an ID number of the record you want to delete: ");
  217. scanf("%d",&numberToDelete);
  218. printf("You entered %d, is this correct? (y/n)",numberToDelete);
  219. getchar();
  220. shouldContinue = getchar();
  221. shouldContinue = toupper(shouldContinue);
  222. /*TODO: Refactor*/
  223. switch(shouldContinue){
  224. case 'Y':
  225. return numberToDelete;
  226. default:
  227. break;
  228. }
  229. }while(shouldContinue != 'Y');
  230. return -1;
  231. }
  232.  
  233. #ifndef DELETEARECORD_H_INCLUDED
  234. #define DELETEARECORD_H_INCLUDED
  235.  
  236. void deleteARecord(void);
  237.  
  238. #endif // DELETEARECORD_H_INCLUDED
  239.  
  240. #include <stdio.h>
  241. #include <stdlib.h>
  242. #include <string.h>
  243. #include <ctype.h>
  244. #include "deleteARecord.h"
  245. #include "main.h"
  246. void deleteARecord(){
  247. int numberToDelete;
  248. struct account *previousa;
  249.  
  250. numberToDelete = promptForDelete();
  251. if(numberToDelete == -1)
  252. {
  253. printf("No items to delete - (-1 return)n");
  254. return;
  255. }
  256. if (firsta == NULL)
  257. {
  258. printf("There are no items to delete!");
  259. return;
  260. }
  261. else
  262. {
  263. currenta = firsta;
  264.  
  265. while(currenta != NULL)
  266. {
  267. if(currenta->number == numberToDelete)
  268. {
  269. if(currenta == firsta) /*Special case for first record because it has no previous record*/
  270. firsta = currenta->next;
  271. else
  272. previousa->next = currenta->next;
  273. free(currenta);
  274. printf("Account #%d deleted.n", numberToDelete);
  275. listAll();
  276. return;
  277. }
  278. else
  279. {
  280. previousa = currenta;
  281. currenta = currenta->next;
  282. }
  283. }
  284. printf("Account #%d was not found.n",numberToDelete);
  285. puts("Nothing deleted");
  286.  
  287. }
  288. }
  289.  
  290. #ifndef MODIFYARECORD_H_INCLUDED
  291. #define MODIFYARECORD_H_INCLUDED
  292.  
  293. void modifyARecord(void);
  294.  
  295.  
  296. #endif // MODIFYARECORD_H_INCLUDED
  297.  
  298. #include <stdio.h>
  299. #include "modifyARecord.h"
  300. #define TRUE 1
  301. #define FALSE 0
  302. #include "main.h"
  303. /*This struct serves as an "object" to hold the numbers and a bool flag*/
  304.  
  305. struct accountNumbers{
  306. int numberToModify;
  307. int newNumber;
  308. unsigned int success;
  309. };
  310. struct accountNumbers *promptForModify(struct accountNumbers *accountNumbersPtr);
  311.  
  312. /**
  313. *
  314. *
  315. * FUNCTION MODIFIES THE RECORD'S ID NUMBER
  316. *
  317. *
  318. */
  319.  
  320. void modifyARecord()
  321. {
  322.  
  323. struct account *previousa;
  324. struct accountNumbers *numbersPtr;
  325. struct accountNumbers theNumbers;
  326. theNumbers.newNumber = 0;
  327. theNumbers.numberToModify =0;
  328. theNumbers.success = FALSE;
  329. numbersPtr = &theNumbers;
  330. promptForModify(numbersPtr);
  331. if(numbersPtr->success == FALSE)
  332. {
  333. printf("CANCELLING...");
  334. }
  335. if (firsta == NULL)
  336. {
  337. printf("There are no items to modify!");
  338. return;
  339. }
  340. else
  341. {
  342. currenta = firsta;
  343.  
  344. while(currenta != NULL)
  345. {
  346. if(currenta->number == numbersPtr->numberToModify)
  347. {
  348. currenta->number = numbersPtr->newNumber;
  349. printf("Account #%d updated.n", numbersPtr->numberToModify);
  350. listAll();
  351. return;
  352. }
  353. else
  354. {
  355. previousa = currenta;
  356. currenta = currenta->next;
  357. }
  358. }
  359. printf("Account #%d was not found.n",numbersPtr->numberToModify);
  360. puts("Nothing deleted");
  361. }
  362. }
  363.  
  364. /**
  365. *
  366. *
  367. * FUNCTION PROMPTS THE USER FOR THE NUMBER TO MODIFY AND THE NUMBER TO REPLACE WITH
  368. *
  369. *
  370. */
  371. struct accountNumbers* promptForModify(struct accountNumbers *accountNumbersPtr)
  372. {
  373. printf("Enter an ID number of the record you want to modify: ");
  374. scanf("%d",&accountNumbersPtr->numberToModify);
  375. printf("You entered %d, now enter the replacement number OR 0 to go back.",accountNumbersPtr->numberToModify);
  376. getchar();
  377. scanf("%i",&accountNumbersPtr->newNumber);
  378. switch(accountNumbersPtr->newNumber)
  379. {
  380. case 0:
  381. accountNumbersPtr->success = FALSE;
  382. return accountNumbersPtr;
  383. break; //Not reachable but for semantics
  384. default:
  385. accountNumbersPtr->success = TRUE;
  386. return accountNumbersPtr;
  387. break; //Not reachable but for semantics
  388. }
  389. }
  390.  
  391. switch(accountNumbersPtr->newNumber)
  392. {
  393. case 0:
  394. accountNumbersPtr->success = FALSE;
  395. return accountNumbersPtr;
  396. break; //Not reachable but for semantics
  397. default:
  398. accountNumbersPtr->success = TRUE;
  399. return accountNumbersPtr;
  400. break; //Not reachable but for semantics
  401. }
  402.  
  403. break; //Not reachable but for semantics
  404.  
  405. newa = (struct account *)malloc(sizeof(struct account));
  406.  
  407. newa = (struct account *)malloc(sizeof(struct account));
  408. /*This line reads each struct from the file and stores in currenta*/
  409. fread(currenta,sizeof(struct account),1,f);
  410. /*If we're at the last record, break out of the loop.*/
  411. if(currenta->next == NULL)
  412. break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement