Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.InputStreamReader;
- import javax.swing.JFrame;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.ScrollPaneConstants;
- import javax.swing.SpringLayout;
- /**
- * @author Mark O'Reilly
- *
- * Interface with Low Coupling and High Cohesion
- * and initial internal documentation
- */
- public class BirthdayTracker extends Frame implements WindowListener
- {
- int maxEntries = 100; //Global variable to define the size of the arrays
- int numberOfEntries = 0; //Global variable to remember how many entries are in the 3 arrays
- int currentEntry = 0; //Global variable to remember which entry in the arrays we are currently looking at / dealing with
- BirthdayDataRecord[] BirthdayInfo = new BirthdayDataRecord[maxEntries];
- Image Background;
- Label lblFirstName, lblLastName, lblLikes, lblDislikes, lblBirthDay, lblBirthMonth;
- TextField txtFirstName, txtLastName, txtLikes, txtDislikes, txtBirthDay, txtBirthMonth,txtBirthsInMonth ,txtSearch;
- Button btnNew, btnSave, btnDel, btnFind, btnExit, btnFirst, btnPrev, btnNext, btnLast, btnBirthsInMonth, btnSearch;
- /**
- * Entry point to the class and application.
- * @param args Array of String arguments.
- */
- public static void main(String[] args)
- {
- Frame myFrame = new BirthdayTracker();
- myFrame.setSize(570,350);
- myFrame.setLocation(400, 200);
- myFrame.setResizable(false);
- myFrame.setVisible(true);
- }
- /**
- * Constructor which sets up the
- * screen layout and listeners.
- */
- public BirthdayTracker()
- {
- setTitle("Birthday Tracker");
- setBackground(Color.cyan);
- SpringLayout myLayout = new SpringLayout();
- setLayout(myLayout);
- LocateLabels(myLayout);
- LocateTextFields(myLayout);
- LocateButtons(myLayout);
- this.addWindowListener(this);
- }
- /**
- * Controlling method for adding multiple labels
- * to the screen
- */
- public void LocateLabels(SpringLayout myLabelLayout)
- {
- lblFirstName = LocateALabel(myLabelLayout, lblFirstName, "First Name:", 20, 20);
- lblLastName = LocateALabel(myLabelLayout, lblLastName, "Last Name:", 20, 50);
- lblLikes = LocateALabel(myLabelLayout, lblLikes, "Likes:", 20, 80);
- lblDislikes = LocateALabel(myLabelLayout, lblDislikes, "Dislikes:", 20, 110);
- lblBirthDay = LocateALabel(myLabelLayout, lblBirthDay, "Birth Day:", 20, 140);
- lblBirthMonth = LocateALabel(myLabelLayout, lblBirthMonth, "Birth Month:", 20, 170 );
- }
- /**
- * Method with low coupling and high cohesion
- * for adding individual labels:
- * - reduces overall code, especially in the
- * LocateLabels method.
- * - makes this method re-usable with minimal
- * adjustment as it is moved from one
- * program to another.
- */
- public Label LocateALabel(SpringLayout myLabelLayout, Label myLabel, String LabelCaption, int x, int y)
- {
- myLabel = new Label(LabelCaption);
- add(myLabel);
- myLabelLayout.putConstraint(SpringLayout.WEST, myLabel, x, SpringLayout.WEST, this);
- myLabelLayout.putConstraint(SpringLayout.NORTH, myLabel, y, SpringLayout.NORTH, this);
- return myLabel;
- }
- /**
- * Controlling method for adding multiple textboxes
- * to the screen
- */
- public void LocateTextFields(SpringLayout myTextFieldLayout)
- {
- txtFirstName = LocateATextField(myTextFieldLayout, txtFirstName, 20, 130, 20);
- txtLastName = LocateATextField(myTextFieldLayout, txtLastName, 20, 130, 50);
- txtLikes = LocateATextField(myTextFieldLayout, txtLikes, 20, 130, 80);
- txtDislikes = LocateATextField(myTextFieldLayout, txtDislikes, 20, 130, 110);
- txtBirthDay = LocateATextField(myTextFieldLayout, txtBirthDay, 10, 130, 140);
- txtBirthMonth = LocateATextField(myTextFieldLayout, txtBirthMonth, 10, 130, 170);
- txtBirthsInMonth = LocateATextField(myTextFieldLayout, txtBirthsInMonth, 5, 255, 280);
- txtSearch = LocateATextField(myTextFieldLayout, txtSearch, 20, 130, 200);
- }
- /**
- * Method with low coupling and high cohesion
- * for adding individual textboxes:
- * - reduces overall code, especially in the
- * LocateTextFields method.
- * - makes this method re-usable with minimal
- * adjustment as it is moved from one
- * program to another.
- */
- public TextField LocateATextField(SpringLayout myTextFieldLayout, TextField myTextField, int width, int x, int y)
- {
- myTextField = new TextField(width);
- add(myTextField);
- myTextFieldLayout.putConstraint(SpringLayout.WEST, myTextField, x, SpringLayout.WEST, this);
- myTextFieldLayout.putConstraint(SpringLayout.NORTH, myTextField, y, SpringLayout.NORTH, this);
- return myTextField;
- }
- /**
- * Controlling method for adding multiple buttons
- * to the screen
- */
- public void LocateButtons(SpringLayout myButtonLayout)
- {
- btnNew = LocateAButton(myButtonLayout, btnNew, "New", 420, 20, 120, 25);
- btnSave = LocateAButton(myButtonLayout, btnSave, "Save", 420, 50, 120, 25);
- btnDel = LocateAButton(myButtonLayout, btnDel, "Delete", 420, 80, 120, 25);
- btnFind = LocateAButton(myButtonLayout, btnFind, "Find", 420, 110, 120, 25);
- btnExit = LocateAButton(myButtonLayout, btnExit, "Exit", 420, 280, 120, 25);
- btnFirst = LocateAButton(myButtonLayout, btnFirst, "|<", 10, 280, 30, 25);
- btnPrev = LocateAButton(myButtonLayout, btnPrev, "<", 40, 280, 30, 25);
- btnNext = LocateAButton(myButtonLayout, btnNext, ">", 70, 280, 30, 25);
- btnLast = LocateAButton(myButtonLayout, btnLast, ">|", 100, 280, 30, 25);
- btnBirthsInMonth = LocateAButton(myButtonLayout, btnBirthsInMonth, "Births in Month of:", 145, 280, 110, 25);
- btnSearch = LocateAButton(myButtonLayout, btnSearch, "Search", 10, 200, 100, 25);
- }
- /**
- * Method with low coupling and high cohesion
- * for adding individual buttons:
- * - reduces overall code, especially in the
- * LocateButtons method.
- * - makes this method re-usable with minimal
- * adjustment as it is moved from one
- * program to another.
- */
- public Button LocateAButton(SpringLayout myButtonLayout, Button myButton, String ButtonCaption, int x, int y, int w, int h)
- {
- myButton = new Button(ButtonCaption);
- add(myButton);
- myButtonLayout.putConstraint(SpringLayout.WEST, myButton, x, SpringLayout.WEST, this);
- myButtonLayout.putConstraint(SpringLayout.NORTH, myButton, y, SpringLayout.NORTH, this);
- myButton.setPreferredSize(new Dimension(w,h));
- return myButton;
- }
- /**
- * Respond to Window events, such as clicking the Close ( X ) button
- */
- public void actionPerformed(ActionEvent e)
- {
- //button first
- if(e.getSource() == btnFirst)
- {
- currentEntry = 0;
- displayEntry(currentEntry);
- }
- //button previous
- if(e.getSource() == btnPrev)
- {
- if(currentEntry > 0)
- {
- currentEntry--;
- displayEntry(currentEntry);
- }
- }
- //button next
- if(e.getSource() == btnFirst)
- {
- if(currentEntry < numberOfEntries - 1)
- {
- currentEntry++;
- displayEntry(currentEntry);
- }
- }
- //button last
- if(e.getSource() == btnLast)
- {
- currentEntry = numberOfEntries - 1;
- displayEntry(currentEntry);
- }
- //button new
- if(e.getSource() == btnNew)
- {
- if(numberOfEntries < maxEntries - 1)
- {
- numberOfEntries++;
- currentEntry = numberOfEntries - 1;
- displayEntry(currentEntry);
- }
- }
- //button exit
- if(e.getSource() == btnExit)
- {
- writeFile();
- System.exit(0);
- }
- }
- public void BdayReportFrame()
- {
- // Setting the frame for the birthdays in month report
- SpringLayout myLayout = new SpringLayout();
- //Setting a value to the "font" variable.
- Font font = new Font("Verdana", Font.BOLD, 15);
- //Defining new colors
- //Setting heading.
- Label bdayheading = new Label("BIRTHDAYS In Month Of: "); //+cboSearch.getSelectedItem());
- //Attaching a font to "bdayheading"
- bdayheading.setFont(font);
- JFrame reportFrame = new JFrame();
- reportFrame.setSize(565,350);
- reportFrame.setLocation(400, 200);
- reportFrame.setResizable(false);
- reportFrame.setVisible(true);
- //Setting close function Dispose of frame and Keep main open.
- reportFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- //Defining new Panel and adding it to the frame.
- Panel panel = new Panel();
- reportFrame.add(panel);
- //Setting layout to panel
- panel.setLayout(myLayout);
- panel.setBackground(Color.LIGHT_GRAY);
- //Setting the design elements to the Jtext area
- JTextArea txtaMonthSearch = new JTextArea();
- txtaMonthSearch.setEditable(false);
- txtaMonthSearch.setBackground(Color.WHITE);
- //adding ScrollPane to the JTextArea txtaMonthSearch.
- JScrollPane sp = new JScrollPane(txtaMonthSearch);
- sp.setPreferredSize(new Dimension(525,250));
- //Setting location of scrollpane
- myLayout.putConstraint(SpringLayout.WEST, sp, 20, SpringLayout.WEST,panel);
- myLayout.putConstraint(SpringLayout.NORTH,sp , 40, SpringLayout.NORTH,panel);
- //Setting location of bdayheading
- myLayout.putConstraint(SpringLayout.WEST, bdayheading, 20, SpringLayout.WEST,panel);
- myLayout.putConstraint(SpringLayout.NORTH,bdayheading , 10, SpringLayout.NORTH,panel);
- //Setting the ScrollPane to only show when required
- sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
- panel.add(sp);
- panel.add(bdayheading);
- txtaMonthSearch.setText("Hi");
- }
- public void windowClosing(WindowEvent we)
- {
- System.exit(0);
- }
- public void windowIconified(WindowEvent we)
- {
- }
- public void windowOpened(WindowEvent we)
- {
- }
- public void windowClosed(WindowEvent we)
- {
- }
- public void windowDeiconified(WindowEvent we)
- {
- }
- public void windowActivated(WindowEvent we)
- {
- }
- public void windowDeactivated(WindowEvent we)
- {
- }
- //Diplay the numbered entry requested
- public void displayEntry(int index)
- {
- txtFirstName.setText(BirthdayInfo[index].getFirstName());
- txtLastName.setText(BirthdayInfo[index].getLastName());
- txtLikes.setText(BirthdayInfo[index].getLikes());
- txtDislikes.setText(BirthdayInfo[index].getDisLikes());
- txtBirthDay.setText(BirthdayInfo[index].getBirthDay());
- txtBirthMonth.setText(BirthdayInfo[index].getBirthMonth());
- }
- public void saveEntry(int index)
- {
- BirthdayInfo[index].setBirthdayInfo(txtFirstName.getText(), txtLastName.getText(), txtLikes.getText(), txtDislikes.getText(), txtBirthDay.getText(), txtBirthMonth.getText());
- writeFile();
- }
- //copy numbered entry requested from screen to the array and then calling
- //to save the whole array into the data file
- public void readFile()
- {
- try
- {
- FileInputStream fstream = new FileInputStream("Birthday.txt");
- DataInputStream in = new DataInputStream(fstream);
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- int index = 0;
- String line;
- while ((line = br.readLine()) != null)
- {
- String[] temp = line.split(",");
- BirthdayInfo[index] = new BirthdayDataRecord(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5]);
- index++;
- }
- numberOfEntries = index;
- in.close();
- }
- catch (Exception e)
- {
- System.err.println("Error Reading File: " + e.getMessage());
- }
- }
- public void writeFile()
- {
- try
- {
- // create file
- PrintWrite out = new PrintWrite(new FileWriter("Brithday_New.txt"));
- //write out each line of the data file: firstname lastname etc...
- for(int m = 0; m < numberOfEntries; m++)
- {
- out.println(BirthdayInfo[m].getFirstName() + "," + BirthdayInfo[m].getLastName() + "," + BirthdayInfo[m].getLikes() + "," + BirthdayInfo[m].getDisLikes() + "," + BirthdayInfo[m].getBirthDay() + "," + BirthdayInfo[m].getBirthMonth());
- }
- out.close();
- }
- catch (Exception e)
- {
- System.err.println("Error Writing File: " + e.getMessage());
- }
- }
- }
- class BirthdayDataRecord
- {
- //declartion of 6 strings for storing the Pc/IP data in memory for EACH PC record
- String FirstName = new String();
- String LastName = new String();
- String Likes = new String();
- String DisLikes = new String();
- String BirthDay = new String();
- String BirthMonth = new String();
- public BirthdayDataRecord()
- {
- FirstName = "FirstName";
- LastName = "LastName";
- Likes = "Likes";
- DisLikes = "Dislikes";
- BirthDay = "Day";
- BirthMonth = "Month";
- }
- public BirthdayDataRecord(String FName, String LName, String likes, String Dislikes, String Day, String Month)
- {
- FirstName = FName;
- LastName = LName;
- Likes = likes;
- DisLikes = Dislikes;
- BirthDay = Day;
- BirthMonth = Month;
- }
- public void setBirthdayInfo(String FName, String LName, String likes, String Dislikes, String Day, String Month)
- {
- FirstName = FName;
- LastName = LName;
- Likes = likes;
- DisLikes = Dislikes;
- BirthDay = Day;
- BirthMonth = Month;
- }
- public void setFirstName(String FName)
- {
- FirstName = FName;
- }
- public void setLastName(String LName)
- {
- LastName = LName;
- }
- public void setLikes(String likes)
- {
- Likes = likes;
- }
- public void setDisLikes(String Dislikes)
- {
- DisLikes = Dislikes;
- }
- public void setBirthDay(String Day)
- {
- BirthDay = Day;
- }
- public void setBirthMonth(String Month)
- {
- BirthMonth = Month;
- }
- public String getFirstName()
- {
- return FirstName;
- }
- public String getLastName()
- {
- return LastName;
- }
- public String getLikes()
- {
- return Likes;
- }
- public String getDisLikes()
- {
- return DisLikes;
- }
- public String getBirthDay()
- {
- return BirthDay;
- }
- public String getBirthMonth()
- {
- return BirthMonth;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement