嗨,我是刚开始踏实的人。我正忙于一个记录牛的各种信息的项目。
在我下面的代码中,我的牛结构有一个CattleHealth和CattleGrowth数组,这些数组应该保存所执行的检查记录。现在,在我的RecordHealth和RecordGrowth函数中,我打算用时间戳记录信息,并将其作为单个记录添加到相应的数组中。
然而,我意识到我可能犯了一个很大的错误使用一个地址的记录,因为这将覆盖信息,每次输入(如果我是错的纠正我)。那么,我如何添加单独的记录,并保持它与正确的牛/牛链接?
contract WagyuRecordContract
{
address farmer;
struct Cattle
{
address RFID;
string Name;
uint256 Weight;
string Gender;
string Colour;
string Breed;
uint Age;
uint DOB;
string Location;
bool Parent;
string SireName;
string DamName;
bool Active;
bool ForSale;
CattleHealth[] HealthRecord;
CattleGrowth[] GrowthRecord;
CattleMovements[] MovementsRecord;
}
struct CattleHealth
{
uint DateRecorded;
string BodyCondition;
string HealthStatus;
string Medication;
}
struct CattleGrowth
{
uint DateRecorded;
uint256 FoodIntake;
uint256 Growth;
}
struct CattleMovements
{
string From;
string To;
}
mapping (address => Cattle) public cattle;
mapping (address=> CattleHealth) public health;
mapping (address=> CattleGrowth) public growth;
modifier Farmer()
{
require(msg.sender == farmer);
_;
}
function addNewCattle(address rfid, string _name, uint _weight, string _gender, string _colour,
string _breed, uint _age, uint _dob) Farmer public
{
cattle[rfid].Name = _name;
cattle[rfid].Weight = _weight;
cattle[rfid].Gender = _gender;
cattle[rfid].Colour = _colour;
cattle[rfid].Breed = _breed;
cattle[rfid].Age = _age;
cattle[rfid].DOB = _dob;
}
function NewCattleDetails(address rfid, bool _parent, string _location, string _sireName, string _damName, bool _active, bool _forSale) public Farmer
{
cattle[rfid].Parent = _parent;
cattle[rfid].Location =_location;
cattle[rfid].SireName = _sireName;
cattle[rfid].DamName =_damName;
cattle[rfid].Active = _active;
cattle[rfid].ForSale = _forSale;
}
function RecordHealth(address rfid, string _bodyCond, string _healthStat, uint256, string _med) Farmer public
{
health[rfid].DateRecorded = now;
health[rfid].BodyCondition = _bodyCond;
health[rfid].HealthStatus = _healthStat;
health[rfid].Medication = _med;
cattle[rfid].HealthRecord.push(health[rfid]);
}
function RecordGrowth(address rfid, uint256 _foodIntake, uint256 _growth) Farmer public
{
growth[rfid].DateRecorded = now;
growth[rfid].FoodIntake = _foodIntake;
growth[rfid].Growth = _growth;
cattle[rfid].GrowthRecord.push(growth[rfid]);
}
}发布于 2018-09-15 20:45:21
是的,你是对的,你只会保持最后的成长和健康记录。我想你可以去掉这两行:
mapping (address=> CattleHealth) public health;
mapping (address=> CattleGrowth) public growth;相反,请在以下文件中创建记录:
CattleHealth[] HealthRecord;
CattleGrowth[] GrowthRecord;采用push()方法。
https://ethereum.stackexchange.com/questions/58816
复制相似问题