iT邦幫忙

2022 iThome 鐵人賽

DAY 29
0
Software Development

C++超級菜鳥也可以懂的物件導向攻略系列 第 29

Dat 29 C++ 把Crypto bot虛擬貨幣程式放進去 class裡

  • 分享至 

  • xImage
  •  

前言:
請參考系列文章前面的Day 23. Crpto bot 功能 與 menu 24. Crypto bot file I/O


讓我們馬上進入正題,
首先做出一個class,把它叫做Crypto好了
(習慣上通常clas的名字地個字母會是大寫)

然後把function裝進去class裡面。
(function在class裡就叫做method)
最後做出object,然後呼叫class裡面的method。

class Crypto{
private:
    
public:
};

#include <iostream>
#include <fstream>
#include <iomanip>
#include <unistd.h>
#include <cstdlib>
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include <chrono>
#include <ctime>

using namespace std;
//void menu();
//void cryptprice(int c);
//void action();
//void cryptinfo();


class Crypto{
private:
    
public:
    Crypto(){
            cout << "*****MENU*******" <<endl;
            cout << "1.Show Current Price" << endl;
            cout << "2.Find Crypto info" <<  endl;
            cout << "3.Find Market Cap" <<  endl;
            cout << "4.Help" <<  endl;
            cout << "5.Exit" <<  endl;
            cout << "****************" <<  endl;
    }
    void action(){
        int option;
        cout << "Type the service you'd like to do: " << endl;
        cin >> option;
        do{
            switch(option){
                case 1:
                {
                    cout << "Enter the Crypto (1.BTC, 2.ETH, 3.USDT 4. Exit):"<<endl;
                    int cname;
                    cin >> cname;
                    cout << "You entered: " << cname << endl;
                    cryptprice(cname);
                    break;
                }
                case 2:
                {
                    cout <<"Enter the name of the crypto you'd like to see info with:" << endl;
                    cout << "1.BTC, 2.ETH, 3.USDT 4. Exit" << endl;
                    int x;
                    cin >> x;
                    cout << "you entered: " << x<< endl;
                    cryptinfo(x);
                    break;}
                case 3:
                {   cout <<"Enter the crypto you'd like to see Market Cap with:" << endl;
                    cout << "1.BTC, 2.ETH, 3.USDT 4. Exit" << endl;
                    int y;
                    cin >> y;
                    cout << "Market cap is: $";
                    marketcap(y);
                    break;}
                case 4:
                    cout <<"Enter the section best describe your question:" << endl;
                    cout <<"1. Trouble Shooting 2. Contact us 3. Back to menu" <<endl;
                    int h;
                    cin >> h;
                    help(h);
                    break;
            }
        }while (option != 5);
        
    }
    
    void cryptprice(int c){
        if(c == 1){
            ifstream file("BTC.txt", ios::in);
            if(file.is_open()){
                string s;
                while(file >> s){
                    cout << s << endl;
                }
            }else{
                cout <<"Error";
            }
            file.close();
        }else if(c == 2){
            ifstream file("ETH.txt", ios::in);
            if(file.is_open()){
                string s;
                while(file >> s){
                    cout << s << endl;
                }
            }else{
                cout <<"Error";
            }
            file.close();
        }else if(c == 3){
            ifstream file("USDT.txt", ios::in);
            if(file.is_open()){
                string s;
                while(file >> s){
                    cout << s << endl;
                }
            }else{
                cout <<"Error";
            }
            file.close();
        }else{
            action();
        }
    }
    
    void cryptinfo(int c){
        if(c == 1){
            cout << "Total Bitcoin: 19,166,487 BTC" << endl;
            cout << "First Block(Bitcoin creation date): 2009-01-09" << endl;
        }else if(c == 2){
            cout << "Total ETH: 19,166,487 BTC" << endl;
            cout << "First Block(Bitcoin creation date): 2009-01-09" << endl;
        }else if (c == 3){
            cout << "Total USDT: 19,166,487 BTC" << endl;
            cout << "First Block(Bitcoin creation date): 2009-01-09" << endl;
        }else {
            action();
        }
    }
    
    void marketcap(int c){
        if(c == 1){
            cout << "367,982,010,627" << endl;
        }else if(c == 2){
            cout << "163,879,592,332" << endl;
        }else if(c == 3){
            cout << "68,269,066,618" <<endl;
        }else if(c == 4) {
            action();
        }
    }
    
    void help(int c){
        if(c == 1){
            cout << "If you endure any system problem please restart the bot and login again." <<endl;
        }else if(c == 2){
            cout <<"Telephone: 120-332-593. Email: crypto-servie@cryptobot.com Open:Mon-Fri AM9:00~PM4:30"<< endl;
        }else {
            action();
        }
    }
    

};




int main() {
    string app;
    Crypto cr;
    cr.action();
 
    return 0;
}

Referemce: w3cshool, programiz, geeksforgeeks


上一篇
Day 28 C++ Polymorphism II - Virtual function 虛擬函數
下一篇
Day 30 - 完賽感想
系列文
C++超級菜鳥也可以懂的物件導向攻略30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言