Salon Appointment Scheduler - Build a Salon Appointment Scheduler - stuck in "display a numbered list of the services"

Tell us what’s happening:
In VM (CodeAlly), so far my progress stucked in these 2 steps, although my salon.sh output is already same with example.txt:
" You should display a numbered list of the services you offer before the first prompt for input, each with the format #) <service>. For example, 1) cut, where 1 is the service_id"

“If you pick a service that doesn’t exist, you should be shown the same list of services again”

I’ve been doing this course both in browser/VM (CodeAlly) and locally (Docker & VSCode). When I run & test the same code locally, it passed completely.

So I was wondering, is there any problem with my code, or with the test itself?

Here’s my salon.sql to recreate the database:
https://raw.githubusercontent.com/jenniferlorenza0307/fcc-RDC-build-salon-appointment-scheduler/main/salon.sql

Your code so far

#! /bin/bash

# initialization, define function
PSQL="psql --username=freecodecamp --dbname=salon --tuples-only -c"

MAIN_MENU () {
  # Print 1st argument if given
  if [[ $1 ]]; then
    echo -e "\n$1"
  fi
  # Show services list
  SERVICES_LIST=$($PSQL "SELECT service_id, name FROM services ORDER BY service_id;")
  echo "$SERVICES_LIST" | while read SID BAR SERVICE_NAME
  do
    echo "$SID) $SERVICE_NAME"
  done

  # go to select_service
  SELECT_SERVICE
}

SELECT_SERVICE () {
  # get service id from user
  read SERVICE_ID_SELECTED

  # check if service_id_selected is valid
  if [[ ! $SERVICE_ID_SELECTED =~ ^[0-9]+$ ]]; then
    # if service_id_selected is not a number, then show list again
    MAIN_MENU "I could not find that service. What would you like today?"
  else  
    # check if service_id_selected exist
    SERVICE_NAME=$($PSQL "SELECT name FROM services WHERE service_id='$SERVICE_ID_SELECTED'" | sed -E 's/^ //g')
    # echo "$SERVICE_NAME"

    if [[ -z $SERVICE_NAME ]]; then
      # if service_id_selected is a number but outside of list, then show list again
      MAIN_MENU "I could not find that service. What would you like today?"
    else :
      # break from loop
    fi
  fi
}

INSERT_NEW_CUSTOMER () {
  # query to insert
  INSERT_CUST_RESULT=$($PSQL "INSERT INTO customers(phone, name) VALUES('$CUSTOMER_PHONE', '$CUSTOMER_NAME')")
}

CREATE_APPOINTMENT () {
  # get customer_id
  CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$CUSTOMER_PHONE'" | sed -E 's/^ +| +$//g')
  
  # insert to appointments table
  INSERT_APT_RESULT=$($PSQL "INSERT INTO appointments(customer_id, service_id, time) VALUES($CUSTOMER_ID, $SERVICE_ID_SELECTED, '$SERVICE_TIME')")

  # print message
  echo -e "\nI have put you down for a $SERVICE_NAME at $SERVICE_TIME, $CUSTOMER_NAME.\n"
}

# ------------------- Main Program Starts Here
echo -e "\n~~~~~ MY SALON ~~~~~\n"
echo -e "Welcome to My Salon, how can I help you?\n"
MAIN_MENU

# get customer's phone
echo -e "\nWhat's your phone number?"
read CUSTOMER_PHONE

# query for customer name by phone
CUSTOMER_NAME=$($PSQL "SELECT name FROM customers WHERE phone='$CUSTOMER_PHONE'" | sed -E 's/^ +| +$//g')

if [[ -z $CUSTOMER_NAME ]]; then
  # if not exist in customers table, then ask for new customer name
  echo -e "\nI don't have a record for that phone number, what's your name?"
  read CUSTOMER_NAME

  INSERT_NEW_CUSTOMER
fi

# ask for service time
echo -e "\nWhat time would you like your $SERVICE_NAME, $CUSTOMER_NAME?"
read SERVICE_TIME

