I am a beginner user of solidity so I have to learn a lot. I am creating a car sales Dapp for a school assignment in remix and I can't figure out how I can keep updating an array value inside a struct using user inputs. I want the possibility to update the mileage for each car wich has to be greater than the previous value of that car using user inputs and it has to be connected to the licenseplate of a car so you can search for it later on. NOTE: It also has to store the mileage history so it has to be added to the array en not completley replace the older value.
The code all the way down with the "//" is something similar to what I want in terms of using as less ass possible gass.
contract CarApp {
address public owner;
struct Car {
string licensePlate;
string chassisNumber;
string brand;
string carType;
string colour;
uint256[] mileageHistory;
address owner;
}
constructor() {
owner = msg.sender;
//carsTotal = 0;
}
mapping (string => Car) public cars;
//mapping (string => uint256) public carsTotal;
function createCar(string memory _licensePlate, string memory _chassisNumber, string memory _brand, string memory _carType, string memory _colour, uint256 _currentMileage) external returns (bool) {
require(bytes(_licensePlate).length > 0, "Please fill in your license plate");
require(bytes(_chassisNumber).length > 0, "Please fill in your chassis number");
require(bytes(_brand).length > 0, "Please fill in the brand of the car");
require(bytes(_carType).length > 0, "Please fill in the car type");
require(bytes(_colour).length > 0, "Please fill in the colour of the car");
cars[_licensePlate].licensePlate = _licensePlate;
cars[_licensePlate].chassisNumber = _chassisNumber;
cars[_licensePlate].brand = _brand;
cars[_licensePlate].carType = _carType;
cars[_licensePlate].colour = _colour;
cars[_licensePlate].mileageHistory.push(_currentMileage);
cars[_licensePlate].owner = msg.sender;
//carsTotal++;
}
}
// function updateMileage(string memory _licensePlate, uint256 _currentMileage) public {
// require(bytes (_licensePlate).length > 0, "Please fill in your license plate");
// require(_currentMileage > cars[plates[_licensePlate]].currentMileage, "The mileage should be higher than the previous one");
// require(cars[plates[_licensePlate]].owner == msg.sender, "Only owner can change mileage");
// cars[plates[_licensePlate]].currentMileage = _currentMileage;
// }
// function updateMileage(string memory _licensePlate, uint256 _mileage) public {
// require(bytes (_licensePlate).length > 0, "Please fill in your license plate");
// require(_mileage > cars[_licensePlate].mileage, "The mileage should be higher than the previous one");
// require(cars[_licensePlate].owner == msg.sender, "Only owner can change mileage");
// cars[_licensePlate].mileage = _mileage;
// } ```