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

This is PHP website language and this is not Java or Python language Lab 3.1 (3

ID: 3876035 • Letter: T

Question

This is PHP website language and this is not Java or Python language

Lab 3.1 (3 points)

Download file for lab 3.1: https://www.dropbox.com/s/yeynmnjgrajt5uy/ch08_ex1.zip?dl=0

Lab 3.2 (2 points)

Download file for this lab 3.2 : https://www.dropbox.com/s/3jkd21ct1l1texi/ch08_ex2.zip?dl=0

Open and test the application 1. Run the application in the ex_starts/ch08_exl directory. Then, test this appli- cation by entering either "R" or "C" in the Customer Type box and a value from 0 to 500 in the Invoice Subtotal box. When you click on the Calculate button, you can see that the discount percent is based on both the customer type and subtotal amount. Open the index.php and invoice_total.php files for this application and review the code. Note that this code uses nested if statements to determine the discount percent 2. 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 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 S500 or more. Also, make sure that customer types that aren't R, C, or T get a 10% discount. Then, test these changes. 4. 5. Test the application again, but use lowercase letters for the customer types 6. Modify the code so the users can enter either capital or lowercase letters for Use a switch statement with nested if statements to get the same results Note that these letters aren't evaluated as capital letters. the customer types. To do that, use logical operators. Then, test this change 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 7. 8. Comment out the entire if statement that's above the switch statement. Then, test to make sure the switch statement works correctly. 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 9.

Explanation / Answer

HERE IS THE SOLUTION FOR THE SECOND problem:

<?php

//set default values to be used when page first loads
$scores = array();
$scores[0] = 70;
$scores[1] = 80;
$scores[2] = 90;
  
$scores_string = '';
$score_total = 0;;
$score_average = 0;
$max_rolls = 0;
$average_rolls = 0;

//take action based on variable in POST array
$action = filter_input(INPUT_POST, 'action');
switch ($action) {
case 'process_scores':
$scores = $_POST['scores'];

// validate the scores
// TODO: Convert this if statement to a for loop
if (empty($scores[0]) ||
empty($scores[1]) ||
empty($scores[2]) ||
!is_numeric($scores[0]) ||
!is_numeric($scores[1]) ||
!is_numeric($scores[2])) {
$scores_string = 'You must enter three valid numbers for scores.';
break;
}

// process the scores
// TODO: Add code that calculates the score total
$scores_string = '';
$score_total=array_sum($scores);
foreach ($scores as $s) {
$scores_string .= $s . '|';
}
$scores_string = substr($scores_string, 0, strlen($scores_string)-1);

// calculate the average
$score_average = $score_total / count($scores);
  
// format the total and average
$score_total_f = number_format($score_total, 2);
$score_average_f = number_format($score_average, 2);

break;
case 'process_rolls':
$number_to_roll = filter_input(INPUT_POST, 'number_to_roll',
FILTER_VALIDATE_INT);

$total = 0;
//$count = 0;
$max_rolls = -INF;

// TODO: convert this while loop to a for loop
for ($count=0;$count < 10000;$count++) {
$rolls = 1;
while (mt_rand(1, 6) != 6) {
$rolls++;
}
$total += $rolls;
//$count++;
$max_rolls = max($rolls, $max_rolls);
}
$average_rolls = $total / $count;

break;
}
include 'loop_tester.php';
?>

loop_tester file:

<!DOCTYPE html>
<html>
<head>
<title>Loop Tester</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>

<body>
<main>
<h1>Loop Tester</h1>
<h2>Process Scores</h2>
<form action="." method="post">
<input type="hidden" name="action" value="process_scores">

<label>Score 1:</label>
<input type="text" name="scores[]"
value="<?php echo htmlspecialchars($scores[0]); ?>"><br>

<label>Score 2:</label>
<input type="text" name="scores[]"
value="<?php echo htmlspecialchars($scores[1]); ?>"><br>

<label>Score 3:</label>
<input type="text" name="scores[]"
value="<?php echo htmlspecialchars($scores[2]); ?>"><br>

<label>&nbsp;</label>
<input type="submit" value="Process Scores"><br>

<label>Scores:</label>
<span><?php echo htmlspecialchars($scores_string); ?></span><br>

<label>Score Total:</label>
<span><?php echo $score_total; ?></span><br>

<label>Average Score:</label>
<span><?php echo $score_average; ?></span><br>
</form>
<h2>Process 1000 Die Rolls</h2>
<form action="." method="post">
<input type="hidden" name="action" value="process_rolls">

<label>Number to Roll:</label>
<select name="number_to_roll">
<!-- TODO: Use a for loop to display these options ! -->
<?php
for($i=1;$i<7;$i++)
{
echo "<option value='$i'>$i</option>";
}
?>
  

</select><br>

<label>&nbsp;</label>
<input type="submit" value="Process Rolls"><br>

<label>Maximum Rolls:</label>
<span><?php echo $max_rolls; ?></span><br>

<label>Average Rolls:</label>
<span><?php echo $average_rolls; ?></span><br>

</form>

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

SOLUTION FOR FIRST PROBLEM:

index.php file_

<?php

$customer_type = filter_input(INPUT_POST, 'type');
$customer_type=strtoupper($customer_type);
$invoice_subtotal = filter_input(INPUT_POST, 'subtotal');
switch ($customer_type) {
case 'R':
if ($invoice_subtotal < 250) {
$discount_percent = .20;
} else if ($invoice_subtotal >= 250 && $invoice_subtotal < 500) {
$discount_percent = .25;
} else if ($invoice_subtotal >= 500) {
$discount_percent = .30;
}
break;
case 'C':
$discount_percent = .20;
break;
case 'T':
if ($invoice_subtotal < 500) {
$discount_percent = .40;
} else if ($invoice_subtotal >= 500) {
$discount_percent = .50;
}
break;
default:
$discount_percent = .10;
break;
}
/*
if ($customer_type == 'R' || $customer_type == 'r') {
if ($invoice_subtotal < 250) {
$discount_percent = .20;
} else if ($invoice_subtotal >= 250 && $invoice_subtotal < 500) {
$discount_percent = .25;
} else if ($invoice_subtotal >= 500) {
$discount_percent = .30;
}
} else if ($customer_type == 'C' || $customer_type == 'c') {
$discount_percent = .20;
}
else if($customer_type == 'T' || $customer_type == 't') {
if ($invoice_subtotal < 500) {
$discount_percent = .40;
} else if ($invoice_subtotal >= 500) {
$discount_percent = .50;
}
}
else {
$discount_percent = .10;
}
*/
$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>

please check the files I had provided you above...It runs..

Please do refer the draftbox links for the modified zip file..

https://www.dropbox.com/s/himsj89m2loqk1u/ch08_ex1.zip?dl=0

https://www.dropbox.com/s/e8trpu4eorhhhg5/ch08_ex2.zip?dl=0


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