0% found this document useful (0 votes)
13 views5 pages

Code Ximport Json

The document defines a GuestTracker class to manage guest data for a hotel. It uses Guest and Reservation classes to store guest and reservation details, and provides methods to add, remove, find, sort guests and save/load data to a JSON file. Sample usage demonstrates tracking two guests, saving/loading data, finding guests by name/email, sorting by check-in date, and generating statistics.

Uploaded by

Harrison Adom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Code Ximport Json

The document defines a GuestTracker class to manage guest data for a hotel. It uses Guest and Reservation classes to store guest and reservation details, and provides methods to add, remove, find, sort guests and save/load data to a JSON file. Sample usage demonstrates tracking two guests, saving/loading data, finding guests by name/email, sorting by check-in date, and generating statistics.

Uploaded by

Harrison Adom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

import json

from datetime import datetime

class Guest:

def __init__(self, name, email, reservation):

self.name = name

self.email = email

self.reservation = reservation

def to_dict(self):

return {

'name': self.name,

'email': self.email,

'reservation': self.reservation.to_dict()

@classmethod

def from_dict(cls, guest_dict):

name = guest_dict['name']

email = guest_dict['email']

reservation_dict = guest_dict['reservation']

reservation = Reservation.from_dict(reservation_dict)

return cls(name, email, reservation)

class Reservation:

def __init__(self, room_type, check_in_date, check_out_date):

self.room_type = room_type

self.check_in_date = check_in_date

self.check_out_date = check_out_date
def to_dict(self):

return {

'room_type': self.room_type,

'check_in_date': self.check_in_date,

'check_out_date': self.check_out_date

@classmethod

def from_dict(cls, reservation_dict):

room_type = reservation_dict['room_type']

check_in_date = reservation_dict['check_in_date']

check_out_date = reservation_dict['check_out_date']

return cls(room_type, check_in_date, check_out_date)

class GuestTracker:

def __init__(self):

self.guests = []

def add_guest(self, guest):

self.guests.append(guest)

def remove_guest(self, guest):

self.guests.remove(guest)

def get_guest_by_email(self, email):

for guest in self.guests:

if guest.email == email:

return guest
return None

def search_guests_by_name(self, name):

result = []

for guest in self.guests:

if name.lower() in guest.name.lower():

result.append(guest)

return result

def sort_guests_by_check_in_date(self):

sorted_guests = sorted(self.guests, key=lambda guest:


datetime.strptime(guest.reservation.check_in_date, '%Y-%m-%d'))

return sorted_guests

def generate_guest_statistics(self):

total_guests = len(self.guests)

room_types = {}

for guest in self.guests:

room_type = guest.reservation.room_type

if room_type in room_types:

room_types[room_type] += 1

else:

room_types[room_type] = 1

return total_guests, room_types

def save_guests_to_file(self, filename):

data = []

for guest in self.guests:

data.append(guest.to_dict())
with open(filename, 'w') as file:

json.dump(data, file)

def load_guests_from_file(self, filename):

with open(filename, 'r') as file:

data = json.load(file)

self.guests = []

for guest_dict in data:

guest = Guest.from_dict(guest_dict)

self.add_guest(guest)

# Example usage

reservation1 = Reservation('Standard', '2023-07-10', '2023-07-15')

guest1 = Guest('Harrison Adom', '[email protected]', reservation1)

reservation2 = Reservation('Deluxe', '2023-07-12', '2023-07-17')

guest2 = Guest('Robert Zigah', '[email protected]', reservation2)

tracker = GuestTracker()

tracker.add_guest(guest1)

tracker.add_guest(guest2)

# Save guests to file

tracker.save_guests_to_file('guests.json')

# Load guests from file

tracker.load_guests_from_file('guests.json')

# Get guest by email


guest = tracker.get_guest_by_email('[email protected]')

if guest:

print('Guest found:', guest.name)

else:

print('Guest not found')

# Search guests by name

search_results = tracker.search_guests_by_name('Zigah')

if search_results:

print('Guests found:')

for guest in search_results:

print(guest.name)

else:

print('No guests found')

# Sort guests by check-in date

sorted_guests = tracker.sort_guests_by_check_in_date()

print('Sorted guests by check-in date:')

for guest in sorted_guests:

print(guest.name, guest.reservation.check_in_date)

# Generate guest statistics

total_guests, room_types = tracker.generate_guest_statistics()

print('Total Guests:', total_guests)

print('Room Type Statistics:')

for room_type, count in room_types.items():

print(room_type, count)

You might also like