-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hotel.sol
149 lines (117 loc) · 4.73 KB
/
Hotel.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract CrypTrip {
uint private hotelsRegistered;
uint private travelersRegistered;
/// Rooms with 2 Person are all full.
error Rooms2Full();
/// Rooms with 3 Person are all full.
error Rooms3Full();
/// Rooms with 5 Person are all full.
error Rooms5Full();
struct Traveler {
uint hotelsBooked;
uint level;
}
struct Hotel {
uint Id;
address payable ownerAddr;
string name;
string addr;
uint pincode;
string city;
uint contactNumber;
uint roomsTwo;
uint roomsThree;
uint roomsFive;
uint roomsTwoPrice;
uint roomsThreePrice;
uint roomsFivePrice;
}
struct Booking {
uint hotelId;
uint checkIn;
uint checkOut;
uint roomsTwoCount;
uint roomsThreeCount;
uint roomsFiveCount;
uint totalPrice;
uint contactNumber;
}
struct BookedAlready {
uint roomsTwoCount;
uint roomsThreeCount;
uint roomsFiveCount;
}
struct ID {
uint id;
}
mapping(address => ID[]) public myHotels;
mapping(uint => mapping(uint => BookedAlready)) roomsAvailable;
mapping(address => Booking) booking;
mapping(uint => Hotel) public hotel;
mapping(string => ID[]) public locations;
mapping(address => Traveler) public traveler;
function createHotel(
string memory _hotelName,
string memory _hotelAddr,
uint _hotelPincode,
string memory _hotelCity,
uint _contactNumber,
uint _roomsTwo,
uint _roomsThree,
uint _roomsFive,
uint _roomsTwoPrice,
uint _roomsThreePrice,
uint _roomsFivePrice
) public {
hotelsRegistered += 1;
hotel[hotelsRegistered] = Hotel(hotelsRegistered, payable(msg.sender), _hotelName, _hotelAddr, _hotelPincode, _hotelCity, _contactNumber, _roomsTwo, _roomsThree, _roomsFive, _roomsTwoPrice, _roomsThreePrice, _roomsFivePrice);
myHotels[msg.sender].push(ID(hotel[hotelsRegistered].Id));
locations[hotel[hotelsRegistered].city].push(ID(hotel[hotelsRegistered].Id));
}
function search(string memory _location) public view returns (ID[] memory) {
ID[] memory abc = locations[_location];
return abc;
}
function checkAvailability(uint _hotelId, uint _checkIn) public view returns(uint rooms2, uint rooms3, uint rooms5){
rooms2 = roomsAvailable[_hotelId][_checkIn].roomsTwoCount;
rooms3 = roomsAvailable[_hotelId][_checkIn].roomsThreeCount;
rooms5 = roomsAvailable[_hotelId][_checkIn].roomsFiveCount;
return (rooms2, rooms3, rooms5);
}
function updateAvailability(uint _hotelId, uint _checkIn, uint a, uint b, uint c) private {
roomsAvailable[_hotelId][_checkIn].roomsTwoCount = a;
roomsAvailable[_hotelId][_checkIn].roomsThreeCount = b;
roomsAvailable[_hotelId][_checkIn].roomsFiveCount = c;
}
function bookHotel (uint _hotelId, uint _checkIn, uint _checkOut, uint _roomsTwoCount, uint _roomsThreeCount, uint _roomsFiveCount, uint _contactNumber) public payable {
require(_checkIn > block.timestamp && _checkOut > _checkIn);
(uint a, uint b, uint c) = checkAvailability(_hotelId, _checkIn);
if(a == hotel[_hotelId].roomsTwo && _roomsTwoCount > 0)
revert Rooms2Full();
if(b == hotel[_hotelId].roomsThree && _roomsThreeCount > 0)
revert Rooms3Full();
if(c == hotel[_hotelId].roomsFive && _roomsFiveCount > 0)
revert Rooms5Full();
if(a + _roomsTwoCount > hotel[_hotelId].roomsTwo)
revert Rooms2Full();
a = a + _roomsTwoCount;
if(b + _roomsThreeCount > hotel[_hotelId].roomsThree)
revert Rooms3Full();
b = b + _roomsThreeCount;
if(c + _roomsFiveCount > hotel[_hotelId].roomsFive)
revert Rooms5Full();
c = c + _roomsFiveCount;
uint roomsTwoPrice = hotel[_hotelId].roomsTwoPrice;
uint roomsThreePrice = hotel[_hotelId].roomsThreePrice;
uint roomsFivePrice = hotel[_hotelId].roomsFivePrice;
uint totalPrice = (_roomsTwoCount * roomsTwoPrice) + (_roomsThreeCount * roomsThreePrice) + (_roomsFiveCount * roomsFivePrice);
require(msg.value == totalPrice);
booking[msg.sender] = Booking(_hotelId, _checkIn, _checkOut, _roomsTwoCount, _roomsThreeCount, _roomsFiveCount, totalPrice, _contactNumber);
updateAvailability(_hotelId, _checkIn, a, b, c);
hotel[_hotelId].ownerAddr.transfer(msg.value);
traveler[msg.sender].hotelsBooked++;
traveler[msg.sender].level++;
}
}