Skip to content
Closed

. #1992

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
69 changes: 69 additions & 0 deletions adding one
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

/**
* @title GarageManager
* @dev Contract to manage a garage of cars for each user
*/
contract GarageManager {
mapping(address => Car[]) private garages;

struct Car {
string make;
string model;
string color;
uint numberOfDoors;
}

error BadCarIndex(uint256 index);

/**
* @dev Adds a new car to the caller's garage
* @param _make The make of the car
* @param _model The model of the car
* @param _color The color of the car
* @param _numberOfDoors The number of doors of the car
*/
function addCar(string memory _make, string memory _model, string memory _color, uint _numberOfDoors) external {
garages[msg.sender].push(Car(_make, _model, _color, _numberOfDoors));
}

/**
* @dev Retrieves the caller's array of cars
* @return An array of `Car` structs
*/
function getMyCars() external view returns (Car[] memory) {
return garages[msg.sender];
}

/**
* @dev Retrieves a specific user's array of cars
* @param _user The address of the user
* @return An array of `Car` structs
*/
function getUserCars(address _user) external view returns (Car[] memory) {
return garages[_user];
}

/**
* @dev Updates a specific car in the caller's garage
* @param _index The index of the car in the garage array
* @param _make The new make of the car
* @param _model The new model of the car
* @param _color The new color of the car
* @param _numberOfDoors The new number of doors of the car
*/
function updateCar(uint256 _index, string memory _make, string memory _model, string memory _color, uint _numberOfDoors) external {
if (_index >= garages[msg.sender].length) {
revert BadCarIndex({index: _index});
}
garages[msg.sender][_index] = Car(_make, _model, _color, _numberOfDoors);
}

/**
* @dev Deletes all cars in the caller's garage
*/
function resetMyGarage() external {
delete garages[msg.sender];
}
}