/* * Create smart contract for tracking cars repair history. * Fill up the code blocks in Solidity for: * - getCar() * - createCar() * - updateKilometers() * - transferOwnership() */ pragma solidity ^0.4.21; /** * Contract to store the mileage of a car */ contract Odometer { event Creation( address indexed from, string indexed vin ); event Transfer( address indexed from, address indexed to, string indexed vin ); struct Car { string vin; address owner; uint kilometers; } mapping (string => Car) cars; function Odometer() public {} /** * Returns the current data of the given car */ function getCar(string vin) public constant returns(address owner, uint kilometers) { /* -- Your code here -- */ return (cars[vin].owner, cars[vin].kilometers) } /** * Creates a track record of a new car. * Transaction will fail (and burn gas!) if the car already exists. */ function createCar(string vin) public { /* -- Your code here -- */ require(!cars[vin].valid); newcar = Car(vin, msg.sender, 0); cars[vin] = newcar; } /** * Updates the current kilometers of the car. Transactions fails and burns gas if * the new kilometer value is lower than the old one. */ function updateKilometers(string vin, uint kilometers) public { /* -- Your code here -- */ require(cars[vin].owner == msg.sender); require(cars[vin].valid); require(kilometers > cars[vin].kilometers) // Car car = Car(vin, cars[vin].owner, kilometer); cars[vin].kilometers = kilometers; } /** * Transfers the ownership of a car to a different address. * Transaction fails and burns gas if the car is not owned by the caller or if the kilometers are * less than the last known state. */ function transferOwnership(string vin, address owner, uint kilometers) public { /* -- Your code here -- */ // require(cars[vin].owner == msg.sender); updateKilometers(vin, kilometers); cars[vin].owner = owner; } }
0.4.18