Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions calculator-app-1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Calculator App

This is a simple browser-based calculator application built with JavaScript. It provides basic arithmetic operations such as addition, subtraction, multiplication, and division.

## Project Structure

```
calculator-app
├── src
│ ├── calculator.js # Contains the Calculator class with arithmetic methods
│ ├── index.js # Entry point for the application
│ └── styles
│ └── style.css # Styles for the user interface
├── public
│ └── index.html # Main HTML document for the application
└── README.md # Documentation for the project
```

## Setup Instructions

1. Clone the repository to your local machine.
2. Navigate to the project directory.
3. Open `public/index.html` in your web browser to view the application.

## Usage Guidelines

- The calculator supports the following operations:
- Addition: `add(a, b)`
- Subtraction: `subtract(a, b)`
- Multiplication: `multiply(a, b)`
- Division: `divide(a, b)` (Note: Division by zero will throw an error)

Feel free to modify the code and enhance the functionality as needed!
24 changes: 24 additions & 0 deletions calculator-app-1/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator App</title>
<link rel="stylesheet" href="../src/styles/style.css">
</head>
<body>
<div class="calculator">
<h1>Calculator</h1>
<input type="text" id="inputA" placeholder="Enter first number" />
<input type="text" id="inputB" placeholder="Enter second number" />
<div class="buttons">
<button id="add">Add</button>
<button id="subtract">Subtract</button>
<button id="multiply">Multiply</button>
<button id="divide">Divide</button>
</div>
<h2>Result: <span id="result"></span></h2>
</div>
<script src="../src/index.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions calculator-app-1/src/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This file is intentionally left blank.
40 changes: 40 additions & 0 deletions calculator-app-1/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Calculator from './calculator.js';

const calculator = new Calculator();

document.addEventListener('DOMContentLoaded', () => {
const resultDisplay = document.getElementById('result');
const form = document.getElementById('calculator-form');

form.addEventListener('submit', (event) => {
event.preventDefault();

const a = parseFloat(document.getElementById('input-a').value);
const b = parseFloat(document.getElementById('input-b').value);
const operation = document.querySelector('input[name="operation"]:checked').value;

let result;

try {
switch (operation) {
case 'add':
result = calculator.add(a, b);
break;
case 'subtract':
result = calculator.subtract(a, b);
break;
case 'multiply':
result = calculator.multiply(a, b);
break;
case 'divide':
result = calculator.divide(a, b);
break;
default:
throw new Error('Invalid operation');
}
resultDisplay.textContent = `Result: ${result}`;
} catch (error) {
resultDisplay.textContent = `Error: ${error.message}`;
}
});
});
50 changes: 50 additions & 0 deletions calculator-app-1/src/styles/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}

.container {
max-width: 400px;
margin: auto;
padding: 20px;
background: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h1 {
text-align: center;
color: #333;
}

input[type="number"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
}

button {
width: 100%;
padding: 10px;
background-color: #28a745;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}

button:hover {
background-color: #218838;
}

.result {
margin-top: 20px;
font-size: 24px;
text-align: center;
color: #333;
}
37 changes: 37 additions & 0 deletions weather-app/test_weather_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest
from unittest.mock import patch
import weather-script as weather_script

class TestWeatherScript(unittest.TestCase):
@patch('weather-script.requests.get')
def test_get_weather_success(self, mock_get):
# Mock a successful API response
mock_response = unittest.mock.Mock()
mock_response.status_code = 200
mock_response.json.return_value = {
"weather": [{"description": "clear sky"}],
"main": {"temp": 25, "humidity": 40},
"wind": {"speed": 3.5}
}
mock_get.return_value = mock_response

with patch('builtins.print') as mock_print:
weather_script.get_weather("London")
mock_print.assert_any_call("Weather in London: Clear sky")
mock_print.assert_any_call("Temperature: 25°C")
mock_print.assert_any_call("Humidity: 40%")
mock_print.assert_any_call("Wind speed: 3.5 m/s")

@patch('weather-script.requests.get')
def test_get_weather_failure(self, mock_get):
# Mock a failed API response
mock_response = unittest.mock.Mock()
mock_response.status_code = 404
mock_get.return_value = mock_response

with patch('builtins.print') as mock_print:
weather_script.get_weather("InvalidCity")
mock_print.assert_called_with("City not found or API error.")

if __name__ == '__main__':
unittest.main()
25 changes: 25 additions & 0 deletions weather-app/weather-script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# weather.py
import requests

API_KEY = "a715d2140ac16e09e147208d9e54f3d8" # Replace with your OpenWeatherMap API key
BASE_URL = "https://api.openweathermap.org/data/2.5/weather"

def get_weather(city):
params = {
"q": city,
"appid": API_KEY,
"units": "metric"
}
response = requests.get(BASE_URL, params=params)
if response.status_code == 200:
data = response.json()
print(f"Weather in {city}: {data['weather'][0]['description'].capitalize()}")
print(f"Temperature: {data['main']['temp']}°C")
print(f"Humidity: {data['main']['humidity']}%")
print(f"Wind speed: {data['wind']['speed']} m/s")
else:
print("City not found or API error.")

if __name__ == "__main__":
city = input("Enter city name: ")
get_weather(city)