Using R script Write a program implement a simple Phone Book § The goal is to cr
ID: 3723461 • Letter: U
Question
Using R script Write a program implement a simple Phone Book § The goal is to create an end-user application that users can use to manage friend's data and contact information § You are the developer of the application, not the user There are many friends § Sometimes you make new friends § Sometimes you unfriend someone § For each friend we want to remember § Last name § First name § Phone number Implement the following functionality: § Print a friend's info § Print the whole telephone book § Insert a friend's info § Delete a friend's info § Look-up a friend's info § Use only vectors and simple data types (e.g. text, numeric)Explanation / Answer
Only vectors have been used to realise the problem.
GetName <- function(){
cat("Enter last name: ")
lname <- readLines("stdin", n = 1);
cat("Enter first name: ")
fname <- readLines("stdin", n = 1);
return <- paste(lname, fname)
}
Telephone_Print <- function() {
for (v in seq_along(names)) {
cat(names[v], " ", contacts[v], " ")
}
}
Friend_Info <- function(lname, fname) {
name <- GetName()
toprint <- which(names == name)
if(length(toprint) == 0)
cat(name, "not found", " ")
for (v in toprint) {
cat(names[v], " ", contacts[v], " ")
}
}
Friend_New <- function() {
name <- GetName()
cat("Enter contact info: ")
contact <- readLines("stdin", n = 1);
names <<- c(names, name)
contacts <<- c(contacts, contact)
}
Friend_Unfriend <- function(){
name <- GetName()
todel <- which(names == name)
if(length(todel) > 0){
names <<- names[-(todel)]
contacts <<- contacts[-(todel)]
}
}
names <- c()
contacts <- c()
destfile <- "data.rda"
if(file.exists(destfile)){
load(file = destfile)
}
while(TRUE){
writeLines(" #Menu 1. Add a friend 2. Delete a friend 3. Friend info 4. Print telephone book 5. Exit")
cat("Choose an action:")
action <- readLines("stdin", n = 1);
cat(" ")
if(action == 1){
Friend_New()
} else if(action == 2){
Friend_Unfriend()
} else if(action == 3){
Friend_Info()
} else if(action == 4){
Telephone_Print()
} else if(action == 5){
cat("Saving data Exitting ")
save(names, contacts, file = destfile)
quit()
} else{
print("Wrong choice, try again!")
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.