SOURCE CODE ASSIGNMENT
MINI MARKET CASHIER SIMULATOR
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
struct tnode{
char namaProduk[50];
int qtyProduk;
int price;
struct tnode *prev, *next;
}*head = NULL, *tail = NULL;
void pushCertainNode(char namaProduk[50], int qtyProduk, int price){
struct tnode *newNode = (struct tnode*)malloc(sizeof(struct tnode));
struct tnode *ptr = head;
strcpy( newNode->namaProduk, namaProduk);
newNode->qtyProduk = qtyProduk;
newNode->price = price;
newNode->prev = NULL;
newNode->next = NULL;
if(head == NULL){
head = tail = newNode;
}
else if(strcasecmp(namaProduk, head->namaProduk) < 0){ // push depan
newNode->next = head;
head->prev = newNode;
head = newNode;
}
else if(strcasecmp(namaProduk, tail->namaProduk) >= 0){ // push belakang
if(strcasecmp(namaProduk, tail->namaProduk) == 0){
tail->qtyProduk += newNode->qtyProduk;
}else{
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
else{
while(strcasecmp(namaProduk, ptr->next->namaProduk) >= 0) ptr = ptr->next; //push tengah
if(strcasecmp(namaProduk, ptr->namaProduk) == 0){
ptr->qtyProduk += newNode->qtyProduk;
}else{
newNode->prev = ptr;
newNode->next = ptr->next;
ptr->next->prev = newNode;
ptr->next = newNode;
}
}
}
void popCertainNode(char namaProduk[50]){
struct tnode *ptr = head;
if(head == tail && strcasecmp(head->namaProduk, namaProduk) == 0){
head = tail = NULL;
free(ptr);
printf("Produk berhasil dihapus!\n");
}
else if(strcasecmp(head->namaProduk, namaProduk) == 0){
head = head->next;
head->prev = NULL;
free(head->prev);
printf("Produk berhasil dihapus!\n");
}
else if(strcasecmp(tail->namaProduk, namaProduk) == 0){
tail = tail->prev;
tail->next = NULL;
free(tail->next);
printf("Produk berhasil dihapus!\n");
}
else{
while(strcasecmp(ptr->namaProduk, namaProduk) != 0) {
if(ptr->next == NULL){
break;
}
else ptr = ptr->next;
}
if(strcasecmp(namaProduk, ptr->namaProduk) == 0){
ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev;
free(ptr);
printf("Produk berhasil dihapus!\n");
}
else printf("Produk tidak ditemukan!\n");
}
}
void editQty(char namaProduk[50], int qtyProdukBaru){
struct tnode *newNode = (struct tnode*)malloc(sizeof(struct tnode));
struct tnode *ptr = head;
strcpy(newNode->namaProduk, namaProduk);
newNode->qtyProduk = qtyProdukBaru;
newNode->prev = NULL;
newNode->next = NULL;
if(head == NULL) return;
else{
while(strcasecmp(namaProduk, ptr->namaProduk) != 0){
if(ptr->next == NULL){
break;
}else ptr = ptr->next;
}
if(strcasecmp(namaProduk, ptr->namaProduk) == 0){
ptr->qtyProduk = newNode->qtyProduk;
printf("Jumlah produk berhasil di edit!\n");
}
else printf("Produk tidak ditemukan!\n");
}
}
void printData(){
struct tnode *ptr = head;
if(head==NULL) return;
else{
for(int i = 0; i < 27; i++) printf("*");
printf(" INDOMARKET ");
for(int i = 0; i < 26; i++) printf("*");
puts("");
for(int i = 0; i < 25; i++) printf("*");
printf(" DATA PEMBELIAN ");
for(int i = 0; i < 24; i++) printf("*");
puts("");
printf("|%10sNama Produk%-10s|%1sQty%-1s|%3sHarga%-3s|%1sTotal Harga%-1s|\n", "","","","","", "", "", ""); // |31| + |5| + |11| + |13|
for(int i = 0 ; i < 65 ; i++) printf("=");
puts("");
while(ptr!=NULL){
printf("|%-31s| %-3d | Rp %-6d | Rp %-8d |\n", ptr->namaProduk, ptr->qtyProduk, ptr->price, (ptr->price * ptr->qtyProduk));
ptr = ptr->next;
}
for(int i = 0 ; i < 65 ; i++) printf("=");
puts("");
}
}
void printBill(){
struct tnode *ptr = head;
int countTotalPrice = 0;
if(head==NULL) return;
else{
for(int i = 0; i < 23; i++) printf(" ");
printf(" INDOMARKET ");
for(int i = 0; i < 22; i++) printf(" ");
puts("");
for(int i = 0; i < 21; i++) printf("*");
printf(" STRUK BELANJA ");
for(int i = 0; i < 21; i++) printf("*");
puts("");
printf("Qty %10sNama Barang%-10s %2sHarga%-2s %3sJumlah%-2s\n", "","","","","", "");
for(int i = 0 ; i < 57 ; i++) printf("-");
puts("");
while(ptr!=NULL){
printf("%-3d %-31s Rp %-6d Rp %-8d\n", ptr->qtyProduk, ptr->namaProduk, ptr->price, (ptr->price * ptr->qtyProduk));
countTotalPrice = countTotalPrice + (ptr->price * ptr->qtyProduk);
ptr = ptr->next;
}
puts("");
printf("%4s", ""); printf("TOTAL%-5s:", ""); //15
printf("%-31s", ""); printf("Rp %-8d\n", countTotalPrice);
puts("");
printf("%23s%-22s\n", "", "Terima Kasih");
printf("%20s%-19s\n", "", "'Kindness is Free'"); //16
}
}
int main(){
struct tnode *ptr = head;
int menu;
char inputNamaProduk[50];
int inputQtyProduk;
int randomPrice;
int randomUpper = 500000, randomLower = 1000;
int j = 0;
char namaProdukToUpper;
do{
system("cls");
printf("=== IndoMarket ===\n");
printf("1. Input produk\n");
printf("2. Edit jumlah produk\n");
printf("3. Hapus produk\n");
printf("4. Lihat Keranjang Pembelian\n");
printf("5. Checkout\n");
printf("Pilih menu >> ");
scanf("%d", &menu);
getchar();
system("cls");
switch (menu)
{
case 1: // INPUT PRODUK
printf("* Input Produk *\n");
printf("Masukkan nama produk: ");
gets(inputNamaProduk);
j = 0;
while(inputNamaProduk[j]){
namaProdukToUpper = inputNamaProduk[j];
toupper(namaProdukToUpper);
j++;
}
printf("Masukkan jumlah produk: ");
scanf("%d", &inputQtyProduk);
randomPrice = ((rand() % (randomUpper - randomLower + 1)) + randomLower); //harga random, batas atas = upper, batas bawah = lower
pushCertainNode(inputNamaProduk, inputQtyProduk, randomPrice);
system("cls");
printf("Produk berhasil di input!\n");
getchar();
printf("\nTekan enter untuk kembali ke menu!\n");
getchar();
break;
case 2: // EDIT JUMLAH PRODUK
printf("* Edit Jumlah Produk *\n");
if(head==NULL) {
system("cls");
printf("Belum ada data, silahkan masukkan data terlebih dahulu!\n");
}
else {
system("cls");
printData();
printf("Masukkan nama produk yang ingin di edit: ");
gets(inputNamaProduk);
printf("Masukkan jumlah produk yang benar: ");
scanf("%d", &inputQtyProduk);
system("cls");
editQty(inputNamaProduk, inputQtyProduk);
getchar();
}
printf("\nTekan enter untuk kembali ke menu!\n");
getchar();
break;
case 3: // HAPUS PRODUK
printf("* Hapus Produk *\n");
if(head==NULL) {
system("cls");
printf("Tidak ada data!\n");
}
else{
printData();
printf("Masukkan nama produk yang ingin di hapus: ");
gets(inputNamaProduk);
system("cls");
popCertainNode(inputNamaProduk);
}
printf("\nTekan enter untuk kembali ke menu!\n");
getchar();
break;
case 4: // LIHAT DATA PEMBELIAN
if(head==NULL) {
system("cls");
printf("Tidak ada data!\n");
} else printData();
printf("\nTekan enter untuk kembali ke menu!\n");
getchar();
break;
case 5: // CHECKOUT
if(head==NULL) {
system("cls");
printf("Tidak ada data pembelian!\n");
}
else{
printBill();
}
getchar();
break;
}
} while(menu != 5);
return 0;
}