/* Zhongtian Xia zhongtix PetShop.sol */ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.0; contract PetShop { //maps pet id number to address of pet owner mapping (uint=>address) public petOwners; //total number of pets in the PetShop uint public numPets; address public shopOwner; constructor() { numPets = 4; shopOwner = msg.sender; } modifier ownerOnly() { require( msg.sender == shopOwner ); _; } //petId must be in the valid range //pet must not already be adopted //a pet costs 1 ether //set pet owner in the mapping //hint: use assertions function adopt(uint petId) public payable { require(petId < numPets,"petId must be in the valid range"); require(petOwners[petId] == address(0), "pet must not already be adopted"); if (msg.sender.balance <= 1 ether) revert("Not enough Ether provided."); payable(msg.sender).transfer(1 ether); petOwners[petId] = msg.sender; } //returns the address of the pet owner function getOwner(uint petId) public view returns(address) { if(petId > numPets) return address(0x0); else return petOwners[petId]; } //add more pets to the petshop //only the owner is allowed to call this //hint: use a modifier function addPets(uint numAdditionalPets) public ownerOnly { numPets += numAdditionalPets; } }
0.4.18