# Create 20 IDs using only digits 0,1,2,3
ids <- replicate(20, paste(sample(0:3, 5, replace = TRUE), collapse = ""))
ids
## [1] "12310" "00202" "23112" "11201" "02130" "22200" "20001" "11231" "23003"
## [10] "33013" "32211" "30002" "03023" "22333" "01231" "01132" "00223" "01000"
## [19] "23003" "00213"
#the 20 created ids by one run----
# 1. IDs and Data Setup
createdids <- c(
"13213", "11111", "33013", "00221", "20212","23312", "30122", "31202", "02330", "13320",
"03222", "02000", "01311", "01312", "32010","02031", "22003", "03300", "33303", "01010")
#Here the code below
food_items <- c("Pasta", "Rice", "Macaroni", "Egg","Shiro", "Misir Wot", "Vegetable", "Firfir")
drink_items <- c("Coffee", "Tea", "Water", "Milk","Juice", "Spris", "Mineral Water", "Soft Drink")
tracking <- data.frame(
ID = createdids,
meals_taken = 0,
stringsAsFactors = FALSE
)
# 2. Time Logic
is_allowed_time <- function(time_hour) {
# Breakfast: 1-2, Lunch: 6-7, Dinner: 12-13.5
return((time_hour >= 1 && time_hour <= 2) ||
(time_hour >= 6 && time_hour <= 7) ||
(time_hour >= 12 && time_hour <= 13.5))
}
# 3. Serve Customer Function
serve_customer <- function(id, time_hour, food, drink, tracking_df) {
cat("====================================\n")
cat("Checking ID:", id, "\n")
row_index <- which(tracking_df$ID == id)
if (length(row_index) == 0) {
cat("ID not found\n\n")
return(tracking_df)
}
if (tracking_df$meals_taken[row_index] >= 3) {
cat("Not allowed: Meal limit reached (3 per day)\n\n")
return(tracking_df)
}
if (!is_allowed_time(time_hour)) {
cat("Not allowed: Outside serving time\n\n")
return(tracking_df)
}
if (!(food %in% food_items)) {
cat("Invalid food selection:", food, "\n\n")
return(tracking_df)
}
if (!(drink %in% drink_items)) {
cat("Invalid drink selection:", drink, "\n\n")
return(tracking_df)
}
# Success: Update and Notify
tracking_df$meals_taken[row_index] <- tracking_df$meals_taken[row_index] + 1
cat("Allowed\n")
cat("Food:", food, "| Drink:", drink, "\n")
cat("Meals used today:", tracking_df$meals_taken[row_index], "\n\n")
return(tracking_df)
}
tracking <- serve_customer("13213", 1.5, "Pasta", "Coffee", tracking) # Valid
## ====================================
## Checking ID: 13213
## Allowed
## Food: Pasta | Drink: Coffee
## Meals used today: 1
tracking <- serve_customer("33013", 4, "Egg", "Tea", tracking) # Invalid Time
## ====================================
## Checking ID: 33013
## Not allowed: Outside serving time
tracking <- serve_customer("20212", 6.5, "Burger", "Milk", tracking) # Invalid Food
## ====================================
## Checking ID: 20212
## Invalid food selection: Burger
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: