jump to navigation

Sort and Display (Java application) May 12, 2010

Posted by inforuser in Java source codes.
Tags: , , , , , , , , ,
trackback

This is not a Java code  tutorial.   Here are the basic requirements for a Java student to get started.

If you’d like to install latest Java features  please follow the link.

http://developers.sun.com/downloads/top.jsp

Make sure you have text pad or similar Java compiling softwares installed.

To get text pad please click. http://www.textpad.com/download/

or for J GRASP please click. http://www.jgrasp.org/index.html

here is a small example of a Java windowed application.

this Application allows you to sort and display.\

this is a personal work and is posted here just for your reference. please read the copyright  Disclaimer for further information.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;
public class NursingGUI_V2 extends JFrame
{
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 400;
ArrayList<Patient> pList = new ArrayList<Patient>();
int baseCareRates[] = {0,3,6,9};
int numPatients = 0;
int points = 0;
int category =0;
int careHours =0;
int totalcareHours = 0;
JLabel nameLabel = new JLabel (“Patient Full Name:”);
JLabel idLabel = new JLabel (“Identity No:”);
JLabel nutritionLabel = new JLabel (“Nutrition:”);
JLabel hygieneLabel = new JLabel (“Hygiene:”);
JLabel activityLabel = new JLabel (“Activity:”);
JLabel dummyLabel1 = new JLabel ();
JLabel dummyLabel2 = new JLabel ();
JLabel dummyLabel3 = new JLabel ();
JLabel dummyLabel4 = new JLabel ();
JLabel dummyLabel5 = new JLabel ();
JLabel dummyLabel6 = new JLabel ();
JTextField nameTextField     = new JTextField (50);
JTextField idTextField = new JTextField (20);
JButton classifyButton = new JButton (“Classify Patient”);
JButton scheduleButton = new JButton (“Schedule Nurses”);
JButton clearButton = new JButton (“Clear”);
JButton exitButton   = new JButton (“Exit”);
JButton sortbynameButton = new JButton (“Sort by Name”);
JButton sortbycategoryButton = new JButton (“Sort by Category”);
JButton deleteButton = new JButton (“Delete Patient”);
JButton savetofileButton = new JButton (“Save to File”);
JComboBox nutritionComboBox  = new JComboBox();
JComboBox hygieneComboBox = new JComboBox();
JComboBox activityComboBox   = new JComboBox();
JTextArea displayArea = new JTextArea();
JScrollPane displayScrollPane= new JScrollPane();
public NursingGUI_V2()
{
//****************** Adding actionlistener to buttons***************************
classifyButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
classifyButton_actionPerformed(e);
}
});
scheduleButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
scheduleButton_actionPerformed(e);
}
});
clearButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
clearButton_actionPerformed(e);
}
});
exitButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exitButton_actionPerformed(e);
}
});
sortbynameButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sortbynameButton_actionPerformed(e);
}
});
sortbycategoryButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sortbycategoryButton_actionPerformed(e);
}
});
deleteButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
deleteButton_actionPerformed(e);
}
});
savetofileButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
savetofileButton_actionPerformed(e);
}
});
createControlPanel();
patReader();
}
public void createControlPanel()
{
//Methods for Creating Panels
JPanel northPanel = createComboBox();
JPanel centralPanel = createTextArea();
JPanel southPanel = createButtons();
JPanel borderPanel = new JPanel();
borderPanel.setLayout (new BorderLayout());
borderPanel.add (northPanel, BorderLayout.NORTH);
borderPanel.add (centralPanel, BorderLayout.CENTER);
borderPanel.add (southPanel, BorderLayout.SOUTH);
borderPanel.setBorder (new TitledBorder(new EtchedBorder(),”NURSING CARE SYSTEM”));
add (borderPanel); //adding panels to frame
}
public JPanel createComboBox()
{
// creating comboboxes and adding items
nutritionComboBox.addItem(“None”);
nutritionComboBox.addItem(“Feed Assistance”);
nutritionComboBox.addItem(“Complete Feeding”);
hygieneComboBox.addItem(“None”);
hygieneComboBox.addItem(“Bath Self”);
hygieneComboBox.addItem(“Bath with Assistance”);
activityComboBox.addItem(“None”);
activityComboBox.addItem(“Up without Assistance”);
activityComboBox.addItem(“Up with Assistance”);
activityComboBox.addItem(“Complete Immobility”);
// adding comboboxes to jpanel1
JPanel gridPanel1 = new JPanel ();
gridPanel1.setLayout (new GridLayout (1,4,1,1));
gridPanel1.add (nameLabel);
gridPanel1.add (nameTextField);
gridPanel1.add (idLabel);
gridPanel1.add (idTextField);
// adding labels, textfields and comboboxes to jpanel2
JPanel gridPanel2 = new JPanel ();
gridPanel2.setLayout (new GridLayout (1,9,1,1));
gridPanel2.add(dummyLabel1);
gridPanel2.add(nutritionLabel);
gridPanel2.add(nutritionComboBox);
gridPanel2.add(dummyLabel2);
gridPanel2.add(hygieneLabel);
gridPanel2.add(hygieneComboBox);
gridPanel2.add(dummyLabel3);
gridPanel2.add(activityLabel);
gridPanel2.add(activityComboBox);
// adding jpanel1 and jpanel2 to a new jpanel3
JPanel panel = new JPanel ();
panel.setLayout (new GridLayout (2,1,1,1));
panel.add (gridPanel1);
panel.add (gridPanel2);
// setting border for jpanel
panel.setBorder (new TitledBorder(new EtchedBorder(),”Patient Data”));
return panel;
}
// ***************creating textarea and adding them to panel***********************
public JPanel createTextArea()
{
//************* Create the panel*************
JPanel displayAreaPanel = new JPanel();
JPanel panel2 = new JPanel();
// Configure the border and layout.
displayAreaPanel.setBorder(new TitledBorder(new EtchedBorder(), “Display Area”));
displayAreaPanel.setLayout(new BorderLayout());
panel2.setLayout(new GridLayout(7,1,1,1));
panel2.add (sortbynameButton);
panel2.add (dummyLabel4);
panel2.add (sortbycategoryButton);
panel2.add (dummyLabel5);
panel2.add (deleteButton);
panel2.add (dummyLabel6);
panel2.add (savetofileButton);
// Configure the individual components.
displayArea.setColumns(20);
displayArea.setRows(20);
displayScrollPane.setViewportView(displayArea);
// Layout and add the individual components.
displayAreaPanel.add(displayScrollPane, BorderLayout.CENTER);
displayAreaPanel.add(panel2, BorderLayout.EAST);
// Return the complete panel.
return displayAreaPanel;
}
// adding buttons to panel
public JPanel createButtons()
{
JPanel panel = new JPanel();
panel.add (classifyButton);
panel.add (scheduleButton);
panel.add (clearButton);
panel.add (exitButton);
panel.setBorder (new TitledBorder(new EtchedBorder(),”Buttons”));
return panel;
}
public void classifyButton_actionPerformed(ActionEvent e)
{
String patName = null;
String patId = null;
int patCategory = 0;
int nutrIndex = 0;
int hygIndex = 0;
int activIndex = 0;
int i = 0;
try
{
// Ensure details have been entered.
if ((nameTextField.getText().equals(“”)) || (idTextField.getText().equals(“”)))
{
throw new NumberFormatException();
}
// Ensure patient does not already exist and classify
if (!exists(idTextField.getText()))
{
// Assign GUI values.
patName = nameTextField.getText();
patId = idTextField.getText();
nutrIndex = nutritionComboBox.getSelectedIndex();
hygIndex = hygieneComboBox.getSelectedIndex();
activIndex = activityComboBox.getSelectedIndex();
// Create a new patient.
Patient thisPatient = new Patient(patName, patId, nutrIndex, hygIndex, activIndex);
// Calculate and apply classification.
patCategory = classifyPatient(nutrIndex, hygIndex, activIndex);
thisPatient.setCategory(patCategory);
thisPatient.setCareHours(baseCareRates[patCategory]);
// Add the patient to the arraylist of patients.
pList.add(numPatients,thisPatient);
numPatients++;
/* Display the details. */
displayArea.setText(“This patient has been classified:\n\n” +
“Patient Name: ” + thisPatient.getName() +
“\t Id No:  ” + thisPatient.getId() +
“\t  Category:  “+ thisPatient.getCategory()
+ “\n\nRequired care hours: ” + baseCareRates[thisPatient.getCategory()]);
}
else
{
JOptionPane.showMessageDialog(null, “This person has already been classified.”, null, WIDTH);
}
}
catch (NumberFormatException eds)
{
JOptionPane.showMessageDialog(null, “Some inputs are missing please enter again.”, null, WIDTH);
}
}
private boolean exists(String id)
{
for(Patient p: pList)
{
if (p.getId().equals(id))
return true;
}
return false;
}
public void scheduleButton_actionPerformed(ActionEvent e)
{
int totalHours = 0;
int numberOfNurses =0;
for(Patient p: pList)
{
totalHours = totalHours + p.getNutritionIndex() + p.getActivityIndex() + p.getHygieneIndex();
}
if(totalHours % 8 == 0)
numberOfNurses = totalHours / 8;
else
numberOfNurses = totalHours / 8 + 1;
if(numberOfNurses < 3)
numberOfNurses = 3;
displayArea.setText(“Hours and Nurses:\n\n” +
“Total number of care hours needed today: ” + totalHours + “.\n” +
“Number of Nurses required today: ” + numberOfNurses);
}
private void clearButton_actionPerformed(ActionEvent e)
{
displayArea.setText(“”);
nameTextField.setText(“”);
idTextField.setText(“”);
}
private void exitButton_actionPerformed(ActionEvent e)
{
String Output_Message = “”;
int Dialog_Result = 0;
Output_Message = “Do you really want to exit?”;
Dialog_Result = JOptionPane.showConfirmDialog (null, Output_Message,”Message”, JOptionPane.YES_NO_OPTION);
if (Dialog_Result == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
public void patReader()
{
String displayString = “”;
FileReader reader = null;
int lineNumber = 1;
try
{
reader = new FileReader(“PatientData.csv”);
Scanner in = new Scanner(reader);
while (in.hasNextLine())
{
String input = in.nextLine();
String section[] = input.split(“,”);
pList.add(new Patient(section[0] , section[1] , Integer.parseInt(section[2]) , Integer.parseInt(section[3]) ,
Integer.parseInt(section[4]) , Integer.parseInt(section[5])));
lineNumber++;
}
for (Patient p: pList)
{
String titleString = “Name \t#ID \t#Nutrition \t#Hygiene \t#Activity \t#Category \n\n”;
displayString = displayString + p.getName() +
“\t” + p.getId() +
“\t” + p.getNutritionIndex() +
“\t” + p.getHygieneIndex() +
“\t” + p.getActivityIndex() +
“\t” + p.getCategory() +”\n”;
displayArea.setText(titleString + displayString);
}
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null, “File not Found”,”Error” , JOptionPane.ERROR_MESSAGE );
}
}
private void deleteButton_actionPerformed(ActionEvent e)
{
try
{
if(!exists(idTextField.getText()))
{
throw new NumberFormatException();
}
String id = idTextField.getText();
for(int i = 0; i< pList.size();i++)
{
Patient p = (Patient) pList.get(i);
if (p.getId().equals(id))
{
pList.remove(p);
JOptionPane.showMessageDialog(null,”Patient: “+ p.getId() + ” removed”,””, JOptionPane.INFORMATION_MESSAGE);
}
}
}
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, “Patient ID cannot be found”, “Error”,JOptionPane.ERROR_MESSAGE);
}
}
private void sortbynameButton_actionPerformed(ActionEvent e)
{
Patient[] patients = new Patient[50];
pList.toArray(patients);
for(int i = 0;i < patients.length -1;i++)
{
for(int j = i+1; j < pList.size(); j++)
{
if(patients[j].getName().compareTo( patients[i].getName()) < 0)
{
Patient patient = patients[i];
patients[i] = patients[j];
patients[j] = patient;
}
}
}
String displayString = “Name \t#ID \t#Nutrition \t#Hygiene \t#Activity \t#Category \n\n”;
displayArea.setText(displayString);
for(int k = 0; k<patients.length; k++)
{
displayString = “”;
displayString = displayString + patients[k].getName() +
“\t” + patients[k].getId() +
“\t” + patients[k].getNutritionIndex() +
“\t” + patients[k].getHygieneIndex() +
“\t” + patients[k].getActivityIndex() +
“\t” + patients[k].getCategory() +”\n”;
displayArea.append(displayString);
}
}
private void sortbycategoryButton_actionPerformed(ActionEvent e)
{
Patient[] patients = new Patient[50];
pList.toArray(patients);
for(int i = 0;i < patients.length -1;i++)
{
for(int j = i+1; j < pList.size(); j++)
{
if(patients[j].getCategory() < patients[i].getCategory() )
{
Patient patient = patients[i];
patients[i] = patients[j];
patients[j] = patient;
}
}
}
String displayString = “Name \t#ID \t#Nutrition \t#Hygiene \t#Activity \t#Category \n\n”;
displayArea.setText(displayString);
for(int k = 0; k<patients.length; k++)
{
displayString = “”;
displayString = displayString + patients[k].getName() +
“\t” + patients[k].getId() +
“\t” + patients[k].getNutritionIndex() +
“\t” + patients[k].getHygieneIndex() +
“\t” + patients[k].getActivityIndex() +
“\t” + patients[k].getCategory() +”\n”;
displayArea.append(displayString);
}
}
/** Classify patients. */
private int classifyPatient(int nutrIndex, int hygIndex, int activIndex)
{
int points = 0;
int patCategory = 0;
/* Nutrition. */
switch(nutrIndex)
{
case 1: points = points + 3;
break;
case 2: points = points + 6;
break;
default:
}
/* Hygiene. */
switch(hygIndex)
{
case 1: points = points + 3;
break;
case 2: points = points + 6;
break;
default:
}
/* Activity. */
switch(activIndex)
{
case 1: points = points + 3;
break;
case 2: points = points + 6;
break;
case 3: points = points + 12;
break;
default:
}
/* Determine category. */
if (points >= 0 && points < 10)
{
patCategory = 1;
}
else if (points > 9 && points < 19)
{
patCategory = 2;
}
else if (points > 18)
{
patCategory = 3;
}
return patCategory;
}
private void savetofileButton_actionPerformed(ActionEvent e)
{
PrintWriter out = null;
int linenumber = 0;
try
{
out = new PrintWriter(new FileOutputStream(“PatientData.csv”));
}
catch(IOException exception)
{
JOptionPane.showMessageDialog(null, “File not Found”, “Error”, JOptionPane.ERROR_MESSAGE);
}
for(Patient p: pList)
{
out.append(p.getName());
out.append(‘,’);
out.append(p.getId());
out.append(‘,’);
out.append(Integer.toString(p.getNutritionIndex()));
out.append(‘,’);
out.append(Integer.toString(p.getHygieneIndex()));
out.append(‘,’);
out.append(Integer.toString(p.getActivityIndex()));
out.append(‘,’);
out.append(Integer.toString(p.getCategory()));
out.append(‘\n’);
}
out.close();
JOptionPane.showMessageDialog(null, “File Saved”, “”, JOptionPane.INFORMATION_MESSAGE );
}
public static void main(String[] args)
{
NursingGUI_V2 nursing = new NursingGUI_V2();
nursing.setResizable(false);
nursing.setSize(1200,600 );
nursing.setVisible(true);
}
}
//Copyright (c)  2010 http://www.inforuser.wordpress.com .  All rights reserved
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS
* IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
//
Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: