Selasa, 16 Juni 2020

AprilMarket source code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node{
    char productName[1000];
    int qty;
    node *next;
}*head,*curr;

node *create(char productName[], int qty){
    node *newnode = (node*) malloc (sizeof(node));
    strcpy(newnode->productName,productName);
    newnode->qty = qty;
    newnode->next = NULL;
    return newnode;
}

void insert(char productName[], int qty){
    node *temp = create(productName, qty);
    if(!head) head = temp;
    else{
        curr = head;
        while(curr->next)curr = curr->next;
        curr->next = temp;
    }   
}

void printAll(){
    if(!head){
        printf("Daftar belanja masih kosong\n");
        return;
    }
   
    curr = head;
    int i = 1;
    while(curr){
        printf("%-3d. %-10s %3d\n", i, curr->productName, curr->qty);
        curr = curr->next;
        i++;
    }
}

void addItem(char productName[], int qty){
    printf("Input item name: ");
    scanf("%s", productName);
    getchar();
    printf("Input quantity: ");
    getchar();
    scanf("%d", &qty);
    getchar();
    getchar();
    insert(productName,qty);
}

void editItem(char productName[]){
    char userInput[100];
    int newQuantity;
   
    if(!head){
        printf("Daftar belanja masih kosong\n");
        getchar();
        getchar();
        return;
    }
   
    printf("Silahkan masukkan nama item yang mau dirubah: ");
    scanf("%s", userInput);
   
    curr = head;
    while(curr){
        if(strcmp(curr->productName,userInput) == 0){
            printf("Silahkan masukkan jumlah barang yang baru: ");
            scanf("%d", &newQuantity);
            curr->qty = newQuantity;
            getchar();
            getchar();
            return;
        }
        curr = curr->next;
    }
}

void removeItem(){
    if(!head){
        printf("Daftar belanja masih kosong\n");
        getchar();
        getchar();
        return;
    }
   
    printAll();
   
    int counter = 1;
    int userInput;
   
    printf("Masukkan angka: ");
    scanf("%d", &userInput);
   
    curr = head;
    if(counter == userInput){
        head = curr->next;
        curr->next = NULL;
        free(curr);
        getchar();
        getchar();
        return;
    }
    while(curr->next){
        counter++;
        if(counter == userInput){
            node *temp = curr->next;
            curr->next = curr->next->next;
            temp->next = NULL;
            free(temp);
            getchar();
            getchar();
            return;
        }
        curr = curr->next;
    }
   
    printf("Angka yang anda masukkan salah\n");
    getchar();
    getchar();
}

void checkout(){
    if(!head){
        printf("Daftar belanja masih kosong\n");
        getchar();
        getchar();
        return;
    }
   
    printAll();
   
    int x = rand() % 1000000 + 10000;
   
    printf("Total harga yang harus dibayarkan: %d\n",  x);
    getchar();
    getchar();
}

int main(){
    char productName[100];
    int qty;
    int choose;
    do{
        system("cls");
        printf("AprilMarket\n");
        printf("-----------------\n");
        printf("[1]. Add Item\n");
        printf("[2]. Edit Item\n");
        printf("[3]. Remove Item\n");
        printf("[4]. Checkout\n");
        printf("[5]. Exit\n");
        printf(">> ");
        scanf("%d", &choose);
        if(choose == 1){
            system("cls");
            addItem(productName, qty);
        }
        else if(choose == 2){
            system("cls");
            editItem(productName);
        }
        else if(choose == 3){
            system("cls");
            removeItem();
        }
        else if(choose == 4){
            system("cls");
            checkout();
        }
    }while(choose != 5);
    printf(". . .Thank you. . .");
   
    return 0;
}

Tidak ada komentar:

Posting Komentar