From a810b056b337d006ba5f7256749f3339ecdf0fde Mon Sep 17 00:00:00 2001 From: hfarazul Date: Tue, 17 May 2022 19:29:13 +0530 Subject: [PATCH] add contract --- contracts/PetPark.sol | 99 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/contracts/PetPark.sol b/contracts/PetPark.sol index d53e90d..022b486 100644 --- a/contracts/PetPark.sol +++ b/contracts/PetPark.sol @@ -4,4 +4,101 @@ pragma solidity ^0.8.0; contract PetPark { -} \ No newline at end of file +enum animalType {None, Fish, Cat, Dog, Rabbit, Parrot} +enum gender {Male, Female} +address immutable owner; + +struct member{ + gender _gender; + uint age; + address memberAddress; +} +mapping (animalType => uint) numberOfAnimals; +mapping (address => animalType) borrowed; +mapping (address => member) members; + +event Added (animalType, uint); +event Borrowed (animalType); +event Returned (animalType); + +constructor (){ + owner = msg.sender; +} + +function add (animalType _animalType, uint _count) public{ + require (owner == msg.sender, "Not an owner"); + require ( _animalType != animalType.None, "Invalid animal"); + numberOfAnimals[_animalType] += _count; + emit Added(_animalType, numberOfAnimals[_animalType]); +} + +function animalCounts(animalType _animalType) public view returns (uint) { + return numberOfAnimals[_animalType]; +} + + +function borrow (uint _age, gender _gender, animalType _animalType) public{ + //basic checks + require ( _age> 0, "Invalid Age"); + require (_animalType != animalType.None, "Invalid animal type"); + require (numberOfAnimals[_animalType] > 0, "Selected animal not available"); + + + // borrower age and gender consistency check + member memory newMemberAdress = members [msg.sender]; + if (newMemberAdress.memberAddress == address(0)){ + // add a member + members[msg.sender] = member(_gender, _age, msg.sender); + } + else{ + // check client information + require (newMemberAdress.age == _age, "Invalid Age"); + require (newMemberAdress._gender == _gender, "Invalid Gender"); + } + + // check if the address already has a animal + animalType _currentpet = borrowed[msg.sender]; + require (_currentpet == animalType.None, "Already adopted a pet"); + + // borrow logic with conditions defined in Problem statement + if(_gender == gender.Male){ + if (_animalType == animalType.Dog || _animalType == animalType.Fish){ + borrowed[msg.sender] = _animalType; + numberOfAnimals [_animalType] -= 1; + emit Borrowed(_animalType); + } + else{ + revert("Invalid animal for men"); + } + } + + else if (_gender == gender.Female){ + if (_animalType == animalType.Cat){ + if (_age >= 40){ + borrowed[msg.sender] = _animalType; + numberOfAnimals [_animalType] -= 1; + emit Borrowed(_animalType); + } + else { + revert("Invalid animal for women under 40"); + } + } + else{ + borrowed[msg.sender] = _animalType; + numberOfAnimals [_animalType] -= 1; + emit Borrowed(_animalType); + } + } +} + + + +function giveBackAnimal () public{ + animalType _currentpet = borrowed[msg.sender]; + require (_currentpet != animalType.None, "No borrowed pets"); + numberOfAnimals [_currentpet] += 1; + emit Returned(_currentpet); + borrowed[msg.sender] = animalType.None; +} + +}