Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct bookstoreDetails{
  6.  
  7. char storeName[33];
  8. char address[65];
  9. char telephone[8];
  10.  
  11. }bsDetails;
  12.  
  13. typedef struct{
  14.  
  15. char bookID[9];
  16. char bookName[33];
  17. float price;
  18. int stocks;
  19. char shelf[5];
  20.  
  21. }bookRecord;
  22.  
  23. typedef struct bookNode{
  24.  
  25. bookRecord bkRec;
  26. struct bookNode* nextBook;
  27.  
  28. }bookNode, *BookList;
  29.  
  30. typedef struct bookStore{
  31.  
  32. bsDetails details;
  33. BookList bList;
  34. int count;
  35.  
  36. }bookStore;
  37.  
  38. typedef struct customerDetails{
  39.  
  40. char custName[33];
  41. char telephone[8];
  42. char email[65];
  43.  
  44. }custDetails;
  45.  
  46. typedef struct personalBkNode{
  47.  
  48. char bookName[33];
  49. struct personalBkNode* nextBk;
  50.  
  51. }personalBkNode, *PersonalBkList;
  52.  
  53. typedef struct{
  54.  
  55. custDetails customer;
  56. PersonalBkList bkList;
  57. int count;
  58.  
  59. }custBkCollection;
  60.  
  61. void populateBookStore(bookStore*);
  62. void displayCustBooks(custBkCollection);
  63. void displayBooksInBS(bookStore);
  64.  
  65. /*Instructions: Write the code of the given function prototypes
  66. 1) initBookStore() - Initializes a given bookstore. Let initial bsDetails values to be anything valid.
  67. 2) findBook() - Given a bookstore and a book ID, the function will return 1 if the book is found in the bookstore
  68. and 0 if it is not in the bookstore.
  69. 3) insertBookBS() - Function will insert a new book record into the bookstore if and only if the book record still does
  70. not exist in the bookstore. Also, the inserted record is inserted in its proper place in the booklist
  71. sorted in ascending order by book ID.
  72. 4) deleteBooksNoStock() - Function will go through all the book records in a bookstore and delete any book record that
  73. has zero stocks.
  74. 5) initCustBkCollection() - Initializes a customer's book collection.
  75. 6) purchaseBook() - Function will go through the list of book records in a bookstore and check if the certain book the customer
  76. wants to purchase exists in the bookstore. If it does exist, insert that book into the customer's book collection
  77. if and only if the book still does not exist in the customer's collection. The book is inserted at the last
  78. part of the customer's collection. Do the necessary increments and decrements. Finally, if insertion was successful,
  79. call function deleteBooksNoStock() to remove any books in the bookstore that have no more stocks.
  80. */
  81.  
  82. void initBookStore(bookStore*);
  83. int findBook(bookStore, char[]);
  84. void insertBookBS(bookStore*, bookRecord);
  85. void deleteBooksNoStock(bookStore*);
  86.  
  87. custBkCollection initCustBkCollection();
  88. void purchaseBook(bookStore*, custBkCollection*, char[]);
  89.  
  90. int main(void)
  91. {
  92. /*Expected output: The list of books with their respective values should have been displayed based on the populate function*/
  93.  
  94. bookStore NBS;
  95. initBookStore(&NBS);
  96. populateBookStore(&NBS);
  97. displayBooksInBS(NBS);
  98. getch();
  99. system("CLS");
  100.  
  101. /*Expected output: Error messages: You already have this book in your collection and Book does not exist in the store should have been displayed.
  102. Then, display Harry Potter Books 1-4.*/
  103.  
  104. custBkCollection myBooks = initCustBkCollection();
  105. purchaseBook(&NBS, &myBooks, "HP000001");
  106. purchaseBook(&NBS, &myBooks, "HP000002");
  107. purchaseBook(&NBS, &myBooks, "HP000003");
  108. purchaseBook(&NBS, &myBooks, "HP000003");
  109. purchaseBook(&NBS, &myBooks, "HP000004");
  110. purchaseBook(&NBS, &myBooks, "HP000004");
  111. displayCustBooks(myBooks);
  112. getch();
  113. system("CLS");
  114.  
  115. /*Expected output: Harry Potter Book 4 should have been deleted. HP Books 1, 2 and 3 should have 6, 3, and 5 stocks respectively*/
  116.  
  117. displayBooksInBS(NBS);
  118. getch();
  119. system("CLS");
  120.  
  121. return 0;
  122.  
  123. }
  124.  
  125. void populateBookStore(bookStore* BS)
  126. {
  127. int i;
  128. bookRecord initialBooks[10] = { {"HP000001", "Harry Potter Book 1", 399.99, 7, "H001"},
  129. {"HP000002", "Harry Potter Book 2", 499.99, 4, "H001"},
  130. {"HP000003", "Harry Potter Book 3", 499.99, 6, "H001"},
  131. {"HP000004", "Harry Potter Book 4", 599.99, 1, "H001"},
  132. {"HP000005", "Harry Potter Book 5", 699.99, 3, "H001"},
  133. {"HP000006", "Harry Potter Book 6", 599.99, 4, "H001"},
  134. {"HP000007", "Harry Potter Book 7", 799.99, 5, "H001"},
  135. {"TL000001", "Twilight", 299.99, 2, "TW01"},
  136. {"TL000002", "New Moon", 399.99, 14, "TW01"},
  137. {"TL000003", "Eclipse", 399.99, 7, "TW01"} };
  138. for(i = 0 ; i < 10; i++){
  139. insertBookBS(BS, initialBooks[i]);
  140. }
  141. }
  142.  
  143. void initBookStore(bookStore* BS)
  144. {
  145. BS->bList = NULL;
  146. BS->count=0;
  147. strcpy(BS->details.storeName,"Jollibee Bookstore");
  148. strcpy(BS->details.address,"SM City Ubec");
  149. strcpy(BS->details.telephone,"#8-70000");
  150.  
  151. }
  152.  
  153. int findBook(bookStore BS, char bookID[])
  154. {
  155. BookList trav;
  156. for(trav=BS.bList; trav!=NULL && strcmp(trav->bkRec.bookID, bookID); trav=trav->nextBook){}
  157. return trav == NULL? 0:1;
  158. }
  159.  
  160. void insertBookBS(bookStore* BS, bookRecord newBk)
  161. {
  162. BookList temp,*trav;
  163. int find=findBook(*BS,newBk.bookID);
  164. if(find==0){
  165. for(trav=&BS->bList; *trav!=NULL && strcmp((*trav)->bkRec.bookID, newBk.bookID) < 0; trav=&(*trav)->nextBook){}
  166. temp= (BookList)malloc(sizeof(bookNode));
  167. if(temp!=NULL){
  168. temp->bkRec=newBk;
  169. temp->nextBook=*trav;
  170. *trav=temp;
  171. BS->count++;
  172. }
  173. }
  174. }
  175.  
  176. void deleteBooksNoStocks(bookStore* BS)
  177. {
  178. BookList temp, *trav;
  179. for(trav= &BS->bList; *trav!=NULL;){
  180. if((*trav)->bkRec.stocks == 0){
  181. temp=*trav;
  182. *trav=temp->nextBook;
  183. free(temp);
  184. BS->count--;
  185. }else{
  186. trav=&(*trav)->nextBook;
  187. }
  188. }
  189.  
  190. }
  191.  
  192. void displayBooksInBS(bookStore BS)
  193. {
  194. printf("Bookstore Name: %s\n\n", BS.details.storeName);
  195. printf("Address: %s\n\n", BS.details.address);
  196. printf("Telephone: %s\n\n", BS.details.telephone);
  197.  
  198. for(;BS.bList != NULL; BS.bList = BS.bList->nextBook){
  199. printf("Book ID: %s\nBook: %s\nPrice: %.2f\nStocks: %d\nShelf: %s\n\n", BS.bList->bkRec.bookID,
  200. BS.bList->bkRec.bookName, BS.bList->bkRec.price, BS.bList->bkRec.stocks, BS.bList->bkRec.shelf);
  201. }
  202. }
  203.  
  204. custBkCollection initCustBkCollection()
  205. {
  206. custBkCollection deets = {{"Kolz Fern","093071","qqq@gmail.com"},NULL,0};
  207. return deets;
  208.  
  209. }
  210.  
  211. void purchaseBook(bookStore* BS, custBkCollection* CBC, char bookID[])
  212. {
  213. BookList trav;
  214. PersonalBkList temp, *trav2;
  215. if(findBook(*BS,bookID)==1){
  216. for(trav=BS->bList;trav!=NULL && strcmp(trav->bkRec.bookID, bookID)!=0; trav=trav->nextBook){}
  217. for(trav2=&CBC->bkList; *trav2!=NULL && strcmp((*trav2)->bookName,trav->bkRec.bookName)!=0;trav2=&(*trav2)->nextBk){}
  218. if(*trav2==NULL){
  219. temp=(PersonalBkList)malloc(sizeof(personalBkNode));
  220. if(temp!=NULL){
  221. strcpy(temp->bookName,trav->bkRec.bookName);
  222. temp->nextBk=*trav2;
  223. *trav2=temp;
  224. CBC->count++;
  225. trav->bkRec.stocks--;
  226. deleteBooksNoStocks(BS);
  227. }
  228. }else{
  229. printf("You already have this book in your collection\n");
  230. }
  231. }else{
  232. printf("Book does not exist in the store\n");
  233. }
  234.  
  235. }
  236.  
  237. void displayCustBooks(custBkCollection CBC)
  238. {
  239. PersonalBkList trav;
  240.  
  241. printf("Name: %s\n\n", CBC.customer.custName);
  242.  
  243. for(trav = CBC.bkList; trav != NULL; trav = trav->nextBk){
  244. printf("%s\n", trav->bookName);
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement