Project Three Solution
Below is a possible solution for the project :
/* A boat rental company has three types of boats.
The type of the boat depends on the number of
persons the boat can carry. */
int show_menu ( void );
/* shows the menu of the four possible options */
int select_type ( int n1, int n2, int n3 );
/* prompts the user for a selection of type boats
and checks the availability */
void prn_available ( int n1, int n2, int n3 );
/* prints the number of available boats */
int available ( int n1, int n2, int n3, int n );
/* returns 1 if type n boat is available, 0 otherwise */
void boat_renting ( int n1, int n2, int n3 );
/* runs a boat renting session with three types of boats */
#include <stdio.h>
int main(void)
{
int n1,n2,n3,choice,n;
printf("\nWelcome to our boat rental company! \n");
printf(" Give number of 1-person boats : ");
scanf("%d", &n1);
printf(" Give number of 2-person boats : ");
scanf("%d", &n2);
printf(" Give number of 3-person boats : ");
scanf("%d", &n3);
boat_renting(n1,n2,n3);
return 0;
}
int show_menu ( void )
{
int choice;
printf("\nChoose one of the following: \n");
printf(" 0. terminate the program;\n");
printf(" 1. see the number of available boats of each type;\n");
printf(" 2. rent a boat, of a type that is available;\n");
printf(" 3. return a boat.\n");
printf("Type 0,1,2, or 3 to select : ");
scanf("%d", &choice);
return choice;
}
int select_type ( int n1, int n2, int n3 )
{
int choose_type;
printf("\nFor how many persons ? ");
scanf("%d", &choose_type);
if ((choose_type < 0) || (choose_type > 3)
|| (available(n1,n2,n3,choose_type) == 0))
{
printf("Sorry, no %d-person boat available.\n", choose_type);
choose_type = 0;
}
else
printf("Thank you for using our %d-person boat.\n", choose_type);
return choose_type;
}
void prn_available ( int n1, int n2, int n3 )
{
printf("\nNumber of 1-person boats available : %d\n", n1);
printf("Number of 2-person boats available : %d\n", n2);
printf("Number of 3-person boats available : %d\n", n3);
}
int available ( int n1, int n2, int n3, int n )
{
switch (n)
{
case 1: return (n1 > 0);
case 2: return (n2 > 0);
case 3: return (n3 > 0);
}
}
void boat_renting ( int n1, int n2, int n3 )
{
int choice,n;
for (;;)
{
choice = show_menu();
if (choice == 0) break;
if (choice == 1)
prn_available(n1,n2,n3);
else
{
n = select_type(n1,n2,n3);
if (n != 0)
if (choice == 2)
switch(n)
{
case 1: n1 = n1 - 1; break;
case 2: n2 = n2 - 1; break;
case 3: n3 = n3 - 1; break;
}
else
switch(n)
{
case 1: n1 = n1 + 1; break;
case 2: n2 = n2 + 1; break;
case 3: n3 = n3 + 1; break;
}
}
}
}