In the code here, I have created a new Customer called "mcycleowner", and mapped it to an input _address. However, I also want to map it to the input _license so that customer info can be queried with either an Eth address or license plate number.
To do so, I created a new Customer "mcyclelicenses", and the function addLicense takes the same inputs as newMcycleOwner (except address), only this Customer is mapped to _license. This function is triggered within the "newMcycleOwner" function so that both customers and mappings are created simultaneously.
However, doing it this way means having two records of each customer's information, which seems like it would be a waste of space, and also whenever customer info needs to be changed, it will have to be changed in two places. Is there a better way to do this?
// < ---------------------- MOTORCYLCES ---------------------- >
mapping (address => Customer) mcycleowners;
address[] public mcycleownerAdds;
function newMcycleOwner(address _address, string _custname, string _color, string _make, uint32 _year, string _license) onlyMember public {
Customer storage mcycleowner = mcycleowners[_address];
mcycleowner.custname = _custname;
mcycleowner.color = _color;
mcycleowner.make = _make;
mcycleowner.year = _year;
mcycleowner.license = _license;
mcycleownerAdds.push(_address);
addLicense(_custname, _color, _make, _year, _license);
}
function getMcycleAdds() public view returns(address[]) {
return mcycleownerAdds;
}
function getMcycleByAdd(address _address) public view returns(string, string) {
return (mcycleowners[_address].custname, mcycleowners[_address].license);
}
function countMcycle() public view returns(uint) {
return mcycleownerAdds.length;
}
// <---------- CREATION OF NEW MAP SO WE CAN QUERY BY LICENSE ---------->
//This seems to create two instances of each customer. Replace with a better method.
mapping (string => Customer) mcyclelicenses;
function addLicense(string _custname, string _color, string _make, uint32 _year, string _license) onlyMember public {
Customer storage mcyclelicense = mcyclelicenses[_license];
mcyclelicense.custname = _custname;
mcyclelicense.color = _color;
mcyclelicense.make = _make;
mcyclelicense.year = _year;
}
function getMcycleByLic(string _license) public view returns(string, string, string, uint32) {
return (mcyclelicenses[_license].custname, mcyclelicenses[_license].color, mcyclelicenses[_license].make, mcyclelicenses[_license].year);
}