I just cant figure out how to get Salon Appointment Scheduler task 18 to work, nl: If a phone number entered doesn’t exist, you should get the customers name and enter it, and the phone number, into the customers table.
My code does insert the data into the tables correctly on account creation. (please note that my code is not complete yet, because I code with the tasks, so I try to write only the necessary code to make each task run successfully.)
Ill change dye to color, sorry for my bad English.
Your code so far
#!/bin/bash
echo -e “\n~~~~~ MY SALON ~~~~~\n”
echo -e “Welcome to My Salon, how can I help you?\n”
PSQL=“psql --username=freecodecamp --dbname=salon -t -c”
# GLOBAL VAR DECLERATIONS
list_of_services=$($PSQL “SELECT * FROM services;”)
found_phone=“”
SEACH_ACCOUNT(){
# PROMPT ACCOUNT PHONE_NUMBER (USED AS UNIQUE IDENTIFIER)
echo -e “\nPlease provide account Phone number [Format: 000-000-0000]”
read CUSTOMER_PHONE
# VALIDATE NUMBER
if [[ ! $CUSTOMER_PHONE =~ ^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$ ]]
then
# IF INVALID, DO:
echo -e “\nERROR\nPHONE NUMBER INVALID\n”
else
# CHECK IF ACCOUNT EXIST
list_of_accounts=$($PSQL “SELECT * FROM customers;”)
found_phone=$(echo “$list_of_accounts” | while read -r ACCOUNT_ID BAR PHONE BAR NAME
do
if [[ $CUSTOMER_PHONE == $PHONE ]]
then
echo $PHONE
return 1
fi
done)
# CHECK IF ACCOUNT WAS FOUND
if [[ -n $found_phone ]]
then
FOUND_ACCOUNT
else
CREATE_ACCOUNT
fi
fi
}
FOUND_ACCOUNT(){
echo ACCOUNT FOUND
}
CREATE_ACCOUNT(){
# GET USER INFO
echo -e “\nNo matching account found.”
echo -e “\nWhat is your name sir?\n”
read CUSTOMER_NAME
$PSQL “INSERT INTO customers(phone, name) VALUES(‘$CUSTOMER_PHONE’,‘$CUSTOMER_NAME’);”
CUSTOMER_ID=$($PSQL “SELECT customer_id FROM customers WHERE phone = ‘$CUSTOMER_PHONE’;”)
echo -e “\nAt what time do you want the service to be done? [Format: HH:MM or HH AM or HH PM]\n”
read SERVICE_TIME
# VALIDATE Time
if [[ ! $SERVICE_TIME =~ ^([01]?[0-9]|2[0-3]):[0-5][0-9]$|^([1-9]|1[0-2])\ (AM|PM)$ ]]
then
# IF INVALID
echo -e “\nERROR\SERVICE TIME IS INVALID\n”
else
$PSQL “INSERT INTO appointments(customer_id, service_id, time) VALUES($CUSTOMER_ID, $SERVICE_ID_SELECTED, ‘$SERVICE_TIME’);”
fi
}
CUT(){
SEACH_ACCOUNT
}
DYE(){
SEACH_ACCOUNT
}
SMOOTHING(){
SEACH_ACCOUNT
}
MAIN_MENU(){
# GET AND HANDLE SERVICE SELECTED
read SERVICE_ID_SELECTED
case $SERVICE_ID_SELECTED in
CUT ;;
DYE ;;
SMOOTHING ;;
*) SHOW_LIST ;;
esac
}
SHOW_LIST(){
echo “$list_of_services” | while read -r SERVICE_ID BAR NAME #-r prevent \ interp
do
echo “$SERVICE_ID) $NAME”
done
MAIN_MENU
}
SHOW_LIST
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0
Challenge Information:
Salon Appointment Scheduler - Build a Salon Appointment Scheduler
Thanks for the reply. No they only ask me to add a customer to the customers table (name and phone number), but it isn’t a speed issue. Test runs in 1.07 seconds. When deleting the phone validation part, the test took 1 sec flat (timed it) and still got the same error. I also cant delete the code for getting the time of appointment, cause task 17 asks: Your script should prompt users to enter a service_id, phone number, a name if they aren’t already a customer, and a time. You should use read to read these inputs into variables named SERVICE_ID_SELECTED, CUSTOMER_PHONE, CUSTOMER_NAME, and SERVICE_TIME.
The error message: SUBTASKS 1.1 :18 You should enter a new customer into the database if they don’t exist.
I now also tried doing the account check a diff way
old way:
# CHECK IF ACCOUNT EXIST
list_of_accounts=$($PSQL "SELECT * FROM customers;")
found_phone=$(echo "$list_of_accounts" | while read -r ACCOUNT_ID BAR PHONE BAR NAME
do
if [[ $CUSTOMER_PHONE == $PHONE ]]
then
echo $PHONE
return 1
fi
done)
new way:
# CHECK IF ACCOUNT EXIST
found_phone=$($PSQL "SELECT * FROM customers WHERE phone = '$CUSTOMER_PHONE';")
What ever anyone do that may have the same issue, delete the if statement that check if phone is valid, use while loop.
SEACH_ACCOUNT(){
# PROMPT ACCOUNT PHONE_NUMBER (USED AS UNIQUE IDENTIFIER)
echo -e "\nPlease provide account Phone number [Format: 000-000-0000]"
read CUSTOMER_PHONE
# VALIDATE NUMBER
while [ ! $CUSTOMER_PHONE =~ ^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$ ]
do
echo -e "\nERROR\nPHONE NUMBER INVALID\n"
echo -e "\nPlease provide account Phone number [Format: 000-000-0000]"
read CUSTOMER_PHONE
done
# CHECK IF ACCOUNT EXIST
found_phone=$($PSQL "SELECT * FROM customers WHERE phone = '$CUSTOMER_PHONE';")
# CHECK IF ACCOUNT WAS FOUND
if [[ -n $found_phone ]]
then
FOUND_ACCOUNT
else
CREATE_ACCOUNT
fi
}