-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject1_HotelRoom.sol
More file actions
70 lines (51 loc) · 2.71 KB
/
Project1_HotelRoom.sol
File metadata and controls
70 lines (51 loc) · 2.71 KB
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
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
// Showcase payment transfers, enums, modifiers & events
contract HotelRoom {
//hotelroom contract will be responsible for booking a hotel room and opening it.
// booking by ether crypto
address payable public owner; //address of a person who owns the hotel room!
//payable lets the address recive etherum cryptocurrency!
constructor() {
owner = payable(msg.sender);
currentStatus = Statuses.Vacant; //defult room status is vacent ! AVALIABLE!
}
function book() public payable onlyWhileVacant costs (2 ether){
// 1- pay the owner of the hotel room
//owner.transfer(msg.value); //msg. value amount of ether sent!
//transfer function sends crypto currency to an address (RECIPIENT : ROOM OWNER)
//3-check price
//require(msg.value >= 2 ether , "Not enough ether provided");
//4-check status ; to not book it twice if its occupied!
//require(currentStatus == Statuses.Vacant , "Currently Occupied!"); //when you put require function inside another function it will check whether it's true or false which means here whether it is occupied or not
//2-to update status of hotel room from vacent to occupied
//change the status to occupied!
currentStatus = Statuses.Occupied; //when you book a room the status of it will change into occupied directly!
//change this line! transfer have issues!
//owner.transfer(msg.value);
//into this : what does this mean??
(bool sent, bytes memory data) = owner.call{value: msg.value}("");
//to get the status of it
require(sent);
emit Occupy(msg.sender, msg.value);
}
// Vacant
//Occupied
enum Statuses { //enum is data structure which holds collection of things which is never gonna change!
Vacant,
Occupied
}// good to keep status of a hotel room!
Statuses public currentStatus; //to track current status!
//Modifiers!
modifier onlyWhileVacant() {
require(currentStatus == Statuses.Vacant, "Currently occupied.");
_;
}
modifier costs(uint256 _amount) {
require(msg.value >= _amount, "Not enough Ether provided.");
_;
}
//blockchain lets me emit an event everytime a function is called!
//emit an event anytime a booking happen! alert on my phone that the room is booked or i can see history of all events! (entire sets of events when the room was booked! )
event Occupy(address _occupant, uint256 _value); //tells the address of who booked the hotel room and the value they paid!!
}