I hope I get help this time around. I`m working on another project, and I do not get errors, but the program is behaving in every way but the one I want it to behave.
What is going wrong is the following:
- when load is clicked the Patient.txt file, the last line loads twice.
- when sort is clicked, it sorts only 4 first entries and doubles them.
- When clear is clicked and load again, it displays all records twice.
- delete/update does not work at all, it can write to file but it writes in a wrong format (too many spaces), and also the other entries are written on the file as well in a completely wrong format which makes the file unreadable for any further operation.
- when the update button is clicked first, it automatically makes an entry with empty text and default combo box parameters and displays it in the text area along with the other text.
What it is supposed to do:
- When any other button other than load is clicked, display message to press Load first.
- When load is clicked, display the text from the Patient.txt file properly (spaced and formatted) in both, the query and update tab.
- When sort is clicked, the records should be sorted by name and displayed in the text area of both tabs.
- When Search is clicked, a small search window pops out and if the record exists display the record in the update tab and load the records parameters in the text fields and set the combo boxes to the proper parameters of the record.
- When clear is clicked it should should clear both text areas and when load is clicked again, it should display all entries once.
- When delete is clicked, it should delete the record whose values are loaded into the text area and combo boxes.
- When Save is clicked, a popup should appear if name text field and phone text field are not filled out, meaning it can`t have a null value, and when saving is successful it should automatically update the text areas with the new entry.
- Text areas should not be editable, only display text from file.
I hope this is detailed enough to get some help. I somehow got everything working to some extent but nothing works as required.
Feel free to run the code yourself and test it yourself, maybe there are some errors which I didn`t even notice
TabbedPanelGUI.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
public class TabbedPanelGUI extends JFrame{
private JTabbedPane tabPanel;
private JLabel titleLabel;
private JPanel titlePanel;
String patientInformation = "";
public static ArrayList<Patient> patientList = new ArrayList<Patient>();
public TabbedPanelGUI() {
buildGUI();
}
public void buildGUI() {
titlePanel = new JPanel();
titleLabel = new JLabel();
tabPanel = new JTabbedPane();
this.setTitle("General Patient Data Manager V1.0");
titlePanel.setLayout(new BorderLayout());
titleLabel.setFont(new Font("Times New Roman", 0, 32));
titleLabel.setForeground(new Color(51, 51, 255));
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
titleLabel.setText("Patient Data of Children with Allergy");
titlePanel.add(titleLabel, BorderLayout.CENTER);
this.getContentPane().add(titlePanel, BorderLayout.PAGE_START);
tabPanel.addTab("Query Records", new QueryPanel().IntinalizePanelForQuery());
tabPanel.addTab("Update Records", new UpdatePanel().IntializePanelForUpdate());
this.getContentPane().add(tabPanel, BorderLayout.CENTER);
this.pack();
}
public class QueryPanel extends JPanel implements ActionListener{
private JPanel queryTabPanel = new JPanel();
private JPanel queryNorthSubPanel = new JPanel();
private JScrollPane queryScrollPanel = new JScrollPane();
private JTextArea queryTextArea = new JTextArea();
private JPanel querySouthSubPanel = new JPanel();
private JButton loadQuery = new JButton();
private JButton sortQuery = new JButton();
private JButton searchQuery = new JButton();
private JButton clearQuery = new JButton();
private JButton exitQuery = new JButton();
public JPanel IntinalizePanelForQuery() {
queryTabPanel.setLayout(new BorderLayout());
queryNorthSubPanel.setBorder(BorderFactory.createTitledBorder(new LineBorder(new Color(204, 204, 255), 1, true),"Display Area"));
queryNorthSubPanel.setLayout(new BorderLayout());
queryTextArea.setColumns(20);
queryTextArea.setRows(5);
queryScrollPanel.setViewportView(queryTextArea);
queryNorthSubPanel.add(queryScrollPanel, BorderLayout.CENTER);
queryTabPanel.add(queryNorthSubPanel, BorderLayout.CENTER);
querySouthSubPanel.setBorder(BorderFactory.createTitledBorder(new LineBorder(new Color(204, 204, 255), 1, true), "Buttons"));
querySouthSubPanel.setLayout(new GridLayout(1, 5, 5, 5));
loadQuery.setText("Load");
loadQuery.addActionListener(this);
querySouthSubPanel.add(loadQuery);
sortQuery.setText("Sort");
sortQuery.addActionListener(this);
querySouthSubPanel.add(sortQuery);
searchQuery.setText("Search");
searchQuery.addActionListener(this);
querySouthSubPanel.add(searchQuery);
clearQuery.setText("Clear");
clearQuery.addActionListener(this);
querySouthSubPanel.add(clearQuery);
exitQuery.setText("Exit");
exitQuery.addActionListener(this);
querySouthSubPanel.add(exitQuery);
queryTabPanel.add(querySouthSubPanel, BorderLayout.SOUTH);
return queryTabPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==loadQuery){
ArrayList<Patient> arr = readFile();
for(int i=0;i<arr.size();i++){
queryTextArea.append(arr.get(i).toString()+"\n");
}
queryTextArea.append("\nThe number of entries are: " + arr.size());
}
if(e.getSource()==sortQuery){
queryTextArea.setText("");
ArrayList<Patient> arr = readFile();
Collections.sort(arr);
int sizeUpdate = arr.size()/2;
for(int i=0;i<sizeUpdate;i++){
queryTextArea.append(arr.get(i).toString()+"\n");
}
queryTextArea.append("\nThe number of entries are: " + arr.size());
}
if(e.getSource()==searchQuery){
queryTextArea.setText("");
String str = JOptionPane.showInputDialog("Please input the search Query");
ArrayList<Patient> arr = readFile();
queryTextArea.append(String.valueOf(searchRecord(arr, str)));
}
if(e.getSource()==clearQuery){
queryTextArea.setText("");
}
if(e.getSource()==exitQuery){
System.exit(-1);
}
}
}
public class UpdatePanel extends JPanel implements ActionListener
{
private String[] genderSoure = { "M", "F" };
private Integer[] ageSource = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14, 15, 16, 17 };
private String[] alergySource = { "Alcohol", "Animal Hair","Chilli & Pepper", "Peanut", "Seafood" };
private JPanel updateTabPanel = new JPanel();
private JPanel updateNorthSubPanel = new JPanel();
private JPanel updateNorthSubTopPanel = new JPanel();
private JTextField patientNameText = new JTextField();
private JTextField telephoneText = new JTextField();
private JPanel updateNorthSubBottomPanel = new JPanel();
private JComboBox<Integer> ageCombo = new JComboBox<Integer>(ageSource);
private JComboBox<String> genderCombo = new JComboBox<String>(genderSoure);
private JComboBox<String> sourceCombo = new JComboBox<String>(alergySource);
private JPanel updateSouthSubPanel = new JPanel();
private JButton updateUpdate = new JButton();
private JButton deleteUpdate = new JButton();
private JButton saveUpdate = new JButton();
private JButton clearUpdate = new JButton();
private JButton exitUpdate = new JButton();
private JPanel updateCenterSubPanel = new JPanel();
private JScrollPane updateScrollPanel = new JScrollPane();
private JTextArea updateTextArea = new JTextArea();
public JPanel IntializePanelForUpdate()
{
updateTabPanel.setLayout(new java.awt.BorderLayout());
updateNorthSubPanel.setBorder(BorderFactory.createTitledBorder(new LineBorder(new Color(204, 204, 255), 1, true),"Patient Data"));
updateNorthSubPanel.setLayout(new BorderLayout());
updateNorthSubTopPanel.setLayout(new GridLayout(1, 4, 5, 5));
JLabel patientlbl = new JLabel("Patient Name");
patientlbl.setHorizontalAlignment(SwingConstants.RIGHT);
updateNorthSubTopPanel.add(patientlbl);
patientNameText.setText(" ");
patientNameText.addActionListener(this);
updateNorthSubTopPanel.add(patientNameText);
JLabel telelbl = new JLabel("Telephone");
telelbl.setHorizontalAlignment(SwingConstants.RIGHT);
updateNorthSubTopPanel.add(telelbl);
telephoneText.setText(" ");
telephoneText.addActionListener(this);
updateNorthSubTopPanel.add(telephoneText);
updateNorthSubPanel.add(updateNorthSubTopPanel, BorderLayout.PAGE_START);
updateNorthSubBottomPanel.setLayout(new GridLayout(1, 6, 5, 5));
JLabel agelbl = new JLabel("Age");
agelbl.setHorizontalAlignment(SwingConstants.RIGHT);
updateNorthSubBottomPanel.add(agelbl);
ageCombo.addActionListener(this);
updateNorthSubBottomPanel.add(ageCombo);
JLabel gendlbl = new JLabel("Gender");
gendlbl.setHorizontalAlignment(SwingConstants.RIGHT);
updateNorthSubBottomPanel.add(gendlbl);
genderCombo.addActionListener(this);
updateNorthSubBottomPanel.add(genderCombo);
JLabel sourcelbl = new JLabel("Source");
sourcelbl.setHorizontalAlignment(SwingConstants.RIGHT);
updateNorthSubBottomPanel.add(sourcelbl);
sourceCombo.addActionListener(this);
updateNorthSubBottomPanel.add(sourceCombo);
updateNorthSubPanel.add(updateNorthSubBottomPanel, BorderLayout.CENTER);
updateTabPanel.add(updateNorthSubPanel, BorderLayout.NORTH);
updateSouthSubPanel.setBorder(BorderFactory.createTitledBorder(new LineBorder(new Color(204, 204, 255), 1, true), "Buttons"));
updateSouthSubPanel.setLayout(new GridLayout(1, 5, 5, 5));
updateUpdate.setText("Update");
updateUpdate.addActionListener(this);
updateSouthSubPanel.add(updateUpdate);
deleteUpdate.setText("Delete");
deleteUpdate.addActionListener(this);
updateSouthSubPanel.add(deleteUpdate);
saveUpdate.setText("Save");
saveUpdate.addActionListener(this);
updateSouthSubPanel.add(saveUpdate);
clearUpdate.setText("Clear");
clearUpdate.addActionListener(this);
updateSouthSubPanel.add(clearUpdate);
exitUpdate.setText("Exit");
exitUpdate.addActionListener(this);
updateSouthSubPanel.add(exitUpdate);
updateTabPanel.add(updateSouthSubPanel, BorderLayout.PAGE_END);
updateCenterSubPanel.setBorder(BorderFactory.createTitledBorder(new LineBorder(new Color(204, 204, 255), 1, true),"Display Area"));
updateCenterSubPanel.setLayout(new BorderLayout());
updateTextArea.setColumns(20);
updateTextArea.setRows(5);
updateScrollPanel.setViewportView(updateTextArea);
updateCenterSubPanel.add(updateScrollPanel, BorderLayout.CENTER);
updateTabPanel.add(updateCenterSubPanel, BorderLayout.CENTER);
return updateTabPanel;
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == updateUpdate)
{
String name = patientNameText.getText()+",";
String phone = telephoneText.getText()+",";
String age = String.valueOf(ageCombo.getSelectedItem())+",";
String gender = String.valueOf(genderCombo.getSelectedItem())+",";
String source = String.valueOf(sourceCombo.getSelectedItem());
ArrayList<Patient> arr = readFile();
arr.add(new Patient(name, phone, age,gender, source));
WriteToFile(arr);
for (int i = 0; i < arr.size(); i++)
{
updateTextArea.append(arr.get(i).toString() + "\n");
}
updateTextArea.append("The number of entries are: " + arr.size());
}
if(e.getSource()==deleteUpdate)
{
String name = patientNameText.getText();
ArrayList<Patient> arr = readFile();
System.out.println(arr.toString());
DeleteRecord(arr, name);
}
if (e.getSource() == clearUpdate)
{
updateTextArea.setText("");
}
if (e.getSource() == exitUpdate)
{
System.exit(-1);
}
}
}
public static void WriteToFile(ArrayList<Patient> obj)
{
try
{
BufferedWriter write = new BufferedWriter(new FileWriter("Patient.txt", true));
for (int i = 0; i < obj.size(); i++)
{
write.write(String.valueOf(obj.get(i)));
write.newLine();
}
write.close();
}catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static ArrayList<Patient> readFile() // Start of readFile
{
try
{
Scanner in = new Scanner(new FileReader("Patient.txt"));// open file
String myEntry = "";
String name = "";
String phone = "";
String age = "";
String gender = "";
String source = "";
while (in.hasNextLine())
{
myEntry = in.nextLine();
StringTokenizer st = new StringTokenizer(myEntry,",");
while (st.hasMoreTokens())
{
name = st.nextToken();
phone = st.nextToken();
age = st.nextToken();
gender = st.nextToken();
source = st.nextToken();
}
TabbedPanelGUI.patientList.add(new Patient(name, phone, age, gender, source));
}//
in.close();// close file
} catch (IOException ex)
{
System.out.println("file loading failed.");
}
return TabbedPanelGUI.patientList;
}
public static Patient searchRecord(ArrayList<Patient> list, String search)
{
Patient temp = null;
boolean isAvailable = false;
for (int i = 0; i < list.size(); i++)
{
if (list.get(i).getName().equals(search))
{
temp = list.get(i);
isAvailable = true;
}
}
if(isAvailable==false)
{
JOptionPane.showMessageDialog(null, "Record Not Found");
}
return temp;
}
public static void DeleteRecord(ArrayList<Patient> list, String search)
{
for (int i = 0; i < list.size(); i++)
{
if (search.equals(list.get(i).getName()))
{
list.remove(i);
WriteToFile(list);
JOptionPane.showMessageDialog(null, search +" Has been Deleted from the records");
}
}
}
public static void UpdateDocs(ArrayList<Patient> list, String search,String UpdatePhone, String UpdateAllergy)
{
for (int i = 0; i < list.size(); i++)
{
if (search.equals(list.get(i).getName()))
{
String name = list.get(i).getName();
String phone = UpdatePhone;
String age = list.get(i).getAge();
String gender = list.get(i).getGender();
String source = UpdateAllergy;
list.set(i, new Patient(name, phone, age, gender, source));
WriteToFile(list);
}
}
}
public static void main(String[] args)
{
JFrame gUI = new TabbedPanelGUI();
gUI.setVisible(true);
gUI.setSize(800, 600);
gUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Patient.java
public class Patient implements Comparable<Patient>
{
private String name = "";
private String phone = "";
private String age = "";
private String gender = "";
private String source = "";
public Patient(String name, String phone, String age, String gender,String source)
{
super();
this.name = name;
this.phone = phone;
this.age = age;
this.gender = gender;
this.source = source;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getSource() {
return source;
}
public void setName(String name) {
this.name = name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setAge(String age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setSource(String source) {
this.source = source;
}
@Override
public String toString() {
return this.name + "\t" + this.phone + "\t" + this.age + "\t"+ this.gender + "\t" + this.source;
}
@Override
public int compareTo(Patient current)
{
String nameOther = current.getName();
return this.name.compareTo(nameOther);
}
}
Patient.txt
John Smith,0749006611,16,M,Animal hairs Allan Pearson,0745008877,9,M,Peanut Kate Jackson,0766090901,12,F,Animal hairs Linda Hawk,0455990012,14,F,Alcohol William North,0766010122,8,M,Alcohol Carl Newton,0743220099,7,M,Chilli & pepper Sarah Harrison,0734111111,9,F,Alcohol David Jones,49008877,8,M,Animal hairs
Ok, so, I know the code is horrible and whatnot, but keep in mind that I`m a complete beginner still, even though that code is edited by a friend of mine, he tried to add a few things and make them work, but as you see it didn`t turn out great.

New Topic/Question
Reply


MultiQuote



|