View Full Version : JAVA and bartending
TheTree
10-24-2006, 04:41 PM
Hi all!!
I sometimes help out at a bartending school, and I came up with the idea of making some kind of JAVA program that would help students study drink recipes---sort of like a flash card system. Like, a drink's name would appear and the student would either check ingredients (radio buttons), or type them in manually. Then, for instance, if the person checked/wrote "gin"---and they were suppose to have vodka in the drink, "gin" would be highlighted and the word "vodka" would appear. Also, I'd wanna have scores.
I'm self taught and have written all kinds of little ditties---and I've looked through everything I've written, but nothing quite gets me what I want---except the scoring part, I wrote one of those already. Anyway, if you could just tell me what "kind" of function(s) (Again, self-taught, I don't know if that's the right terminology.) it sounds like I would need; ie "shuffle", that'd be great!!
Any help would be GREATLY appreciated!!
Take care,
TheTree
P.S. It'd also be good to have the entire drink recipe appear in some way---either at the finish of a question---or, the student could push a button/select it from a menu at the end of the test, or something like that.
Thanks bushels!!
daniel_g
10-25-2006, 08:17 AM
You coud make a method or function for every kind of drink. For example:
public void vodka(String in1, String in2, ...){
/*
* Use regular if/else statements to check what the user input was,
* and depending on every possible case, give values to any textfields
* you have created. You can use the method setText().
*/
}
in1, in2, etc are the ingredients for the drink.
Create a button and a listener for it. The listener should call the specified method, and set the values for in1, in2, etc by using whatever the user wrote or checked on a textfield.
I don't know what else to tell you. But try to make it work without the GUI at first. Once you get it working, you can focus on the GUI.
Aradon
10-25-2006, 09:48 AM
Yeah, I was unsure how to answer this completely. There are several ways you can go about this problem that range from ArrayLists to HashMaps.
You may want to create a class called Drink which holds an ArrayList of ingredients. That way you can check to see if certain ingredients exist, if they do or don't you can do with it as you please.
import java.util.ArrayList;
public class drink
{
ArrayList ingred;
public drink(ArrayList ar)
{
ingred = ar;
}
public boolean exists(String ingred)
{
return (ar.indexOf(ingred) != -1);
}
}
Or something like that. That's super psuedo code meaning I didn't really try it out, I just wrote it on the fly. There's generics to think of when doing that as well.
In any case there are many ways to do this problem, so what do were you thinking about while doing this problem? What type of solution did you see?
I didn't want to do my own work so I thought I'd setup a text version template of a possible solution for you. I hope this wasn't a homework assignment.
Notes:
-The recipe is randomly selected.
-The checkRecipe method only checks if the given recipe is correct, nothing more. You can add something to check the differences.
-Ingredients are Case Sensitive. Gin is not the same as gin when it checks the recipe. You'll probably want to change this.
-Ingredients are entered one per line. The user enters 'done' when they're finished entering ingredients.
-It doesn't matter what order the ingredients are entered in.
i.e. For Martini correct input looks like:
Gin
Vermouth
done
Steps to Add a new Recipe:
-Add private static int NEWRECIPE = n; (where n = 1 + the number of recipes already in the system). Add this to the top of the Bartender class.
-Change private Vector martini, tomCollins; to
private Vector martini, tomCollins, newRecipe;
-In the Bartender constructor add 1 to the numberOfRecipes variable
-In the getSelectedRecipe method add
else if(selectedRecipe == NEWRECIPE)
recipe = "New Recipe";
where NEWRECIPE matches NEWRECIPE from step 1
-Add a makeNewRecipe method, following the style of the others
-Add the statement makeNewRecipe() to the makeRecipes() method.
-In checkRecipe method add
else if(selectedRecipe == NEWRECIPE)
currentRecipe = newRecipe;
import java.io.*;
import java.util.*;
public class Bartender
{
private static int MARTINI = 1;
private static int TOMCOLLINS = 2;
private int numberOfRecipes;
private int selectedRecipe;
private Random generator;
private Vector martini, tomCollins;
private Vector currentRecipe = new Vector();
public Bartender()
{
numberOfRecipes = 2;
generator = new Random();
makeRecipes();
}
public void selectRecipe()
{
selectedRecipe = 1 + generator.nextInt(numberOfRecipes);
}
public String getSelectedRecipe()
{
String recipe = "";
if(selectedRecipe == MARTINI)
recipe = "Martini";
else if(selectedRecipe == TOMCOLLINS)
recipe = "Tom Collins";
return recipe;
}
private void makeRecipes()
{
makeMartini();
makeTomCollins();
}
private void makeMartini()
{
martini = new Vector();
martini.add("Gin");
martini.add("Vermouth");
}
private void makeTomCollins()
{
tomCollins = new Vector();
tomCollins.add("Gin");
tomCollins.add("Club Soda");
tomCollins.add("Lemon Juice");
}
public boolean checkRecipe(Vector ingredients)
{
boolean isCorrect = true;
if(selectedRecipe == MARTINI)
currentRecipe = martini;
else if(selectedRecipe == TOMCOLLINS)
currentRecipe = tomCollins;
if(ingredients.size() == currentRecipe.size())
{
for(int i = 0; i < ingredients.size()-1; i++)
{
if(!currentRecipe.contains(ingredients.elementAt(i)))
return false;
}
}
else
isCorrect = false;
return isCorrect;
}
public void printSelectedRecipe()
{
System.out.println(currentRecipe.toString());
}
public static void main(String[] args)
{
Bartender b = new Bartender();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String ingredient = " ";
Vector ingredientsEntered = new Vector();
boolean isCorrect;
String again = "y";
while(again.equals("y"))
{
b.selectRecipe();
System.out.println("Recipe: " + b.getSelectedRecipe());
//Ask for user input
System.out.println("Enter the ingredients one per line. Enter 'done' when finished: ");
//Keep asking for user input until 'done' is entered
while(!ingredient.equals("done")){
//Read in user input
try {
ingredient = in.readLine();
if(!ingredient.equals("done"))
ingredientsEntered.add(ingredient);
} catch (IOException e) {
System.out.println("An IOException has occurred.\n" + e);
}
}
//Check recipie
isCorrect = b.checkRecipe(ingredientsEntered);
if(isCorrect)
System.out.println("That's Correct!");
else{
System.out.println("That's Incorrect");
System.out.println("The correct ingredient list is:");
b.printSelectedRecipe();
}
//Play again?
System.out.print("Choose another Recipe? y/n: ");
//Read in user input
try {
again = in.readLine();
if(again.equals("y"))
{
ingredient = " ";
ingredientsEntered.clear();
}
} catch (IOException e) {
System.out.println("An IOException has occurred.\n" + e);
}
}
}
}
I only did minimal testing so you may find errors. Since this is just a basic system it's ment to be more of a starting point then a complete solution. There may be a better implementation, I don't garuantee that my logic is always the best.
EDIT: Added a printSelectedRecipe() method to print the correct ingredients if the user is incorrect.
EDIT2: I've decided today that I don't like my above implementation since it requires one to edit source code to add a new recipe. A better method would be something like read the recipes from a file and make recipe objects based on the data. That way it would be simple to add a recipe, just edit the file. Just thought I'd mention it.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.