In: Computer Science
double calculateCost (char, int, int) with Drjava
calculateCost will compute and return the cost of the vacation It
will take as input 3 pieces of information. First, a char
representing the type of room being rented (‘d’ double, ‘k’
kingsize, ‘p’ for penthouse). The second input is an int
representing the number of days they will stay, the last input is
an int representing the number of people staying.
To compute the cost of the stay we start with a base price,
determined by the type of the room. Daily fee for the room: $15 for
double, $20 for king size, $30 for penthouse. Only two people per
room, add a $20 flat fee for each extra person for a folding
bed.
If they stay more than 7 days there is a 10% discount, more than 14
days a 15% discount and more than 21 days a 20% discount.
Example: Ted, who is 23, is renting a penthouse for 12 days for a
party with 5 of his best buds. He owes $30*12days + $20*3 extra
people for the folding beds and he gets a discount of 10% because
he is staying more than 7 days.
The total would be $360 + $60 is the total fee of $420
The discount is 10% so $42
The amount to pay is $420 - $42 is $378
Explanation:I have written the method ouble calculateCost (char, int, int) and have also shown the output by using the main method, please find the image attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.
//code
public class RoomCost {
public double calculateCost(char type, int
noOfDays, int noOfPeople) {
double totalCost = 0;
// room cost
if (type == 'd') {
totalCost +=
noOfDays * 15;
} else if (type == 'k') {
totalCost +=
noOfDays * 20;
} else if (type == 'p') {
totalCost +=
noOfDays * 30;
}
// folding bed cost
if (noOfPeople > 2)
totalCost += (20
* (noOfPeople - 2));
// discount on stay
if (noOfDays > 7 &&
noOfDays <= 14) {
totalCost -=
(0.10 * totalCost);
} else if (noOfDays > 14
&& noOfDays <= 21) {
totalCost -=
(0.15 * totalCost);
} else if (noOfDays > 21)
{
totalCost -=
(0.20 * totalCost);
}
return totalCost;
}
public static void main(String[] args) {
RoomCost roomCost = new
RoomCost();
System.out.println(
"The amount to pay is $" +
roomCost.calculateCost('p', 12, 5));
}
}
Output: