The Artima Developer Community
Sponsored Link

Java Answers Forum
Java CookBook. Please help!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Tony

Posts: 1
Nickname: ringo
Registered: Apr, 2003

Java CookBook. Please help! Posted: Apr 17, 2003 11:30 PM
Reply to this message Reply
Advertisement
I need help finishing up my program.
I mainly need help in the sort, search, and delete methods. Any other help would be great as well.
(Direct Code will be greatly accepted)

Code so far.


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class Lab8 extends Applet implements ActionListener
{
final int MAXRECIPES = 100;
private ArrayList recipes = new ArrayList();
String name; //= new String();
String time; //= new String();
String calories; //= new String();
String ing; //= new String();
String inst; //= new String();
String cat; //= new String();
//String nameArray[] = new String[MAXRECIPES];
//String timeArray[] = new String[MAXRECIPES];
//String calArray[] = new String[MAXRECIPES];
//String catArray[] = new String[MAXRECIPES];
//String ingArray[] = new String[MAXRECIPES];
//String instArray[] = new String[MAXRECIPES];
String sent;
private boolean itemSelected = false;
private int indexSelected = -1;
private CookBook[] recipe = new CookBook[MAXRECIPES];
CookBook[] temp;
Label title = new Label("Java Cookbook");
Button submit = new Button("Submit");
Button clear = new Button("Clear");
Button sort = new Button("Sort");
Button delete = new Button("Delete");
Button search = new Button("Search");
Button display = new Button("Display");
Label rnlabel = new Label("Recipe Name");
TextField recName = new TextField(30);
Label ptlabel = new Label("Preperation Time (Minutes)");
TextField prepTime = new TextField(5);
Label nclabel = new Label("Calories");
TextField numCal = new TextField(7);
Label catLabel = new Label("Category");
Choice category = new Choice();
Label ingLabel = new Label("Ingredients");
TextArea ingredients = new TextArea(15,20);
Label instLabel = new Label("Instructions");
TextArea instructions = new TextArea(15,20);
Label sllabel = new Label("Search By:");
Choice searchList = new Choice();
Label selabel = new Label("Search For:");
TextField searchEnt = new TextField(30);
Label dispLabel = new Label("Results");
TextArea displayArea = new TextArea("",15,30);
Label deletelabel = new Label("Name of Recipe to Delete");
TextField delField = new TextField(20);

Panel displayPanel = new Panel(new GridLayout(2,1));
Panel p1 = new Panel(),
p2 = new Panel(),
p3 = new Panel(),
p4 = new Panel(),
p5 = new Panel(),
p6 = new Panel(),
p7 = new Panel(),
p8 = new Panel(),
p9 = new Panel(),
p10 = new Panel(),
p11 = new Panel(),
p12 = new Panel(),
p13 = new Panel(),
p14 = new Panel();

public void init()
{
category.add("Entree");
category.add("Salad");
category.add("Side Dish");
category.add("Dessert");
searchList.add("Prep Time");
searchList.add("Calories");

setLayout(new GridLayout());
p1.setBackground(Color.yellow);
p14.setBackground(Color.blue);
p1.setLayout(new GridLayout(6,1));
p2.setFont(new Font("Arial",Font.BOLD,30));
p2.setForeground(Color.magenta);
p2.add(title);
p3.add(rnlabel);
p3.add(recName);
p4.add(ptlabel);
p4.add(prepTime);
p4.add(nclabel);
p4.add(numCal);
p4.add(catLabel);
p4.add(category);
p5.setLayout(new GridLayout(1,2));
p5.add(ingLabel);
p5.add(ingredients);
p6.setLayout(new GridLayout(1,2));
p6.add(instLabel);
p6.add(instructions);
p9.add(submit);
p9.add(clear);
p9.add(sort);
p9.add(delete);
p9.add(deletelabel);
p9.add(delField);

p14.setLayout(new BorderLayout());
p14.setForeground(Color.green);
p10.add(sllabel);
p10.add(searchList);
searchList.setForeground(Color.black);
p10.add(selabel);
searchEnt.setForeground(Color.black);
p10.add(searchEnt);
p11.setLayout(new GridLayout(2,1));
p11.add(dispLabel);
displayArea.setForeground(Color.black);
p11.add(displayArea);
p13.setForeground(Color.black);
p13.add(search);
p13.add(display);

p1.add(p2);
p1.add(p3);
p1.add(p4);
p1.add(p5);
p1.add(p6);
//p1.add(p7);
//p1.add(p8);
p1.add(p9);
p14.add("North",p10);
p14.add("Center",p11);
//p14.add(p12);
p14.add("South",p13);

displayPanel.add(p1);
displayPanel.add(p14);
add(displayPanel);

submit.addActionListener(this);
clear.addActionListener(this);
sort.addActionListener(this);
delete.addActionListener(this);
search.addActionListener(this);
display.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{ name = recName.getText();
time = prepTime.getText();
calories = numCal.getText();
cat = category.getSelectedItem();
ing = ingredients.getText();
inst = instructions.getText();
sent = searchEnt.getText();

if(e.getSource()==submit) procSubmit();
if(e.getSource()==clear) procClear();
if(e.getSource()==sort) procSort();
if(e.getSource()==delete) procDelete();
if(e.getSource()==search) procSearch();
if(e.getSource()==display) procDisplay();
itemSelected=false;
repaint();
}

public void procSubmit()
{
recipe[CookBook.getnumRecipes()] = new CookBook(recName.getText(), Integer.parseInt(prepTime.getText()),
Integer.parseInt(numCal.getText()),category.getSelectedItem(),ingredients.getTe xt(),instructions.getText());

System.out.println("Submit button"+recipe);
}

private void procClear()
{
recName.setText(null);
prepTime.setText(null);
numCal.setText(null);
ingredients.setText(null);
instructions.setText(null);
searchEnt.setText(null);
displayArea.setText(null);
delField.setText(null);
}

private void procSort()
{
/*int[] tempArray = new Co
for( int i = 1; i <recipe.length; i++)
{
String[] temp = recipe;
int c = i;
for(; c>0 && temp.compareTo(recipe[c-1]) > 0; c--)
recipe[c] = recipe[c-1];
recipe[c] = temp;
}
displayArea.setText(recipe[]);
}*/

for(int i=0;i<recipe.length-1;i++)
{
int minIndex=i;
for(int j=i;j<recipe.length;j++)
{
if(recipe[j].compareTo(recipe[minIndex]<0)) minIndex=j;
}
String temp = recipe;
recipe=recipe[minIndex];
recipe[minIndex]=temp;
procDisplay();
}
}

private void procDelete()
{
int itemNum=0;
List list = new List(recipes);
list.remove(itemNum);
recipes=list.toArray(new CookBook[0]);

System.out.println("Delete button");
}
}

private void procSearch()
{

for(int i=0;i<recipe.length-1;i++){
int i1 = recipe.time;
if(sent.equals(i1)) procDisplay();
}

System.out.println("Search button");
}

private void procDisplay()
{

repaint();
for(int index=0;index<CookBook.getnumRecipes(); index++){

System.out.println("Recipes are "+recipe[index]);
displayArea.insert(recipe[index].toString(),30);
}
}
}

class CookBook
{
String name;
int time;
int calories;
String cat;
String ing;
String inst;
static int numRecipes=0;

public CookBook(String name,int time,int calories,String cat,
String ing, String inst)
{
this.name=name;
this.time=time;
this.calories=calories;
this.cat=cat;
this.ing=ing;
this.inst=inst;
numRecipes ++;
}

public static int getnumRecipes()
{
return numRecipes;
}

}

Topic: Error handling Previous Topic   Next Topic Topic: Count Character Occurrence

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use