Berikut ini adalah source code untuk manajemen barang jual beli yang sederhana.
Nama : Christopher Wibisono
NIM : 2301913822
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
int total;
struct ll
{
int qty;
char nama[50];
int price;
struct ll *next;
struct ll *prev;
}*head = NULL, *tail = NULL, *curr;
void print()
{
curr = head;
if (curr == NULL)
printf("No Data!\n");
else
{
int i = 1;
while(curr != NULL)
{
printf("---------------------\n");
printf("Data %d : %s\n",i,curr->nama);
printf("Quantity %d : %d\n",i,curr->qty);
printf("Price %d : %d\n",i,curr->price);
curr = curr->next;
i++;
}
}
}
void push()
{
curr = (struct ll*)malloc(sizeof(struct ll));
printf("Input Name : ");
scanf("%[^\n]",curr->nama);
printf("Input Quantity : ");
scanf("%d",&curr->qty);
getchar();
curr->price = rand() % 100 + 1;
printf("Price Generated : $%d\n",curr->price);
curr->next = curr->prev = NULL;
if (head == NULL)
{
head = tail = curr;
}
else if (strcmp(curr->nama,head->nama) < 0)
{
curr->next = head;
head->prev = curr;
head = curr;
}
else if (strcmp(curr->nama,tail->nama) > 0)
{
curr->prev = tail;
tail->next = curr;
tail = curr;
}
else
{
struct ll *temp = head;
while (strcmp(curr->nama,temp->next->nama) > 0)
{
temp = temp->next;
}
curr->next = temp->next;
temp->next->prev = curr->prev;
temp->next = curr;
}
}
void update()
{
print();
char nk[50];
if(head != NULL)
{
struct ll *temp = head;
strcpy(nk,"NULL");
printf("Input data to be updated (name) : ");
scanf("%[^\n]",nk);
getchar();
while(temp != NULL && strcmp(nk,temp->nama) != 0)
{
temp = temp->next;
}
if(temp == NULL)
{
printf("Data %s not found!\n",nk);
}
else
{
printf("Update quantity : ");
scanf("%d",&temp->qty);
getchar();
}
}
}
int q;
void kurang(int h)
{
q = 0;
do
{
printf("Input Quantity to be checked out : ");
scanf("%d",&q);
getchar();
if(q > h)
printf("Quantity in storage less than %d\n",q);
else if(q == 0)
printf("Cannot checkout 0 items\n");
} while (q > h || q == 0);
}
void cekout()
{
if(head == NULL)
{
printf("No Data!\n");
}
else
{
char nx[50];
int no = 0;
while(strcmp(nx,"0") != 0)
{
strcpy(nx,"NULL");
print();
if (head == NULL)
break;
printf ("Input 0 to end checkout\n");
printf ("Input data to checkout : ");
scanf("%[^\n]",nx);
getchar();
if (strcmp(nx,"0") == 0)
break;
int qx = 0;
if (head == tail && strcmp(nx,head->nama) == 0)
{
qx = head->qty;
kurang(qx);
head->qty-=q;
total+=head->price*q;
if(head->qty == 0)
{
free(head);
head = tail = NULL;
}
}
else if (strcmp(nx,head->nama) == 0)
{
curr = head;
qx = curr->qty;
kurang(qx);
total+=curr->price*q;
curr->qty-=q;
if(curr->qty == 0)
{
head = head->next;
free(curr);
head->prev = NULL;
}
else
{
head = curr;
}
}
else if (strcmp(nx,tail->nama) == 0)
{
curr = tail;
qx = curr->qty;
kurang(qx);
total+=curr->price*q;
curr->qty-=q;
if(curr->qty == 0)
{
tail = tail->prev;
free(curr);
tail->next = NULL;
}
else
{
tail = curr;
}
}
else
{
curr = head;
ll *temp;
while(curr != NULL && strcmp(curr->nama,nx) != 0)
{
temp = curr;
curr = curr->next;
}
if (curr == NULL)
{
printf("Data : %s not found!\n",nx);
}
else
{
qx = curr->qty;
kurang(qx);
total+=curr->price*q;
curr->qty-=q;
if(curr->qty == 0)
{
temp->next = curr->next;
curr->next->prev = temp;
free(curr);
}
}
}
}
printf("Total Price : %d\n",total);
printf("Checkout Price : FREE\n");
printf("kindness is free\n");
}
}
void del()
{
if (head == NULL)
{
printf("No Data!\n");
}
else
{
print();
char x[50];
printf("Input data to be deleted : ");
scanf("%[^\n]",x);
getchar();
if (head == tail && strcmp(x,head->nama) == 0)
{
free(head);
head = tail = NULL;
}
else if (strcmp(x,head->nama) == 0)
{
curr = head;
head = head->next;
free(curr);
head->prev = NULL;
}
else if (strcmp(x,tail->nama) == 0)
{
curr = tail;
tail = tail->prev;
free(curr);
tail->next = NULL;
}
else
{
curr = head;
ll *temp;
while(curr != NULL && strcmp(x,curr->nama) != 0)
{
temp = curr;
curr = curr->next;
}
if (curr == NULL)
{
printf("Data : %s not found!\n",x);
}
else
{
temp->next = curr->next;
curr->next->prev = temp;
free(curr);
}
}
}
}
int main()
{
int ch;
do
{
ch = -1;
printf ("Menu :\n");
printf ("0. Exit\n");
printf ("1. Add Data\n");
printf ("2. Update Data\n");
printf ("3. Checkout Data\n");
printf ("4. View Data\n");
printf ("5. Delete Data\n");
printf ("Your Choice : ");
scanf("%d",&ch);
getchar();
if (ch == 1)
{
push();
}
else if (ch == 2)
{
update();
}
else if (ch == 3)
{
total = 0;
cekout();
}
else if (ch == 4)
{
printf ("List of data :\n");
print();
}
else if (ch == 5)
{
del();
}
printf ("Process Completed!\nPress enter to continue...");
getchar();
system ("cls");
}
while(ch != 0);
printf("Christopher Wibisono - 2301913822\n");
return 0;
}
Jika ingin mencoba - coba, berikut saya cantumkan link untuk download file .cpp dari source code diatas.
https://drive.google.com/open?id=1UOEhjK7BOn4Io_3OZ5Q6hpn1tPRPFH7l
Terima kasih atas kunjungannya :3
Comments
Post a Comment