Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <locale.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <ctype.h>
- #include <conio.h>
- #define MAX 100
- struct produto
- {
- int idProduto, quantidade;
- char nome[30];
- double valor;
- };
- int WriteProduto(struct produto Produto)
- {
- int arquivosGravados;
- void IncrementaQuantidade();
- FILE *file = fopen("produtos.txt", "ab");
- if (file == NULL)
- {
- printf("Erro de abertura de arquivo.\n");
- return 0;
- }
- arquivosGravados = fwrite(&Produto,sizeof(struct produto), 1, file);
- if (arquivosGravados != 1)
- {
- printf("Erro de gravacao do produto.\n");
- return 0;
- }
- fclose(file);
- IncrementaQuantidade();
- return 1;
- }
- struct produto ReadProduto(int idProduto)
- {
- struct produto Produto;
- FILE *file = fopen("produtos.txt", "rb");
- fseek(file, idProduto*sizeof(struct produto), SEEK_SET);
- fread(&Produto, sizeof(struct produto), 1, file);
- fclose(file);
- return Produto;
- }
- int CarregarProdutos(struct produto Produto[], int tamanhoVetor)
- {
- int quantidade;
- FILE *file = fopen("produtos.txt", "rb");
- if (file == NULL)
- {
- printf("\nErro. Nenhum produto registrado.");
- return 0;
- }
- quantidade = fread(Produto, sizeof(struct produto), tamanhoVetor, file);
- if (quantidade != tamanhoVetor)
- {
- printf("\nErro de gravacao.");
- return 0;
- }
- return 1;
- }
- int GetQuantidade()
- {
- int quantidade, zero = 0;
- FILE *file = fopen("config.txt", "rb");
- if (file == NULL)
- {
- fclose(file);
- file = fopen("config.txt", "wb");
- fwrite(&zero, sizeof(int), 1, file);
- fclose(file);
- file = fopen("config.txt", "rb");
- }
- fread(&quantidade, sizeof(int), 1, file);
- fclose(file);
- return quantidade;
- }
- void IncrementaQuantidade()
- {
- int valor = GetQuantidade() + 1;
- FILE *file = fopen("config.txt", "wb");
- fwrite(&valor, sizeof(int), 1, file);
- fclose(file);
- }
- void desenharCabecalhoNota(void)
- {
- printf("\n =======================================================\n");
- printf("\t\t Pobr's Wallet - NOTA FISCAL");
- printf("\n =======================================================\n");
- }
- void desenharCabecalho(void)
- {
- printf("\n =======================================================\n");
- printf("\t\t Pobr's Wallet");
- printf("\n =======================================================\n");
- }
- void desenharCabecalhoEstoque(void)
- {
- printf("\n =======================================================\n");
- printf("\t\t Estoque - Pobr's Wallet");
- printf("\n =======================================================\n");
- }
- int main()
- {
- setlocale(LC_ALL, "Portuguese");
- int opInicial, produtos, unidadesProduto[produtos], qtdItens, respostaCliente, i, O, qtdItensEstoque, idCompra;
- double cpfCliente, valorProduto[produtos], valorPagar = 0;
- char nomeProduto[produtos], nomeCliente[25], procuraProduto[50];
- struct produto Produto, Produtos[MAX];
- //desenharCabecalho();
- printf("\n Olá Gilberto, bem-vindo!");
- system("pause");
- Sleep(1500);
- do
- {
- fflush(stdin);
- system("cls");
- desenharCabecalho();
- do
- {
- printf("\n O que deseja fazer? \n\n\t [1] - ACESSAR ESTOQUE \n\t [2] - ABRIR ATENDIMENTO \n\t [3] - SAIR");
- printf("\n Resposta: "); scanf("%i", &opInicial);
- if (opInicial != 1 && opInicial != 2 && opInicial != 3) printf("\n Desculpe, mas não entendi.");
- }while(opInicial != 1 && opInicial != 2 && opInicial != 3);
- switch(opInicial)
- {
- case 1:
- printf("\n Acessando o estoque ...");
- Sleep(500);
- system("cls");
- desenharCabecalho();
- do
- {
- printf("\n O que deseja fazer? \n\n\t [1] - ADICIONAR NOVOS PRODUTOS \n\t [2] - DELETEAR PRODUTOS EXISTENTES \n\t [3] - VOLTAR");
- printf("\n Resposta: "); scanf("%d", &O);
- if (O < 1 && O > 3) printf ("\n Desculpe, mas não entendi.");
- }while (O < 1 || O > 3);
- switch(O)
- {
- case 1:
- do
- {
- printf("\n Quantos produtos deseja adicionar: "); scanf("%d", &produtos);
- if(produtos < 0) printf(" ERRO. Reveja sua resposta.");
- }while(produtos < 0);
- int i;
- for (i = 0; i < produtos; i++)
- {
- fflush(stdin);
- printf(" Nome do %iº produto: ", i+1); scanf("%s", &Produto.nome);
- printf(" Unidades do %iº produto: ", i+1); scanf("%i", &Produto.quantidade);
- printf(" Valor unitário: R$ "); scanf("%lf", &Produto.valor);
- if (WriteProduto(Produto)) printf("\n Produto adcionado com sucesso.\n");
- else printf("\n Erro ao adcionar o arquivo.");
- }
- printf("\n Voltando para o menu inicial ...");
- Sleep(1500);
- break;
- case 2:
- printf("\n Opção não configurada.");
- Sleep(1500);
- break;
- }
- break;
- case 2:
- printf("\n Abrindo atendimeno ...");
- system("cls");
- desenharCabecalho();
- printf(" Olá, como é seu nome? "); scanf("%s", &nomeCliente);
- printf("\n Bem vindo %s ao Pobr's Market!", nomeCliente);
- printf("\n Abrindo estoque ...");
- Sleep(2000);
- system("CLS");
- do
- {
- desenharCabecalho();
- if (CarregarProdutos(Produtos, GetQuantidade()))
- {
- int j;
- for (j = 0; j < GetQuantidade(); j++)
- {
- printf("\n IdProduto = %2d\tNome = %30s\tQuantidade = %d\tValor = %3.2lf", Produtos[*Produtos+j].idProduto, Produtos[*Produtos+j].nome, Produtos[*Produtos+j].quantidade,Produtos[*Produtos+j].valor);
- }
- printf("\n\n Que produto deseja comprar(id): "); scanf("%d", &idCompra);
- printf(" Quantas unidades deseja: "); scanf("%i", &qtdItens);
- valorPagar += Produtos[idCompra].valor*qtdItens;
- do
- {
- printf("\n\n [1] - Continuar compra;\n [2] - Encerrar compra!\n Resposta: "); scanf("%i", &respostaCliente);
- }while(respostaCliente != 1 && respostaCliente != 2);
- printf("\n Total a pagar: %lf", valorPagar);
- getch();
- }
- else printf("\n Erro ao carregar os produtos.");
- Sleep(700);
- }while(respostaCliente != 2 );
- break;
- case 3:
- printf("\nFechando o sistema ...");
- Sleep(700);
- exit(1);
- break;
- }
- }while (opInicial != 3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement