<?php
class Calculator {
public $none;
public $ntwo;
public $operator;
function __construct($none, $ntwo, $operator) {
$this->none = $none;
$this->ntwo = $ntwo;
$this->operator = $operator;
}
// function calcu() {
// if ($this->operator === 'sum') {
// return $this->none + $this->ntwo;
// } elseif ($this->operator === 'sub') {
// return $this->none - $this->ntwo;
// } elseif ($this->operator === 'into') {
// return $this->none * $this->ntwo;
// } elseif ($this->operator === 'Devition') {
// if ($this->ntwo != 0) {
// return $this->none / $this->ntwo;
// } else {
// return "Cannot divide by zero";
// }
// } else {
// return "Invalid operator";
// }
// }
function calculate() {
switch ($this->operator) {
case 'sum':
return $this->none + $this->ntwo ;
case 'sub':
return $this->none - $this->ntwo ;
case 'into':
return $this->none * $this->ntwo;
case 'Devition':
if ($this->ntwo != 0) {
return $this->none / $this->ntwo;
} else {
return "Cannot / by zero";
}
default:
return "Invalid operator";
}
}
}
@$none = $_POST['none'];
@$ntwo = $_POST['ntwo'];
@$operator = $_POST['operator'];
@$result = new Calculator($none, $ntwo, $operator);
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" >
<title>Calculator | Using OOP</title>
</head>
<body>
<div class="w-75 m-auto mt-5">
<form action="./calculator.php" method="post">
<div class="container">
<div class="row">
<div class="col">
<input type="number" class="form-control form-control-sm" name="none" placeholder="Number One" Required value="{{ old('none', 'default value') }}">
</div>
<div class="col">
<input type="number" class="form-control form-control-sm" name="ntwo" placeholder="Number Two" Required value="{{ old('ntwo', 'default value') }}">
</div>
<div class="col">
<select class="form-select form-select-sm mb-3" aria-label=".form-select-lg example" name="operator">
<option disabled selected>Select Operator</option>
<option value="sum">+</option>
<option value="sub">-</option>
<option value="into">*</option>
<option value="Devition">/</option>
</select>
</div>
<div class="col">
<button type="submit" class="btn btn-primary btn-sm">Calculator</button>
</div>
</div>
</div>
</form>
<h2>Result : <?php echo $result->calculate() ?> </h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" ></script>
</body>
</html>