├── README.md └── Str /README.md: -------------------------------------------------------------------------------- 1 | # Structs -------------------------------------------------------------------------------- /Str: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.17; 3 | 4 | /** 5 | * @title GarageManager 6 | * @dev Contract to manage a garage of cars for each user 7 | */ 8 | contract GarageManager { 9 | // Mapping to store the garage of cars for each user 10 | mapping(address => Car[]) private garages; 11 | 12 | // Struct to represent a car 13 | struct Car { 14 | string make; // Make of the car 15 | string model; // Model of the car 16 | string color; // Color of the car 17 | uint numberOfDoors; // Number of doors of the car 18 | } 19 | 20 | // Custom error for handling invalid car index 21 | error BadCarIndex(uint256 index); 22 | 23 | /** 24 | * @dev Adds a new car to the caller's garage 25 | * @param _make The make of the car 26 | * @param _model The model of the car 27 | * @param _color The color of the car 28 | * @param _numberOfDoors The number of doors of the car 29 | */ 30 | function addCar(string memory _make, string memory _model, string memory _color, uint _numberOfDoors) external { 31 | // Push a new car struct with the provided details to the caller's garage 32 | garages[msg.sender].push(Car(_make, _model, _color, _numberOfDoors)); 33 | } 34 | 35 | /** 36 | * @dev Retrieves the caller's array of cars 37 | * @return An array of `Car` structs 38 | */ 39 | function getMyCars() external view returns (Car[] memory) { 40 | // Return the array of cars stored in the caller's garage 41 | return garages[msg.sender]; 42 | } 43 | 44 | /** 45 | * @dev Retrieves a specific user's array of cars 46 | * @param _user The address of the user 47 | * @return An array of `Car` structs 48 | */ 49 | function getUserCars(address _user) external view returns (Car[] memory) { 50 | // Return the array of cars stored in the garage of the specified user 51 | return garages[_user]; 52 | } 53 | 54 | /** 55 | * @dev Updates a specific car in the caller's garage 56 | * @param _index The index of the car in the garage array 57 | * @param _make The new make of the car 58 | * @param _model The new model of the car 59 | * @param _color The new color of the car 60 | * @param _numberOfDoors The new number of doors of the car 61 | */ 62 | function updateCar(uint256 _index, string memory _make, string memory _model, string memory _color, uint _numberOfDoors) external { 63 | // Check if the provided index is valid 64 | if (_index >= garages[msg.sender].length) { 65 | revert BadCarIndex({index: _index}); // Revert with custom error if the index is invalid 66 | } 67 | // Update the specified car with the new details 68 | garages[msg.sender][_index] = Car(_make, _model, _color, _numberOfDoors); 69 | } 70 | 71 | /** 72 | * @dev Deletes all cars in the caller's garage 73 | */ 74 | function resetMyGarage() external { 75 | // Delete all cars from the caller's garage 76 | delete garages[msg.sender]; 77 | } 78 | } 79 | --------------------------------------------------------------------------------