[Solved] Vacation - PHP Task (with examples)

  

4
Topic starter

You are given a group of people, type of the group, on which day of the week they are going to stay. Based on that information calculate how much they have to pay and print that price on the console. Use the table below. In each cell is the price for a single person. The output should look like that: "Total price: {price}". The price should be formatted to the second decimal point:

vacation php task

There are also discounts based on some conditions:

  • Students – if the group is bigger than or equal to 30 people you should reduce the total price by 15%
  • Business – if the group is bigger than or equal to  100 people, 10 of them can stay for free
  • Regular – if the group is bigger than or equal 10 and less than or equal to 20 reduce the total price by 5%

You should reduce the prices in that EXACT order

Examples:

vacation php task examples

2 Answers
3

Here is my answer (see line number 41 to 43):

<?php
 
$groupCount = intval(readline());
$groupType = readline();
$dayOfWeek = readline();
$pricePerNight = 0;
 
switch ($groupType) {
 
    case 'Students':
        if ($dayOfWeek == 'Friday') {
            $pricePerNight = 8.45;
        } else if ($dayOfWeek == 'Saturday') {
            $pricePerNight = 9.8;
        } else if ($dayOfWeek == 'Sunday') {
            $pricePerNight = 10.46;
        }
        break;
 
    case 'Business':
        if ($dayOfWeek == 'Friday') {
            $pricePerNight = 10.90;
        } else if ($dayOfWeek == 'Saturday') {
            $pricePerNight = 15.60;
        } else if ($dayOfWeek == 'Sunday') {
            $pricePerNight = 16;
        }
        break;
 
    case 'Regular':
        if ($dayOfWeek == 'Friday') {
            $pricePerNight = 15;
        } else if ($dayOfWeek == 'Saturday') {
            $pricePerNight = 20;
        } else if ($dayOfWeek == 'Sunday') {
            $pricePerNight = 22.50;
        }
        break;
}
 
if ($groupType == 'Business' && $groupCount >= 100) {
    $groupCount -= 10;
}
 
$total = $pricePerNight * $groupCount;
 
if ($groupType == 'Students' && $groupCount >= 30) {
    $total *= 0.85;
}
 
if ($groupType == 'Regular' && $groupCount >= 10 && $groupCount <= 20) {
    $total *= 0.95;
}
 
$formatted = number_format($total, 2, '.', '');
echo 'Total price: ' . $formatted;
2

To @samfred5830 > lines 41 to 43 can be put after:

$total = $pricePerNight * $groupCount;

Here's how (see lines 47 to 49):

<?php
 
$groupCount = intval(readline());
$groupType = readline();
$dayOfWeek = readline();
$pricePerNight = 0;
 
switch ($groupType) {
 
    case 'Students':
        if ($dayOfWeek == 'Friday') {
            $pricePerNight = 8.45;
        } else if ($dayOfWeek == 'Saturday') {
            $pricePerNight = 9.8;
        } else if ($dayOfWeek == 'Sunday') {
            $pricePerNight = 10.46;
        }
        break;
 
    case 'Business':
        if ($dayOfWeek == 'Friday') {
            $pricePerNight = 10.90;
        } else if ($dayOfWeek == 'Saturday') {
            $pricePerNight = 15.60;
        } else if ($dayOfWeek == 'Sunday') {
            $pricePerNight = 16;
        }
        break;
 
    case 'Regular':
        if ($dayOfWeek == 'Friday') {
            $pricePerNight = 15;
        } else if ($dayOfWeek == 'Saturday') {
            $pricePerNight = 20;
        } else if ($dayOfWeek == 'Sunday') {
            $pricePerNight = 22.50;
        }
        break;
}
 
$total = $pricePerNight * $groupCount;
 
if ($groupType == 'Students' && $groupCount >= 30) {
    $total *= 0.85;
}
 
if ($groupType == 'Business' && $groupCount >= 100) {
    $total -= $pricePerNight * 10;
}
 
if ($groupType == 'Regular' && $groupCount >= 10 && $groupCount <= 20) {
    $total *= 0.95;
}
 
$formatted = number_format($total, 2, '.', '');
echo 'Total price: ' . $formatted;
Share: