iT邦幫忙

2022 iThome 鐵人賽

DAY 11
0
Software Development

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

Day11 - C++好簡單之來做一個ATM系統吧! switch和do..while

  • 分享至 

  • xImage
  •  

30天挑戰目標是搭建一個簡單的command line系統,那要有個首頁讓user可以做選擇對不對?
今天要用這個來聊聊如何做一個簡單的介面,包含基本指令順便練習一下把function分出來。

物件導向就是分類大法:

  • 函數function(裝功能的分類箱)
  • 結構struct(裝variable的分類箱)

今天會用到function裝我們的menu。

(我是使用mac系統,所以有些指令可能與windows會有所不同)
(這篇沒有使用namespace std喔)

第一步:目錄Menu

有四種功能,看帳戶餘額、提款、存款、轉帳。

注意:這個menu是一個function唷!因為沒有retun value所以是void

當然也可以直接寫進int main裡,只是單純練習一下如何分類,關於function是什麼請搭直通車Day07函數&Day08函數參數

void menu(){
    std::cout << "******MENU******" << std::endl;
    std::cout << "1.Check deposit" << std::endl;
    std::cout << "2.Deposit" << std::endl;
    std::cout << "3.Withdraw" << std::endl;
    std::cout << "4.Transfer" << std::endl;
    std::cout << "5.Exit" << std::endl;
    std::cout << "6.Help" << std::endl;
    std::cout << "****************" << std::endl;
}

接下來在選項的方面,可以有幾種選擇,用之前講過的if...else,或是用switch
switch把所有可能的情況都列出來,然後符合情況以後要記得加break,說明不用再繼續跑第二個switch了,
不然會繼續跑下去喔。
Switch用法如下:

switch (option) {    //option是我們之前的variable int option, 所以是數字
        case 1:   //所以case 1,就等於我們打 1 的時候會執行的情況
            std::cout <<"Balance: $"<< balance<< std::endl;
            break;  //停止switch不再往case 2跑
        case 2:      //case 2,就等於我們打 2 的時候會執行的情況
            std::cout << "Deposit: " << std::endl;
            double deposit;
            std::cin >> deposit;
            balance = balance + deposit;
            break;
        case 3:
            std::cout <<"Withdraw: $" << std::endl;
            double withdraw;
            std::cin >> withdraw;
            if(withdraw < balance)
                balance = balance - withdraw;
            else
                std::cout << "NOT enought money" << std::endl;
            break;
        case 4:{
            std::string transfto_name;
            double amount;
            std::cout <<"Transfer your money to whom?\n";
            std::cin >> transfto_name;
            std::cout <<"How much do you want to transfer? \n";
            std::cin >> amount;
            std::cout << "You transfered " << amount <<" to " << transfto_name<< std::endl;
            if(amount < balance)
                balance = balance - amount;
            else std::cout <<"Not enought money in you account to tranfer. \n";
            break;}
//        case 5:
//            std::cout <<"Exit now \n";
            break;
        case 6:
            std::cout << "Need help? Don't worry we're here 24/7!"<< std::endl;
            std::cout << "If you have trouble with our ATM service, please don't hesitate to contact us. Call:2003-449-229"<< std::endl;

很好很好,最後要做個exit功能,防止這個程式無限制循環跑下去對不對?
如果這樣直接放進main,也可以跑,但打完指令後會用無限重複回到menu頁面,如下所示:

    menu(); //顯示menu
    std::cout << "Type the option to proceed" << std::endl; 
    std::cin >> option;

為了要讓程式知道何時要結束顯示上面的東西,我們需要用到do....while,最終程式如下:

#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <curses.h>
#include <stdlib.h>

void menu(){
    std::cout << "******MENU******" << std::endl;
    std::cout << "1.Check deposit" << std::endl;
    std::cout << "2.Deposit" << std::endl;
    std::cout << "3.Withdraw" << std::endl;
    std::cout << "4.Transfer" << std::endl;
    std::cout << "5.Exit" << std::endl;
    std::cout << "6.Help" << std::endl;
    std::cout << "****************" << std::endl;

}


int main() {
    int option;
    double balance = 500;
    
    do {
    menu();
    std::cout << "Type the option to proceed" << std::endl;
    std::cin >> option;
    switch (option) {
        case 1:
            std::cout <<"Balance: $"<< balance<< std::endl;
            break;
        case 2:
            std::cout << "Deposit: " << std::endl;
            double deposit;
            std::cin >> deposit;
            balance = balance + deposit;
            break;
        case 3:
            std::cout <<"Withdraw: $" << std::endl;
            double withdraw;
            std::cin >> withdraw;
            if(withdraw < balance)
                balance = balance - withdraw;
            else
                std::cout << "NOT enought money" << std::endl;
            break;
        case 4:{
            std::string transfto_name;
            double amount;
            std::cout <<"Transfer your money to whom?\n";
            std::cin >> transfto_name;
            std::cout <<"How much do you want to transfer? \n";
            std::cin >> amount;
            std::cout << "You transfered " << amount <<" to " << transfto_name<< std::endl;
            if(amount < balance)
                balance = balance - amount;
            else std::cout <<"Not enought money in you account to tranfer. \n";
            break;}
//        case 5:
            std::cout << "Need help? Don't worry we're here 24/7!"<< std::endl;
            std::cout << "If you have trouble with our ATM service, please don't hesitate to contact us. Call:2003-449-229"<< std::endl;
            break;
        case 6:   
//            std::cout <<"Exit now \n";

    }
    }while (option != 6);   //當輸入的選項不是6的時候 重複跑上面的menu()、輸入選項。
                          //當輸入6(exit)時,停止上面的menu,main裡沒有東西可以跑成程式就結束了
    
    
}

do{menu(),回到選單}...while{輸入的數字option不是6}...

當輸入的選項不是6的時候 重複跑上面的menu()、輸入選項。
當輸入6(exit)時,停止上面的menu,main裡沒有東西可以跑成程式就結束了

Reference: CodeBeuaty(youtube), w3school, geeksforgeeks


上一篇
Day10 - C++物件導向前奏 - 結構Struct
下一篇
Day 12 - C++ 物件導向1 - 類別Class
系列文
C++超級菜鳥也可以懂的物件導向攻略30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言