Last active
August 13, 2018 01:25
-
-
Save phuctu1901/6947cca207bf5179b570cf4603cd32a2 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=false&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.21; | |
contract Vehicles{ | |
struct Person { | |
address owner; | |
string yearOfRegis; | |
string etag_id; | |
} | |
struct Vehicle{ | |
string plate_id; | |
Person[] persons; | |
uint256 lenghtPerson; | |
string name; | |
string typeOfVehicle; | |
string color; | |
string yearOfPro; | |
string charactic; | |
string manufac; | |
string serial; | |
string status; //active, deactive, stolen. | |
} | |
mapping (string => Vehicle) vehicles; | |
mapping (address => address) permissionList; | |
string[] public vehiclesList; | |
function checkOwner(address _owner, string _plate_id) public view returns(bool){ | |
if (vehicles[_plate_id].persons[vehicles[_plate_id].lenghtPerson -1 ].owner != _owner){ | |
return (false); | |
} | |
else{ | |
return (true); | |
} | |
} | |
function checkPermission() public view returns(bool){ | |
if (permissionList[msg.sender] == address(0)) { | |
return false; | |
} | |
else{ | |
return true; | |
} | |
} | |
function addPermission(address _address) public{ | |
address newOwner = permissionList[_address]; | |
permissionList[_address]= newOwner; | |
} | |
function addVehicle(address _owner, string _plate_id, string _name, string _type, string _color, string _yearOfPro, string _yearOfRegis, string _charac, string _manufac, string _serial, string _etag_id) public returns (bool){ | |
if (!checkPermission()){ | |
return false; | |
} | |
else{ | |
Vehicle storage vehicle = vehicles[_plate_id]; | |
Person storage p = vehicle.persons[vehicle.lenghtPerson] ; | |
p.owner = _owner; | |
p.yearOfRegis= _yearOfRegis; | |
p.etag_id = _etag_id; | |
vehicle.persons[vehicle.lenghtPerson] = p; | |
vehicle.plate_id = _plate_id; | |
vehicle.name = _name; | |
vehicle.typeOfVehicle = _type; | |
vehicle.color = _color; | |
vehicle.yearOfPro = _yearOfPro; | |
vehicle.charactic = _charac; | |
vehicle.manufac = _manufac; | |
vehicle.serial = _serial; | |
vehicle.status = "active"; | |
vehicle.lenghtPerson++; | |
vehiclesList.push(_plate_id) -1; | |
return true; | |
} | |
} | |
function changeOwner(string _plate_id, address _owner, address _new_owner, string _yearOfRegis, string _etag_id) public returns (bool){ | |
if (!checkPermission()){ | |
return false; | |
} | |
if (!checkOwner(_owner, _plate_id)){ | |
return false; | |
} | |
else{ | |
Person storage p = vehicles[_plate_id].persons[vehicles[_plate_id].lenghtPerson] ; | |
p.owner = _new_owner; | |
p.yearOfRegis = _yearOfRegis; | |
p.etag_id = _etag_id; | |
vehicles[_plate_id].lenghtPerson ++; | |
return true; | |
} | |
} | |
function renewEtagID(string _plate_id, address _owner) public view returns(bool, string){ | |
if (!checkPermission()){ | |
return (false, ""); | |
} | |
if (!checkOwner(_owner, _plate_id)){ | |
return (false, ""); | |
} | |
else{ | |
string storage _etag_id = vehicles[_plate_id].persons[vehicles[_plate_id].lenghtPerson -1 ].etag_id; | |
return (true, _etag_id); | |
} | |
} | |
function changeStatus(string _plate_id, address _owner, string _status) public returns(bool){ | |
if (!checkPermission()){ | |
return false; | |
} | |
if (!checkOwner(_owner, _plate_id)){ | |
return false; | |
} | |
else{ | |
vehicles[_plate_id].status = _status; | |
return (true); | |
} | |
} | |
function getFixedInfo(string _plate_id) view public returns (string, string, string, string, string) { | |
return(vehicles[_plate_id].name, vehicles[_plate_id].typeOfVehicle, vehicles[_plate_id].yearOfPro, vehicles[_plate_id].manufac, vehicles[_plate_id].serial); | |
} | |
function getDynamicInfo(string _plate_id) view public returns (address, string, string, string, string){ | |
address _owner = vehicles[_plate_id].persons[vehicles[_plate_id].lenghtPerson].owner; | |
string storage _yearOfRegis = vehicles[_plate_id].persons[vehicles[_plate_id].lenghtPerson].yearOfRegis; | |
return (_owner, _yearOfRegis , vehicles[_plate_id].color, vehicles[_plate_id].charactic, vehicles[_plate_id].status); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment