/* Computer Science & Programming Midterm Lab Exam, 11/27/02 Jason M. Snouffer */ #include #include using namespace std; class Cell { public: // ------------------------------ Cell ------------------------------ /* Default constructor "this" cellphone is created with - The last two numbers dialed set to zero (0) indicating that no calls have been made yet. - A zero balance in the account - The ability to make international calls turned ON. */ Cell(); // ------------------------------ Cell ----------------------------- /* Constructor: "this" cellphone is created. * * - The last two numbers dialed are set to zero * * - If initial_balance is positive, then "this" cellphone is created with * that many dollars in the account, otherwise with a balance of zero in * the account. Please note: I recommend that you store the balance * internally inside the cellphone class as a number of CENTS * * - The ability to make international calls turned ON if international_on is true, * otherwise the ability to make international calls turned OFF. */ Cell(int initial_balance, bool international_on); // ---------------------- print ------------------------ /* * Prints the current status of "this" phone * * Status includes current account balance (in cents), whether * you can make international calls, and the last two numbers dialed. * * Example 1: * * ----> Balance: 350 cents; International: OFF; * ----> Last 1: 2564500; Last 2: 2566000 * * Example 2 (after using the default constructor): * * ----> Balance: 0 cents; International: ON; * ----> Last 1: ; Last 2: * */ void print() const; // ----------------------- add_money -------------------------- /* * If amount is positive, then adds that many dollars to the current * balance of "this" (you can only add whole dollar amounts) * If amount is not positive, then the function does nothing * * Notice that amount is in dollars, but I recommend you keep track * of the balance inside of "this" in cents */ void add_money(int amount); // ------------------------ account_balance ------------------- /* * Returns the current balance in "this" phone's account as a whole * number of CENTS! * */ int account_balance() const; // ----------------------- toggle_international_status ------------ /* * Changes whether or not you are allowed to make international calls. * * If you were allowed to make international calls before calling the * function, then the function changes it so you are not allowed. * * If you were not allowed to make international calls before * calling the function, then the function changes it so you are * allowed. */ void toggle_international_status(); // -------------------- make_local_call ------------------------ /* * Attempts to make a local call (i.e. a call inside the US) * with "this" phone for min minutes at a cost of 25 cents * per minute. * * If the current balance in "this" phone's account isn't enough * to make the call for min minutes, the function returns false * and "this" is unchanged. * * If the current balance in "this" phone's account is enough * to make the call for min minutes, then the last number and * second to last number dialed are both updated, * the account is debited by the cost of the call, and the * function returns true; * * To get full credit for this function, you must use the * update_last_number_dialed function (see the "private" section of * the class interface) */ bool make_local_call(int phone_number, int min); // -------------------- make_international_call ------------------------ /* * Attempts to make an international call * with "this" phone for min minutes at a cost of $5 (500 cents) * per minute. * * If the phone is set to not be allowed to make international calls, * then this function prints an error message and "this" is unchanged. * If the current balance in "this" phone's account isn't enough * to make the call for min minutes, the function returns false * and "this" is unchanged. * * If the current balance in "this" phone's account is enough * to make the call for min minutes, then the last number and * second to last number dialed are both updated, * the account is debited by the cost of the call, and the * function returns true; * * To get full credit for this function, you must use the * update_last_number_dialed function (see the "private" section of * the class interface) */ bool make_international_call(int phone_number, int min); // --------------- set_up_a_buddy --------------------- /* * This function is for pelephone's special thanksgiving * offer. It allows "this" and another user to introduce * a third buddy, and that buddy gets a new phone with * the best of both of the first two. * * Specifically: * * This function returns a phone where: * -- the account balance is the sum of the account * balances of "this" phone and "another_users" * phone. * -- if either "this" or "another_user" has * international dialing turned on, then the * new phone has international dialing turned on, * otherwise international dialing is turned off. * -- the last two numbers dialed are set to zero. * * Note: this function does not modify "this" or * another_user's phone. * */ Cell set_up_a_buddy (Cell another_user) const; /* This function works just like the set_up_a_buddy function except that it drains the accounts of both "this" and another_user (i.e. after the function has run, the phone returned really has gotten all of the minutes out of "this" and "another_user". */ Cell more_realistic_buddy (Cell& another_user); private: // --------------------- update_last_number_dialed ------------ /* * Updates the call log of "this" phone so that the last number * dialed is now new_number, and the second to last number dialed * is whatever the last number dialed used to be. * * This function is in the "private" section of the class because * we don't want to allow the person writing main and the "normal" * functions to be able to use this function. It's intended to * mostly be used by the functions that make international and * local calls. * * When you're writing the code for this function don't worry about * the fact that it's a private not a public function -- the coding * from the class perspective is pretty much the same. */ void update_last_number_dialed(int new_number); // ***************************************************** // ***************************************************** // ***************************************************** // YOU FILL THIS IN WITH PRIVATE VARIABLES // ***************************************************** // ***************************************************** // ***************************************************** int money_in_account; int last_number_dialed; int second_to_last_number_dialed; bool international_calls_on; }; // DO NOT CHANGE THE CODE BELOW THIS LINE (EXCEPT WHERE NOTED FOR EXTRA CREDIT // DO NOT CHANGE THE CODE BELOW THIS LINE (EXCEPT WHERE NOTED FOR EXTRA CREDIT // DO NOT CHANGE THE CODE BELOW THIS LINE (EXCEPT WHERE NOTED FOR EXTRA CREDIT // DO NOT CHANGE THE CODE BELOW THIS LINE (EXCEPT WHERE NOTED FOR EXTRA CREDIT // DO NOT CHANGE THE CODE BELOW THIS LINE (EXCEPT WHERE NOTED FOR EXTRA CREDIT //-------------------------- regular prototypes --------------------- bool should_i_continue(); /* asks user whether to continue or quit */ void success_message(int num); /*prints out how many points you may get */ void yes_or_no (bool x); /* prints yes if x is true, no if x is false */ // EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 // EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 // EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 // EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 // You may write here and below main (look for comment) for extra credit 1 // /* This function computes the number of local minutes that you have left in a given cell phone assuming it costs you 25 cents a minute to make a local call. precondition: argument recieved is a valid Cell class variable postcondition: an integer is returned */ int local_minutes(Cell phone); int main() { cout << " ------------------- Part 1 ---------------------" << endl; Cell one; cout << "Here's our first phone:" << endl << endl; one.print(); success_message(15); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 2 ---------------------" << endl; cout << "\nOK, now let's test the non-default constructor:\n" << "Creating a phone with a $1.00 balance: \n" << endl; Cell two(1, true); two.print(); cout << "\nAnd another that's not allowed to call internationally:\n" << endl; Cell three(-53, false); three.print(); success_message(25); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 3 ---------------------" << endl; cout << "Let's add $7.00 to the first phone:\n" << endl; one.add_money(7); one.print(); cout << "\nAnd let's add negative $51 to the third phone:\n" << endl; three.add_money(-51); three.print(); cout << "\nThe balance on the second phone is: ****** " << two.account_balance() << " ******" << endl; success_message(45); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 4 ---------------------" << endl; cout << "\nLet's change the international calling status on \n" << "phones 2 & 3\n" << endl; two.toggle_international_status(); cout << "Phone 2: " << endl; two.print(); three.toggle_international_status(); cout <<"\nPhone 3: " << endl; three.print(); success_message(55); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 5 ---------------------" << endl; cout <<"Let's try to make some local calls: "; cout <<"\nHow about a 7 minute local call on phone 1?" << endl; bool answer; answer = one.make_local_call(5551212,7); cout <<"\n>>>>>>>>>"; yes_or_no(answer); cout << endl << endl; cout << "Here's phone 1:\n\n"; one.print(); cout <>>>>>>>>>>>>>>> " << counter << " <<<<<<<<<<\n\n"; cout << "Now here's what two looks like: \n" << endl; two.print(); success_message(75); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 6 ---------------------" << endl; cout << "OK, let's top up the accounts in phones 2 & 3\n"; two.add_money(10); three.add_money(10); cout << endl <<"Phone Two:" << endl; two.print(); cout << endl <<"Phone Three:" << endl; three.print(); cout << endl << "Can we make an international call on phone 2? ###### "; answer = two.make_international_call(1144222,1); yes_or_no(answer); cout << endl << "Can we make an international call on phone 3? ###### "; answer = three.make_international_call(1144081,1); yes_or_no(answer); cout << endl <<"Phone Two:" << endl; two.print(); cout << endl <<"Phone Three:" << endl ; three.print(); success_message(85); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 7 ---------------------" << endl; cout <<"Let's turn off international calling on phone 1: " << endl << endl; one.toggle_international_status(); one.print(); cout << endl; Cell jeremy; cout << "Let's make a fourth cell phone for our pal Jeremy!" << endl; jeremy = two.set_up_a_buddy(three); cout <<"Here it is: " << endl; jeremy.print(); Cell ampersand; cout << "\nAnd finally, one for good old professor Ampersand:" << endl; ampersand = one.set_up_a_buddy(two); cout << "Here it is:" << endl; ampersand.print(); success_message(100); // Remove the begin comment symbol on the previous line to attempt // extra credit 1 if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Extra Credit 1 ---------------------" << endl; cout << endl << "Here are the number of minutes of local calls you can make on\n" << "each phone:" << endl; cout << "One: " << local_minutes(one) << ", Two: " << local_minutes(two) << ", Three: " << local_minutes(three) << ", Jeremy: " << local_minutes(jeremy) << ", Ampersand: " << local_minutes(ampersand) << endl << endl; // Remove the end comment symbol on the next line to attempt // extra credit 1 // Remove the begin comment symbol on the previous line to attempt // extra credit 2 if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Extra Credit 2 ---------------------" << endl; cout << "Tinky Winky takes all of Jeremy and Ampersand's minutes!!" << endl; Cell tinky_winky; tinky_winky = jeremy.more_realistic_buddy(ampersand); cout << endl << endl; cout << "Tinky Winky:" << endl; tinky_winky.print(); cout << endl << endl; cout << "Jeremy: " << endl; jeremy.print(); cout << endl << endl; cout << "Professor Ampersand: " << endl; ampersand.print(); // Remove the end comment symbol on the next line to attempt // extra credit 2 return (EXIT_SUCCESS); } // Normal function implementations start here // EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 // EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 // EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 // EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 EXTRA CREDIT 1 // You may write here and above main (look for comment) for extra credit 1 /* This function computes the number of local minutes that you have left in a given cell phone assuming it costs you 25 cents a minute to make a local call. precondition: argument recieved is a valid Cell class variable postcondition: an integer is returned */ int local_minutes(Cell phone) { return (phone.account_balance() / 25); } //----------------------------- should_i_continue ------------------- /* asks the user whether they want to continue or to quit */ bool should_i_continue() { cout << "\nDo you want to continue (type y for yes)? "; char ans; cin >> ans; cout << endl << endl << endl; if ((ans == 'y') || (ans == 'Y')) return (true); else return (false); } // ------------------ success_message ---------------------- /*prints out how many points you may get */ void success_message(int num) { cout << "\n\n-- If you successfully complete the program to here\n" << "-- You are eligible for up to " << num << " points." << endl; } // ------------------- yes_or_no --------------------------------- /* prints yes if x is true, no if x is false */ void yes_or_no (bool x) { if (x) cout << "Yes!"; else cout << "No!"; cout << endl; } /* DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT */ // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // ------------------------------ Cell ------------------------------ /* Default constructor "this" cellphone is created with - The last two numbers dialed set to zero (0) indicating that no calls have been made yet. - A zero balance in the account - The ability to make international calls turned ON. */ Cell::Cell() { money_in_account = 0; last_number_dialed = 0; second_to_last_number_dialed = 0; international_calls_on = true; } // ------------------------------ Cell ----------------------------- /* Constructor: "this" cellphone is created. * * - The last two numbers dialed are set to zero * * - If initial_balance is positive, then "this" cellphone is created with * that many dollars in the account, otherwise with a balance of zero in * the account. Please note: I recommend that you store the balance * internally inside the cellphone class as a number of CENTS * * - The ability to make international calls turned ON if international_on is true, * otherwise the ability to make international calls turned OFF. */ Cell::Cell(int initial_balance, bool international_on) { if (initial_balance >= 0) money_in_account = initial_balance * 100; else money_in_account = 0; if (international_on) international_calls_on = true; else international_calls_on = false; last_number_dialed = 0; second_to_last_number_dialed = 0; } // ---------------------- print ------------------------ /* * Prints the current status of "this" phone * * Status includes current account balance (in cents), whether * you can make international calls, and the last two numbers dialed. * * Example 1: * * ----> Balance: 350 cents; International: OFF; * ----> Last 1: 2564500; Last 2: 2566000 * * Example 2 (after using the default constructor): * * ----> Balance: 0 cents; International: ON; * ----> Last 1: ; Last 2: * */ void Cell::print() const { cout<<"----> Balance: " < Last 1: "; if(last_number_dialed) cout<"; cout<<"; Last 2: "; if(second_to_last_number_dialed) cout<"; cout<= 0) money_in_account += (amount * 100); } // ------------------------ account_balance ------------------- /* * Returns the current balance in "this" phone's account as a whole * number of CENTS! * */ int Cell::account_balance() const { return money_in_account; } // ----------------------- toggle_international_status ------------ /* * Changes whether or not you are allowed to make international calls. * * If you were allowed to make international calls before calling the * function, then the function changes it so you are not allowed. * * If you were not allowed to make international calls before * calling the function, then the function changes it so you are * allowed. */ void Cell::toggle_international_status() { if(international_calls_on) international_calls_on = false; else international_calls_on = true; } // -------------------- make_local_call ------------------------ /* * Attempts to make a local call (i.e. a call inside the US) * with "this" phone for min minutes at a cost of 25 cents * per minute. * * If the current balance in "this" phone's account isn't enough * to make the call for min minutes, the function returns false * and "this" is unchanged. * * If the current balance in "this" phone's account is enough * to make the call for min minutes, then the last number and * second to last number dialed are both updated, * the account is debited by the cost of the call, and the * function returns true; * * To get full credit for this function, you must use the * update_last_number_dialed function (see the "private" section of * the class interface) */ bool Cell::make_local_call(int phone_number, int min) { if (money_in_account >= (min * 25) ) { update_last_number_dialed(phone_number); money_in_account -= (min * 25); return true; } else return false; } // -------------------- make_international_call ------------------------ /* * Attempts to make an international call * with "this" phone for min minutes at a cost of $5 (500 cents) * per minute. * * If the phone is set to not be allowed to make international calls, * then this function returns false and "this" is unchanged. * If the current balance in "this" phone's account isn't enough * to make the call for min minutes, the function returns false * and "this" is unchanged. * * If the current balance in "this" phone's account is enough * to make the call for min minutes, then the last number and * second to last number dialed are both updated, * the account is debited by the cost of the call, and the * function returns true; * * To get full credit for this function, you must use the * update_last_number_dialed function (see the "private" section of * the class interface) */ bool Cell::make_international_call(int phone_number, int min) { if( (international_calls_on) && (money_in_account >= (min * 500)) ) { update_last_number_dialed(phone_number); money_in_account -= (min * 500); return true; } else return false; } // --------------- set_up_a_buddy --------------------- /* * This function is for pelephone's special thanksgiving * offer. It allows "this" and another user to introduce * a third buddy, and that buddy gets a new phone with * the best of both of the first two. * * Specifically: * * This function returns a phone where: * -- the account balance is the sum of the account * balances of "this" phone and "another_users" * phone. * -- if either "this" or "another_user" has * international dialing turned on, then the * new phone has international dialing turned on, * otherwise international dialing is turned off. * -- the last two numbers dialed are set to zero. * * Note: this function does not modify "this" or * another_user's phone. * */ Cell Cell::set_up_a_buddy (Cell another_user) const { Cell buddy; buddy.money_in_account = (money_in_account + another_user.money_in_account); if( (international_calls_on) || (another_user.international_calls_on) ) { buddy.international_calls_on = true; } else buddy.international_calls_on = false; return buddy; } // --------------------- update_last_number_dialed ------------ /* * Updates the call log of "this" phone so that the last number * dialed is now new_number, and the second to last number dialed * is whatever the last number dialed used to be. * * This function is in the "private" section of the class because * we don't want to allow the person writing main and the "normal" * functions to be able to use this function. It's intended to * mostly be used by the functions that make international and * local calls. * * When you're writing the code for this function don't worry about * the fact that it's a private not a public function -- the coding * from the class perspective is pretty much the same. */ void Cell::update_last_number_dialed(int new_number) { second_to_last_number_dialed = last_number_dialed; last_number_dialed = new_number; } /* This function works just like the set_up_a_buddy function except that it drains the accounts of both "this" and another_user (i.e. after the function has run, the phone returned really has gotten all of the minutes out of "this" and "another_user". */ Cell Cell::more_realistic_buddy (Cell& another_user) { Cell buddy; buddy.money_in_account = (money_in_account + another_user.money_in_account); money_in_account = 0; another_user.money_in_account = 0; if( (international_calls_on) || (another_user.international_calls_on) ) { buddy.international_calls_on = true; } else buddy.international_calls_on = false; return buddy; }