Tubes Klp 2
Published: 2020-01-01 16:49:04

/*
Syamsul Rijal 60200118061
Hasbullah 60200118061
*/

#include <iostream>
#include <conio.h>
#include <iomanip>
#define MAX 20
using namespace std;

int t1 = 0;

struct Stack {
	char menu[MAX][MAX];
	int top, harga[MAX], stok[MAX] , t[MAX];
}Tumpukan;

void init(){
	Tumpukan.top = -1;
}

bool kosong() {
  return Tumpukan.top == -1;
}

bool penuh() {
	return Tumpukan.top == MAX-1;
}

int clear()
{
	while((getchar())^'\n');
}

void push() {
   if (penuh()) {
		cout << "\nPenuh"<<endl;
	}
	else {
    Tumpukan.top++;
    t1++;
    cout << "\nMasukkan Menu = ";clear();gets( Tumpukan.menu[Tumpukan.top]);
    cout << "Masukkan Harga = "; cin >> Tumpukan.harga[Tumpukan.top];
    cout << "Masukkan stok = "; cin >> Tumpukan.stok[Tumpukan.top];
    Tumpukan.t[Tumpukan.top] = t1;
    
	}
}

void pop() {
    Tumpukan.top--;
}

void printStack() {
	if (kosong()) {
		cout << "kosong";
	}
	else {
		cout<<"=====================================================\n";
		cout<<"|        menu         |     harga     |     stok    |\n";
		cout<<"=====================================================\n";
		for (int i = 0; i <= Tumpukan.top; i++){
			cout<<"| "<<setiosflags(ios::left)<<setw(20)<<Tumpukan.menu[i]<<"|";
			cout<<" "<<setiosflags(ios::left)<<setw(14)<<Tumpukan.harga[i]<<"|";
			cout<<" "<<setiosflags(ios::left)<<setw(12)<<Tumpukan.stok[i]<<"|";	
			cout<<"\n=====================================================\n";
		}
	}
}

void stoktertinggi(){
	int tmp;
	
	for(int i = 0; i<=Tumpukan.top; i++){	
		for(int j=0; j<i; j++){
			if(Tumpukan.stok[i]<=Tumpukan.stok[j]){
				tmp=Tumpukan.stok[i];
				Tumpukan.stok[i]=Tumpukan.stok[j];
				Tumpukan.stok[j]=tmp;				
			}
		}
	}
	cout<<"=====================================================\n";
	cout<<"|        menu         |     harga     |     stok    |\n";
	cout<<"=====================================================\n";
	for (int i = Tumpukan.top; i >= 0; i--){
		cout<<"| "<<setiosflags(ios::left)<<setw(20)<<Tumpukan.menu[i]<<"|";
		cout<<" "<<setiosflags(ios::left)<<setw(14)<<Tumpukan.harga[i]<<"|";
		cout<<" "<<setiosflags(ios::left)<<setw(12)<<Tumpukan.stok[i]<<"|";	
		cout<<"\n=====================================================\n";
	}
}

void hargaterendah(){
	int tmp;
	
	for(int i = 0; i<=Tumpukan.top; i++){	
		for(int j=0; j<i; j++){
			if(Tumpukan.harga[i]>=Tumpukan.harga[j]){
				tmp=Tumpukan.harga[i];
				Tumpukan.harga[i]=Tumpukan.harga[j];
				Tumpukan.harga[j]=tmp;				
			}
		}
	}
	cout<<"=====================================================\n";
	cout<<"|        menu         |     harga     |     stok    |\n";
	cout<<"=====================================================\n";
	for (int i = Tumpukan.top; i >= 0; i--){
		cout<<"| "<<setiosflags(ios::left)<<setw(20)<<Tumpukan.menu[i]<<"|";
		cout<<" "<<setiosflags(ios::left)<<setw(14)<<Tumpukan.harga[i]<<"|";
		cout<<" "<<setiosflags(ios::left)<<setw(12)<<Tumpukan.stok[i]<<"|";	
		cout<<"\n=====================================================\n";
	}
}

int main() {
	int pilihan,pilih,pil,pesan,total,kembalian,uang;
	init();
	do {
    	awal:
		cout<<"\n1. tambah\n"
        	<<"2. Hapus (Pop)\n"
        	<<"3. Menyusun Menu\n"
        	<<"4. Pembayaran\n"
        	<<"5. Keluar\n"
        	<<"Masukkan Pilihan: ";
		cin >> pilihan;
		switch (pilihan)
		{
		case 1:
			push();
			printStack();
			getch();
			system("cls");
			break;
		case 2:
			pop();
			printStack();
			getch();
			system("cls");
			break;
		case 3:
			cout<<"\n1. Stok tertinggi\n"
				<<"2. Harga terendah\n"
				<<"Masukkan pilihan :";
			cin>>pilih;
			if(pilih==1)
			{
				stoktertinggi();
			}
			else if(pilih==2)
			{
				hargaterendah();
			}
			else{
				cout<<"Salah input";
			}
			getch();
			system("cls");
			goto awal;
			break;
		case 4:
			printStack();
			cout<<"Masukkan Pilihan : ";cin>>pil;
			cout<<"jumlah pesanan   : ";cin>>pesan;
			
			
			for (int i = 0; i <= Tumpukan.top; i++){
				if (pil == Tumpukan.t[i])
				{
					cout<<"stok  : " << Tumpukan.stok[i] << endl;
					cout<<"Pesan : "<<pesan<<endl;
					Tumpukan.stok[i]=Tumpukan.stok[i]-pesan;
					total=pesan*Tumpukan.harga[i];
				}
				
			}
			
			cout << "total bayar : " << total << endl;
			cout<<"Masukkan jumlah pembayaran 	: ";cin>>uang;
			kembalian=uang-total;
			cout<<"Kembalian : "<<kembalian;
			
			break;
		default:
			cout<<"Salah input";
		}
	} while (pilihan!=5);
}