// Calculator Bot // Acts like a simple calculator. Advanced bot for students that are done with the tutorial using System; namespace ChatBot { class CalculatorBot : BasicBot { string number1; //Global strings string operand; public override string HandleMessage(string message, string user) { if (state == "1") { if (User.RegexMatch("[0-9]+", message)) { number1 = message; //Saves the number state = "2"; return "Please enter an operand (+, -, /, *): "; } else return "Please type a number."; } else if (state == "2") { if (User.RegexMatch("[/+-/*//]", message)) { operand = message; //Saves the operator state = "3"; return "Please enter the second integer: "; } else return "Please type an operator."; } else if (state == "3") { if (User.RegexMatch("[0-9]+", message)) { state = "0"; //Resets the state int num1 = Convert.ToInt32(number1); //Convert the string into an int int num2 = Convert.ToInt32(message); //Convert the string into and int double answer; if (operand.CompareTo("+") == 0) answer = num1 + num2; else if (operand.CompareTo("*") == 0) answer = num1 * num2; else if (operand.CompareTo("/") == 0) answer = (double)num1 / num2; //Type cast into a double else if (operand.CompareTo("-") == 0) answer = num1 - num2; else answer = 0; return num1 + " " + operand + " " + num2 + " = " + answer; } else return "Please type a number."; } else { state = "1"; return "Please enter the first integer: "; } } } }