Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Change the code that determines the discount percent - 3. Change the if statemen

ID: 3914426 • Letter: C

Question

Change the code that determines the discount percent -

3. Change the if statement so customers of type R with a subtotal that’s greater than or equal to $250 but less than $500 get a 25% discount and those with a subtotal of $500 or more get a 30% discount. Next, change the if statement so customers of type C always get a 20% discount. Then, test these changes.

4. Add another type to the if statement so customers of type T get a 40% discount for subtotals of less than $500, and a 50% discount for subtotals of $500 or more. Also, make sure that customer types that aren’t R, C, or T get a 10% discount. Then, test these changes.

5. Test the application again, but use lowercase letters for the customer types. Note that these letters aren’t evaluated as capital letters.

6. Modify the code so the users can enter either capital or lowercase letters for the customer types. To do that, use logical operators. Then, test this change.

Use a switch statement with nested if statements to get the same results -

7. Code a switch statement right before the if statement. This statement should provide the structure for handling the three cases for customer types: R, C, and T. Then, within each of these cases, you can copy the related code from the if statement below to provide for the discounts that are based on subtotal variations. In other words, you can nest the if statements within the switch cases.

8. Comment out the entire if statement that’s above the switch statement. Then, test to make sure the switch statement works correctly.

9. If you haven’t done so already, modify the switch statement so it works for both lowercase and uppercase entries of the three customer types. To do that, use the strtoupper function to convert the customer type to uppercase before it’s evaluated. Then, test that change.

index.php -

<?php

$customer_type = filter_input(INPUT_POST, 'type');
$invoice_subtotal = filter_input(INPUT_POST, 'subtotal');

if ($customer_type == 'R') {
if ($invoice_subtotal < 100) {
$discount_percent = .0;
} else if ($invoice_subtotal >= 100 && $invoice_subtotal < 250) {
$discount_percent = .1;
} else if ($invoice_subtotal >= 250) {
$discount_percent = .25;
}
} else if ($customer_type == 'C') {
if ($invoice_subtotal < 250) {
$discount_percent = .2;
} else {
$discount_percent = .3;
}
} else {
$discount_percent = .0;
}

$discount_amount = $invoice_subtotal * $discount_percent;
$invoice_total = $invoice_subtotal - $discount_amount;

$percent = number_format(($discount_percent * 100));
$discount = number_format($discount_amount, 2);
$total = number_format($invoice_total, 2);

include 'invoice_total.php';

?>

invoice_total.php -

<!DOCTYPE html>
<html>
<head>
<title>Invoice Total Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<main>
<h1>Invoice Total Calculator</h1>
<p>Enter the values below and click "Calculate".</p>
<form action="." method="post">
<div id="data" >
<label>Customer Type:</label>
<input type="text" name="type"
value="<?php echo htmlspecialchars($customer_type); ?>"><br>

<label>Invoice Subtotal:</label>
<input type="text" name="subtotal"
value="<?php echo htmlspecialchars($invoice_subtotal); ?>"><br>

<label>Discount Percent:</label>
<input type="text" disabled
value="<?php echo $percent; ?>"><span>%</span><br>

<label>Discount Amount:</label>
<input type="text" disabled
value="<?php echo $discount; ?>"><br>

<label>Invoice Total:</label>
<input type="text" disabled
value="<?php echo $total; ?>"><br>
</div>
<div id="buttons" >
<label>&nbsp;</label>
<input type="submit" value="Calculate" id="calculate_button"><br>
</div>
</form>

</main>
</body>
</html>

Explanation / Answer

Please note that the following code is complete and the if statements have been commented out

<?php

$customer_type = filter_input(INPUT_POST, 'type');
$invoice_subtotal = filter_input(INPUT_POST, 'subtotal');

/*if ($customer_type == 'R' || $customer_type == 'r') {
if ($invoice_subtotal < 100) {
$discount_percent = .0;
} else if ($invoice_subtotal >= 100 && $invoice_subtotal < 250) {
$discount_percent = .1;
} else if ($invoice_subtotal >= 250 && #invoice_subtotal < 500) {
$discount_percent = .25;
}else if ($invoice_subtotal >= 500) {
$discount_percent = .3;
}
} else if ($customer_type == 'C' || $customer_type == 'c') {
$discount_percent = .2;
} else if ($customer_type == 'T' || $customer_type == 't') {
if ($invoice_subtotal < 500) {
$discount_percent = .4;
}else {
$discount_percent = .5;
} else {
$discount_percent = .1;
}*/

switch($customer_type){

case 'R':

case 'r':if ($invoice_subtotal < 100) {
$discount_percent = .0;
} else if ($invoice_subtotal >= 100 && $invoice_subtotal < 250) {
$discount_percent = .1;
} else if ($invoice_subtotal >= 250 && #invoice_subtotal < 500) {
$discount_percent = .25;
}else if ($invoice_subtotal >= 500) {
$discount_percent = .3;
}

break;

case 'C':

case 'c': $discount_percent = .2;

break;

case 'T':

case 't': if ($invoice_subtotal < 500) {
$discount_percent = .4;
}else {
$discount_percent = .5;
}

break;

default: $discount_percent = .1;

}

$discount_amount = $invoice_subtotal * $discount_percent;
$invoice_total = $invoice_subtotal - $discount_amount;

$percent = number_format(($discount_percent * 100));
$discount = number_format($discount_amount, 2);
$total = number_format($invoice_total, 2);

include 'invoice_total.php';

?>

invoice_total.php -

<!DOCTYPE html>
<html>
<head>
<title>Invoice Total Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<main>
<h1>Invoice Total Calculator</h1>
<p>Enter the values below and click "Calculate".</p>
<form action="." method="post">
<div id="data" >
<label>Customer Type:</label>
<input type="text" name="type"
value="<?php echo htmlspecialchars($customer_type); ?>"><br>

<label>Invoice Subtotal:</label>
<input type="text" name="subtotal"
value="<?php echo htmlspecialchars($invoice_subtotal); ?>"><br>

<label>Discount Percent:</label>
<input type="text" disabled
value="<?php echo $percent; ?>"><span>%</span><br>

<label>Discount Amount:</label>
<input type="text" disabled
value="<?php echo $discount; ?>"><br>

<label>Invoice Total:</label>
<input type="text" disabled
value="<?php echo $total; ?>"><br>
</div>
<div id="buttons" >
<label>&nbsp;</label>
<input type="submit" value="Calculate" id="calculate_button"><br>
</div>
</form>

</main>
</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote