⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connection.cpp.html

📁 《Big C++ 》Third Edition电子书和代码全集-Part1
💻 HTML
字号:
<html>

<head>
	<title>connection.cpp</title>
</head>

<body>
<pre>  1  #include "message.h"
  2  #include "mailbox.h"
  3  #include "mailsystem.h"
  4  #include "connection.h"
  5  #include "telephone.h"
  6  
  7  
  8  Connection::Connection(MailSystem&amp; s, Telephone&amp; p)
  9     : system(s), phone(p),
 10     INITIAL_PROMPT("Enter mailbox number followed by #\n"),
 11     MAILBOX_MENU_TEXT("Enter 1 to listen to your messages\n"
 12     "Enter 2 to change your passcode\n"
 13     "Enter 3 to change your greeting\n"),
 14     MESSAGE_MENU_TEXT("Enter 1 to listen to the current message\n"
 15     "Enter 2 to save the current message\n"
 16     "Enter 3 to delete the current message\n"
 17     "Enter 4 to return to the main menu\n")
 18  {
 19     reset_connection();
 20  }
 21  
 22  void Connection::dial(string key)
 23  {
 24     if (state == CONNECTED)
 25        connect(key);
 26     else if (state == RECORDING)
 27        login(key);
 28     else if (state == CHANGE_PASSCODE)
 29        change_passcode(key);
 30     else if (state == CHANGE_GREETING)
 31        change_greeting(key);
 32     else if (state == MAILBOX_MENU)
 33        mailbox_menu(key);
 34     else if (state == MESSAGE_MENU)
 35        message_menu(key);
 36  }
 37  
 38  void Connection::record(string voice)
 39  {
 40     if (state == RECORDING || state == CHANGE_GREETING)
 41        current_recording += voice;
 42  }
 43  
 44  void Connection::hangup()
 45  {
 46     if (state == RECORDING)
 47        current_mailbox-&gt;add_message(new Message(current_recording));
 48     reset_connection();
 49  }
 50  
 51  void Connection::reset_connection()
 52  {
 53     current_recording = "";
 54     accumulated_keys = "";
 55     state = CONNECTED;
 56     phone.speak(INITIAL_PROMPT);
 57  }
 58  
 59  void Connection::connect(string key)
 60  {
 61     if (key == "#")
 62     {
 63        current_mailbox = system.find_mailbox(accumulated_keys);
 64        if (current_mailbox != NULL)
 65        {
 66           state = RECORDING;
 67           phone.speak(current_mailbox-&gt;get_greeting());
 68        }
 69        else
 70           phone.speak("Incorrect mailbox number. Try again!");
 71        accumulated_keys = "";
 72     }
 73     else
 74        accumulated_keys += key;
 75  }
 76  
 77  void Connection::login(string key)
 78  {
 79     if (key == "#")
 80     {
 81        if (current_mailbox-&gt;check_passcode(accumulated_keys))
 82        {
 83           state = MAILBOX_MENU;
 84           phone.speak(MAILBOX_MENU_TEXT);
 85        }
 86        else
 87           phone.speak("Incorrect passcode. Try again!");
 88        accumulated_keys = "";
 89     }
 90     else
 91        accumulated_keys += key;
 92  }
 93  
 94  void Connection::change_passcode(string key)
 95  {
 96     if (key == "#")
 97     {
 98        current_mailbox-&gt;set_passcode(accumulated_keys);
 99        state = MAILBOX_MENU;
100        phone.speak(MAILBOX_MENU_TEXT);
101        accumulated_keys = "";
102     }
103     else
104        accumulated_keys += key;
105  }
106  
107  void Connection::change_greeting(string key)
108  {
109     if (key == "#")
110     {
111        current_mailbox-&gt;set_greeting(current_recording);
112        current_recording = "";
113        state = MAILBOX_MENU;
114        phone.speak(MAILBOX_MENU_TEXT);
115     }
116  }
117  
118  void Connection::mailbox_menu(string key)
119  {
120     if (key == "1")
121     {
122        state = MESSAGE_MENU;
123        phone.speak(MESSAGE_MENU_TEXT);
124     }
125     else if (key == "2")
126     {
127        state = CHANGE_PASSCODE;
128        phone.speak("Enter new passcode followed by the # key");
129     }
130     else if (key == "3")
131     {
132        state = CHANGE_GREETING;
133        phone.speak("Record your greeting, then press the # key");
134     }
135  }
136  
137  void Connection::message_menu(string key)
138  {
139     if (key == "1")
140     {
141        string output = "";
142        Message* m = current_mailbox-&gt;get_current_message();
143        if (m == NULL) output += "No messages.\n";
144        else output += m-&gt;get_text() + "\n";
145        output += MESSAGE_MENU_TEXT;
146        phone.speak(output);
147     }
148     else if (key == "2")
149     {
150        current_mailbox-&gt;save_current_message();
151        phone.speak(MESSAGE_MENU_TEXT);
152     }
153     else if (key == "3")
154     {
155        current_mailbox-&gt;remove_current_message();
156        phone.speak(MESSAGE_MENU_TEXT);
157     }
158     else if (key == "4")
159     {
160        state = MAILBOX_MENU;
161        phone.speak(MAILBOX_MENU_TEXT);
162     }
163  }</pre>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -