Студопедия

Главная страница Случайная страница

Разделы сайта

АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника






Recipe Database






Here is a recipe database app that allows you to create, edit, search, store, retrieve, and view recipes. You can use this program as an outline for creating apps that store any sort of related information. You'll use the techniques of list management, file management, menu selection, text editing, serialization, subroutines, and others, often in your RFO Basic programs, so study this example carefully:

! First, check to see if the data file exists: file.exists b, " recipes.txt"! If the file doesn't exist, create it: if b=0! Create a new list structure, then add some default recipe titles,! ingredients, and instructions. Notice the use of the tilde character! (~) to extend the list contents across multiple lines. The current! state of the list.add and array.load functions do not allow string! concatenation using the tilde character within the function parameters.! In order to add concatenated strings to a list, save the concatenated! strings to a variable before executing the list.add function (see! https://rfobasic.freeforums.org/post6133.html? hilit=#p6133 for a! discussion of this topic): longstring$ = " Remove entree from " + ~ " packaging\nHeat in microwave\nCool and eat with fork" list.create s, newrecipes list.add newrecipes, " Oatmeal", " Oatmeal\nWater\nMaple Syrup" ~ " Boil water\nAdd oatmeal\nAdd maple syrup\nCool and eat" ~ " Frozen Dinner", " Frozen Entree\nFork", longstring$! Serialize the data into a single string, as shown earlier in the! tutorial. The serialized data is concatenated using the string! separater " ###", and saved in the variable savedata$: list.size newrecipes, size savedata$ = " " for i = 1 to size list.get newrecipes, i, m$ if i = size savedata$ = savedata$ + m$ else savedata$ = savedata$ + m$ + " ###" endif next i! Save the serialized data to the " recipes.txt" text file, then close the! if conditional structure: text.open w, myfile, " recipes.txt" text.writeln myfile, savedata$ text.close myfileendif! Load the saved data file (the new one, if just created above, or an old! one, if previously saved by a user): loadSavedRecipes:! Read the serialized text string: grabfile serialdata$, " recipes.txt"! Create a blank array: array.delete recipe$[]! Split the string at the " ###" characters: split recipe$[], serialdata$, " ###"! Convert the array into a list structure: list.create s, recipes list.add.array recipes, recipe$[]! Create a separate list containing just the recipe titles (every! third item in the list): list.size recipes, sizelist.create s, titlesfor i = 1 to size step 3 list.get recipes, i, n$ list.add titles, n$next i! Display the title, ingredients and instructions for one recipe selected! by the user: viewRecipe:! Add an option to the recipe titles list to create a new recipe! (only if that option hasn't already been added): list.search titles, " Create New Recipe...", cif c = 0 list.add titles, " Create New Recipe..." endif! Allow the user to select a recipe from the titles list: select indx, titles, " Select a recipe to view/edit (hold to quit)", d! If the user long-clicks, end the program: if d = 1 then end! If the user chooses to create a new recipe, jump to the appropriate! subroutine: list.get titles, indx, rcp$if rcp$ = " Create New Recipe..." goto createNewRecipe endif! Otherwise, clear the screen and begin the recipe display routine: cls! Pay particular attention to this line. Remember that every 3 items! in the recipe list structure are: title/ingredients/instructions, so! the index of the selected title is going to be the first of every! 3 items. If you pick the second title from the title list, it's! position in the recipes list is 2 * 3 - 2 (second group of 3 items,! 2 back from the index of that last item). Mathematically, that can! be represented like this: s = indx * 3 - 2! Pick out and print the selected title from the recipe list (found at! the index above): list.get recipes, s, n$ print " TITLE: " print n$ + " \n"! Pick out and print the selected ingrediants from the recipe list! (found at the index above +1): list.get recipes, s+1, a$ print " INGREDIENTS: " print a$ + " \n"! Pick out and print the selected ingrediants from the recipe list! (found at the index above +2): list.get recipes, s+2, p$ print " COOKING INSTRUCTIONS: " print p$print " "! Give the use the option to click a key to continue (make sure the! onscreen keyboard is showing): print " (Click any key to continue...)" kb.hidepause 1000kb.togglepause 1000let done = 0do inkey$ k$ if k$ < > " @" then let done = 1until done! When the viewRecipe routine is done, go back and do it all again: goto viewRecipe! Here's the subroutine to create a new recipe. It's basically the same! as the code used earlier to create a new data file. It simply requests! title, ingredients, and instructions texts from the user. Those texts! are assigned variable labels and then added to the existing recipe list! structure. The recipe list is then serialized into a long concatenated! string and re-saved to the file " recipes.txt". When this routine is! complete, the program returns back to the loadSavedRecipes routine to! begin again: createNewRecipe: input " Title: ", title$, " " text.input ingredients$, " (Type ingredients here)" text.input instructions$, " (Type instructions here)" list.add recipes, title$list.add recipes, ingredients$list.add recipes, instructions$list.size recipes, sizesavedata$ = " " for i = 1 to size list.get recipes, i, m$ if i = size savedata$ = savedata$ + m$ else savedata$ = savedata$ + m$ + " ###" endifnext itext.open w, myfile, " recipes.txt" text.writeln myfile, savedata$text.close myfilegoto loadSavedRecipes

