-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibrary.sol
More file actions
31 lines (26 loc) · 877 Bytes
/
Copy pathLibrary.sol
File metadata and controls
31 lines (26 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract Library{
struct Book{
string title;
bool isAvailable;
}
Book[] public books;
function addBook(string memory title) public{
books.push(Book(title, true));
}
function borrowBook(uint index) public{
require(index < books.length, "Invalid book index");
require(books[index].isAvailable, "Book is not available");
books[index].isAvailable = false;
}
function returnBook(uint index) public{
require(index < books.length, "Invalid book index");
books[index].isAvailable = true;
}
function getBook(uint index) public view returns (string memory, bool){
require(index < books.length, "Invalid book index");
Book memory book = books[index];
return (book.title, book.isAvailable);
}
}