//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.18; contract LibrarySystem { struct Book { string author; string bookName; bool borrowed; } struct Borrower { string borrowerName; } struct Transaction{ uint bookId; uint borrowerId; address _; } mapping (uint => Book) public books; mapping (uint => Borrower) public borrowers; mapping (uint => Transaction) public transactions; uint private bookCounter; uint private borrowerCounter; uint private transactionCounter; address private creator; constructor() public { creator = msg.sender; bookCounter = 0; borrowerCounter = 0; transactionCounter = 0; } function addBook(string author, string bookName) public { bookCounter++; books[bookCounter] = Book(author, bookName, false); } function returnBook(uint index) public { require(books[index].borrowed == true); books[index].borrowed = false; } function borrowBook(uint _bookId, uint _borrowerId) public { require(books[_bookId].borrowed == false && bookCounter != 0 && compareStringsByBytes(books[_bookId].bookName, "") == false && compareStringsByBytes(borrowers[_borrowerId].borrowerName, "") == false); transactionCounter++; transactions[transactionCounter] = Transaction(_bookId,_borrowerId,creator); books[_bookId].borrowed = true; } function addBorrower(string name) public { borrowerCounter++; borrowers[borrowerCounter] = Borrower(name); } function isBookBorrowed(uint index) public view returns(bool){ require(compareStringsByBytes(books[index].bookName,"") == false); return books[index].borrowed; } function viewTransactionById(uint index) public view returns(string bookName, string borrowerName, address accountId){ string memory _bookName = books[transactions[index].bookId].bookName; string memory _borrowerName = borrowers[transactions[index].borrowerId].borrowerName; return (_bookName, _borrowerName, transactions[index]._); } function compareStringsByBytes(string string1, string string2) public pure returns(bool){ bytes memory _s1 = bytes(string1); bytes memory _s2 = bytes(string2); return keccak256(_s1) == keccak256(_s2); } }
0.4.25