0% found this document useful (0 votes)
33 views6 pages

Code Xi - Import Json

The document defines Guest, Reservation, and GuestTracker classes to manage guest and reservation data. The Guest class represents a guest with properties like ID, name, email, and reservations. The Reservation class represents a reservation with properties like ID, room type, check-in/out dates. The GuestTracker class manages a list of guests, and allows adding/removing guests and reservations, updating guest info, and loading/saving data from a JSON file. New methods were added to GuestTracker for adding/removing reservations and updating guest info.

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)
33 views6 pages

Code Xi - Import Json

The document defines Guest, Reservation, and GuestTracker classes to manage guest and reservation data. The Guest class represents a guest with properties like ID, name, email, and reservations. The Reservation class represents a reservation with properties like ID, room type, check-in/out dates. The GuestTracker class manages a list of guests, and allows adding/removing guests and reservations, updating guest info, and loading/saving data from a JSON file. New methods were added to GuestTracker for adding/removing reservations and updating guest info.

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/ 6

import json

class Guest:

def __init__(self, guest_id, name, email, reservations):

self.guest_id = guest_id

self.name = name

self.email = email

self.reservations = reservations

def add_reservation(self, reservation):

self.reservations.append(reservation)

def remove_reservation(self, reservation):

self.reservations.remove(reservation)

def to_dict(self):

return {

'guest_id': self.guest_id,

'name': self.name,

'email': self.email,

'reservations': [reservation.to_dict() for reservation in self.reservations]

@classmethod

def from_dict(cls, guest_dict):

guest_id = guest_dict['guest_id']

name = guest_dict['name']

email = guest_dict['email']
reservations = [Reservation.from_dict(reservation_dict) for reservation_dict in
guest_dict['reservations']]

return cls(guest_id, name, email, reservations)

class Reservation:

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

self.reservation_id = reservation_id

self.room_type = room_type

self.check_in_date = check_in_date

self.check_out_date = check_out_date

def to_dict(self):

return {

'reservation_id': self.reservation_id,

'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):

reservation_id = reservation_dict['reservation_id']

room_type = reservation_dict['room_type']

check_in_date = reservation_dict['check_in_date']

check_out_date = reservation_dict['check_out_date']

return cls(reservation_id, 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_id(self, guest_id):

for guest in self.guests:

if guest.guest_id == guest_id:

return guest

return None

def get_guest_by_email(self, email):

for guest in self.guests:

if guest.email == email:

return guest

return None

def add_reservation(self, guest_id, reservation):

guest = self.get_guest_by_id(guest_id)

if guest:

guest.add_reservation(reservation)

print("Reservation added successfully.")

else:

print("Guest not found.")

def remove_reservation(self, guest_id, reservation_id):


guest = self.get_guest_by_id(guest_id)

if guest:

reservation = None

for res in guest.reservations:

if res.reservation_id == reservation_id:

reservation = res

break

if reservation:

guest.remove_reservation(reservation)

print("Reservation removed successfully.")

else:

print("Reservation not found.")

else:

print("Guest not found.")

def update_guest_info(self, guest_id, name, email):

guest = self.get_guest_by_id(guest_id)

if guest:

guest.name = name

guest.email = email

print("Guest information updated successfully.")

else:

print("Guest not found.")

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

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

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

reservation2 = Reservation(2, 'Deluxe', '2023-07-20', '2023-07-25')

guest1.add_reservation(reservation1)

guest1.add_reservation(reservation2)

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

reservation3 = Reservation(3, 'Standard', '2023-07-12', '2023-07-17')

guest2.add_reservation(reservation3)

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')

# Add a new reservation for a guest

tracker.add_reservation(1, Reservation(4, 'Suite', '2023-08-01', '2023-08-05'))

# Remove a reservation for a guest

tracker.remove_reservation(1, 2)

# Update guest information

tracker.update_guest_info(2, 'Sumalia Issahaku ', '[email protected]')

# Retrieve guest information

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

if guest:

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

print('Reservations:')

for reservation in guest.reservations:

print('Reservation ID:', reservation.reservation_id)

print('Room Type:', reservation.room_type)

print('Check-in Date:', reservation.check_in_date)

print('Check-out Date:', reservation.check_out_date)

else:

print('Guest not found')

# we've added new methods to the GuestTracker class:

 add_reservation: Allows you to add a reservation for a guest given their guest ID.

 remove_reservation: Allows you to remove a reservation for a guest given their guest ID and
reservation ID.

 update_guest_info: Allows you to update the name and email of a guest given their guest ID.

You might also like