CREATE_APPOINTMENT

Your browser information:

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

Challenge: Salon Appointment Scheduler - Build a Salon Appointment Scheduler

Link to the challenge:

There is a problem with the tests on this particular course I think. I had the same issue recently but eventually, after I’d spammed the ‘Run’ button many times, all of the tests suddenly passed and I was able to complete the project.

I spoilered your code, only because it’s likely a full solution to the project.

Thanks for your reply!

Just now I tried to clear my browser history, restart the VM and re-run the tests, and it suddenly passed! :smile:

How much time you click on Run button? I’ve done it several times but it doesn’t do anything to the result :sweat_smile:

Here are my code for this challenge:

#!/bin/bash

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

echo -e "\n~~~~~ MY SALON ~~~~~"

MAIN_MENU() {
  if [[ $1 ]]
  then
    echo -e "\n$1"
  else 
    echo "Welcome to My Salon, how can I help you?"
  fi
  
  SERVICE_LIST=$($PSQL "select service_id, name from services")
  echo "$SERVICE_LIST" | while read SERVICE_ID BAR SERVICE_NAME
  do
    echo "$SERVICE_ID) $SERVICE_NAME"
  done
  
  read SERVICE_ID_SELECTED 
  

  case $SERVICE_ID_SELECTED in
    1) APPOINTMENT_MENU ;;
    2) APPOINTMENT_MENU ;;
    3) APPOINTMENT_MENU ;;
    4) APPOINTMENT_MENU ;;
    5) APPOINTMENT_MENU ;;
    *) MAIN_MENU "I could not find that service. What would you like today?" ;;

  esac
}

APPOINTMENT_MENU() {
  # get service name
  SERVICE_NAME=$($PSQL "select name from services where service_id = $SERVICE_ID_SELECTED") 
  # get customer info
  echo -e "\nWhat's your phone number?"
  read CUSTOMER_PHONE

  CUSTOMER_NAME=$($PSQL "SELECT name FROM customers WHERE phone = '$CUSTOMER_PHONE'")
  
  # if customer doesn't exist
  if [[ -z $CUSTOMER_NAME ]]
  then
    # get new customer name
    echo -e "\nI don't have a record for that phone number, What's your name?"
    read CUSTOMER_NAME

    # insert new customer
    INSERT_CUSTOMER_RESULT=$($PSQL "INSERT INTO customers(name, phone) VALUES('$CUSTOMER_NAME', '$CUSTOMER_PHONE')") 
  fi

  # get customer_id
  CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$CUSTOMER_PHONE'")

  echo -e "\nWhat time would you like your $(echo $SERVICE_NAME | sed -r 's/^ *| *$//g'), $(echo $CUSTOMER_NAME | sed -r 's/^ *| *$//g')?"
  read SERVICE_TIME

  # insert bike rental
  INSERT_SERVICE_RESULT=$($PSQL "INSERT INTO appointments(customer_id, service_id, time) VALUES($CUSTOMER_ID, $SERVICE_ID_SELECTED, '$SERVICE_TIME')")

  
  # send to main menu
  echo -e "\nI have put you down for a $(echo $SERVICE_NAME | sed -r 's/^ *| *$//g') at $(echo $SERVICE_TIME | sed -r 's/^ *| *$//g'), $(echo $CUSTOMER_NAME | sed -r 's/^ *| *$//g')."

}

MAIN_MENU

Even after I tried using the same code from @jenniferlorenza0307 for this challenge, still I’m not getting anywhere

2 Likes

i don’t know if this helps anyone, but displaying the menu using sed like this did NOT pass the test:

echo “$AVAILABLE_SERVICES” | while read ID NAME
do
echo “$ID) $NAME” | sed -e ‘s/|//’
done

although it displays the correct output. Changing to this:

echo “$AVAILABLE_SERVICES” | while read ID BAR NAME
do
echo “$ID) $NAME”
done
DID pass. In this case the bar variable ‘sucks up’ the | instead of filtering it out.
Maybe helps someone.