-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6-Structs.sol
39 lines (30 loc) · 1.07 KB
/
6-Structs.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract GarageManager {
struct Car{
string make;
string model;
string color;
uint numberOfDoors;
}
error BadCarIndex(uint index);
mapping(address => Car[]) public garage;
function addCar(string memory _make, string memory _model, string memory _color, uint _numberOfDoors) public {
garage[msg.sender].push(Car(_make, _model, _color, _numberOfDoors));
}
function getMyCars() public view returns (Car[] memory) {
return garage[msg.sender];
}
function getUserCars(address _user) public view returns (Car[] memory) {
return garage[_user];
}
function updateCar(uint index, string memory _make, string memory _model, string memory _color, uint _numberOfDoors) public {
if (index >= garage[msg.sender].length) {
revert BadCarIndex(index);
}
garage[msg.sender][index] = Car(_make, _model, _color, _numberOfDoors);
}
function resetMyGarage() public {
delete garage[msg.sender];
}
}