thecoderenroute

Untitled

May 8th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. void printAll();
  7. int search();
  8. void issue(int, int);
  9. void returner(int,int);
  10. void reserve(int, int);
  11. void returner(int ,int);
  12. void add_new_book(int);
  13. int number_of_books();
  14.  
  15. int student[1000];
  16. int rno;
  17. int number_of_books();
  18.  
  19. struct library_records{
  20.     char name[100];
  21.     char author[100];
  22.     int reserved;
  23.     int issued;
  24.     int no;
  25.     //char date[1000][8];
  26.     // int student[1000];
  27.     int taker;
  28.     int reserved_by;
  29. } record[1000];
  30.  
  31. void main()
  32. {
  33.     char again;
  34.     do
  35.     {
  36.     int temp;
  37.     printf("\nWhat would you like to do?");
  38.     printf("\n1. Add new book. ");
  39.     printf("\n2. Issue a book. ");
  40.     printf("\n3. Return a book. ");
  41.     printf("\n4. View all Books in Library.\n");
  42.     int ch;
  43.     scanf("%d", &ch);
  44.     switch(ch)
  45.     {
  46.     case 1:
  47.         add_new_book(number_of_books());
  48.         break;
  49.     case 2:
  50.         printf("\n Enter Roll no.");
  51.         scanf("%d",&rno);
  52.         printf("Roll number: %d",rno);
  53.         int temp1 = search();
  54.         if(temp1>=0)
  55.         {
  56.                 issue(temp1,rno);
  57.         }
  58.         break;
  59.     case 3:
  60.         printf("\n Enter book number: ");
  61.         scanf("%d",&temp);
  62.         printf("\n Enter Roll no.");
  63.         scanf("%d",&rno);
  64.         returner(temp,rno);
  65.         break;
  66.     case 4:
  67.         printAll();
  68.         break;
  69.  
  70.     default:
  71.         printf("\n Try Again");
  72.     }
  73.     printf("\n\nWould you like to go again?[y/n]");
  74.     scanf(" %c", &again);
  75.     }while(again == 'y');
  76.  
  77. }
  78.  
  79. void printAll(){
  80.     int i=0;
  81.     while(i<number_of_books())
  82.     {
  83.         printf("\n %d. %s by %s",i+1,record[i].name,record[i].author);
  84.  
  85.         if(record[i].issued)
  86.         {
  87.             printf("\tIssued\t");
  88.             if(record[i].reserved)
  89.             {
  90.                 printf("Reserved");
  91.             }
  92.             else
  93.             {
  94.                 printf("not Reserved");
  95.             }
  96.         }
  97.         else
  98.         {
  99.             printf("\tnot Issued");
  100.         }
  101.         i++;
  102.     }
  103.     printf("\n");
  104. }
  105.  
  106. int search()
  107. {
  108.     char str[100];
  109.     int n;
  110.     // bool found = false;
  111.     int i=0;
  112.     printf("\nSelect search criteria: ");
  113.     printf("\n1. Book Name.");
  114.     printf("\n2. Author name. ");
  115.     printf("\n3. Book number.\n");
  116.     int ch;
  117.     scanf("%d", &ch);
  118.     switch(ch)
  119.     {
  120.     case 1:
  121.         printf("\n Enter Book name:");
  122.         //fgets(str,100, stdin);
  123.         scanf("%s",&str);
  124.         while(strcmp(record[i].name,""))
  125.         {
  126.             if(strcmp(record[i].name,str) == 0)
  127.                 return i;
  128.             i++;
  129.         }
  130.         printf("Book doesn't exist. Try another one\n");
  131.         //search();
  132.         return -1;
  133.         break;
  134.     case 2:
  135.         printf("\n Enter Author's name: ");
  136.         // gets(str);
  137.         scanf("%s",&str);
  138.         while(strcmp(record[i].author,""))//  != '\0')
  139.         {
  140.             if(strcmp(record[i].author,str) == 0)
  141.             {
  142.                 return i;
  143.             }
  144.             i++;
  145.         }
  146.         printf("Book doesn't exist. Try another one\n");
  147.         //search();
  148.         return -1;
  149.         break;
  150.     case 3:
  151.         printf("\n Enter Book Number: ");
  152.         scanf("%d",&n);
  153.         if(n-1< number_of_books())
  154.             return n-1;
  155.         else
  156.         {
  157.             printf("Book doesn't exist. Try another one");
  158.         }
  159.         break;
  160.     default:
  161.         printf("\n Make a proper Selection. Returning to Main Menu");
  162.         // return -1;
  163.     }
  164.     return -1;
  165. }
  166.  
  167. void issue(int n, int s)
  168. {
  169.     if(!record[n].issued && !student[s])
  170.     {
  171.         record[n].issued = 1;
  172.         record[n].taker = s;
  173.         student[s] = 1;
  174.         printf("Book no. %d: %s by author %s is being issued to Student %d",record[n].no+1,record[n].name,record[n].author, record[n].taker);
  175.     }
  176.     else if(!record[n].issued)
  177.     {
  178.         printf("Student needs to return pending books before issuing new ones!");
  179.     }
  180.     else
  181.         reserve(n,s);
  182. }
  183. void reserve(int n, int s)
  184. {
  185.     char ch;
  186.     printf("\n The book you would like to Issue has already been issued to Someone else, would you like to: ");
  187.     printf("1. reserve it? [y/n]");
  188.     scanf(" %c", &ch);
  189.     //int *p;
  190.     if(ch == 'Y' || ch == 'y')
  191.     {
  192.         if(!record[n].reserved)
  193.         {
  194.             record[n].reserved = 1;
  195.             record[n].reserved_by =s;
  196.             printf("Book number %d successfully reserved by Student %d", record[n].no+1,record[n].reserved_by);
  197.         }
  198.         else
  199.             printf("\n The book has already been reserved. Please Try another book");
  200.     }
  201.     else
  202.     {
  203.         if(!record[n].reserved)
  204.             printf("You can try to claim it again later"); // %s", return_date(date[n]));
  205.         else
  206.             printf("This book has been reserved by someone else after the first issues, Try agan later");
  207.     }
  208. }
  209. void returner(int x,int s)
  210. {
  211.     int n = x-1;
  212.     if(record[n].issued)
  213.     {
  214.         if(record[n].taker == s)
  215.         {
  216.             printf("Book no. %d: %s by %s has been successfully returned by %d",x,record[n].name, record[n].author,record[n].taker);
  217.             student[s] = 0;
  218.             if(!record[n].reserved)
  219.                 record[n].issued = 0;
  220.             else
  221.             {
  222.                 record[n].reserved=0;
  223.                 record[n].taker = record[n].reserved_by;
  224.                 student[record[n].reserved_by] = 1;
  225.             }
  226.         }
  227.         else{
  228.             printf("Invalid info.");
  229.         }
  230.  
  231.     }
  232.     else{
  233.         printf("Wrong book number.");
  234.     }
  235.  
  236. }
  237.  
  238. //char[8] availabilty
  239.  
  240. void add_new_book(int n)
  241. {
  242.     printf("\nPlease Enter the book's name: ");
  243.     scanf("%s",&record[n].name);
  244.     // scanf("%s", &name[n]);
  245.     printf("Book's name is %s", record[n].name);
  246.     printf("\nPlease Enter the book's Author's name: ");
  247.     scanf("%s", record[n].author);
  248.     // scanf("%s", author[n]);
  249.     printf("Book's Author's name is %s", record[n].author);
  250.     record[n].reserved = 0;
  251.     record[n].issued = 0;
  252.     record[n].no = n;
  253. }
  254.  
  255. int number_of_books()
  256. {
  257.     int x = 0;
  258.     int i = 0;
  259.     while(strcmp(record[i].name,"") != 0) // != '\0')
  260.     {
  261.         x++;
  262.         i++;
  263.     }
  264.     // printf("No. of books in the library are: %d",x);
  265.     return x;
  266. }
Add Comment
Please, Sign In to add comment