PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/locadora_em_c/item.c

http://ruby-pro.googlecode.com/
C | 834 lines | 674 code | 93 blank | 67 comment | 136 complexity | 3038c963bbc856326d2c10a61aab1c9c MD5 | raw file
  1. /*
  2. * File: item.c
  3. * Author: samir
  4. *
  5. * Created on 14 de Junho de 2010, 18:25
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <string.h>
  11. #include <regex.h>
  12. #include "item.h"
  13. #include "status.h"
  14. #include "exceptions.h"
  15. #include "dvd.h"
  16. #include "location.h"
  17. #include "strings.h"
  18. #include "validations.h"
  19. #include "date.h"
  20. /*
  21. * Item module
  22. */
  23. int check_by_id_item(char *input) {
  24. int id;
  25. do {
  26. printf("Qual ID? ");
  27. read_string(input);
  28. } while (!validate_id(input));
  29. id = atoi(input);
  30. // Verificar se o ID existe
  31. if (id > 0 && item_index_exist(id)) {
  32. return id;
  33. } else {
  34. printf(ID_NOT_FOUND_ERROR, __FILE__, "filme");
  35. return NON_EXIST;
  36. }
  37. }
  38. Item * item_malloc() {
  39. Item *item = malloc(sizeof (Item));
  40. if (!item) {
  41. printf(ALLOC_ERROR, __FILE__);
  42. exit(1);
  43. }
  44. item_initialize(item);
  45. return item;
  46. }
  47. void item_initialize(Item * item) {
  48. item->id = NON_EXIST;
  49. item->id_dvd = NON_EXIST;
  50. item->id_location = NON_EXIST;
  51. item->return_date = 0;
  52. item->returned = FALSE;
  53. item->price = 0.0;
  54. }
  55. Item * search_item_by_id(int id) {
  56. FILE *file_stream = NULL;
  57. Item *item;
  58. file_stream = fopen(ITEMS_FILEPATH, "rb");
  59. if (!file_stream) {
  60. printf(READ_OPEN_ERROR, __FILE__, ITEMS_FILEPATH);
  61. exit(1);
  62. }
  63. item = item_malloc();
  64. fread(item, sizeof (Item), 1, file_stream);
  65. while (!feof(file_stream)) {
  66. if (item->id == id) {
  67. fclose(file_stream);
  68. return item;
  69. }
  70. fread(item, sizeof (Item), 1, file_stream);
  71. }
  72. fclose(file_stream);
  73. item->id = NON_EXIST;
  74. return item;
  75. }
  76. Item * search_item_by_movietitle(Location *location, char *input) {
  77. FILE *file_stream = NULL;
  78. Item *item;
  79. DVD *dvd;
  80. Movie *movie;
  81. file_stream = fopen(ITEMS_FILEPATH, "rb");
  82. if (!file_stream) {
  83. printf(READ_OPEN_ERROR, __FILE__, ITEMS_FILEPATH);
  84. exit(1);
  85. }
  86. item = item_malloc();
  87. fread(item, sizeof (Item), 1, file_stream);
  88. while (!feof(file_stream)) {
  89. dvd = search_dvd_by_id(item->id_dvd);
  90. movie = search_movie_by_id(dvd->id_movie);
  91. if (location->id == item->id_location && !(strcasecmp(input, movie->title))) {
  92. fclose(file_stream);
  93. free(dvd);
  94. free(movie);
  95. return item;
  96. }
  97. fread(item, sizeof (Item), 1, file_stream);
  98. }
  99. // Nсo achou pelo nome exato, entсo tentaremos uma substring
  100. regex_t reg;
  101. if (regcomp(&reg, input, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
  102. fprintf(stderr, "%s: ERRO na compilacao da expressao regular.\n", __FILE__);
  103. fclose(file_stream);
  104. free(dvd);
  105. free(movie);
  106. item->id = NON_EXIST;
  107. return item;
  108. }
  109. fseek(file_stream, 0, SEEK_SET);
  110. fread(item, sizeof (Client), 1, file_stream);
  111. while (!feof(file_stream)) {
  112. if (location->id == item->id_location && !(regexec(&reg, movie->title, 0, (regmatch_t *) NULL, 0))) {
  113. fclose(file_stream);
  114. free(dvd);
  115. free(movie);
  116. return item;
  117. }
  118. fread(item, sizeof (Client), 1, file_stream);
  119. }
  120. // Nada foi encontrado
  121. free(dvd);
  122. free(movie);
  123. fclose(file_stream);
  124. item->id = NON_EXIST;
  125. return item;
  126. }
  127. int items_file_is_empty() {
  128. FILE *file_stream = NULL;
  129. file_stream = fopen(ITEMS_FILEPATH, "rb");
  130. if (file_stream) {
  131. fclose(file_stream);
  132. return FALSE;
  133. } else {
  134. return TRUE;
  135. }
  136. }
  137. int item_index_exist(int index) {
  138. Item *item;
  139. item = item_malloc();
  140. item = search_item_by_id(index);
  141. if (item->id == NON_EXIST) {
  142. free(item);
  143. return FALSE;
  144. }
  145. free(item);
  146. return TRUE;
  147. }
  148. int item_first_index_avaliable() {
  149. FILE *file_stream = NULL;
  150. int old_id = NON_EXIST, new_id = NON_EXIST;
  151. file_stream = fopen(ITEMS_ID_FILEPATH, "rb+");
  152. if (file_stream) {
  153. fread(&old_id, sizeof (old_id), 1, file_stream);
  154. rewind(file_stream);
  155. new_id = old_id + 1;
  156. fwrite(&new_id, sizeof (new_id), 1, file_stream);
  157. fclose(file_stream);
  158. return old_id;
  159. } else {
  160. printf("Aviso: arquivo \"%s\" foi criado agora.\n", ITEMS_ID_FILEPATH);
  161. /* Nсo conseguiu abrir um arquivo existente, entсo, criarр. */
  162. file_stream = fopen(ITEMS_ID_FILEPATH, "wb+");
  163. if (file_stream) {
  164. new_id = 2;
  165. fwrite(&new_id, sizeof (new_id), 1, file_stream);
  166. fclose(file_stream);
  167. return 1;
  168. } else {
  169. printf(CREATE_FILE_ERROR, __FILE__, ITEMS_ID_FILEPATH);
  170. exit(1);
  171. }
  172. }
  173. }
  174. int insert_item(Item * item) {
  175. FILE *file_stream = NULL;
  176. item->id = item_first_index_avaliable();
  177. file_stream = fopen(ITEMS_FILEPATH, "rb+");
  178. if (file_stream) {
  179. fseek(file_stream, 0, SEEK_END);
  180. if (!fwrite(item, sizeof (Item), 1, file_stream)) {
  181. printf(WRITE_FILE_ERROR, __FILE__, ITEMS_FILEPATH);
  182. fclose(file_stream);
  183. return FALSE;
  184. }
  185. fclose(file_stream);
  186. } else {
  187. printf("Aviso: arquivo \"%s\" foi criado agora.\n", ITEMS_FILEPATH);
  188. /* Nсo conseguiu abrir um arquivo existente, entсo, criarр. */
  189. file_stream = fopen(ITEMS_FILEPATH, "wb+");
  190. if (file_stream) {
  191. if (!fwrite(item, sizeof (Item), 1, file_stream)) {
  192. printf(WRITE_FILE_ERROR, __FILE__, ITEMS_FILEPATH);
  193. fclose(file_stream);
  194. return FALSE;
  195. }
  196. fclose(file_stream);
  197. } else {
  198. printf(CREATE_FILE_ERROR, __FILE__, ITEMS_FILEPATH);
  199. return FALSE;
  200. }
  201. }
  202. return TRUE;
  203. }
  204. int update_item(Item *item) {
  205. FILE *file_stream = NULL;
  206. Item *aux;
  207. file_stream = fopen(ITEMS_FILEPATH, "rb+");
  208. if (!file_stream) {
  209. printf(FILE_NOT_FOUND_ERROR, __FILE__, ITEMS_FILEPATH);
  210. return FALSE;
  211. }
  212. aux = item_malloc();
  213. // Procurar o registro a ser alterado no arquivo
  214. fread(aux, sizeof (Item), 1, file_stream);
  215. while (!feof(file_stream)) {
  216. if (aux->id == item->id) {
  217. fseek(file_stream, -(sizeof (Item)), SEEK_CUR);
  218. if (!fwrite(item, sizeof (Item), 1, file_stream)) {
  219. printf(WRITE_FILE_ERROR, __FILE__, ITEMS_FILEPATH);
  220. fclose(file_stream);
  221. free(aux);
  222. return FALSE;
  223. }
  224. fclose(file_stream);
  225. free(aux);
  226. return TRUE;
  227. }
  228. fread(aux, sizeof (Item), 1, file_stream);
  229. }
  230. // Se chegar atж aqui ж porque nсo encontrou nada
  231. fclose(file_stream);
  232. free(aux);
  233. return FALSE;
  234. }
  235. int erase_item(Item *item) {
  236. FILE *file_stream = NULL, *file_stream_tmp = NULL;
  237. Item *aux;
  238. file_stream = fopen(ITEMS_FILEPATH, "rb+");
  239. if (!file_stream) {
  240. printf(FILE_NOT_FOUND_ERROR, __FILE__, ITEMS_FILEPATH);
  241. return FALSE;
  242. }
  243. file_stream_tmp = fopen(ITEMS_TMP_FILEPATH, "wb");
  244. if (!file_stream_tmp) {
  245. printf(FILE_NOT_FOUND_ERROR, __FILE__, ITEMS_TMP_FILEPATH);
  246. return FALSE;
  247. }
  248. aux = item_malloc();
  249. fread(aux, sizeof (Item), 1, file_stream);
  250. while (!feof(file_stream)) {
  251. if (aux->id != item->id) {
  252. fwrite(aux, sizeof (Item), 1, file_stream_tmp);
  253. }
  254. fread(aux, sizeof (Item), 1, file_stream);
  255. }
  256. free(aux);
  257. fclose(file_stream);
  258. fclose(file_stream_tmp);
  259. if (remove(ITEMS_FILEPATH)) {
  260. return FALSE;
  261. }
  262. if (rename(ITEMS_TMP_FILEPATH, ITEMS_FILEPATH)) {
  263. return FALSE;
  264. }
  265. // Verificar se o arquivo ficou com 0 bytes
  266. file_stream = fopen(ITEMS_FILEPATH, "rb");
  267. if (!file_stream) {
  268. printf(FILE_NOT_FOUND_ERROR, __FILE__, ITEMS_FILEPATH);
  269. return FALSE;
  270. }
  271. fseek(file_stream, 0, SEEK_END);
  272. // Se o arquivo tiver 0 bytes, serр removido.
  273. if (!ftell(file_stream)) {
  274. remove(ITEMS_FILEPATH);
  275. }
  276. return TRUE;
  277. }
  278. void copy_item(Item * dest, Item * src) {
  279. dest->id = src->id;
  280. dest->id_dvd = src->id_dvd;
  281. dest->id_location = src->id_location;
  282. dest->return_date = src->return_date;
  283. dest->returned = src->returned;
  284. dest->price = src->price;
  285. }
  286. int get_size_items() {
  287. FILE *file_stream = NULL;
  288. file_stream = fopen(ITEMS_FILEPATH, "rb");
  289. if (!file_stream) {
  290. printf(FILE_NOT_FOUND_ERROR, __FILE__, ITEMS_FILEPATH);
  291. return FALSE;
  292. }
  293. fseek(file_stream, 0, SEEK_END);
  294. return ftell(file_stream) / sizeof (Item);
  295. }
  296. Item * item_file_to_a() {
  297. FILE * file_stream = NULL;
  298. Item *vetor;
  299. int i, size;
  300. // Antes de tudo, precisamos testar se hр algum item no arquivo
  301. if (items_file_is_empty()) {
  302. printf(EMPTY_ERROR, __FILE__, "item");
  303. return FALSE;
  304. }
  305. file_stream = fopen(ITEMS_FILEPATH, "rb");
  306. if (!file_stream) {
  307. printf(FILE_NOT_FOUND_ERROR, __FILE__, ITEMS_FILEPATH);
  308. return FALSE;
  309. }
  310. size = get_size_items();
  311. if (!size) {
  312. printf("%s: Nao foi possivel obter a quantidade de items.\n", __FILE__);
  313. return FALSE;
  314. }
  315. vetor = malloc(size * sizeof (Item));
  316. if (!vetor) {
  317. printf(ALLOC_ERROR, __FILE__);
  318. return FALSE;
  319. }
  320. for (i = 0; i < size; i++) {
  321. fread(vetor + i, sizeof (Item), 1, file_stream);
  322. }
  323. fclose(file_stream);
  324. return vetor;
  325. }
  326. void puts_item_row_list() {
  327. printf("------ Item | No. DVD | Filme | Data de entrega | Devolvido | Valor ------\n");
  328. }
  329. void puts_item(Item *item, char quiet) {
  330. Movie * movie;
  331. DVD *dvd;
  332. char *date;
  333. dvd = search_dvd_by_id(item->id_dvd);
  334. movie = search_movie_by_id(dvd->id_movie);
  335. if (!quiet) {
  336. puts_item_row_list();
  337. }
  338. printf("%d ", item->id);
  339. printf("%d ", item->id_dvd);
  340. printf("%s\t ", movie->title);
  341. date = date_to_s(&item->return_date);
  342. printf("%s\t", date);
  343. if (item->returned) {
  344. printf("sim\t");
  345. } else {
  346. printf("nao\t");
  347. }
  348. printf("R$ %.2lf\n", item->price);
  349. free(movie);
  350. free(date);
  351. free(dvd);
  352. }
  353. void puts_items_by_location(Location *location, char quiet) {
  354. FILE *file_stream = NULL;
  355. Item *item;
  356. // Antes de tudo, precisamos testar se hр algum item no arquivo
  357. if (items_file_is_empty()) {
  358. printf(EMPTY_ERROR, __FILE__, "item");
  359. return;
  360. }
  361. file_stream = fopen(ITEMS_FILEPATH, "rb");
  362. if (!file_stream) {
  363. printf(READ_OPEN_ERROR, __FILE__, ITEMS_FILEPATH);
  364. exit(EXIT_FAILURE);
  365. }
  366. item = item_malloc();
  367. if (!quiet)
  368. puts_item_row_list();
  369. fread(item, sizeof (Item), 1, file_stream);
  370. while (!feof(file_stream)) {
  371. if (item->id_location == location->id) {
  372. update_item_price(item);
  373. puts_item(item, TRUE);
  374. }
  375. fread(item, sizeof (Item), 1, file_stream);
  376. }
  377. fclose(file_stream);
  378. free(item);
  379. }
  380. void puts_items_by_location_only_titles(Location *location) {
  381. FILE *file_stream = NULL;
  382. Item *item;
  383. // Antes de tudo, precisamos testar se hр algum item no arquivo
  384. if (items_file_is_empty()) {
  385. printf(EMPTY_ERROR, __FILE__, "item");
  386. return;
  387. }
  388. file_stream = fopen(ITEMS_FILEPATH, "rb");
  389. if (!file_stream) {
  390. printf(READ_OPEN_ERROR, __FILE__, ITEMS_FILEPATH);
  391. exit(EXIT_FAILURE);
  392. }
  393. item = item_malloc();
  394. fread(item, sizeof (Item), 1, file_stream);
  395. while (!feof(file_stream)) {
  396. if (item->id_location == location->id) {
  397. puts_movie_title_by_dvd_id(item->id_dvd);
  398. printf(", ");
  399. }
  400. fread(item, sizeof (Item), 1, file_stream);
  401. }
  402. fclose(file_stream);
  403. free(item);
  404. }
  405. void puts_all_items(char quiet) {
  406. FILE *file_stream = NULL;
  407. Item *item;
  408. file_stream = fopen(ITEMS_FILEPATH, "rb");
  409. if (!file_stream) {
  410. printf(EMPTY_ERROR, __FILE__, "item");
  411. return;
  412. }
  413. item = item_malloc();
  414. if (!quiet) {
  415. puts_item_row_list();
  416. }
  417. fread(item, sizeof (Item), 1, file_stream);
  418. while (!feof(file_stream)) {
  419. update_item_price(item);
  420. puts_item(item, TRUE);
  421. fread(item, sizeof (Item), 1, file_stream);
  422. }
  423. fclose(file_stream);
  424. free(item);
  425. }
  426. double get_total_items_returned_by_location(Location *location) {
  427. FILE *file_stream = NULL;
  428. Item *item;
  429. double count = 0.0;
  430. file_stream = fopen(ITEMS_FILEPATH, "rb");
  431. if (!file_stream) {
  432. printf(EMPTY_ERROR, __FILE__, "item");
  433. return;
  434. }
  435. item = item_malloc();
  436. fread(item, sizeof (Item), 1, file_stream);
  437. while (!feof(file_stream)) {
  438. if (location->id == item->id_location && item->returned) {
  439. count += get_dvd_price(item->id_dvd);
  440. }
  441. fread(item, sizeof (Item), 1, file_stream);
  442. }
  443. fclose(file_stream);
  444. free(item);
  445. return count;
  446. }
  447. double get_total_to_pay(Location *location) {
  448. double total = 0.0;
  449. FILE *file_stream = NULL;
  450. Item *item;
  451. file_stream = fopen(ITEMS_FILEPATH, "rb");
  452. if (!file_stream) {
  453. printf(EMPTY_ERROR, __FILE__, "item");
  454. return;
  455. }
  456. item = item_malloc();
  457. fread(item, sizeof (Item), 1, file_stream);
  458. while (!feof(file_stream)) {
  459. if (location->id == item->id_location && !item->returned) {
  460. update_item_price(item);
  461. total += item->price;
  462. }
  463. fread(item, sizeof (Item), 1, file_stream);
  464. }
  465. fclose(file_stream);
  466. free(item);
  467. return total;
  468. }
  469. char items_location_is_empty(Location *location) {
  470. FILE *file_stream = NULL;
  471. Item *item;
  472. file_stream = fopen(ITEMS_FILEPATH, "rb");
  473. if (!file_stream) {
  474. printf(EMPTY_ERROR, __FILE__, "item");
  475. exit(EXIT_FAILURE);
  476. }
  477. item = item_malloc();
  478. fread(item, sizeof (Item), 1, file_stream);
  479. while (!feof(file_stream)) {
  480. if (location->id == item->id_location) {
  481. fclose(file_stream);
  482. free(item);
  483. return FALSE;
  484. }
  485. fread(item, sizeof (Item), 1, file_stream);
  486. }
  487. fclose(file_stream);
  488. free(item);
  489. return TRUE;
  490. }
  491. char location_has_returned_items(Location *location) {
  492. FILE *file_stream = NULL;
  493. Item *item;
  494. file_stream = fopen(ITEMS_FILEPATH, "rb");
  495. if (!file_stream) {
  496. printf(EMPTY_ERROR, __FILE__, "item");
  497. exit(EXIT_FAILURE);
  498. }
  499. item = item_malloc();
  500. fread(item, sizeof (Item), 1, file_stream);
  501. while (!feof(file_stream)) {
  502. if (location->id == item->id_location && !item->returned) {
  503. fclose(file_stream);
  504. free(item);
  505. return FALSE;
  506. }
  507. fread(item, sizeof (Item), 1, file_stream);
  508. }
  509. fclose(file_stream);
  510. free(item);
  511. return TRUE;
  512. }
  513. char update_item_price(Item *item) {
  514. struct tm *time_info;
  515. time_t time_now = 0;
  516. DVD *dvd;
  517. int diffday = 0;
  518. dvd = search_dvd_by_id(item->id_dvd);
  519. if (dvd->id == NON_EXIST) {
  520. return FALSE;
  521. }
  522. // Calcula diferenуa de tempos
  523. time_now = time(NULL);
  524. time_info = localtime(&time_now);
  525. // Parse para o formato time_t
  526. time_info->tm_hour = 0;
  527. time_info->tm_min = 0;
  528. time_info->tm_sec = 1;
  529. time_info->tm_isdst = 0;
  530. time_now = mktime(time_info);
  531. diffday = time_now / 60 / 60 / 24 - item->return_date / 60 / 60 / 24;
  532. if (diffday <= 0) {
  533. // Tudo em dia, preуo normal
  534. item->price = dvd->price_location;
  535. } else {
  536. // Se tiver atrasado, o valor cobrado pela diрria terр base = 50% do preуo de aluguel
  537. item->price = dvd->price_location + (diffday * (0.5 * dvd->price_location));
  538. }
  539. free(dvd);
  540. if (update_item(item)) {
  541. return TRUE;
  542. } else {
  543. return FALSE;
  544. }
  545. }
  546. char form_item_insert(Location* location, char *input) {
  547. /* Caso de uso:
  548. * 1 - O sistema pergunta ao usuрrio qual filme quer adicionar.
  549. * 2 - O usuрrio pesquisa um filme
  550. * 3 - O sistema verifica um dvd disponьvel do filme
  551. * 4 - Se houver, o sistema relaciona o item ao dvd e adiciona Я locaусo.
  552. * 5 - Se nсo houver, o sistema informa o ocorrido.
  553. * 6 - Fim do caso de uso
  554. */
  555. Item *item;
  556. Movie *movie;
  557. DVD *dvd;
  558. item = item_malloc();
  559. do {
  560. do {
  561. printf("> Qual filme deseja adicionar? ");
  562. movie = form_movie_select(input);
  563. } while (movie->id == NON_EXIST);
  564. puts_movie_row_list();
  565. puts_movie(movie);
  566. // Tem certeza?
  567. } while (!be_sure(input));
  568. dvd = search_dvd_by_movie(movie, TRUE);
  569. if (dvd->id == NON_EXIST) {
  570. printf("%s: Nao ha DVD disponivel para este filme.\n", __FILE__);
  571. free(dvd);
  572. free(item);
  573. free(movie);
  574. return FALSE;
  575. }
  576. // Atribuiушes
  577. item->id_location = location->id;
  578. item->id_dvd = dvd->id;
  579. item->returned = FALSE;
  580. item->return_date = time(NULL) + (60 * 60 * 24 * (3)); // 3 dias
  581. item->price = dvd->price_location;
  582. location->total += dvd->price_location;
  583. // Escrevendo no arquivo as alteraушes
  584. if (insert_item(item)) {
  585. printf("Filme \"%s\" inserido com sucesso.\n", movie->title);
  586. free(item);
  587. free(dvd);
  588. free(movie);
  589. return TRUE;
  590. } else {
  591. printf("Filme \"%s\" nao foi inserido corretamente!\n", movie->title);
  592. free(item);
  593. free(dvd);
  594. free(movie);
  595. return FALSE;
  596. }
  597. }
  598. void form_item_remove(Location* location, char *input) {
  599. /* Caso de uso:
  600. * 1 - O sistema mostra os ьtens da locaусo.
  601. * 2 - O sistema pergunta ao usuрrio qual item quer remover.
  602. * 3 - O usuрrio escolhe um ьtem atraves do ID.
  603. * 4 - Se houver, o sistema remove com sucesso.
  604. * 5 - Se nсo houver, o sistema informa o ocorrido.
  605. * 6 - Fim do caso de uso
  606. */
  607. Item *item;
  608. item = form_item_select(location, input);
  609. // Verifica se ж ьtem vрlido
  610. if (item->id == NON_EXIST) {
  611. free(item);
  612. return;
  613. }
  614. // Tem certeza?
  615. if (!be_sure(input)) {
  616. printf("Abortando remocao de filme.\n\n");
  617. free(item);
  618. return;
  619. }
  620. location->total -= get_dvd_price(item->id_dvd);
  621. // Escrevendo no arquivo as alteraушes
  622. if (erase_item(item)) {
  623. printf("Filme removido com sucesso.\n");
  624. } else {
  625. printf("Filme nao foi removido corretamente!\n");
  626. }
  627. free(item);
  628. }
  629. void form_item_return(Location* location, char *input) {
  630. /* Caso de uso:
  631. * 1 - O sistema pergunta ao usuрrio qual item quer devolver.
  632. * 2 - O usuрrio escolhe um ьtem.
  633. * 3 - Se houver, o sistema devolve com sucesso.
  634. * 4 - Se nсo houver, o sistema informa o ocorrido.
  635. * 5 - Fim do caso de uso
  636. */
  637. Item *item;
  638. puts_items_by_location(location, FALSE);
  639. item = form_item_select(location, input);
  640. // Verifica se ж ьtem vрlido
  641. if (item->id == NON_EXIST) {
  642. free(item);
  643. return;
  644. }
  645. // Tem certeza?
  646. if (!be_sure(input)) {
  647. printf("Abortando devolucao de item.\n\n");
  648. free(item);
  649. return;
  650. }
  651. item->returned = TRUE;
  652. // Escrevendo no arquivo as alteraушes
  653. if (update_item(item)) {
  654. printf("Item devolvido com sucesso.\n");
  655. } else {
  656. printf("Item nao foi removido corretamente!\n");
  657. }
  658. free(item);
  659. }
  660. int form_items_insert(Location *location, char *input) {
  661. /* Objetivo: perguntar se o usuрrio quer adicionar mais ьtens. */
  662. int count = 0;
  663. do {
  664. if (form_item_insert(location, input)) {
  665. count++;
  666. }
  667. printf("> Deseja inserir mais filmes a esta locacao? [S]im ou [n]ao? ");
  668. read_string(input);
  669. } while (!strcasecmp(input, "S") || strcasecmp(input, "N"));
  670. return count;
  671. }
  672. void form_items_remove(Location *location, char *input) {
  673. /* Objetivo: perguntar se o usuрrio quer remover mais ьtens. */
  674. // Primeiramente, testa se hр ьtens na locaусo
  675. if (items_location_is_empty(location)) {
  676. printf("%s: Nao ha itens nesta locacao.\n", __FILE__);
  677. return;
  678. }
  679. do {
  680. form_item_remove(location, input);
  681. printf("> Deseja remover mais itens desta locacao? [S]im ou [n]ao? ");
  682. read_string(input);
  683. } while (!strcasecmp(input, "S") || strcasecmp(input, "N"));
  684. }
  685. void form_items_return(Location *location, char *input) {
  686. /* Objetivo: perguntar se o usuрrio quer devolver mais ьtens. */
  687. // Primeiramente, testa se hр ьtens aptos Я devoluусo na locaусo
  688. if (location_has_returned_items(location)) {
  689. printf("%s: Nao ha itens para ser devolvido nesta locacao.\n", __FILE__);
  690. return;
  691. }
  692. do {
  693. form_item_return(location, input);
  694. printf("> Deseja devolver mais itens desta locacao? [S]im ou [n]ao? ");
  695. read_string(input);
  696. } while (!strcasecmp(input, "S") || strcasecmp(input, "N"));
  697. }
  698. Item * form_item_select(Location *location, char *input) {
  699. int id;
  700. Item *item;
  701. item = item_malloc();
  702. do {
  703. printf("Digite [1] para pesquisar por ID ou [2] para pesquisar por titulo: ");
  704. read_string(input);
  705. switch (*input) {
  706. case '1':
  707. // Verifica se ж um ID vрlido
  708. id = check_by_id_item(input);
  709. if (id == NON_EXIST) {
  710. item->id = NON_EXIST;
  711. return item;
  712. }
  713. // Procura o item pelo ID
  714. item = search_item_by_id(id);
  715. *input = '1';
  716. break;
  717. case '2':
  718. // Verifica se ж um nome vрlido
  719. if (!check_by_name(input)) {
  720. item->id = NON_EXIST;
  721. return item;
  722. }
  723. // Procura o item pelo titulo
  724. item = search_item_by_movietitle(location, input);
  725. *input = '2';
  726. break;
  727. default:
  728. printf("Opcao invalida!\n");
  729. }
  730. // Caso nсo ache, retorna com ID = NON_EXIST
  731. if (item->id == NON_EXIST) {
  732. if (*input == '1')
  733. printf(ID_NOT_FOUND_ERROR, __FILE__, "item");
  734. else if (*input == '2')
  735. printf(NAME_NOT_FOUND_ERROR, __FILE__, "item");
  736. item->id = NON_EXIST;
  737. return item;
  738. }
  739. } while (*input != '1' && *input != '2');
  740. return item;
  741. }