Here's the code without comments:

file.exists b, " recipes.txt" if b=0 longstring$ = " Remove entree from " + ~ " packaging\nHeat in microwave\nCool and eat with fork" list.create s, newrecipes list.add newrecipes, " Oatmeal", " Oatmeal\nWater\nMaple Syrup" ~ " Boil water\nAdd oatmeal\nAdd maple syrup\nCool and eat" ~ " Frozen Dinner", " Frozen Entree\nFork", longstring$ list.size newrecipes, size savedata$ = " " for i = 1 to size list.get newrecipes, i, m$ if i = size savedata$ = savedata$ + m$ else savedata$ = savedata$ + m$ + " ###" endif next i text.open w, myfile, " recipes.txt" text.writeln myfile, savedata$ text.close myfileendifloadSavedRecipes: grabfile serialdata$, " recipes.txt" array.delete recipe$[] split recipe$[], serialdata$, " ###" list.create s, recipes list.add.array recipes, recipe$[] list.size recipes, size list.create s, titles for i = 1 to size step 3 list.get recipes, i, n$ list.add titles, n$ next iviewRecipe: list.search titles, " Create New Recipe...", c if c = 0 list.add titles, " Create New Recipe..." endif select indx, titles, " Select a recipe to view/edit", d if d = 1 then end list.get titles, indx, rcp$ if rcp$ = " Create New Recipe..." goto createNewRecipe endif cls s = indx * 3 - 2 list.get recipes, s, n$ print " TITLE: " print n$ + " \n" list.get recipes, s+1, a$ print " INGREDIENTS: " print a$ + " \n" list.get recipes, s+2, p$ print " COOKING INSTRUCTIONS: " print p$ print " " print " (Click any key to continue...)" kb.hide pause 1000 kb.toggle pause 1000 let done = 0 do inkey$ k$ if k$ < > " @" then let done = 1 until donegoto viewRecipecreateNewRecipe: input " Title: ", title$, " " text.input ingredients$, " (Type ingredients here)" text.input instructions$, " (Type instructions here)" list.add recipes, title$ list.add recipes, ingredients$ list.add recipes, instructions$ list.size recipes, size savedata$ = " " for i = 1 to size list.get recipes, i, m$ if i = size savedata$ = savedata$ + m$ else savedata$ = savedata$ + m$ + " ###" endif next i text.open w, myfile, " recipes.txt" text.writeln myfile, savedata$ text.close myfilegoto loadSavedRecipes

This may seem like a lot of code for such a simple program, but the above app demonstrates some absolutely essential code patterns. Learning to think in terms of variables, data list structures, loops, and conditional structures is fundamental to achieving the overwhelming majority of useful programming goals. Saving serialized data to a file is useful if you need to share with other devices, upload the data to a web server to be parsed and entered into a database, display it online, etc. In a more straightforward way, this app could be easily converted to store contact information, retail inventory information, or any other sort of personal or business information, basically by renaming the field labels. For example, instead of " Title", " Ingredients", and " Cooking Instructions", the fields could be labeled " Item", " Size", and " Description", or " Name", " Phone Number(s), and " Address", etc. Try adjusting the loops, index variables, and list structures a bit to create an app that stores more than 3 fields. Next, we'll create a GUI version of this program.

10. HTML/Javascript GUIs

You've already seen how the RFO Basic " input" and " text.input" functions can be used to acquire text typed by users, the " select" function can be used to display and accept menu choices, and " print" can be used to output data. For the simple types of utility scripts that RFO Basic is perfectly suited to create, these input/output functions will often be all that are ever needed. To create more complex data entry screens, or to build more visually appealing Graphic User Interfaces (" GUI" s), RFO Basic allows you to use standard HTML and Javascript pages to display buttons, text fields, drop down selection lists, check boxes, text areas, images, and other familiar form elements. You can use any of the common Javascript libraries supported by the Android browser (jQuery Mobile, Sencha Touch, jQTouch), to provide rich graphic user interface interactions for your RFO Basic apps.






© 2023 :: MyLektsii.ru :: Мои Лекции
Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав.
Копирование текстов разрешено только с указанием индексируемой ссылки на источник.