1

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;
//     } ```

1 Answer 1

0

From your code, I get the impression that you want to append mileage to a history of mileage inputs.

I'm assuming this is experimental, for learning, so I'm setting aside suggestions about issues that will be become clearer as you progress in knowledge about the EVM and Solidity.

You have most of what you need to append another input:

  function updateMileage(string memory _licensePlate, uint256 _mileage) public {

Let's make some changes to do what you're trying to accomplish:

Check the car exists. We know that all registered cars have at list one mileage entry:

//         require(bytes (_licensePlate).length > 0, "Please fill in your license plate");
require(cars[_licensePlate].mileageHistory.length > 0, "Car not registered");

Require that mileage entries are greater than the last recorded one:

//         require(_mileage > cars[_licensePlate].mileage, "The mileage should be higher than the previous one");
uint256 lastMileageIndex = cars[_licensePlate].mileageHistory.length - 1;
uint256 lastMileage = cars[_licensePlate].mileageHistory[lastMileageIndex];
require(_mileage > lastMileage, "Odometer only goes forward");

Only the owner can update their car:

require(cars[_licensePlate].owner == msg.sender, "Only owner can change mileage");

Append it:

//         cars[_licensePlate].mileage = _mileage;
cars[_licensePlate].mileageHistory.push(_mileage); 

None of that has been compiled/tested, so please forgive any minor oversights or typos. The suggestions should help find your footing working with arrays inside mapped structs.

Hope it helps.

2
  • Thanks for your help! The code gives me this error at the [index]: DeclarationError: Undeclared identifier. How can I resolve this?
    – User122333
    Mar 14 at 9:29
  • Typo in the example. Corrected it. index should be lastMileageIndex Mar 17 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.