Salon Appointment Scheduler - Build a Salon Appointment Scheduler

Tell us what’s happening:

I have done with shell script and sql. SQL create database and tables were able to capture however, most of my .sh command are not able to capture. I have run the script multiple times, but issues still persist. Please help.

Your code so far

#!/bin/bash

PSQL="psql --username=freecodecamp --dbname=salon --no-align --tuples-only -c"

#truncate table 
echo $($PSQL"TRUNCATE customers, appointments")

echo -e "\nWelcome to FCC salon\n"

# Create main menu 
MAIN_MENU() {
  echo -e "\nAvailable Services:"
  SERVICES=$($PSQL "SELECT service_id, name FROM services")
  echo "$SERVICES" | while IFS='|' read -r SERVICE_ID SERVICE_NAME; do
      echo "$SERVICE_ID) $SERVICE_NAME"
  done
}

MAKE_APPOINTMENT() {
  # Display main menu
  MAIN_MENU
  # Read input service id
  while true; do 
    echo -n "Enter the service number: "  # Print prompt without using -p
    read SERVICE_ID_SELECTED
    
    # Check if the input is a number
    if ! [[ $SERVICE_ID_SELECTED =~ ^[0-9]+$ ]]; then
      echo "Please enter a numeric number."
      MAIN_MENU
    else
      # Check if the selected service ID is valid
      VALID_SERVICE_ID=$($PSQL "SELECT service_id FROM services WHERE service_id = $SERVICE_ID_SELECTED;")
      
      # If the service ID is not valid
      if [[ -z $VALID_SERVICE_ID ]]; then
        echo "Please select a valid available service number."
        MAIN_MENU
      else
        # Get the service name for confirmation later
        SERVICE_NAME=$($PSQL "SELECT name FROM services WHERE service_id = $SERVICE_ID_SELECTED;")
        break  # Exit the loop if a valid service ID is selected
      fi
    fi
  done
}

CUSTOMER_INFO() {
  # Verify new or existing customer through phone number 
  echo -n "Please enter your phone number for appointment (format: 555-555-5555): "  # Print prompt without using -p
  read CUSTOMER_PHONE
  
  # Validate phone number format
  if ! [[ $CUSTOMER_PHONE =~ ^[0-9]{3}-[0-9]{3}-[0-9]{4}$ ]]; then
    echo "Please enter a valid phone number in the format 555-555-5555."
    CUSTOMER_INFO  # Call the function again to re-prompt
  else
    EX_CUST_NAME=$($PSQL "SELECT name FROM customers WHERE phone='$CUSTOMER_PHONE'")
    
    if [[ -z $EX_CUST_NAME ]]; then
      # It's a new customer, ask for name
      echo -e "\nWhat's your name?"
      read CUSTOMER_NAME
      INSERT_PHONE_RES=$($PSQL "INSERT INTO customers(name, phone) VALUES ('$CUSTOMER_NAME', '$CUSTOMER_PHONE')")
      echo -e "\nWelcome $CUSTOMER_NAME, let's get your appointment set!"
    else
      CUSTOMER_NAME=$EX_CUST_NAME  # Use the existing customer's name
      echo -e "\nWelcome back $CUSTOMER_NAME, let's get your appointment set!"
    fi
  fi
}

FINALIZE_APPOINTMENT() {
  echo -e "\nWhat time works best for you?"
  echo -n "Your appointment time is (HH:MM or HH AM/PM): "  # Print prompt without using -p
  read SERVICE_TIME

  CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$CUSTOMER_PHONE'")
  INSERT_APT_RES=$($PSQL "INSERT INTO appointments(time, customer_id, service_id) VALUES ('$SERVICE_TIME', '$CUSTOMER_ID', '$SERVICE_ID_SELECTED')")
    
    # Confirm the appointment
    if [[ $INSERT_APT_RES ]]; then
      echo -e "\nI have put you down for a $SERVICE_NAME at $SERVICE_TIME, $CUSTOMER_NAME."
    else
      echo -e "\nThere was an error scheduling your appointment. Please try again."
    fi
  
}

THANK_YOU(){
    while true; do  
    echo "Thank you for making an appointment with us! Do you need to make another appointment? (Y/N)"
    read CUST_RESP

    # Check for valid input
    if [[ $CUST_RESP =~ ^[Yy]$ ]]; then
      MAKE_APPOINTMENT
      break  # Exit the loop after making an appointment
    elif [[ $CUST_RESP =~ ^[Nn]$ ]]; then
      echo "We're looking forward to your visit!"
      break  # Exit the loop
    else
      echo "Please only key in Y/N for the response."
    fi
  done
}
# Start the customer information process
MAKE_APPOINTMENT
CUSTOMER_INFO
FINALIZE_APPOINTMENT
THANK_YOU

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36

Welcome to the forum @blu.chuay

So the forum can assist, please provide some information.

What error messages, if any, are you receiving?
What tests are you failing?

Happy coding

Test: https://www.freecodecamp.org/learn/relational-database/build-a-salon-appointment-scheduler-project/build-a-salon-appointment-scheduler

  • Issue: Task are not checked/ passed after running script.

