TEXT   107

lv82

Guest on 22nd April 2022 06:53:58 AM

  1. MAIN.C
  2.  
  3. #include "Header.h"
  4.  
  5. int main(void) {
  6.  
  7.     int inputOption;
  8.     char* membersFileName = "Clanovi.txt";
  9.     char* moviesFileName = "posudbe.txt";
  10.  
  11.     do {
  12.  
  13.         printMenu();
  14.  
  15.         scanf("%d", &inputOption); getchar();
  16.  
  17.         switch (inputOption) {
  18.  
  19.         case 1:
  20.             createFile(membersFileName);
  21.             break;
  22.  
  23.         case 2:
  24.             readFile(membersFileName);
  25.             break;
  26.  
  27.         case 3:
  28.             saveMember(membersFileName);
  29.             break;
  30.         case 4:
  31.             rentMovie(membersFileName, moviesFileName);
  32.             break;
  33.         case 5:
  34.             exit(EXIT_SUCCESS);
  35.         default:
  36.             puts("Ta operacija ne postoji!\n");
  37.             break;
  38.         }
  39.     } while (inputOption != 5);
  40.  
  41.     return 0;
  42. }
  43.  
  44.  
  45. HEADER.H
  46.  
  47. #ifndef Header_h
  48. #define Header_h
  49.  
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52.  
  53. typedef struct memberInfo
  54. {
  55.     char lName[20];
  56.     char fName[20];
  57.     int id;
  58.     char address[100];
  59.     int phoneNumber;
  60. }MEMBER;
  61.  
  62. typedef struct movie {
  63.     char name[128];
  64.     int date, memberId;
  65. }MOVIE;
  66.  
  67. void printMenu(void);
  68. void createFile(char* fileName);
  69. void readFile(char* fileName);
  70. void saveMember(char* fileName);
  71. void rentMovie(char* membersFileName, char* moviesFileName);
  72. void getMovieNumber(int* movieNumber);
  73. void getMovieToRent(MOVIE* movie);
  74. int checkForMember(char* memberFileName, MOVIE* movie);
  75. int checkMemberRentStatus(char* moviesFileName, MOVIE* movie);
  76. void registerRentedMovie(char* moviesFileName, MOVIE* movie);
  77.  
  78. #endif /* Header_h */
  79.  
  80.  
  81.  
  82. FUNCTIONS.C
  83.  
  84. #include "Header.h"
  85.  
  86. void printMenu(void) {
  87.     printf("Pick your options: \n");
  88.     printf("1- Kreiranje datoteke Clanovi.txt\n");
  89.     printf("2- Citanje iz datoteke Clanovi.txt\n");
  90.     printf("3- Dodavanje novih clanova u Clanovi.txt\n");
  91.     printf("4- Posudba filma.\n");
  92.     printf("5- Zavrsetak programa.\n");
  93. }
  94.  
  95. void createFile(char* fileName) {
  96.     FILE* file = fopen(fileName, "ab");
  97.     puts("\nFile created.\n");
  98.     fclose(file);
  99. }
  100.  
  101. void readFile(char* fileName) {
  102.     FILE* inFile = fopen(fileName, "rb");
  103.     MEMBER member;
  104.     if (inFile != NULL) {
  105.         while (fread(&member, sizeof(MEMBER), 1, inFile) == 1) {
  106.             printf("\nName: %s %s\n", member.fName, member.lName);
  107.             printf("ID: %d\nPhone num: %d\n", member.id, member.phoneNumber);
  108.             printf("Address: %s\n", member.address);
  109.         }
  110.         fclose(inFile);
  111.     }
  112.     puts("\n---- END OF FILE ----\n");
  113. }
  114.  
  115. void saveMember(char* fileName) {
  116.     FILE* file = fopen(fileName, "ab");
  117.  
  118.     MEMBER member;
  119.     printf("Enter member first name: ");
  120.     scanf(" %[^\n]", member.fName);
  121.  
  122.     printf("Enter member last name: ");
  123.     scanf(" %[^\n]", member.lName);
  124.  
  125.     printf("Enter member code: ");
  126.     scanf("%d", &member.id);
  127.     getchar();
  128.  
  129.     printf("Enter memeber address: ");
  130.     scanf(" %[^\n]", member.address);
  131.  
  132.     printf("Enter member phone number: ");
  133.     scanf("%d", &member.phoneNumber); getchar();
  134.  
  135.     fwrite(&member, sizeof(MEMBER), 1, file);
  136.  
  137.     puts("\nMember added.\n");
  138.  
  139.     fclose(file);
  140. }
  141.  
  142. void rentMovie(char* membersFileName, char* moviesFileName) {
  143.     int movieNumber;
  144.     getMovieNumber(&movieNumber);
  145.  
  146.     int i; MOVIE movie;
  147.     for (i = 0; i < movieNumber; i++) {
  148.  
  149.         getMovieToRent(&movie);
  150.  
  151.         int memberExists, canRent;
  152.         memberExists = checkForMember(membersFileName, &movie);
  153.         canRent = checkMemberRentStatus(moviesFileName, &movie);
  154.  
  155.         if (memberExists && canRent) {
  156.             registerRentedMovie(moviesFileName, &movie);
  157.             printf("Movie no: %d, with the name: %s rented.\n", i + 1, movie.name);
  158.         }
  159.         else {
  160.             printf("\nRenting failed.\nMember exists status: %d\n", memberExists);
  161.             printf("Member can rent status: %d\n\n", canRent);
  162.         }
  163.     }
  164. }
  165.  
  166. void getMovieNumber(int* movieNumber) {
  167.     do {
  168.         puts("How many movies do you what to checkout? (1 - 4)");
  169.         scanf("%d", movieNumber);
  170.     } while (*movieNumber <= 0 || *movieNumber > 4);
  171. }
  172.  
  173. void getMovieToRent(MOVIE* movie) {
  174.     printf("Enter movie name: ");
  175.     scanf(" %[^\n]s", movie->name);
  176.  
  177.     do {
  178.         printf("Enter date in range (0 - 365): ");
  179.         scanf("%d", &movie->date); getchar();
  180.     } while (movie->date < 0 || movie->date > 365);
  181.  
  182.     printf("Enter member code: ");
  183.     scanf("%d", &movie->memberId); getchar();
  184. }
  185.  
  186. int checkForMember(char* memberFileName, MOVIE* movie) {
  187.     FILE* members = fopen(memberFileName, "rb");
  188.     if(members == NULL) return 0;
  189.     MEMBER member;
  190.     while (fread(&member, sizeof(MEMBER), 1, members) == 1) {
  191.         if (member.id == movie->memberId) {
  192.             fclose(members);
  193.             return 1;
  194.         }
  195.     }
  196.     fclose(members);
  197.     return 0;
  198. }
  199.  
  200. int checkMemberRentStatus(char* moviesFileName, MOVIE* movie) {
  201.     FILE* movies = fopen(moviesFileName, "rb");
  202.     if (movies == NULL) {
  203.         movies = fopen(moviesFileName, "ab");
  204.     }
  205.  
  206.     MOVIE tempMovie;
  207.     int counter = 0;
  208.  
  209.     while (fread(&tempMovie, sizeof(MOVIE), 1, movies) == 1) {
  210.         if (tempMovie.memberId == movie->memberId) counter++;
  211.         if (counter >= 4) {
  212.             fclose(movies);
  213.             return 0;
  214.         }
  215.     }
  216.     fclose(movies);
  217.     return 1;
  218. }
  219.  
  220. void registerRentedMovie(char* moviesFileName, MOVIE* movie) {
  221.     FILE* movies = fopen(moviesFileName, "ab");
  222.     fwrite(movie, sizeof(MOVIE), 1, movies);
  223.     fclose(movies);
  224. }

Raw Paste


Login or Register to edit or fork this paste. It's free.