Skip to content

Instantly share code, notes, and snippets.

@jesyspa
Forked from anonymous/gist:11398448
Last active August 29, 2015 14:00
Show Gist options
  • Save jesyspa/11400622 to your computer and use it in GitHub Desktop.
Save jesyspa/11400622 to your computer and use it in GitHub Desktop.
#include <iostream>
/*
* Programming Assignment 02
* This program will accept, from the user, the type of recreational equipment
* being rented, as well as the duration of the rental. It will then display a
* summary of the sale, first listing the duration of the rental, then the
* recreational equipment being rented, and then the total cost of the rental.
*
* Program Preconditions
* 1. The user will enter the type of equipment rental, which will be S for
* skateboard, I for inline skates, T for touring bike, or M for mountain
* bike.
* 2. The user must enter the duration of the rental, in minutes, which should
* be a whole number between 1 and 720.
*
* Program Postconditions
* 1. The program will display the duration of rental, in minutes.
* 2. The program will display the type of equipment rental, which will be
* skateboard, inline skates, touring bike or mountain bike.
* 3. The program will display the total cost of the rental in dollars (if any)
* and cents (if any).
*/
/* REVIEW: The documentation belongs more to the declarations than the
* definitions; that's what people will look at when they want to find out more
* about a function.
*/
// This function asks the user for the equipment type, and returns that type as
// either an 's' (for skateboard), 'i' (for inline skate), 't' (for touring
// bike), or 'm' (for mountain bike).
char get_equipment_type();
// This function asks the user for the duration of the rental, in minutes, and
// returns that number.
int get_rental_duration();
// This function accepts the equipment type ('s', 'i', 't', or 'm') and the
// duration of the rental, and returns //the cost of the rental in dollars (if
// any) and cents (if any).
int compute_rental_cost(char type, int time);
// This function accepts the equipment type ('s', 'i', 't', or 'm') and the
// total cost. It will then convert the //total cost of the rental to dollars
// (if any) and cents (if any), and display the information to the user in a
// well−formatted Englist language sentence.
void report_rental_cost(char type, int time, int price);
int main() {
/* REVIEW: Main points:
* - Don't use 'using namespace std;' (http://stackoverflow.com/q/1452721/559931)
* - Avoid useless comments.
* - Don't mix tabs and spaces.
* - Don't define things any earlier than they have to be.
* - Make things const when possible.
* - Prefer to return a value directly, rather than putting it in a variable
* and then returning immediately afterwards.
*/
std::cout << "Trail Rentals Cashiering Program, version 1.0" << std::endl;
std::cout << "(c) Ping, 2014" << std::endl;
char const type = get_equipment_type(); // REVIEW: Get? From where?
int const time = get_rental_duration();
int const price = compute_rental_cost(type, time);
report_rental_cost(type, time, price);
std::cout << "Finishing Trail Rentals Cashiering Program, version 1.0" << std::endl;
}
char get_equipment_type() {
char userresponse;
std::cout << "Enter the type of rental," << std::endl;
std::cout << "S(kateboard), I(nline skate), T(ouring bike), or M";
std::cout << "(ountain bike): " << std::endl;
std::cin >> userresponse;
// You might want to check that input succeeded at all.
if (userresponse == 's' || userresponse == 'S')
return 's';
if (userresponse == 'i' || userresponse == 'I')
return 'i';
if (userresponse == 't' || userresponse == 'T')
return 't';
else
return 'm';
}
int get_rental_duration() {
int userresponse;
std::cout << "Enter the duration of rental in minutes: ";
std::cin >> userresponse;
if (userresponse > 0 && userresponse < 721)
return userresponse;
return 60;
}
int compute_rental_cost(char type, int time) {
// REVIEW: You were incorrectly using = instead of == here.
if (type == 's' || type == 'i')
return time * 15;
if (type == 't')
return time * 17;
if (type == 'm' && time <= 60)
return 900;
return 900 + ((time -60) * 19);
}
void report_rental_cost(char type, int time, int price) {
// REVIEW: I'm pretty sure you got your maths wrong here initially.
int const dollarcost = price / 100;
int const centcost = price % 100;
std::cout << "This " << time << " minute " << type << " rental will cost ";
// REVIEW: You don't handle all possible cases here.
if (dollarcost == 0 && centcost > 1)
std::cout << centcost << " cents.\n";
else if (dollarcost == 1 && centcost > 1)
std::cout << dollarcost << " dollar and " << centcost << " cents.\n";
else if (dollarcost >= 1 && centcost == 0)
std::cout << dollarcost << " dollars.\n";
else if (dollarcost >= 1 && centcost == 1)
std::cout << dollarcost << " dollars and " << centcost << " cent.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment