Berikut adalah source code dari AprilMarket (Aplikasi dengan konsep Minimarket)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int list=0;
struct market{
char product[105];
int quantity;
int nomor;
struct market *next;
struct market *prev;
}*head = NULL, *tail = NULL;
void pushTail(char* x, int y, int z){
struct market *curr = (struct market*)malloc(sizeof(struct market));
curr->quantity = y; curr->nomor = z;
strcpy(curr->product,x);
curr->next = NULL; curr->prev = NULL;
if(head == NULL) head = tail = curr;
else{
tail->next = curr;
curr->prev = tail;
tail = curr;
}
}
bool checkinquantity(int x){
if(x < 1){
printf("Order\'s quantity can\'t be less than 1!\n");
return true;
}
else return false;
}
bool checkinproduct(char *x){
if(x == NULL || strlen(x) == 0 || x[0] == ' '){
printf("Order\'s name can\'t empty!");
return true;
} else return false;
}
void menu(){
printf("Welcome to AprilMarket\n");
printf("----------------------\n");
printf("1. Add Order\n");
printf("2. Edit Order\n");
printf("3. Delete Order\n");
printf("4. Checkout\n");
printf("5. Exit\n");
printf("----------------------\n");
printf("Choose >> ");
}
void printOrder(){
struct market *curr = (struct market*)malloc(sizeof(struct market));
curr = head;
printf("Order List:\n\n");
for(int i=0; i<list; i++){
printf("[%d] Name: %s || Quantity: %d.\n", i+1, curr->product, curr->quantity);
curr = curr->next;
}
}
void popHead(){
struct market *curr = head;
if(curr == tail){
head = tail = NULL;
free(curr);
} else {
head = head->next;
free(curr);
head->prev = NULL;
}
}
void popTail(){
struct market *curr = tail;
if(curr == head){
head = tail = NULL;
free(curr);
} else {
tail = tail->prev;
free(curr);
tail->next = NULL;
}
}
void orderremove(int x){
struct market *curr = head;
int point=0;
while(curr != NULL){
if(curr->nomor = x){
point = 1;
break;
}
}
if(point == 1){
if(curr == head) popHead();
else if(curr == tail) popTail();
else{
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
free(curr);
}
}
}
void orderchange(int x){
struct market *curr = head;
int point=0;
while(curr != NULL){
if(curr->nomor = x){
point = 1;
break;
}
}
if(point == 1){
char newname[105]; int newquan;
do{
printf("Input your new order\'s name: ");
scanf("%[^\n]", newname); getchar();
}while(checkinproduct(newname));
do{
printf("Input your new order\'s quantity: ");
scanf("%d", &newquan); getchar();
}while(checkinquantity(newquan));
memcpy(curr->product,newname,sizeof(newname));
curr->quantity = newquan;
printf("Order updated!\n");
} else {
printf("Can\'t find order!\n");
}
}
int main(){
srand(time(0));
int choice, quantity, number, nomor=1;
char product[100];
do{
menu();
scanf("%d", &choice); getchar();
system("cls");
switch(choice){
case 1:{
do{
printf("Input your order\'s name: ");
scanf("%[^\n]", product); getchar();
}while(checkinproduct(product));
do{
printf("Input your order\'s quantity: ");
scanf("%d", &quantity); getchar();
}while(checkinquantity(quantity));
pushTail(product,quantity,nomor);
++list; ++nomor;
break;
}
case 2:{
if(list == 0) printf("Order List is empty!\n");
else{
printOrder();
do{
printf("Select number of order you want to edit [1-%d | 0 to exit]: ", list);
scanf("%d", &number); getchar();
if(number == 0) break;
}while(number <= 0 || number > list);
if(number != 0) orderchange(number);
}
break;
}
case 3:{
if(list == 0) printf("Order List is empty!\n");
else{
printOrder();
do{
printf("Select number of order you want to delete [1-%d | 0 to exit]: ", list);
scanf("%d", &number); getchar();
if(number == 0) break;
}while(number <= 0 || number > list);
if(number != 0) orderremove(number);
}
break;
}
case 4:{
if(list == 0) printf("Order list is empty!\n");
else{
printf("\n");
printf("=====< Checkout >=====\n");
struct market *curr = head;
for(int i=0; i<list; i++){
printf("[%d] Name: %s || Quantity: %d.\nPrice: Rp. %d",
i+1, curr->product, curr->quantity, (rand()*curr->quantity)%1000000);
curr = curr->next;
}
printf("\n=======================\n");
printf("Total: Rp. %d\n\n", (rand()%1000000));
printf("Kindness is Free\n");
}
break;
}
}
}while(choice != 5);
}
Tidak ada komentar:
Posting Komentar