Uploaded by R. K.

Quiz PHP Software Developer 2022

advertisement
Quiz
Technical Development DepartmentPHP
Software Developer
Candidate
: Muhamma Kamaruzzaman Bin Mohamad Nasri
Date
: 28.02.2023
Score
:
Checked By
:
Jul 2022
Question 1 : Logic in PHP Coding
For each string, print the minimum number of deletions on repeating characters required.
Input
AAAA
BBBBB
ABABABAB
AAABBB
Output
3
4
0
4
<?php
$arr = ['AAAA', 'BBBBB', 'ABABABABAB', 'AAABBB'];
foreach ($arr as $key => $value) {
$c = -1;
$result = preg_replace('/(\w)(?=\1)/', "", $value, -1, $c);
echo $result . '<br>';
}
Jul 2022
Question 2 : Visualize Solution and PHP Coding
Your company would like to develop a reward system. The requirement are as below:
1. Customers will be rewarded with Points when Sales Order in “Complete” status.
2. Every USD$1 sales amount will be rewarded with 1 point, if the sales amount is not USD,
convert to equivalent amount in USD for reward amount calculation.
3. Reward amount will be credited into the customer account with expiry date, which is 1 year
later.
4. Points can be used for new order payment, every 1 point equivalent to USD$0.01.
Please provide the following :
1. Flowchart or sequence UML diagram on the reward system
2. Design MySQL database schema for this reward system
3. PHP functions to
a. calculate and use reward points for new order payment
b. calculate and credit user reward points after order completion
1) Flowchart or sequence UML diagram on the reward system
2
Jul 2022
) Design MySQL database schema for this reward system
Jul 2022
3.a) calculate and use reward points for new order payment
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "reward";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
// echo "Connected successfully";
}
$sql = "SELECT * FROM `order` WHERE cust_id = 1;"; // find ali order list
$result = $conn->query($sql);
$point = 0;
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
// check old order date have exceed 365 days or not.
$date1 = date_create(date('Y-m-d', strtotime('+365 days' . $row['order_date'])));
$date2 = date_create($row['order_date']);
$diff = date_diff($date1, $date2);
$ttl_day = $diff->format("%a");
if ($ttl_day <= 365) {
$point += $row['reward_point'];
}
}
} else {
echo "0 results";
}
echo $point; // this point will be add to new order for price deduction.
$conn->close();
Jul 2022
3.b) calculate and credit user reward points after order completion
class FindSame
{
function calcRewardPoint($curr, $amount)
{
$url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currencyapi@1/latest/currencies/usd/" . $curr . ".json";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
$outputDecode = json_decode($response, true);
return round($amount / round($outputDecode[$curr], 2), 0);
}
}
$curr = 'myr'; // parse currency type
$amount = 50; // total order amount
$t = new FindSame();
if ($curr != 'usd') {
$point = $t->calcRewardPoint($curr, $amount);
} else {
$point = round($amount, 0);
}
echo $point; // result
Jul 2022
Question 3 : MySQL Query
Based on the given tables, write a SINGLE query to retrieve the total order and sales amount of all
the orders. Your output should display Number_Of_Order, Total_Sales_Amount
Orders Table
Order_ID
Order_Date
Sales_Type
1001
2007-05-01 12:10:10
Normal
1002
2007-05-07 05:28:55
Normal
1003
2007-05-19 17:17:00
Promotion
1004
2007-05-22 22:47:16
Promotion
1005
2007-05-27 08:15:07
Promotion
1006
2007-06-01 06:35:59
Normal
Orders_Products Table
Order_Product_ID
Order_ID
Item_Name
Normal_Price
Promotion_Price
2000
1001
Radio
800.00
712.99
2001
1002
Portable Audio
16.00
15.00
2002
1002
THE SIMS
9.99
8.79
2003
1003
Radio
800.00
712.99
2004
1004
Scanner
124.00
120.00
2005
1005
Portable Audio
16.00
15.00
2006
1005
Radio
800.00
712.99
2007
1006
Camcorders
359.00
303.00
2008
1006
Radio
800.00
712.99
NOTE: Refer Normal_Price if Sales_Type is 'Normal'
Refer Promotion_Price if Sales_Type is 'Promotion'
Jul 2022
Answer
SELECT
`order_pro`.`order_id`,
COUNT(`order_pro`.`order_id`) AS number_of_order,
(
SUM(
CASE
WHEN `order_ls`.`sales_type` = 'Normal' THEN normal_price
WHEN `order_ls`.`sales_type` = 'Promotion' THEN promotion_price
END)
) AS total_sales_amount
FROM `order_pro`
LEFT JOIN `order_ls` ON `order_ls`.`order_id`=`order_pro`.`order_id`
GROUP BY `order_pro`.`order_id`;
Jul 2022
Download