#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
//This program does't come with proper validation since it's not mention in the query. So be wise while using this program.
struct Node{
char barang[21];
int qty, price;
Node *next, *prev;
}*head, *tail;
Node *create(char* barang, int qty, int price){
Node *temp = (Node*)malloc(sizeof(Node));
strcpy(temp->barang, barang);
temp->qty = qty;
temp->price = price;
temp->next = temp->prev = NULL;
return temp;
}
void pushHead(Node *temp){
if(!head)head = tail = temp;
else{
head->prev = temp;
temp->next = head;
head = temp;
}
}
void pushTail(Node *temp){
if(!head)head = tail = temp;
else{
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
void pushMid(Node *temp){
if(!head)head = tail = temp;
else if(strcmp(head->barang, temp->barang) > 0)pushHead(temp);
else if(strcmp(tail->barang, temp->barang) < 0)pushTail(temp);
else{
Node *curr = head;
while(strcmp(curr->barang, temp->barang) < 0){
curr = curr->next;
}
curr->prev->next = temp;
temp->prev = curr->prev;
temp->next = curr;
curr->prev = temp;
}
}
void popHead(){
Node *temp = head;
if(!head){
printf("No Data");
return;
}
else if(head==tail){
head = tail = NULL;
}else{
head = temp->next;
temp->next = temp->prev = head->prev = NULL;
temp = NULL;
}
free(temp);
}
void popTail(){
Node *temp = tail;
if(!head){
printf("No Data");
return;
}
else if(head==tail){
head = tail = NULL;
}else{
tail = temp->prev;
temp->next = temp->prev = tail->next = NULL;
temp = NULL;
}
free(temp);
}
void popSearch(char* barang){
if(!strcmp(head->barang, barang))popHead();
else if(!strcmp(tail->barang, barang))popTail();
else {
Node *curr = head;
while(strcmp(head->barang, barang)){
curr = curr->next;
if(!curr){
printf("No Data");
return;
}
}
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
curr->next = curr->prev = NULL;
free(curr);
}
}
void cls(){
for(size_t i = 0; i < 30; ++i){
puts("");
}
}
bool view(){
cls();
Node *curr = head;
if(!head){
printf("Please Input Some Item First!");
getchar();
return false;
}
puts("=========================================");
while(curr!=NULL){
printf("| %20s | %4d | %7d |\n", curr->barang, curr->qty, curr->price);
curr = curr->next;
}
puts("=========================================\n");
return true;
}
void input(){
int qty, price, temp, len;
char barang[101];
do{
if(head){
view();
}
printf("Input nama barang [3-20 Characters] [No duplicate name]: ");
scanf("%[^\n]", barang);getchar();
len = strlen(barang);
}while(len < 3 || len > 20);
printf("Input quantity barang : ");
scanf("%d",&qty);getchar();
srand(time(NULL));
temp = rand() % 10 + 1;
price = temp*1000;
pushMid(create(barang,qty,price));
cls();
printf("Item berhasil di input! ");
getchar();
}
void update(){
char barang[21];
int qty;
Node* curr;
if(!view()){
return;
}
printf("Input nama barang yang ingin diupdate [Case Sensitive] : ");
scanf("%[^\n]", barang); getchar();
if(!strcmp(head->barang, barang))curr = head;
else if(!strcmp(tail->barang, barang))curr = tail;
else{
curr = head;
while(!strcmp(curr->barang, barang)){
curr = curr->next;
if(!curr){
printf("No Data");
return;
}
}
}
printf("Input nama barang baru : ");
scanf("%[^\n]", barang);getchar();
printf("Input quantity barang baru : ");
scanf("%d", &qty);getchar();
srand(time(NULL));
int temp = rand()%10 + 1;
int price = temp*1000;
curr->price = price;
strcpy(curr->barang,barang);
curr->qty = qty;
cls();
printf("Update Succesfull! ");
getchar();
}
void del(){
char barang[21];
if(!view()){
return;
}
printf("Input nama barang yang ingin dihapus [Case Sensitive] : ");
scanf("%[^\n]", barang);getchar();
popSearch(barang);
cls();
printf("Delete Succesfull! ");
getchar();
}
int calculate(){
int total = 0;
Node *curr = head;
while(curr!=NULL){
total+=(curr->qty*curr->price);
curr = curr->next;
}
return total;
}
bool checkout(){
cls();
if(!view()){
return false;
}
int total = calculate();
puts("------------------------------------------");
printf("Total : %d\n\n", total);
printf("Proceed?");getchar();
cls();
printf("Kindness is FREE! Thanks for shopping!");
getchar();
return true;
}
void menu(){
int choose;
bool exit;
do{
if(head){
view();
}else{
cls();
puts("---No Item Inputted Yet---\n\n");
}
puts("1. Input New Item");
puts("2. Update Item");
puts("3. Delete Item");
puts("4. Checkout");
printf("Choose >> ");
scanf("%d", &choose);getchar();
switch(choose){
case 1:
input();
continue;
case 2:
update();
continue;
case 3:
del();
continue;
case 4:
exit = checkout();
continue;
}
}while(!exit);
}
int main(){
menu();
return 0;
}
Tidak ada komentar:
Posting Komentar