Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Haque Farazul - Pet Park Assignment #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 98 additions & 1 deletion contracts/PetPark.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,101 @@ pragma solidity ^0.8.0;

contract PetPark {

}
enum animalType {None, Fish, Cat, Dog, Rabbit, Parrot}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommended to have enum names start with capital letters

enum gender {Male, Female}
address immutable owner;

struct member{
gender _gender;
uint age;
address memberAddress;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storage is costly in solidity. You don't need to store this address since you are already have the address as key in members mapping

}
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use a modifier here for checking owner as it provides better code readability

require ( _animalType != animalType.None, "Invalid animal");
numberOfAnimals[_animalType] += _count;
emit Added(_animalType, numberOfAnimals[_animalType]);
}

function animalCounts(animalType _animalType) public view returns (uint) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since solidity public mappings are automatically exposed as functions, you can just make animalCounts as a public mapping

return numberOfAnimals[_animalType];
}


function borrow (uint _age, gender _gender, animalType _animalType) public{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be marked external since it is not being called anywhere internally

//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;
}

}