-
Notifications
You must be signed in to change notification settings - Fork 0
/
RentABook.sol
154 lines (101 loc) · 3.45 KB
/
RentABook.sol
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
pragma solidity ^0.4.18;
contract RentABook {
address public owner;
address public student;
uint public rentalDate;
// uint public rentalHours;
uint public price;
bool public bookRented;
uint public returnDate;
uint public minRentalDays;
uint constant Min_Days = 1;
uint public maxRentalDays;
uint constant Max_Days = 10;
bool public available;
event UpdateRent(address indexed student, uint rentalDate, uint returnDate, uint price);
event rentBook(address indexed student, uint returnDate);
modifier ifOwner {
require(msg.sender == owner);
_;
}
modifier ifStudent {
require(msg.sender == student);
_;
}
modifier whenBookAvailable {
require(!bookRented || now > returnDate);
_;
}
modifier whenBookIsRented {
require(bookRented && now <= returnDate);
_;
}
modifier IsAvailable {
require(available);
_;
}
function RentABook() public {
owner = msg.sender;
}
function rentableSetup(uint _pricePerDay, uint _minRentalDays, uint _maxRentalDays)
public ifOwner {
require(!available);
require(_minRentalDays >= Min_Days && _maxRentalDays <= Max_Days &&
_minRentalDays <= _maxRentalDays);
available = true;
minRentalDays =_minRentalDays;
maxRentalDays = _maxRentalDays;
setRentalPerDay(_pricePerDay);
}
function setBookAvailable(bool _available) public ifOwner {
available = _available;
}
function setRentalPerDay(uint _priceCost) public ifOwner whenBookAvailable {
require(_priceCost > 0);
price = _priceCost;
}
function setRentalPerhour(uint _priceCost) public ifOwner whenBookAvailable {
require(_priceCost > 0);
price = _priceCost;
}
function rent() public payable IsAvailable whenBookAvailable {
require(msg.value > 0);
require(price > 0);
uint rentForNumberOfdays = msg.value/ price;
require(rentForNumberOfdays >= minRentalDays && rentForNumberOfdays <= maxRentalDays);
returnDate = now + rentForNumberOfdays;
bookRented = true;
student = msg.sender;
rentalDate = now;
UpdateRent(student, rentalDate, returnDate,price);
}
function remainBalanceDays() public view whenBookIsRented returns(uint) {
return (returnDate - now);
}
function rentalTotalDays() public view whenBookIsRented returns(uint) {
return (returnDate - rentalDate);
}
function rentalElapsedTime() public view whenBookIsRented returns(uint) {
return (now - rentalDate);
}
function rentalTotalTime() public view whenBookIsRented returns (uint){
return (returnDate - rentalDate);
}
function returnBook() public ifStudent whenBookIsRented {
uint amountPaid = 0;
if(rentalElapsedTime() < Min_Days){
amountPaid = (rentalTotalTime() - Min_Days) * price ;
}else{
amountPaid = remainBalanceDays();
}
rentBook(student, now);
resetBookRent();
msg.sender.transfer(amountPaid);
}
function resetBookRent () private {
bookRented = false;
student = address(0);
rentalDate = 0;
returnDate = 0;
}
}