In: Computer Science
// C program to create a program for a company called Miller
Apartments based on the series of questions and answers
#include <stdio.h>
#include <stdlib.h>
// function declaration
int displayMenu();
void millerMain();
int inputNumberBedrooms();
int inputNumberBathrooms();
float determineRent(int bedrooms, int bathrooms, char
utilityChoice);
int main()
{
int choice;
// loop that continues until user exits
do
{
choice = displayMenu(); // input the choice
if(choice == 1) // user doesn't want to exit
millerMain();
}while(choice != 0);
printf("Thank you for choosing Miller
Apartments!\nGoodbye.");
return 0;
}
// function to display the choice and input and return user
choice
int displayMenu()
{
int choice;
printf("\n---------------------------------------------------------");
printf("\n|Welcome to Miller Apartments! |");
printf("\n---------------------------------------------------------");
printf("\n|Menu|");
printf("\n|New Search: 1|");
printf("\n|Exit Program: 0|");
printf("\n---------------------------------------------------------");
// input user choice
printf("\nEnter your choice: ");
scanf("%d",&choice);
// validate user choice and re-prompt until valid
while(choice < 0 || choice > 1)
{
printf("Invalid choice, please try again!");
printf("\nEnter your choice: ");
scanf("%d",&choice);
}
return choice;
}
// function to simulate the process to calculating the
rent
void millerMain()
{
int bedrooms, bathrooms;
char utilityChoice;
float rent, maxRent ;
char anotherSearch = 'N';
// loop that contains till the user wants
do{
// input number of bedrooms
bedrooms = inputNumberBedrooms();
// input number of bathrooms
bathrooms = inputNumberBathrooms();
// input whether user want to include utilities
printf("Would you like to have utilities included in the rent? Y/N:
");
scanf(" %c",&utilityChoice);
// validate utility choice and re-prompt until valid
while(utilityChoice != 'y' && utilityChoice != 'Y'
&& utilityChoice != 'n' && utilityChoice
!='N')
{
printf("Invalid choice, please try again!");
printf("\nWould you like to have utilities included in the rent?
Y/N: ");
scanf(" %c",&utilityChoice);
}
// return rent based on user choice
rent = determineRent(bedrooms, bathrooms, utilityChoice);
// input the maximum rent amount user is willing to pay
printf("Enter the maximum amount of rent you are willing to pay:
");
scanf("%f",&maxRent);
// rent determined > rent willing to pay
if(rent > maxRent){
printf("Based on your specifications, your total rent of $%.2f is
more than your max rent!",rent);
// input if user wants to perform another search
printf("\nWould you like to search using different criteria? Y/N:
");
scanf(" %c",&anotherSearch);
// validate user choice and re-prompt until valid
while(anotherSearch != 'y' && anotherSearch != 'Y'
&& anotherSearch != 'n' && anotherSearch
!='N')
{
printf("Invalid choice, please try again!");
printf("\nWould you like to search using different criteria? Y/N:
");
scanf(" %c",&anotherSearch);
}
}
else // rent determined < rent user is willing to pay
{
// display the message
anotherSearch = 'N';
printf("Congratulations! We have an apartment for you to
rent!");
printf("\nYour total rent will be $%.2f",rent);
if(utilityChoice == 'y' || utilityChoice == 'Y')
printf(" which includes utilities!");
else
printf(" which does NOT include utilities!");
}
}while(anotherSearch == 'y' || anotherSearch == 'Y');
}
// function to input number of bedrooms and return it
int inputNumberBedrooms()
{
int bedrooms;
// input number of bedrooms
printf("How many bedrooms would you like? 1 - 4: ");
scanf("%d",&bedrooms);
// validate number of bedrooms and re-prompt until valid
while(bedrooms < 1 || bedrooms > 4)
{
printf("Invalid number of bedrooms, please try again!");
printf("\nHow many bedrooms would you like? 1 - 4: ");
scanf("%d",&bedrooms);
}
return bedrooms;
}
// function to input number of bathrooms and return it
int inputNumberBathrooms()
{
int bathrooms;
// input number of bathrooms
printf("How many bathrooms would you like? 1 - 2: ");
scanf("%d",&bathrooms);
// validate number of bathrooms and re-prompt until valid
while(bathrooms < 1 || bathrooms > 2)
{
printf("Invalid number of bathrooms, please try again!");
printf("\nHow many bathrooms would you like? 1 - 2: ");
scanf("%d",&bathrooms);
}
return bathrooms;
}
// function to compute and return the rent based on user
choices
float determineRent(int bedrooms, int bathrooms, char
utilityChoice)
{
float rent;
if(bedrooms == 1)
rent = 600.00;
else if(bedrooms == 2)
rent = 750.00;
else if(bedrooms == 3)
rent = 900.00;
else
rent = 1050.00;
if(bathrooms == 2)
rent += 50.00;
if(utilityChoice == 'y' || utilityChoice == 'Y')
rent += bedrooms*50.00;
return rent;
}
//end of program
Output: