Posted by: coresoul April 28, 2006
$20/java programming
Login in to Rate this Post:     0       ?        
Problem

I will Pay you $ 20.00 by Pay pal if u do this program before 4 today. two hrs left.

Problem:  

     Program will read a text file to create Food objects which will consist of a food name, a serving size, and a number of calories. A text file is read which contains the calories contained in specific food items.  From this data, an array of Food objects will be created and used to populate a combo box for user selection. The Food objects will also be used in calculating the balance calories for a given user on a given day.  

     User input will be used to create User objects which will be user name, age, gender, height, weight, and Food Diary objects (composition).  The Food Diary objects will consist of Date (composition), Food object (composition), number of servings, and a balance.  Food Diary objects will consist of the activity on any given day (see text area in sample output screen below).  All User objects are saved to a file as objects and can be retrieved on subsequent runs of the program.

     The program will calculate the number of calories the user is allowed per day to maintain the user’s current weight (see below for formula to use for this). 

 

BMRMen = 66 + (13.7 * weightPounds/2.2) + (5 * heightInches*2.54)  - (6.8 * age)

BMRWomen = 655 + (9.6 * weightPounds/2.2) + (1.8 * heightInches*2.54)  - (4.7 * age)

 

 

I wrote the following code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
 
public class FoodCaloriesGUI extends JFrame
{
	private ObjectOutputStream outputFile;
	private ObjectInputStream inputFile;
	private String fileName="FoodCalories.txt";
	//private int recordCount=0;
	private int month, day, year,serving,calories,balance;
	private String name,food,date;
	
	public FoodCaloriesGUI()
	{
		super ("Food Calories calculator");
		//readTextFile();
		//openFileForReading();
		readRecordAndDisplay();
		String outputArea="name\n"+"Date"+date+"\n"+"Food\t"+"Serving \t"+"Calories\n"+
		food+"\t"+serving+"\t"+calories+"\n"+"------------------------------"+"\n"+"Balance\t\t"+balance;
		JLabel			lblName, lblAge, lblWeight,lblHeight,lblFemale,lblMale,lblFood;
		JTextField		txtName, txtAge, txtWeight,txtHeight;
		JButton			btnSave, btnNewUser;
		ButtonGroup 	radioGroup;
		JTextArea dataarea =new JTextArea(outputArea);
		JRadioButton 	rbMale,rbFemale;
		JComboBox		cbFood;
		lblName=			new JLabel("Name: ");
		lblAge=				new JLabel("Age: ");
		lblWeight=			new JLabel("Weight(lbs): ");
		lblHeight=			new JLabel("Height(inches): ");
		lblFemale=			new JLabel("Female: ");
		lblMale=			new JLabel("Male: ");
		lblFood=			new JLabel("Choose a Food: ");
		
		
		txtName=			new JTextField(15);
		txtAge=				new JTextField(3);
		txtWeight=			new JTextField(6);
		txtHeight=			new JTextField(6);
		
		
		cbFood=				new JComboBox();
		cbFood.setPreferredSize(new Dimension(100, 20));
		
		Container c = getContentPane();
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new FlowLayout());
		btnSave=			new JButton("SAVE");
		northPanel.add(btnSave);
		
		btnNewUser=			new JButton("NEW USER");
		northPanel.add(btnNewUser);
		
		//btnSave.addActionListener(this);
		//btnNewUser.addActionListener(this);
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new FlowLayout());
		centerPanel.add(lblName);
		
		centerPanel.add(txtName);
		
		centerPanel.add(lblAge);
		
		centerPanel.add(txtAge);
		
		centerPanel.add(lblWeight);
		
		centerPanel.add(txtWeight);
		
		centerPanel.add(lblHeight);
		
		centerPanel.add(txtHeight);
		
		centerPanel.setLayout(new FlowLayout());
		rbMale=				new JRadioButton("Male",false);
		rbFemale=			new JRadioButton("female",false);
		radioGroup =		new ButtonGroup();
		radioGroup.add(rbMale);
		radioGroup.add(rbFemale);
		centerPanel.add(rbMale);
		centerPanel.add(rbFemale);
		centerPanel.add(lblFood);
		centerPanel.add(cbFood);
		JPanel southPanel = new JPanel();
		southPanel.setLayout(new FlowLayout());
		southPanel.add (dataarea);
		
		
		c.add(northPanel,BorderLayout.NORTH);
		c.add(centerPanel,BorderLayout.CENTER);
		c.add(southPanel,BorderLayout.SOUTH);
		
		setSize(650,300);
		setVisible(true);
	}
	public void openFileForReading()
	{
		try
		{
			inputFile=new ObjectInputStream(new FileInputStream("foodoutput.txt"));
		}
		catch (IOException e)
		{
			JOptionPane.showMessageDialog(this, "error opening file", "Error",JOptionPane.ERROR_MESSAGE);
		}
	}
	public void readTextFile()
	{
		double bmrmale=0.0,bmrfemale=0.0;
		try
		{
			FileReader file=new FileReader(fileName);
			BufferedReader inputFile=new BufferedReader(file);
			outputFile=new ObjectOutputStream(new FileOutputStream("foodoutput.txt"));
			String txtfileline=inputFile.readLine();
			while (txtfileline!=null)
			{
				txtfileline= txtfileline.trim();
				if (!txtfileline.equals(""))
				{
					StringTokenizer tokenizer = new StringTokenizer(txtfileline,"*");
					String food= tokenizer.nextToken();
					int serving=Integer.parseInt(tokenizer.nextToken());
					int calories=Integer.parseInt(tokenizer.nextToken());
					FoodCaloriesData record = new FoodCaloriesData(food,serving,calories);
					JOptionPane.showMessageDialog(null,"current Object: " + record);
					outputFile.writeObject(record);
					outputFile.flush();
				}
			}
		}
		catch (IOException ioe)
		{
			JOptionPane.showMessageDialog(null,"Error reading ","Error",JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
	}
	
		public void readRecordAndDisplay()
		{
			
		}
		
		public static void main (String args[])
		{
			FoodCaloriesGUI app= new FoodCaloriesGUI();
			app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
		}
	}
	


Text file: FoodCalories.txt
1000 ISLAND, SALAD DRSNG,LOCAL*1 TBSP*25
1000 ISLAND, SALAD DRSNG,REGLR*1 TBSP*60
100% NATURAL CEREAL *1 OZ*135
40% BRAN FLAKES, KELLOGG'S*1 OZ*90
40% BRAN FLAKES, POST*1 OZ*90


Class:FoodCaloriesData
 
import java.io.*;
public class FoodCaloriesData implements Serializable
	{
		private String food;
		private int serving;
		private int calories;
		public FoodCaloriesData ( String theFood,int theServing,int theCalories)
		{
			food=theFood;
			serving=theServing;
			calories=theCalories;
		}
		public int getServing()
		{
			return serving;
		}
		public String getFood()
		{
			return food;
		}
		public int getCalories()
		{
			return calories;
		}
		public void setServing(int theServing)
		{
			serving=( ( ( theServing > 0 ) && (theServing < 100) ) ?
 theServing : 0 );
		}
		public void setFood(String theFood)
		{
			food="";
		}
		public void setCalories(int theCalories)
		{
			calories=( ( ( theCalories > 0 ) && (theCalories < 100) ) ?
 theCalories : 0 );
		}
	}
 

 

Read Full Discussion Thread for this article