Source code untuk Tugas GSLC
Source code untuk Tugas GSLC
Aplikasi untuk jual beli barangNama : Leander Ignacio Jose Antonius
NIM : 2301922285
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct item
{
char namabarang[20];
int hargabarang;
int quantitybarang;
struct item *next;
struct item *previous;
}*head=NULL, *tail=NULL;
void Printitem()
{
if(head==NULL&&tail==NULL)
{
printf("No Data\n");
}
else
{
struct item *curr=head;
int nomor=1;
printf("==========================================\n");
while(curr!=NULL)
{
printf("Data %d\n",nomor);
printf("Nama barang : %s\n",curr->namabarang);
printf("Harga barang : %d\n",curr->hargabarang);
printf("Jumlah barang : %d\n",curr->quantitybarang);
curr=curr->next;
printf("==========================================\n");
nomor++;
}
nomor=1;
}
}
void Updateitem()
{
if(head==NULL&&tail==NULL)
{
printf("No data\n");
}
else
{
struct item *curr=head;
char nama[100];int jumlah;
Printitem();
printf("Masukkan nama barang yang ingin di update : ");
scanf("%[^\n]",&nama);
while(strcmp(curr->namabarang,nama)!=0 && curr->next!=NULL)
{
curr=curr->next;
}
if(strcmp(curr->namabarang,nama) != 0)
{
printf("Data not found!\n");
}
else
{
printf("==========================================\n");
printf("Nama barang : %s\n",curr->namabarang);
printf("Harga barang : %d\n",curr->hargabarang);
printf("Jumlah barang : %d\n",curr->quantitybarang);
printf("==========================================\n");
printf("Masukkan Quantity yang baru : ");
scanf("%d",&jumlah);getchar();
curr->quantitybarang=curr->quantitybarang+jumlah;
}
}
}
int HargaGenerator()
{
int upper=100000,lower=10000;
int num=(rand()%(upper-lower+1))+lower;
return num;
}
int Additem()
{
struct item *newitem=(struct item*) malloc(sizeof(struct item));
char nama[20];
int harga;
int quantity;
newitem->next=NULL;
newitem->previous=NULL;
printf("Masukkan nama barang : ");
scanf("%[^\n]",nama);
strcpy(newitem->namabarang,nama);
printf("Masukkan jumlah barang : ");
scanf("%d",&quantity);getchar();
newitem->quantitybarang=quantity;
harga=HargaGenerator();
newitem->hargabarang=harga;
if (head == NULL&&tail == NULL)
{
head = tail = newitem;
}
else if(strcmp(head->namabarang,newitem->namabarang) > 0)
{
newitem->next=head;
head->previous=newitem;
head=newitem;
}
else if(strcmp(tail->namabarang,newitem->namabarang) < 0)
{
newitem->previous=tail;
tail->next=newitem;
tail=newitem;
}
else
{
struct item *temp = head;
while (strcmp(temp->namabarang,newitem->namabarang)<0)
{
temp=temp->next;
}
newitem->next=temp->next;
temp->next->previous=newitem->previous;
temp->next=newitem;
}
}
void Deleteitem()
{
if(head == NULL) return;
char nama[100];
Printitem();
printf("Masukkan nama barang yang ingin di delete : ");
scanf("%[^\n]",&nama);getchar();
struct item *curr=head;
if(head==tail && strcmp(nama, head->namabarang) == 0)
{
head = tail = NULL;
free(head);
return;
}
if(strcmp(nama,head->namabarang)==0)
{
head=curr->next;
curr->next->previous=NULL;
curr=NULL;
free(curr);
return;
}
else if(strcmp(nama, tail->namabarang)==0)
{
tail=curr->previous;
tail->next = NULL;
curr = NULL;
free(curr);
return;
}
curr=head->next;
while(strcmp(curr->namabarang,nama)!=0 && curr->next!=tail)
{
curr=curr->next;
}
if(strcmp(curr->namabarang,nama) != 0)
{
printf("Data not found!\n");
}
else
{
curr->previous->next = curr->next;
curr->next->previous=curr->previous;
curr = NULL;
free(curr);
}
}
void Checkout()
{
if(head==NULL&&tail==NULL)
{
printf("No data\n");
}
else
{
struct item *curr=head;
char nama[100];int jumlah;
int jumlahharga;
Printitem();
while(true)
{
printf("Masukkan nama barang yang ingin di checkout [jika ingin selesai checkout ketik exit]: ");
scanf("%[^\n]",&nama);
if(strcmpi(nama,"exit")==0)
{
printf("==========================================\n");
printf("Harga total : %d\n",jumlahharga);
printf("Harga yang harus dibayar : Gratis\n");
printf("Kindness is free\n");
break;
}
else
{
while(strcmp(curr->namabarang,nama)!=0 && curr->next!=NULL)
{
curr=curr->next;
}
if(strcmp(curr->namabarang,nama) != 0)
{
printf("Data not found!\n");
}
else
{
printf("==========================================\n");
printf("Nama barang : %s\n",curr->namabarang);
printf("Harga barang : %d\n",curr->hargabarang*curr->quantitybarang);
printf("Jumlah barang : %d\n",curr->quantitybarang);
printf("==========================================\n");
printf("Masukkan Quantity yang mau di checkout : ");
scanf("%d",&jumlah);
getchar();
printf("\n");
printf("Harga total : %d\n",jumlahharga=jumlahharga+(jumlah*curr->hargabarang));
curr->quantitybarang=curr->quantitybarang-jumlah;
}
}
}
}
}
int main()
{
int input=0;
while(input!=6)
{
printf("1. Add item\n");
printf("2. View item\n");
printf("3. Update item\n");
printf("4. Delete item\n");
printf("5. Checkout\n");
printf("6. Exit\n");
printf("input >> ");
scanf("%d",&input);
getchar();
printf("\n");
if(input==1)
{
Additem();
}
else if(input==2)
{
Printitem();
}
else if(input==3)
{
Updateitem();
}
else if(input==4)
{
Deleteitem();
}
else if(input==5)
{
Checkout();
}
else if(input>6)
{
printf("Wrong input\n");
}
}
return 0;
}
Link download cpp : Click here
Sekian dan maaf jika masih ada bug, terima kasih.
Comments
Post a Comment