Salon Appointment Scheduler - Build a Salon Appointment Scheduler

Tell us what’s happening:

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

Welcome to the forum @ewie777

Are you asked to validate the format of phone numbers?

Doing anything extra may slow down the tests, and cause them to fail.

Happy coding

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.

My databases:

salon=> select * from services;
 service_id |      name      
------------+----------------
          1 | hair cut
          2 | hair dye
          3 | hair smoothing
(3 rows)

salon=> \d services
                                         Table "public.services"
   Column   |         Type          | Collation | Nullable |                   Default                    
------------+-----------------------+-----------+----------+----------------------------------------------
 service_id | integer               |           | not null | nextval('services_service_id_seq'::regclass)
 name       | character varying(30) |           |          | 
Indexes:
    "services_pkey" PRIMARY KEY, btree (service_id)
Referenced by:
    TABLE "appointments" CONSTRAINT "appointments_service_id_fkey" FOREIGN KEY (service_id) REFERENCES services(service_id)

salon=> select * from customers;
 customer_id |    phone     | name  
-------------+--------------+-------
           1 | 000-000-0000 | Jan
           2 | 644-564-5653 | Raul
           3 | 214-645-6544 | Kobus
           4 | 645-214-3223 | Piet
           5 | 655-555-4211 | Jakob
(5 rows)

salon=> \d customers
                                          Table "public.customers"
   Column    |         Type          | Collation | Nullable |                    Default                     
-------------+-----------------------+-----------+----------+------------------------------------------------
 customer_id | integer               |           | not null | nextval('customers_customer_id_seq'::regclass)
 phone       | character varying(12) |           |          | 
 name        | character varying(30) |           |          | 
Indexes:
    "customers_pkey" PRIMARY KEY, btree (customer_id)
    "customers_name_key" UNIQUE CONSTRAINT, btree (name)
    "customers_phone_key" UNIQUE CONSTRAINT, btree (phone)
Referenced by:
    TABLE "appointments" CONSTRAINT "appointments_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(customer_id)

salon=> select * from appointments;
 appointment_id | customer_id | service_id | time  
----------------+-------------+------------+-------
              1 |           4 |          1 | 12:18
              2 |           5 |          3 | 09:50
(2 rows)

salon=> \d appointments
                                             Table "public.appointments"
     Column     |         Type         | Collation | Nullable |                       Default                        
----------------+----------------------+-----------+----------+------------------------------------------------------
 appointment_id | integer              |           | not null | nextval('appointments_appointment_id_seq'::regclass)
 customer_id    | integer              |           |          | 
 service_id     | integer              |           |          | 
 time           | character varying(5) |           |          | 
Indexes:
    "appointments_pkey" PRIMARY KEY, btree (appointment_id)
Foreign-key constraints:
    "appointments_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
    "appointments_service_id_fkey" FOREIGN KEY (service_id) REFERENCES services(service_id)

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

But it didn’t fix the issue.

SOLUTION FOUND.

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

}

Thanks for the reply once again Teller.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.