Site to calculate electricity bill using HTML, CSS and Java Script
>> HTML code to enter customer name, customer id and units consumed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Electricity Bill Calculator</title>
</head>
<body>
<div class="container">
<h1>Electricity Bill Calculator</h1>
<form id="bill-form">
<label for="name">Name:</label>
<input type="text" id="name" required><br>
<label for="id">Customer ID:</label>
<input type="text" id="id" required><br>
<label for="units">Units Consumed:</label>
<input type="number" id="units" required><br>
<button type="button" id="calculate">Calculate Bill</button>
</form>
<div id="bill-result" class="hidden">
<h2>Bill Details</h2>
<p><strong>Name:</strong> <span id="result-name"></span></p>
<p><strong>Customer ID:</strong> <span id="result-id"></span></p>
<p><strong>Units Consumed:</strong> <span id="result-units"></span></p>
<p><strong>Total Cost:</strong> <span id="result-cost"></span></p>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
>> CSS code for the site.
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: bisque;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #ffffff;
border-radius: 5px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
form {
margin-bottom: 20px;
}
label {
display: inline-block;
width: 120px;
margin-right: 10px;
font-weight: bold;
}
input {
padding: 5px;
width: 100%;
margin-bottom: 10px;
}
button {
background-color: #007bff;
color: #fff;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
}
.hidden {
display: none;
}
>> Java Script to set data in local storage.
document.addEventListener('DOMContentLoaded', () => {
var billForm = document.getElementById('bill-form');
var calculateButton = document.getElementById('calculate');
var resultContainer = document.getElementById('bill-result');
var resultName = document.getElementById('result-name');
var resultId = document.getElementById('result-id');
var resultUnits = document.getElementById('result-units');
var resultCost = document.getElementById('result-cost');
calculateButton.addEventListener('click', () => {
var name = document.getElementById('name').value;
var id = document.getElementById('id').value;
var units = parseFloat(document.getElementById('units').value);
if (units <= 199) {
charges = units * 1.20;
} else if (units <= 399) {
charges = 199 * 1.20 + (units - 199) * 1.50;
} else if (units <= 599) {
charges = 199 * 1.20 + 200 * 1.50 + (units - 399) * 1.80;
} else {
charges = 199 * 1.20 + 200 * 1.50 + 200 * 1.80 + (units - 599) * 2;
}
if (charges > 500) {
extra_charge = charges * 0.15;
charges += extra_charge;
}
if (charges < 100) {
charges = 100;
}
resultName.textContent = name;
resultId.textContent = id;
resultUnits.textContent = units;
resultCost.textContent = `₹${charges.toFixed(2)}`;
resultContainer.classList.remove('hidden');
// Save data to localStorage
var userData = {
name,
id,
units,
charges
};
localStorage.setItem('user_data', JSON.stringify(userData));
});
});
>> Outputs generated:
Website output on chrome |
Comments
Post a Comment