Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // UAP
- //
- // Created by Alexander Kevin on 6/8/14.
- // Copyright (c) 2014 Alexander Kevin. All rights reserved.
- //
- //info seputar UAP
- //- 100 menit , kerjakan 1 soal
- //- Inheritance
- //- Array of Objects
- //- gk boleh ada global variabel
- //- gk boleh input/ output within class
- //
- //NOTES:
- //1) There are 2 classes needed here
- //2) Inheritance
- //3) String class is allowed
- //4) Vector (???) / Array of objects
- //5) Another variety of the problem: such as update data or find and display
- //
- //|> PRE-MENU
- // - Input USERNAME:
- // 1) Combination of numbers and alphabets
- // 2) 5 - 20 characters
- // 3) One word only
- //
- //|> MAIN MENU
- // - Display USERNAME
- // - Menu 1 : Make an Order
- // - Menu 2 : View Your Order
- // - Menu 3 : Delete Order
- // - Menu 4 : Pay Order
- //
- //|> MENU 1 - Make an order
- // - Validate maximum number of existing order is 4 (both yogurt and special yogurt)
- // - Input FLAVOUR (Coffee, Green Tea, Mocha)
- // - Input TYPE (Balkan, Swiss, Greek)
- // - Input SIZE (Small, Medium, Large)
- // - Ask for TOPPING:
- // (+) If YES, then input TOPPING (Lychee, Dark Chocolate, Marshmallow)
- // (-) Continue program
- // - Generate ORDERID:
- // Format: ORD-ABC-XXYY
- // Where ABC -> First 3 character of USERNAME
- // X -> Random alphabet (A-Z)
- // Y -> Random number (0-9)
- // - Calculate PRICE by sum up the value according the order made:
- // 1) FLAVOUR
- // - Coffee : 15000
- // - Green Tea : 16000
- // - Mocha : 17000
- // 2) TYPE
- // - Balkan : 6000
- // - Swiss : 7000
- // - Greek : 8000
- // 3) SIZE
- // - Small : 2000
- // - Medium : 3000
- // - Large : 4000
- // 4) TOPPING
- // - Lychee : 4000
- // - Dark Chocolate : 5000
- // - Marshmallow : 6000
- // - Display the detail of the order
- //
- //|> MENU 2 - View your order
- // - Validate whether there is order or not (display message)
- // - Display all the order have been made
- //
- //|> MENU 3 - Delete Order
- // - Validate whether there is order or not (display message)
- // - Input ORDERID, validate that input should be matched the ORDERID formatting
- // Format: ORD-ABC-XXYY
- // Where ABC -> First 3 character of USERNAME
- // X -> an alphabet (A-Z)
- // Y -> a number (0-9)
- // - If the data is found, delete the data. Else, display error message
- //
- //|> MENU 4 - Pay Order
- // - Validate whether there is order or not (display message)
- // - If there is data exist, calculate total payment of all orders
- // - Input user payment (Minimum is the amount of payment)
- // - Calculate the change, if there is any
- // - Display message
- // - Exit the program
- //
- //
- #include <iostream>
- #include <string>
- #include <time.h>
- #define abc cin.sync();cin.clear();//menganti pengektikan cin sync,clear dengan hanya mengetik abc
- using namespace std;
- //ga ada global variable +2 point
- void menu(string,int);//function prototype
- string regist();//untuk buat id
- bool cekUser(string);//untuk loop cek id
- class basic{//deklarasi class <- 5 point , implementasi 5 point, definition 10 point, skor dari class total 35 point;
- protected://dibuat protected supaya bisa diakses anak class/inheritancenya
- string type;//<-data member 2 point
- string size;//pake string bair ga ribet
- string flavor;
- int price;
- string code;
- public:
- basic(){//constructor
- type="";
- size="";
- flavor="";
- price=0;
- code="";
- }
- void setBasic(string a,string b,string c,string d){//<-Setter(passing by value) 2point. Ga boleh ada input(cin) dalam class (2point)
- flavor=a;
- type=b;
- size=c;
- price=0;
- code="ORD-";//code awal pasti pake depan ORD-
- code=code+d.substr(0,3)+'-';//ngambil 3 huruf pertama user
- srand(time(NULL));//buat ngerandom
- char x;//buat nyetaknya
- x=rand()%26+65;//26 dari total alphabet(a-z), 65 dari ascii A=65 harus huruf besar*
- code=code+x;//code ditambah huruf yg d random diatas
- x=rand()%26+65;
- code=code+x;
- x=rand()%10+48;//10 dari total angka(0-9), 48 dari ASCII 0=48;
- code=code+x;//code ditambah angka yg di random di atas
- x=rand()%10+48;
- code=code+x;
- if(flavor=="Coffee")//kalo input kopi harga sekian dst
- price+=15000;
- else if(flavor=="Green Tea")
- price+=16000;
- else
- price+=17000;
- if(flavor=="Balkan")
- price+=6000;
- else if(flavor=="Swiss")
- price+=7000;
- else
- price+=8000;
- if(flavor=="Small")
- price+=2000;
- else if(flavor=="Medium")
- price+=3000;
- else
- price+=4000;
- }
- string getType(){//getter(2point)
- return type;
- }
- string getSize(){
- return size;
- }
- string getFlavor(){
- return flavor;
- }
- int getPrice(){
- return price;
- }
- string getCode(){
- return code;
- }
- ~basic(){
- }
- };
- class detail:public basic{// inheritance dari class public 5point
- private:
- string topping;
- public:
- detail(){
- topping="No Topping";
- }
- void setTopping(string a){// di passing value stringnya (setter)
- topping=a;
- if(topping=="Lychee")//tentuin harga
- price+=4000;
- else if(topping=="Dark Chocolate")
- price+=5000;
- else
- price+=6000;
- }
- string getTopping(){//getter
- return topping;
- }
- ~detail(){//destructor
- cout<<code<<" DELETED"<<endl;
- }
- };
- void e(){//buat spasi(tampilan)
- int x=0;
- do{
- cout<<endl;
- x++;
- }while(x<27);
- }
- bool cekUser(string a){//booltipe data yg mereturn true / false, baiknya diggunain di loop
- if(a.length()<5||a.length()>20)//cek ukuran
- return false;
- else if(a.find(" ")<20)//cek ga boleh ada lebih dari 1 kata, cari " ". kalo ga ketemu dia mereturn 490xxxx
- return false;
- int w=0,z=0,x=0;//w untuk is alpha, z untuk isdigit, x untuk loop kalo di for sering dipake isitilah int i=0
- while(x<a.length()){
- if(isalpha(a[x]))//is alpha cek huruf atau enga kalo iya dia return true
- w=1;
- if(isdigit(a[x]))//is digit cek angka atau enga kalo iya dia return true
- z=1;
- x++;
- }
- if(w==1&&z==1)//baru bakal kirim true kalo smeua syarat bener
- return true;
- else
- return false;
- }
- string regist(){//untuk registrasi id
- e();//sekedar tampilan
- string a;
- cout<<"Username Requirement :"<<endl<<"1) Combination of numbers and alphabets"<<endl<<"2) 5 - 20 characters"<<endl<<"3) One word only"<<endl;
- while(!cekUser(a)){//loop cek username
- cout<<"Masukan Username Anda :";//output 2 point
- getline(cin,a);abc;//input 2 point, kalo string jangan lupa pake getline(cin,<nama_string>);
- cekUser(a);//di passing yg barusan "a" yg di input user
- }
- return a;
- }
- void menu(string id,int jumlah){
- system("cls");
- e();
- static detail ord[10];//<- array of object (5 point) dibikin static supaya constructor ga kepanggil terus2an. kelemaanya satu ga dinamis (cuma bisa 10/sesuai yg ditentuin di awal)
- //^ bisa diganti juga dengan nantinya pake object *array[]=new object[n];tar di Bagian apus data diubah jadi delete array[];(lebih ribet dan berhubung ga ada di penilaian pake static aja)
- int count;
- count=jumlah;//tampung nilai jumlah <- jadi repot gini soalnya ga boleh pake var Global
- cout<<"MAIN MENU"<<endl
- <<"Welcome, "<<id<<endl
- <<"1. Make an Order"<<endl
- <<"2. View Your Order"<<endl
- <<"3. Delete Order"<<endl
- <<"4. Pay Order"<<endl;
- int answer;
- cout<<"Masukan Pilihan : ";
- cin>>answer;abc;//abc penganti cin.sync(),cin.clear() yg tadi di define diatas
- switch(answer){
- case 1:
- {
- string a1,a2,a3,a4;
- system("cls");
- e();
- int flag=0;
- while(flag==0){//cek udah bener inputnya, kalo mau bisa di bikin kaya bool cekFlavor() cuma tar passing2 nya ribet sendiri
- cout<<"Input FLAVOUR (Coffee, Green Tea, Mocha) : ";
- getline(cin,a1);abc;
- if(a1=="Coffee"||a1=="Green Tea"||a1=="Mocha")
- flag=1;
- }
- flag=0;//balikin ke 0 jangan lupa
- while(flag==0){//cek udah bener inputnya
- cout<<"Input TYPE (Balkan, Swiss, Greek) : ";
- getline(cin,a2);abc;
- if(a2=="Balkan"||a2=="Swiss"||a2=="Greek")
- flag=1;
- }
- flag=0;
- while(flag==0){//cek udah bener inputnya
- cout<<"Input SIZE (Small, Medium, Large) : ";
- getline(cin,a3);abc;
- if(a3=="Small"||a3=="Medium"||a3=="Large")
- flag=1;
- }
- cout<<"Add Topping[Y/N]?";//cek mau tambah topping ga
- getline(cin,a4);abc;
- if(a4=="Y"){
- flag=0;
- while(flag==0){
- cout<<"Input TOPPING (Lychee, Dark Chocolate, Marshmallow) : ";
- getline(cin,a4);abc;
- if(a4=="Lychee"||a4=="Dark Chocolate"||a4=="Marshmallow")
- flag=1;
- }
- ord[count].setTopping(a4);//implementasi array 3 point
- }
- ord[count].setBasic(a1,a2,a3,id);//implementasi array 3 point
- cout<<endl<<"Detail Order"<<endl<<"==========================="<<endl
- <<"Flavor : "<<ord[count].getFlavor()<<endl//ga di buat void view() soalnya di class jangan ada Input maupun output mengurangi nilai 2 point kalo ampe ada
- <<"Type : "<<ord[count].getType()<<endl//jadi mau ga mau mesti di luar class kaya gini
- <<"Size : "<<ord[count].getSize()<<endl
- <<"Topping : "<<ord[count].getTopping()<<endl
- <<"Price: "<<ord[count].getPrice()<<endl
- <<"Code : "<<ord[count].getCode()<<endl;
- cin.get();//supaya ga langsung ilang
- count++;//udah semua beres valid jangan lupa di ++ countnya
- menu(id,count);//lempar lagi id nya ama count yg udah ditambah barusan. sebenrnya bisa ga pasiing kalo boleh var global >,<
- break;
- }
- case 2:
- system("cls");
- e();
- if(count>0){//cek ada ga pesennya kalo ga ada langsung else
- cout<<"Detail Order"<<endl<<"==========================="<<endl;//ini juga sama mestinya bisa di singkat pake void view() cuma berhubung ga boleh ya sudah.
- for(int i=0;i<count;i++){
- cout
- <<"Flavor : "<<ord[i].getFlavor()<<endl//i itu index arraynya
- <<"Type : "<<ord[i].getType()<<endl
- <<"Size : "<<ord[i].getSize()<<endl
- <<"Topping : "<<ord[i].getTopping()<<endl
- <<"Price : "<<ord[i].getPrice()<<endl
- <<"Code : "<<ord[i].getCode()<<endl<<endl;
- }
- }
- else{//kalo belom pesen
- cout<<"Belum Melakukan Pemesanan Apapun, Kembali ke menu";
- }
- cin.get();
- menu(id,count);
- break;
- case 3:
- {
- system("cls");
- e();
- string hapus;
- int flag=-1;
- if(count>0){//cek udah order belom
- cout<<"List Order"<<endl<<"==========================="<<endl;//tunjukin dulu list kodenya, ga mungkin user afal. kalo di contoh aplikasnya ini ga ada jadi terserah mau dibuat apa enga
- for(int i=0;i<count;i++){
- cout<<"Code : "<<ord[i].getCode()<<endl<<endl;
- }
- while(flag==-1){//falg -1 kenapa? karena mungkin aja yg mau dia pus di index 0, tapi kan ga mungkin di -1
- cout<<"Masukan Code yang ingin di delete : ";
- getline(cin,hapus);abc;
- for(int i=0;i<count;i++){//cek ada di index mana
- if(ord[i].getCode()==hapus)
- flag=i;//catet indeknya di flag
- }
- if(flag==-1)//kalo ternyata ga ada/ slaah input
- cout<<"Code yang anda masukan salah"<<endl;
- }
- for(int i=flag;i<count-1;i++)//pindhain ke paling belakang kaya algo waktu delete-sort
- ord[i]=ord[i+1];
- ord[count].~detail();//panggil destructornya buat ngapus isi object, kalo di atas mau pake yg object *array[]=new object[]; , disini ganti jadi delete array[];
- count--;//udah sukss apus di -- jangan lupa;
- cout<<"Sukses Mengahapus Order "<<hapus;//hapus intu kode smenetara yg diinput diatas
- }
- else//kalo belom pesen
- cout<<"Belum Melakukan Pemesanan Apapun, Kembali ke menu";
- cin.get();
- menu(id,count);
- break;
- }
- case 4:
- system("cls");
- e();
- if(count>0){
- int total=0,payment=0;//total buat itung biaya, payment buat tar dinput user
- for(int i=0;i<count;i++){//looping cek masing2 di jumlah
- total+=ord[i].getPrice();
- }
- cout<<"total payment of all orders : Rp. "<<total<<",00"<<endl;//print totalnya
- while(payment<total){//looping kurang ga bayarnya
- cout<<"Masukan Input user payment (Minimum is the amount of payment) : ";
- cin>>payment;abc;
- }
- if(payment>total)//kalo ada kembalian
- cout<<"Your Change is Rp. "<<(payment-total)<<",00"<<endl;//(payment-total) itu biar langsung ngeprint tanpa perlu nambah variable buat sekedar nampung doank
- cout<<"Thank You, See you next time!";//udah jangan di menu() lagi kan abis check out program mati
- }
- else{//kalo belom ada
- cout<<"Belum Melakukan Pemesanan Apapun, Kembali ke menu";
- cin.get();
- menu(id,count);//balik ke menu
- break;
- }
- cin.get();//satu2nya jalan keluar program adalah dengan pesen min 1 dan bayar
- break;
- default:
- menu(id,count);//kalo user slaah input menu
- break;
- }
- }
- int main(){
- int count=0;//buat ngasih tau masih 0;kalo di declare di menu atas setiap masuk menu bakla balik ke 0 >,< jadi harus dipisah decalrenya. again karena ga boleh var global !
- menu(regist(),count);//regis buat user id, count yg tadi dipoer diatas;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement