import pandas as pd
import matplotlib.pyplot as plt
# Sample CSV file name
CSV_FILE = 'hotel_data.csv'
# Function to create the hotel data CSV file if it doesn't exist
def create_sample_data():
  data = {
    'Date': ['2024-12-01', '2024-12-01', '2024-12-02', '2024-12-02', '2024-12-03'],
    'Action': ['Check-in', 'Check-out', 'Check-in', 'Check-out', 'Check-in'],
    'Guest_Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva']}
  df = pd.DataFrame(data)
  df.to_csv(CSV_FILE, index=False)
# Function to read the data from the CSV file
def read_data():
  try:
    df = pd.read_csv(CSV_FILE)
  except FileNotFoundError:
    print("CSV file not found. Creating sample data ...")
    create_sample_data()
    df = pd.read_csv(CSV_FILE)
  return df
# Function to save data to CSV
def save_data(df):
  df.to_csv(CSV_FILE, index=False)
# Function to count check-ins and check-outs per day
def count_checkins_checkouts(df):
  # Group by 'Date' and 'Action' and count occurrences
  count_data = df.groupby(['Date', 'Action']).size().unstack(fill_value=0)
  return count_data
# Function to plot a bar graph of check-ins and check-outs per day
def plot_checkins_checkouts(df):
  count_data = count_checkins_checkouts(df)
  # Plot a bar graph
  ax = count_data.plot(kind='bar', figsize=(10, 6))
  ax.set_title('Check-ins and Check-outs per Day')
  ax.set_ylabel('Number of Guests')
  ax.set_xlabel('Date')
  ax.set_xticklabels(count_data.index, rotation=45)
  plt.tight_layout()
  plt.show()
# Function to add a check-in or check-out entry
def add_entry(df):
  print("\n--- Add Check-in or Check-out ---")
  action = input("Enter action (Check-in/Check-out): ").strip().lower()
  # Check if the action is valid
  if action not in ['check-in', 'check-out']:
       print("Invalid action. Please enter 'Check-in' or 'Check-out'.")
       return df
  date = input("Enter date (YYYY-MM-DD): ").strip()
  guest_name = input("Enter guest name: ").strip()
  # Create a new row with the entered data
  new_entry = pd.DataFrame({
       'Date': [date],
       'Action': [action.capitalize()],
       'Guest_Name': [guest_name]
  })
  # Append the new entry to the dataframe
  df =df._append(new_entry, ignore_index=True)
  # Save the updated data back to the CSV
  save_data(df)
  print(f"\n{guest_name} has successfully been added for {action} on {date}.")
  return df
# Main function to run the hotel management system
def main():
  # Read the data from the CSV
  df = read_data()
  while True:
    print("\n--- Hotel Management System ---")
    print("1. Add Check-in or Check-out")
    print("2. Show Data")
    print("3. Plot Check-ins and Check-outs")
    print("4. Exit")
    choice = input("Enter your choice (1/2/3/4): ").strip()
    if choice == '1':
      # Add check-in/check-out entry
      df = add_entry(df)
    elif choice == '2':
      # Display current hotel data
      print("\nCurrent Hotel Data:")
         print(df)
    elif choice == '3':
         # Plot the check-ins and check-outs per day
         plot_checkins_checkouts(df)
    elif choice == '4':
         # Exit the program
         print("Exiting the system. Goodbye!")
         break
    else:
         print("Invalid choice. Please enter a valid option.")
main()