DEV Community

Cover image for Scenario-based programs
Janu Nirmal
Janu Nirmal

Posted on

Scenario-based programs

Scenario 1:

1)Shopping Discount System
Input:
amount
isMember(boolean)
Rules:
1. amount ≥ 5000 => 20% discount
2. amount ≥ 2000 => 10% discount
3. Otherwise, no discount
4. members get extra 10% discount
5. Print final price after discount.

Flow diagram:

Program code

<!DOCTYPE html>

<html>
   <title>Shopping task scenario</title>
<head>
   <body>
 <script>
   let amount = 5000;
   let ismember = true;
   let discount = 0;

   if(amount >= 5000)
   {
      discount = 20;
   }

   else if(amount >= 2000)
      {
         discount = 10;
      }

      else
      {
         discount = 0;
      }

      if(ismember)
      {
         discount = discount + 10;
      }

      let finalprice = amount - (amount * discount/100)

      document.write("Final price concluded" + finalprice)

   </script>
   </body>

</head>
</html>



Enter fullscreen mode Exit fullscreen mode

Output

Final price concluded3500
Enter fullscreen mode Exit fullscreen mode

Scenario 2

2)Login Access Check
Input
username
password
isBlocked(boolean)
Rules

  1. Allow login only if:
  2. username is "Test"
  3. password is "pwd"
  4. user is not blocked
  5. Otherwise print an appropriate message.

Flow diagram

Program code

<!DOCTYPE html>
<html>
<head>
    <title>Login access scenario</title>
</head>

<body>
    <script>

        let username = "admin";
        let password = "1234";
        let isblocked = true;

        if (username == "Test") {

            if (password == "pwd") {
                document.write("User is not blocked");
            }
            else {
                document.write("Username is wrong");
            }

        }
        else {
            document.write("Password");
        }

    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

Password
Enter fullscreen mode Exit fullscreen mode

Scenario 3:

3)Traffic Signal System

Input
Signal color variable:

Rules:

  1. red = stop
  2. yellow = get ready
  3. green = go

Flow diagram

Program code

<!DOCTYPE html>
<html>
<head>
    <title>Traffic Signal</title>
</head>

<body>

    <script>

        let color = "red";

        if (color == "red") {
            document.write("Stop in signal");
        }

        else if (color == "yellow") {
            document.write("Wait in signal");
        }

        else {
            document.write("Go in the signal");
        }

    </script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

Stop in signal
Enter fullscreen mode Exit fullscreen mode

Scenario 4

ATM Withdrawal
Inputs

  1. balance
  2. withdrawAmount

Rules:

  1. If withdrawAmount > balance = "Insufficient funds"
  2. If withdrawAmount ≤ 0 = "Invalid amount"
  3. Else => deduct and show remaining balance

Flow diagram

Program code

<!DOCTYPE html>
<html>
<head>
    <title>Withdrawal Scenario</title>
</head>

<body>

<script>

let withdrawAmount = 200;
let balance = 2000;

if (withdrawAmount > balance) {

    document.write("Balance is insufficient");

}
else if (withdrawAmount <= 0) {

    document.write("Invalid amount");

}
else {

    let remainingBalance = balance - withdrawAmount;

    document.write(
        "Remaining balance calculated: "
        + remainingBalance
    );

}

</script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

Remaining balance calculated1800
Enter fullscreen mode Exit fullscreen mode

Scenario 5

**Mobile Data Usage

***Inputs*

  1. User has used data
  2. Used GB out of the limit GB.

Rules

  1. If usage ≥ 100% = "Limit exceeded"
  2. If usage ≥ 80% => "Warning: nearing limit"
  3. Else = "Usage normal"

Flow diagram

Program code

<!DOCTYPE html>
<html>
<head>
    <title>Data Usage</title>
</head>

<body>

    <script>

        let GB = 80;

        if (GB >= 100) {

            document.write("Limit exceeded");

        }
        else if (GB >= 80) {

            document.write("Warning: nearing limit");

        }
        else {

            document.write("Usage normal");

        }

    </script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

Warning: nearing limit
Enter fullscreen mode Exit fullscreen mode

Scenario 6

Electricity Bill

Inputs
Units consumed

Rules

  1. 0-100 => 5/unit
  2. 101-300 => 7/unit
  3. 300+ 10/unit
  4. Calculate the total bill.

