A cafeteria management system is a program designed to control and
monitor students’ access to meals in the university cafeteria.
This project uses R programming to simulate a cafeteria
system.
The system is designed to:
The first step is to define the digits that can be used to create student IDs.
In this project, only the digits:
are allowed.
# Define the allowed digits
digits <- 0:3
# Display the digits
digits
## [1] 0 1 2 3
This code creates a vector called digits containing:
0 1 2 3
These digits will be used to generate all possible student IDs.
Now we generate all possible student IDs using the four allowed digits.
Since each ID has 4 digits, and each position can take 0, 1, 2, or 3, we generate all combinations.
# Generate all possible 4-digit combinations
combinations <- expand.grid(
d1 = digits,
d2 = digits,
d3 = digits,
d4 = digits
)
# Display first few combinations
head(combinations)
## d1 d2 d3 d4
## 1 0 0 0 0
## 2 1 0 0 0
## 3 2 0 0 0
## 4 3 0 0 0
## 5 0 1 0 0
## 6 1 1 0 0
The function expand.grid() creates every possible
combination of the four digits.
For example:
and so on.
The combinations created above are still stored in separate
columns.
Now we combine each row into a single 4-digit student ID.
# Combine each row into one student ID
student_ids <- apply(combinations, 1, function(x) paste0(x, collapse = ""))
# Display first few student IDs
head(student_ids)
## [1] "0000" "1000" "2000" "3000" "0100" "1100"
The apply() function works row by row.
For each row, paste0(..., collapse = "") joins the four
digits into one string.
Example:
0 0 0 0 becomes "0000"0 0 0 1 becomes "0001"Now we sort the IDs so that they appear in a neat order.
# Sort student IDs
student_ids <- sort(student_ids)
# Display first few sorted IDs
head(student_ids)
## [1] "0000" "0001" "0002" "0003" "0010" "0011"
# Display last few sorted IDs
tail(student_ids)
## [1] "3322" "3323" "3330" "3331" "3332" "3333"
This arranges the student IDs in ascending order, from:
up to:
Now we count how many student IDs were generated.
# Count total number of student IDs
length(student_ids)
## [1] 256
Because:
Total number of IDs is:
\[ 4 \times 4 \times 4 \times 4 = 256 \]
So the system generates 256 student IDs.
Now we create the student database.
Each student will have:
The status will determine whether the student is allowed to eat in the cafeteria.
# Create the student database
students_db <- data.frame(
id = student_ids,
status = ifelse(grepl("^[02]", student_ids), "cafe", "non-cafe"),
stringsAsFactors = FALSE
)
# Display first few rows
head(students_db)
## id status
## 1 0000 cafe
## 2 0001 cafe
## 3 0002 cafe
## 4 0003 cafe
## 5 0010 cafe
## 6 0011 cafe
data.frame()This creates a table with two columns:
idstatusgrepl("^[02]", student_ids)This checks whether the student ID starts with:
02If yes → "cafe"
If no → "non-cafe"
Now we inspect some records to make sure the classification works correctly.
# Display first 20 student records
head(students_db, 20)
## id status
## 1 0000 cafe
## 2 0001 cafe
## 3 0002 cafe
## 4 0003 cafe
## 5 0010 cafe
## 6 0011 cafe
## 7 0012 cafe
## 8 0013 cafe
## 9 0020 cafe
## 10 0021 cafe
## 11 0022 cafe
## 12 0023 cafe
## 13 0030 cafe
## 14 0031 cafe
## 15 0032 cafe
## 16 0033 cafe
## 17 0100 cafe
## 18 0101 cafe
## 19 0102 cafe
## 20 0103 cafe
This helps us verify that the IDs are correctly classified.
For example:
0000 → cafe0001 → cafe1000 → non-cafe2000 → cafe3000 → non-cafeNow we create an empty attendance log.
This log will record students who have successfully eaten.
The attendance log contains:
# Initialize attendance log
attendance_log <- data.frame(
id = character(),
meal = character(),
timestamp = character(),
stringsAsFactors = FALSE
)
# Display attendance log
attendance_log
## [1] id meal timestamp
## <0 rows> (or 0-length row.names)
At the beginning, the attendance log is empty because no student has eaten yet.
Now we define the cafeteria meal schedule.
We create a function to check the time and return the correct meal.
# Function to determine meal based on time
case_when_custom <- function(tm) {
if (tm >= "06:30" && tm <= "08:30") return("Breakfast")
if (tm >= "11:00" && tm <= "13:00") return("Lunch")
if (tm >= "16:00" && tm <= "18:30") return("Dinner")
return("none")
}
This function takes a time (for example "07:00") and
returns:
"Breakfast""Lunch""Dinner""none" if the cafeteria is closed.Before building the full cafeteria system, we test whether the meal-time function works correctly.
# Test meal-time function
case_when_custom("07:00")
## [1] "Breakfast"
case_when_custom("12:00")
## [1] "Lunch"
case_when_custom("17:00")
## [1] "Dinner"
case_when_custom("14:00")
## [1] "none"
Expected results:
"07:00" → Breakfast"12:00" → Lunch"17:00" → Dinner"14:00" → noneNow we create the main function called
check_cafeteria().
This function performs all important checks.
check_cafeteria <- function(input_id, test_time = NULL) {
# Convert input into a 4-character ID
input_id <- sprintf("%04d", as.integer(input_id))
# Use current time or test time
current_time <- if (is.null(test_time)) format(Sys.time(), "%H:%M") else test_time
# Determine meal type
meal_type <- case_when_custom(current_time)
# 1. Check if cafeteria is open
if (meal_type == "none") {
return(sprintf("CLOSED: Current time is %s. Please return during meal hours.", current_time))
}
# 2. Check if student ID is registered
if (!(input_id %in% students_db$id)) {
return(sprintf("INVALID: ID %s is not registered.", input_id))
}
# 3. Check student status
student_status <- students_db$status[students_db$id == input_id]
if (student_status == "non-cafe") {
return(sprintf("DENIED: ID %s is a Non-cafe student.", input_id))
}
# 4. Check repeated meal access
already_eaten <- any(attendance_log$id == input_id & attendance_log$meal == meal_type)
if (already_eaten) {
return(sprintf("DENIED: ID %s already you have eaten %s today.", input_id, meal_type))
}
# 5. Record successful meal access
new_entry <- data.frame(
id = input_id,
meal = meal_type,
timestamp = current_time,
stringsAsFactors = FALSE
)
attendance_log <<- rbind(attendance_log, new_entry)
return(sprintf("SUCCESS: Welcome ID %s! Enjoy your %s.", input_id, meal_type))
}
Now let us explain the logic of the main cafeteria function step by step.
sprintf("%04s", as.character("12"))
## [1] " 12"
This ensures the ID is always treated as a 4-digit string.
For example:
12 becomes " 12" in %04s, so
for numeric-like IDs it’s better to pass them already as
"0012" or use a stronger fix later if needed.In our testing, we use full 4-digit IDs like:
"0000""2201"So it works correctly.
The function checks whether the given time falls under:
If the cafeteria is closed, access is denied.
The function checks whether the ID exists in the student database.
If the ID is not found, the system returns:
"INVALID"
Even if the ID exists, the student may still be a non-cafeteria student.
If the student is classified as "non-cafe", access is
denied.
A student is allowed to eat only once per meal.
So if a student already ate breakfast, they cannot eat breakfast again.
This is checked using:
any(attendance_log$id == input_id & attendance_log$meal == meal_type)
If all checks pass:
Now we test the program using different situations.
print(check_cafeteria("0000", "07:00"))
## [1] "SUCCESS: Welcome ID 0000! Enjoy your Breakfast."
"0000" exists0 → cafeteria student"07:00" → BreakfastSUCCESS: Welcome ID 0000! Enjoy your Breakfast.
print(check_cafeteria("0000", "07:30"))
## [1] "DENIED: ID 0000 already you have eaten Breakfast today."
The same student "0000" tries to eat Breakfast
again.
DENIED: ID 0000 already you have eaten Breakfast today.
print(check_cafeteria("1000", "12:00"))
## [1] "DENIED: ID 1000 is a Non-cafe student."
"1000" exists1 → non-cafe studentDENIED: ID 1000 is a Non-cafe student.
print(check_cafeteria("9999", "12:00"))
## [1] "INVALID: ID 9999 is not registered."
ID "9999" does not exist in the generated database.
INVALID: ID 9999 is not registered.
print(check_cafeteria("0112", "14:35"))
## [1] "CLOSED: Current time is 14:35. Please return during meal hours."
"14:35" is outside:
CLOSED: Current time is 14:35. Please return during meal hours.
print(check_cafeteria("2201", "17:35"))
## [1] "SUCCESS: Welcome ID 2201! Enjoy your Dinner."
"2201" exists2 → cafeteria student"17:35" → DinnerSUCCESS: Welcome ID 2201! Enjoy your Dinner.
print(check_cafeteria("2201", "17:59"))
## [1] "DENIED: ID 2201 already you have eaten Dinner today."
The same student tries to eat Dinner again.
DENIED: ID 2201 already you have eaten Dinner today.
Now we run all test cases together.
print(check_cafeteria("0000", "07:00")) # Valid Breakfast Access
## [1] "DENIED: ID 0000 already you have eaten Breakfast today."
print(check_cafeteria("0000", "07:30")) # Double Entry Attempt
## [1] "DENIED: ID 0000 already you have eaten Breakfast today."
print(check_cafeteria("1000", "12:00")) # Non-cafe Student
## [1] "DENIED: ID 1000 is a Non-cafe student."
print(check_cafeteria("9999", "12:00")) # Invalid ID
## [1] "INVALID: ID 9999 is not registered."
print(check_cafeteria("0112", "14:35")) # Closed Hours Attempt
## [1] "CLOSED: Current time is 14:35. Please return during meal hours."
print(check_cafeteria("2201", "17:35")) # Dinner Access
## [1] "DENIED: ID 2201 already you have eaten Dinner today."
print(check_cafeteria("2201", "17:59")) # Double Dinner Attempt
## [1] "DENIED: ID 2201 already you have eaten Dinner today."
Now we display the attendance log after all successful entries.
attendance_log
## id meal timestamp
## 1 0000 Breakfast 07:00
## 2 2201 Dinner 17:35
This log should contain only students who were successfully allowed to eat.
For example:
"0000" at Breakfast"2201" at DinnerOnly successful entries are stored.
This program works in the following order:
This system helps in:
This version still has some limitations:
This project can be improved in the future by adding:
In conclusion, this Cafeteria Management System is a useful simulation program written in R.
The system successfully performs:
This makes the system suitable as a beginner-level university cafeteria management project.
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.