-
Notifications
You must be signed in to change notification settings - Fork 50
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,101 @@ pragma solidity ^0.8.0; | |
|
||
contract PetPark { | ||
|
||
} | ||
enum animalType {None, Fish, Cat, Dog, Rabbit, Parrot} | ||
enum gender {Male, Female} | ||
address immutable owner; | ||
|
||
struct member{ | ||
gender _gender; | ||
uint age; | ||
address memberAddress; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
return numberOfAnimals[_animalType]; | ||
} | ||
|
||
|
||
function borrow (uint _age, gender _gender, animalType _animalType) public{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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