Flow diagram

Program code

<!DOCTYPE html>
<html>
<head>
    <title>Electricity Bill</title>
</head>

<body>

    <script>

        let unitsConsumed = 200;
        let bill = 0;

        if (unitsConsumed <= 100) {

            bill = unitsConsumed * 5;

        }
        else if (unitsConsumed <= 300) {

            bill = unitsConsumed * 7;

        }
        else {

            bill = unitsConsumed * 10;

        }

        document.write("Total bill: " + bill);

    </script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

Total bill1000
Enter fullscreen mode Exit fullscreen mode

Scenario 7

Student Grade System

Inputs
marks

Rules:

  1. If marks ≥ 90 → Grade A
  2. If marks ≥ 75 → Grade B
  3. If marks ≥ 50 → Grade C
  4. Otherwise → Fail
  5. Print the grade.

Flow diagram

Program code

<!DOCTYPE html>
<html>

<head>
    <title>Marks Grade</title>
</head>

<body>

    <script>

        let marks = 65;

        if (marks >= 90) {

            document.write("Grade A");

        }
        else if (marks >= 75) {

            document.write("Grade B");

        }
        else if (marks >= 50) {

            document.write("Grade C");

        }
        else {

            document.write("Failed");

        }

    </script>

</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Output

Grade C
Enter fullscreen mode Exit fullscreen mode

Scenario 8

Age Eligibility for Voting

Inputs
age

Rules:

  1. If age ≥ 18 → Eligible to vote
  2. Otherwise → Not eligible
  3. Print eligibility status.

Program code

<!DOCTYPE html>
<html>

<head>
    <title>Voting Eligibility</title>
</head>

<body>

    <script>

        let age = 10;

        if (age >= 18) {

            document.write("Concern person is eligible for voting");

        }
        else {

            document.write("Concern person is not eligible for voting");

        }

    </script>

</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Output

Concern person is not eligible for voting
Enter fullscreen mode Exit fullscreen mode

Scenario 9

Password Strength Checker

Input
passwordLength

Rules:

  1. If length ≥ 12 → Strong password
  2. If length ≥ 8 → Medium password
  3. Otherwise → Weak password
  4. Print password strength.

Program code

<!DOCTYPE html>
<html>

<head>
    <title>Password Strength Checker</title>
</head>

<body>

    <script>

        let passwordLength = 10;

        if (passwordLength >= 12) {

            document.write("Password strength is strong");

        }
        else if (passwordLength >= 8) {

            document.write("Password strength is medium");

        }
        else {

            document.write("Password strength is weak");

        }

    </script>

</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Scenario - 10

Movie Ticket Pricing

Inputs
age

Rules:

  1. If age < 5 → Free ticket
  2. If age ≤ 18 → Ticket price = 100
  3. If age ≤ 60 → Ticket price = 150
  4. Otherwise → Senior citizen price = 120
  5. Print ticket price.

Program code

<!DOCTYPE html>
<html>

<head>
    <title>Movie Ticket Booking</title>
</head>

<body>

    <script>

        let age = 10;
        let ticketFare = 0;

        if (age < 5) {

            ticketFare = 0;

        }
        else if (age <= 18) {

            ticketFare = 100;

        }
        else if (age <= 60) {

            ticketFare = 150;

        }
        else {

            ticketFare = 120;

        }

        document.write("The final ticket price is " + ticketFare);

    </script>

</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Output

The final ticket price is100
Enter fullscreen mode Exit fullscreen mode

Scenario 11

Temperature Alert System

Inputs
temperature

Rules:

  1. If temperature ≥ 40 → Heat alert
  2. If temperature ≥ 30 → Warm weather
  3. Otherwise → Normal weather
  4. Print weather status.

Program code

<!DOCTYPE html>
<html>

<head>
    <title>Temperature Monitor System</title>
</head>

<body>

    <script>

        let temperature = 20;
        let weather = "";

        if (temperature >= 40) {

            weather = "Heat alert";

        }
        else if (temperature >= 30) {

            weather = "Warm weather";

        }
        else {

            weather = "Normal";

        }

        document.write("The weather condition is " + weather);

    </script>

</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Output

The wether condition isNormal
Enter fullscreen mode Exit fullscreen mode

Top comments (0)