SimpleLibrarySystem.sol
pragma solidity ^0.4.25;
contract SimpleLibrarySystem {
// data needed
// 必要なデータです
uint totalNumberOfBooks;
uint totalNumberOfBorrowers;
uint currentNumberOfBorrowers;
struct bookDetails {
uint id;
string titleOfBook;
string authorOfBook;
bool isBorrowed;
}
struct borrowerDetails {
uint id;
uint bookID;
string borrowerName;
}
// initialize the contract
// スマートコントラクトを初期化します
constructor() public {
totalNumberOfBooks = 0;
totalNumberOfBorrowers = 0;
currentNumberOfBorrowers = 0;
}
mapping (uint => bookDetails) books;
mapping (uint => borrowerDetails) borrowers;
// function to add books
// 本を追加すること機能です
function addBooks(string title, string author) public {
totalNumberOfBooks+=1;
books[totalNumberOfBooks] = bookDetails(totalNumberOfBooks, title, author, false);
}
// function to borrow a book
// the book to be borrowed should be available
// 本を借りること機能です
// 借りる本は利用可能になるはずです
function borrowBook(uint bookID, string name) public {
require(books[bookID].isBorrowed == false);
require(currentNumberOfBorrowers < totalNumberOfBooks);
totalNumberOfBorrowers+=1;
currentNumberOfBorrowers+=1;
borrowers[totalNumberOfBorrowers] = borrowerDetails(totalNumberOfBorrowers, bookID, name);
books[bookID].isBorrowed = true;
}
// function to return a book
// you cannot return an unborrowed book
// 本を返すこと関数です
// 借りてない本を返すことはできません
function returnBook(uint bookID) public {
require(books[bookID].isBorrowed == true);
currentNumberOfBorrowers-=1;
books[bookID].isBorrowed = false;
}
// function to see the total number of books
// 書籍の総数を表示すること機能です
function getTotalNumberOfBooks() public constant returns (uint) {
return totalNumberOfBooks;
}
// function to see the total number of borrowers
// 借り手の総数を表示すること機能です
function getCurrentNumberOfBorrowers() public constant returns (uint) {
return currentNumberOfBorrowers;
}
}
こんにちは!
最初の2つの行では、私は使用するソリディティバージョンと契約名を初期化しました。次の部分は契約によって必要とされるデータです。このプログラムではと書籍と借り手と借用した本の数を把握する必要があります。 本の詳細と借り手の詳細の両方のデータ構造を作成する必要もあります。本の詳細にはかりてのブール値とタイトルと著者が含まれています。借り手の詳細には、bookIDと借り手の名前が含まれます。
// data needed
// 必要なデータです
uint totalNumberOfBooks;
uint totalNumberOfBorrowers;
uint currentNumberOfBorrowers;
struct bookDetails {
uint id;
string titleOfBook;
string authorOfBook;
bool isBorrowed;
}
struct borrowerDetails {
uint id;
uint bookID;
string borrowerName;
}
次の部分は、コンストラクタを作ることです。totalNumberOfBooksとtotalNumberOfBorrowersとcurrentNumberOfBorrowersを0に初期化します。本の構造と借り手の構造もマップします.
// initialize the contract
// スマートコントラクトを初期化します
constructor() public {
totalNumberOfBooks = 0;
totalNumberOfBorrowers = 0;
currentNumberOfBorrowers = 0;
}
mapping (uint => bookDetails) books;
mapping (uint => borrowerDetails) borrowers;
最後の部分はアクションです。 書籍を追加したりと書籍を借りたりと書籍を返却したりと書籍の総数を確認したりと現在の借り手の数を確認する機能があります。利用可能なできない本を借りることはできません. また、あなたは借りていない本を返すことはできませんでした。
// function to add books
// 本を追加すること機能です
function addBooks(string title, string author) public {
totalNumberOfBooks+=1;
books[totalNumberOfBooks] = bookDetails(totalNumberOfBooks, title, author, false);
}
// function to borrow a book
// the book to be borrowed should be available
// 本を借りること機能です
// 借りる本は利用可能になるはずです
function borrowBook(uint bookID, string name) public {
require(books[bookID].isBorrowed == false);
require(currentNumberOfBorrowers < totalNumberOfBooks);
totalNumberOfBorrowers+=1;
currentNumberOfBorrowers+=1;
borrowers[totalNumberOfBorrowers] = borrowerDetails(totalNumberOfBorrowers, bookID, name);
books[bookID].isBorrowed = true;
}
// function to return a book
// you cannot return an unborrowed book
// 本を返すこと関数です
// 借りてない本を返すことはできません
function returnBook(uint bookID) public {
require(books[bookID].isBorrowed == true);
currentNumberOfBorrowers-=1;
books[bookID].isBorrowed = false;
}
// function to see the total number of books
// 書籍の総数を表示すること機能です
function getTotalNumberOfBooks() public constant returns (uint) {
return totalNumberOfBooks;
}
// function to see the total number of borrowers
// 借り手の総数を表示すること機能です
function getCurrentNumberOfBorrowers() public constant returns (uint) {
return currentNumberOfBorrowers;
}