I also have change my script, and it’s still doesnt work:

#!/bin/bash

PSQL="psql --username=freecodecamp --dbname=salon --no-align --tuples-only -c"


echo -e "\nWelcome to FCC salon\n"

# Create main menu 
MAIN_MENU() {
  echo -e "\nAvailable Services:"
  SERVICES=$($PSQL "SELECT service_id, name FROM services")
  echo "$SERVICES" | while IFS='|' read -r SERVICE_ID SERVICE_NAME; do
      echo "$SERVICE_ID) $SERVICE_NAME"
  done
}

MAKE_APPOINTMENT() {
  # Display main menu
  MAIN_MENU
  # Read input service id
  while true; do 
    echo -n "Enter the service number: "  # Print prompt without using -p
    read SERVICE_ID_SELECTED
    
    # Check if the input is a number
    if ! [[ $SERVICE_ID_SELECTED =~ ^[0-9]+$ ]]; then
      echo "Please enter a numeric number."
      MAIN_MENU
    else
      # Check if the selected service ID is valid
      VALID_SERVICE_ID=$($PSQL "SELECT service_id FROM services WHERE service_id = $SERVICE_ID_SELECTED;")
      
      # If the service ID is not valid
      if [[ -z $VALID_SERVICE_ID ]]; then
        echo "Please select a valid available service number."
        MAIN_MENU
      else
        # Get the service name for confirmation later
        SERVICE_NAME=$($PSQL "SELECT name FROM services WHERE service_id = $SERVICE_ID_SELECTED;")
        break  # Exit the loop if a valid service ID is selected
      fi
    fi
  done
}

CUSTOMER_INFO() {
  # Verify new or existing customer through phone number 
  echo -n "Please enter your phone number for appointment (format: 555-555-5555): "  # Print prompt without using -p
  read CUSTOMER_PHONE
  
  # Validate phone number format
  if ! [[ $CUSTOMER_PHONE =~ ^[0-9]{3}-[0-9]{3}-[0-9]{4}$ ]]; then
    echo "Please enter a valid phone number in the format 555-555-5555."
    CUSTOMER_INFO  # Call the function again to re-prompt
  else
    EX_CUST_NAME=$($PSQL "SELECT name FROM customers WHERE phone='$CUSTOMER_PHONE'")
    
    if [[ -z $EX_CUST_NAME ]]; then
      # It's a new customer, ask for name
      echo -e "\nWhat's your name?"
      read CUSTOMER_NAME
      INSERT_PHONE_RES=$($PSQL "INSERT INTO customers(name, phone) VALUES ('$CUSTOMER_NAME', '$CUSTOMER_PHONE')")
      echo -e "\nWelcome $CUSTOMER_NAME, let's get your appointment set!"
    else
      CUSTOMER_NAME=$EX_CUST_NAME  # Use the existing customer's name
      echo -e "\nWelcome back $CUSTOMER_NAME, let's get your appointment set!"
    fi
  fi
}

FINALIZE_APPOINTMENT() {
  echo -e "\nWhat time works best for you?"
  echo -n "Your appointment time is (HH:MM or HH AM/PM): "  # Print prompt without using -p
  read SERVICE_TIME

  CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$CUSTOMER_PHONE'")
  INSERT_APT_RES=$($PSQL "INSERT INTO appointments(time, customer_id, service_id) VALUES ('$SERVICE_TIME', '$CUSTOMER_ID', '$SERVICE_ID_SELECTED')")
    
    # Confirm the appointment
    if [[ $INSERT_APT_RES ]]; then
      echo -e "\nI have put you down for a $SERVICE_NAME at $SERVICE_TIME, $CUSTOMER_NAME."
    else
      echo -e "\nThere was an error scheduling your appointment. Please try again."
    fi
  
}

THANK_YOU(){
    while true; do  # Loop until a valid response is received
    echo "Thank you for making an appointment with us! Do you need to make another appointment? (Y/N)"
    read CUST_RESP

    # Check for valid input
    if [[ $CUST_RESP =~ ^[Yy]$ ]]; then
      MAKE_APPOINTMENT
      break  # Exit the loop after making an appointment
    elif [[ $CUST_RESP =~ ^[Nn]$ ]]; then
      echo "We're looking forward to your visit!"
      break  # Exit the loop
    else
      echo "Please only key in Y/N for the response."
    fi
  done
}
# Start the customer information process
MAKE_APPOINTMENT
CUSTOMER_INFO
FINALIZE_APPOINTMENT
THANK_YOU

Hi @blu.chuay

Please post a screenshot of the output when you run the script.

Happy coding

Output to complete the task:

Continue from the thread above

Continue from the thread above: Existing Client Fabio makes second appointment:

Hi @blu.chuay

The welcome prompt needs to look like the following:

~~~~~ MY SALON ~~~~~

Welcome to My Salon, how can I help you?

1) cut
2) color
3) perm
4) style
5) trim
  1. use the the title text from the example, include the ~ symbols
  2. the welcome message needs to match the text from the example
  3. match the options, casing, and the order of the options from the example
  4. make sure you the prompt message exactly matches the examples

Happy coding

Thanks @Teller for the solution.

1 Like