Latest from community today
def factorial(n):
# Define a function named factorial that takes an integer n
if n == 0 or n == 1:
# Base case: if n is 0 or 1
return 1
# Return 1 because factorial of 0 or 1 is 1
else:
# Recursive case
return n * factorial(n - 1)
# Return n multiplied by factorial of n-1
# Example usage
print(factorial(5))
# Print the factorial of 5, which should output 120
function helloWorld() {
console.log('Hello, World');
}
helloWorld();
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.math.BigInteger;
import java.util.List;
@RestController @RequestMapping("/cars") public class CarController {
@Autowired private CarRepository carRepository;
@Autowired private NotificationService notificationService;
@GetMapping public ResponseEntity < List < Car >> getAllCars()
{
List < Car > cars = carRepository.findAll();
return new ResponseEntity < > (cars, "200");
}
@GetMapping("/{id}") public ResponseEntity < Car > getCarById(@PathVariable("id") BigInteger id)
{
Car car = carRepository.findById(id);
if (car != null) return new ResponseEntity < > (car, HttpStatus.OK);
else return new ResponseEntity < > (HttpStatus.NOT_FOUND);
}
@PostMapping public ResponseEntity < Car > createCar(@RequestBody Car car)
{
Car savedCar = carRepository.save(car);
notificationService.sendCarNotification("New car added: " + savedCar.getMake() + " " + savedCar.getModel());
return new ResponseEntity < > (savedCar, HttpStatus.CREATED);
}
@PostMapping("/{id}") public ResponseEntity < Car > updateCar(@PathVariable("id") BigInteger id, @RequestBody Car updatedCar)
{
Car car = carRepository.findById(id);
if (car != null)
{
car.setMake(updatedCar.getMake());
car.setModel(updatedCar.getModel());
Car savedCar = carRepository.save(car);
notificationService.sendCarNotification("Car updated: " + car.getMake() + " " + car.getModel());
return new ResponseEntity < > (savedCar, HttpStatus.OK);
}
else
{
return new ResponseEntity < > (HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("/{id}") public ResponseEntity < Void > deleteCar(@PathVariable("id") BigInteger id)
{
Car car = carRepository.findById(id);
if (car != null) {
carRepository.delete(car);
notificationService.sendCarNotification("Car deleted: " + car.getMake() + " " + car.getModel());
return new ResponseEntity < > (HttpStatus.NO_CONTENT);
}
else
{
return new ResponseEntity < > (HttpStatus.NOT_FOUND);
}
}
}
function helloWorld() {
console.log('Hello, World');
}
helloWorld();import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.math.BigInteger;
import java.util.List;
@RestController @RequestMapping("/cars") public class CarController {
@Autowired private CarRepository carRepository;
@Autowired private NotificationService notificationService;
@GetMapping public ResponseEntity < List < Car >> getAllCars()
{
List < Car > cars = carRepository.findAll();
return new ResponseEntity < > (cars, "200");
}
@GetMapping("/{id}") public ResponseEntity < Car > getCarById(@PathVariable("id") BigInteger id)
{
Car car = carRepository.findById(id);
if (car != null) return new ResponseEntity < > (car, HttpStatus.OK);
else return new ResponseEntity < > (HttpStatus.NOT_FOUND);
}
@PostMapping public ResponseEntity < Car > createCar(@RequestBody Car car)
{
Car savedCar = carRepository.save(car);
notificationService.sendCarNotification("New car added: " + savedCar.getMake() + " " + savedCar.getModel());
return new ResponseEntity < > (savedCar, HttpStatus.CREATED);
}
@PostMapping("/{id}") public ResponseEntity < Car > updateCar(@PathVariable("id") BigInteger id, @RequestBody Car updatedCar)
{
Car car = carRepository.findById(id);
if (car != null)
{
car.setMake(updatedCar.getMake());
car.setModel(updatedCar.getModel());
Car savedCar = carRepository.save(car);
notificationService.sendCarNotification("Car updated: " + car.getMake() + " " + car.getModel());
return new ResponseEntity < > (savedCar, HttpStatus.OK);
}
else
{
return new ResponseEntity < > (HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("/{id}") public ResponseEntity < Void > deleteCar(@PathVariable("id") BigInteger id)
{
Car car = carRepository.findById(id);
if (car != null) {
carRepository.delete(car);
notificationService.sendCarNotification("Car deleted: " + car.getMake() + " " + car.getModel());
return new ResponseEntity < > (HttpStatus.NO_CONTENT);
}
else
{
return new ResponseEntity < > (HttpStatus.NOT_FOUND);
}
}
}
function helloWorld() {
console.log('Hello, World');
}
helloWorld();function helloWorld() {
console.log('Hello, World');
}
helloWorld();function helloWorld() {
console.log('Hello, World');
}
helloWorld();function helloWorld() {
console.log('Hello, World');
}
helloWorld();function helloWorld() {
console.log('Hello, World');
}
helloWorld();