Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.91 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.WindowEvent;
  4. import java.awt.event.WindowListener;
  5. import java.io.BufferedReader;
  6. import java.io.DataInputStream;
  7. import java.io.FileInputStream;
  8. import java.io.FileWriter;
  9. import java.io.InputStreamReader;
  10. import javax.swing.JFrame;
  11. import javax.swing.JScrollPane;
  12. import javax.swing.JTextArea;
  13. import javax.swing.ScrollPaneConstants;
  14. import javax.swing.SpringLayout;
  15.  
  16. /**
  17.  * @author Mark O'Reilly
  18.  *
  19.  * Interface with Low Coupling and High Cohesion
  20.  *    and initial internal documentation
  21.  */
  22.  
  23. public class BirthdayTracker extends Frame implements WindowListener
  24. {
  25.     int maxEntries = 100;  //Global variable to define the size of the arrays
  26.     int numberOfEntries = 0;  //Global variable to remember how many entries are in the 3 arrays
  27.     int currentEntry = 0;  //Global variable to remember which entry in the arrays we are currently looking at / dealing with
  28.    
  29.     BirthdayDataRecord[] BirthdayInfo = new BirthdayDataRecord[maxEntries];
  30.    
  31.     Image Background;
  32.     Label lblFirstName, lblLastName, lblLikes, lblDislikes, lblBirthDay, lblBirthMonth;
  33.     TextField txtFirstName, txtLastName, txtLikes, txtDislikes, txtBirthDay, txtBirthMonth,txtBirthsInMonth ,txtSearch;
  34.     Button btnNew, btnSave, btnDel, btnFind, btnExit, btnFirst, btnPrev, btnNext, btnLast, btnBirthsInMonth, btnSearch;
  35.  
  36.    
  37.     /**
  38.     * Entry point to the class and application.
  39.     * @param args Array of String arguments.
  40.     */
  41.     public static void main(String[] args)
  42.     {
  43.         Frame myFrame = new BirthdayTracker();
  44.         myFrame.setSize(570,350);
  45.         myFrame.setLocation(400, 200);
  46.         myFrame.setResizable(false);
  47.         myFrame.setVisible(true);
  48.     }
  49.  
  50.    
  51.     /**
  52.     * Constructor which sets up the
  53.     * screen layout and listeners.
  54.     */
  55.     public BirthdayTracker()
  56.     {
  57.         setTitle("Birthday Tracker");
  58.         setBackground(Color.cyan);
  59.  
  60.         SpringLayout myLayout = new SpringLayout();
  61.         setLayout(myLayout);
  62.        
  63.         LocateLabels(myLayout);
  64.         LocateTextFields(myLayout);
  65.         LocateButtons(myLayout);
  66.  
  67.         this.addWindowListener(this);
  68.     }
  69.  
  70.    
  71.     /**
  72.     * Controlling method for adding multiple labels
  73.     *    to the screen
  74.     */
  75.     public void LocateLabels(SpringLayout myLabelLayout)
  76.     {
  77.         lblFirstName = LocateALabel(myLabelLayout, lblFirstName, "First Name:", 20, 20);
  78.         lblLastName = LocateALabel(myLabelLayout, lblLastName, "Last Name:", 20, 50);
  79.         lblLikes = LocateALabel(myLabelLayout, lblLikes, "Likes:", 20, 80);
  80.         lblDislikes = LocateALabel(myLabelLayout, lblDislikes, "Dislikes:", 20, 110);
  81.         lblBirthDay = LocateALabel(myLabelLayout, lblBirthDay, "Birth Day:", 20, 140);
  82.         lblBirthMonth = LocateALabel(myLabelLayout, lblBirthMonth, "Birth Month:", 20, 170 );
  83.  
  84.     }
  85.  
  86.        
  87.     /**
  88.     * Method with low coupling and high cohesion
  89.     *    for adding individual labels:
  90.     *    - reduces overall code, especially in the
  91.     *         LocateLabels method.
  92.     *    - makes this method re-usable with minimal
  93.     *         adjustment as it is moved from one
  94.     *         program to another.
  95.     */
  96.     public Label LocateALabel(SpringLayout myLabelLayout, Label myLabel, String  LabelCaption, int x, int y)
  97.     {
  98.         myLabel = new Label(LabelCaption);
  99.         add(myLabel);        
  100.         myLabelLayout.putConstraint(SpringLayout.WEST, myLabel, x, SpringLayout.WEST, this);
  101.         myLabelLayout.putConstraint(SpringLayout.NORTH, myLabel, y, SpringLayout.NORTH, this);
  102.         return myLabel;
  103.     }
  104.    
  105.  
  106.     /**
  107.     * Controlling method for adding multiple textboxes
  108.     *    to the screen
  109.     */
  110.     public void LocateTextFields(SpringLayout myTextFieldLayout)
  111.     {
  112.         txtFirstName  = LocateATextField(myTextFieldLayout, txtFirstName, 20, 130, 20);
  113.         txtLastName  = LocateATextField(myTextFieldLayout, txtLastName, 20, 130, 50);
  114.         txtLikes = LocateATextField(myTextFieldLayout, txtLikes, 20, 130, 80);
  115.         txtDislikes = LocateATextField(myTextFieldLayout, txtDislikes, 20, 130, 110);
  116.         txtBirthDay = LocateATextField(myTextFieldLayout, txtBirthDay, 10, 130, 140);
  117.         txtBirthMonth = LocateATextField(myTextFieldLayout, txtBirthMonth, 10, 130, 170);
  118.         txtBirthsInMonth = LocateATextField(myTextFieldLayout, txtBirthsInMonth, 5, 255, 280);
  119.         txtSearch = LocateATextField(myTextFieldLayout, txtSearch, 20, 130, 200);
  120.        
  121.     }
  122.  
  123.        
  124.     /**
  125.     * Method with low coupling and high cohesion
  126.     *    for adding individual textboxes:
  127.     *    - reduces overall code, especially in the
  128.     *         LocateTextFields method.
  129.     *    - makes this method re-usable with minimal
  130.     *         adjustment as it is moved from one
  131.     *         program to another.
  132.     */
  133.     public TextField LocateATextField(SpringLayout myTextFieldLayout, TextField myTextField, int width, int x, int y)
  134.     {
  135.         myTextField = new TextField(width);
  136.         add(myTextField);        
  137.         myTextFieldLayout.putConstraint(SpringLayout.WEST, myTextField, x, SpringLayout.WEST, this);
  138.         myTextFieldLayout.putConstraint(SpringLayout.NORTH, myTextField, y, SpringLayout.NORTH, this);
  139.         return myTextField;
  140.     }
  141.  
  142.  
  143.     /**
  144.     * Controlling method for adding multiple buttons
  145.     *    to the screen
  146.     */
  147.     public void LocateButtons(SpringLayout myButtonLayout)
  148.     {
  149.         btnNew = LocateAButton(myButtonLayout, btnNew, "New", 420, 20, 120, 25);
  150.         btnSave = LocateAButton(myButtonLayout, btnSave, "Save", 420, 50, 120, 25);
  151.         btnDel = LocateAButton(myButtonLayout, btnDel, "Delete", 420, 80, 120, 25);
  152.         btnFind = LocateAButton(myButtonLayout, btnFind, "Find", 420, 110, 120, 25);
  153.         btnExit = LocateAButton(myButtonLayout, btnExit, "Exit", 420, 280, 120, 25);
  154.         btnFirst = LocateAButton(myButtonLayout, btnFirst, "|<", 10, 280, 30, 25);
  155.         btnPrev = LocateAButton(myButtonLayout, btnPrev, "<", 40, 280, 30, 25);
  156.         btnNext = LocateAButton(myButtonLayout, btnNext, ">", 70, 280, 30, 25);
  157.         btnLast = LocateAButton(myButtonLayout, btnLast, ">|", 100, 280, 30, 25);
  158.         btnBirthsInMonth = LocateAButton(myButtonLayout, btnBirthsInMonth, "Births in Month of:", 145, 280, 110, 25);
  159.         btnSearch = LocateAButton(myButtonLayout, btnSearch, "Search", 10, 200, 100, 25);
  160.        
  161.     }
  162.  
  163.        
  164.     /**
  165.     * Method with low coupling and high cohesion
  166.     *    for adding individual buttons:
  167.     *    - reduces overall code, especially in the
  168.     *         LocateButtons method.
  169.     *    - makes this method re-usable with minimal
  170.     *         adjustment as it is moved from one
  171.     *         program to another.
  172.     */
  173.     public Button LocateAButton(SpringLayout myButtonLayout, Button myButton, String  ButtonCaption, int x, int y, int w, int h)
  174.     {    
  175.         myButton = new Button(ButtonCaption);
  176.         add(myButton);
  177.         myButtonLayout.putConstraint(SpringLayout.WEST, myButton, x, SpringLayout.WEST, this);
  178.         myButtonLayout.putConstraint(SpringLayout.NORTH, myButton, y, SpringLayout.NORTH, this);
  179.         myButton.setPreferredSize(new Dimension(w,h));
  180.         return myButton;
  181.     }
  182.  
  183.  
  184.     /**
  185.     * Respond to Window events, such as clicking the Close ( X ) button
  186.     */
  187.    
  188.  public void actionPerformed(ActionEvent e)
  189.  {
  190.      //button first
  191.      if(e.getSource() == btnFirst)
  192.      {
  193.          currentEntry = 0;
  194.          displayEntry(currentEntry);
  195.      }
  196.      
  197.      //button previous
  198.      if(e.getSource() == btnPrev)
  199.      {
  200.          if(currentEntry > 0)
  201.          {
  202.          currentEntry--;
  203.          displayEntry(currentEntry);
  204.          }
  205.      }
  206.      //button next
  207.      if(e.getSource() == btnFirst)
  208.      {
  209.          if(currentEntry < numberOfEntries - 1)
  210.          {
  211.             currentEntry++;
  212.             displayEntry(currentEntry);
  213.          }
  214.      }
  215.           //button last
  216.      if(e.getSource() == btnLast)
  217.      {
  218.             currentEntry = numberOfEntries - 1;
  219.             displayEntry(currentEntry);
  220.      }
  221.           //button new
  222.      if(e.getSource() == btnNew)
  223.      {
  224.          if(numberOfEntries < maxEntries - 1)
  225.          {
  226.              numberOfEntries++;
  227.              currentEntry = numberOfEntries - 1;
  228.              
  229.              displayEntry(currentEntry);
  230.          }
  231.      }
  232.      //button exit
  233.      if(e.getSource() == btnExit)
  234.      {
  235.          writeFile();
  236.          System.exit(0);
  237.      }
  238.  }
  239.  
  240.  
  241.  public void BdayReportFrame()
  242.     {  
  243.         // Setting the frame for the birthdays in month report        
  244.         SpringLayout myLayout = new SpringLayout();
  245.         //Setting a value to the "font" variable.  
  246.         Font font = new Font("Verdana", Font.BOLD, 15);
  247.         //Defining new colors
  248.         //Setting heading.
  249.         Label bdayheading = new Label("BIRTHDAYS In Month Of: "); //+cboSearch.getSelectedItem());
  250.         //Attaching a font to "bdayheading"
  251.         bdayheading.setFont(font);      
  252.         JFrame reportFrame = new JFrame();
  253.         reportFrame.setSize(565,350);
  254.         reportFrame.setLocation(400, 200);
  255.         reportFrame.setResizable(false);
  256.         reportFrame.setVisible(true);
  257.         //Setting close function Dispose of frame and Keep main open.
  258.         reportFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  259.         //Defining new Panel and adding it to the frame.
  260.         Panel panel = new Panel();
  261.         reportFrame.add(panel);
  262.         //Setting layout to panel
  263.         panel.setLayout(myLayout);
  264.         panel.setBackground(Color.LIGHT_GRAY);
  265.         //Setting the design elements to the Jtext area
  266.        JTextArea txtaMonthSearch = new JTextArea();
  267.         txtaMonthSearch.setEditable(false);
  268.         txtaMonthSearch.setBackground(Color.WHITE);
  269.         //adding ScrollPane to the JTextArea txtaMonthSearch.
  270.         JScrollPane sp = new JScrollPane(txtaMonthSearch);
  271.         sp.setPreferredSize(new Dimension(525,250));
  272.         //Setting location of scrollpane
  273.         myLayout.putConstraint(SpringLayout.WEST, sp, 20, SpringLayout.WEST,panel);
  274.         myLayout.putConstraint(SpringLayout.NORTH,sp , 40, SpringLayout.NORTH,panel);
  275.         //Setting location of bdayheading
  276.         myLayout.putConstraint(SpringLayout.WEST, bdayheading, 20, SpringLayout.WEST,panel);
  277.         myLayout.putConstraint(SpringLayout.NORTH,bdayheading , 10, SpringLayout.NORTH,panel);
  278.        
  279.         //Setting the ScrollPane to only show when required
  280.         sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
  281.        
  282.         panel.add(sp);  
  283.         panel.add(bdayheading);
  284.        
  285.         txtaMonthSearch.setText("Hi");
  286.     }
  287.        
  288.  
  289.  
  290.  
  291.    
  292.            
  293.     public void windowClosing(WindowEvent we)
  294.     {
  295.           System.exit(0);
  296.     }
  297.  
  298.     public void windowIconified(WindowEvent we)
  299.     {
  300.     }
  301.  
  302.     public void windowOpened(WindowEvent we)
  303.     {
  304.     }
  305.  
  306.     public void windowClosed(WindowEvent we)
  307.     {
  308.     }
  309.  
  310.     public void windowDeiconified(WindowEvent we)
  311.     {
  312.     }
  313.  
  314.     public void windowActivated(WindowEvent we)
  315.     {
  316.     }
  317.  
  318.     public void windowDeactivated(WindowEvent we)
  319.     {
  320.     }
  321.      
  322.     //Diplay the numbered entry requested
  323.     public void displayEntry(int index)
  324.     {
  325.         txtFirstName.setText(BirthdayInfo[index].getFirstName());
  326.         txtLastName.setText(BirthdayInfo[index].getLastName());
  327.         txtLikes.setText(BirthdayInfo[index].getLikes());
  328.         txtDislikes.setText(BirthdayInfo[index].getDisLikes());
  329.         txtBirthDay.setText(BirthdayInfo[index].getBirthDay());
  330.         txtBirthMonth.setText(BirthdayInfo[index].getBirthMonth());
  331.     }
  332.    
  333.    
  334.     public void saveEntry(int index)
  335.     {
  336.         BirthdayInfo[index].setBirthdayInfo(txtFirstName.getText(), txtLastName.getText(), txtLikes.getText(), txtDislikes.getText(), txtBirthDay.getText(), txtBirthMonth.getText());
  337.         writeFile();
  338.     }
  339.     //copy numbered entry requested from screen to the array and then calling
  340.     //to save the whole array into the data file
  341.    
  342.    
  343.     public void readFile()
  344.     {
  345.         try
  346.         {
  347.             FileInputStream fstream = new FileInputStream("Birthday.txt");
  348.             DataInputStream in = new DataInputStream(fstream);
  349.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  350.             int index = 0;
  351.             String line;
  352.            
  353.            
  354.             while ((line = br.readLine()) != null)
  355.                     {
  356.                         String[] temp = line.split(",");
  357.                         BirthdayInfo[index] = new BirthdayDataRecord(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5]);
  358.                         index++;
  359.                     }
  360.             numberOfEntries = index;
  361.             in.close();
  362.         }
  363.         catch (Exception e)
  364.         {
  365.             System.err.println("Error Reading File: " + e.getMessage());
  366.         }
  367.     }
  368.    
  369.    
  370.     public void writeFile()
  371.     {
  372.         try
  373.         {
  374.             // create file
  375.             PrintWrite out = new PrintWrite(new FileWriter("Brithday_New.txt"));
  376.             //write out each line of the data file: firstname lastname etc...
  377.             for(int m = 0; m < numberOfEntries; m++)
  378.             {
  379.                 out.println(BirthdayInfo[m].getFirstName() + "," + BirthdayInfo[m].getLastName() + "," + BirthdayInfo[m].getLikes() + "," + BirthdayInfo[m].getDisLikes() + "," + BirthdayInfo[m].getBirthDay() + "," + BirthdayInfo[m].getBirthMonth());
  380.             }
  381.             out.close();
  382.         }
  383.         catch (Exception e)
  384.         {
  385.             System.err.println("Error Writing File: " + e.getMessage());
  386.         }            
  387.     }
  388. }
  389.  
  390. class BirthdayDataRecord
  391. {
  392.     //declartion of 6 strings for storing the Pc/IP data in memory for EACH PC record
  393.     String FirstName = new String();
  394.     String LastName = new String();
  395.     String Likes = new String();
  396.     String DisLikes = new String();
  397.     String BirthDay = new String();
  398.     String BirthMonth = new String();
  399.    
  400.    
  401.     public BirthdayDataRecord()
  402.     {
  403.         FirstName = "FirstName";
  404.         LastName = "LastName";
  405.         Likes = "Likes";
  406.         DisLikes = "Dislikes";
  407.         BirthDay = "Day";
  408.         BirthMonth = "Month";
  409.     }
  410.    
  411.    
  412.     public BirthdayDataRecord(String FName, String LName, String likes, String Dislikes, String Day, String Month)
  413.     {
  414.         FirstName = FName;
  415.         LastName = LName;
  416.         Likes = likes;
  417.         DisLikes = Dislikes;
  418.         BirthDay = Day;
  419.         BirthMonth = Month;
  420.     }
  421.    
  422.    
  423.    
  424.     public void setBirthdayInfo(String FName, String LName, String likes, String Dislikes, String Day, String Month)
  425.     {
  426.         FirstName = FName;
  427.         LastName = LName;
  428.         Likes = likes;
  429.         DisLikes = Dislikes;
  430.         BirthDay = Day;
  431.         BirthMonth = Month;
  432.     }
  433.    
  434.    
  435.     public void setFirstName(String FName)
  436.     {
  437.         FirstName = FName;
  438.     }
  439.    
  440.    
  441.     public void setLastName(String LName)
  442.     {
  443.         LastName = LName;
  444.     }
  445.    
  446.    
  447.     public void setLikes(String likes)
  448.     {
  449.         Likes = likes;
  450.     }
  451.    
  452.    
  453.     public void setDisLikes(String Dislikes)
  454.     {
  455.         DisLikes = Dislikes;
  456.     }
  457.    
  458.    
  459.     public void setBirthDay(String Day)
  460.     {
  461.         BirthDay = Day;
  462.     }
  463.  
  464.    
  465.     public void setBirthMonth(String Month)
  466.     {
  467.         BirthMonth = Month;
  468.     }
  469.    
  470.    
  471.     public String getFirstName()
  472.     {
  473.        return FirstName;
  474.     }
  475.    
  476.    
  477.     public String getLastName()
  478.     {
  479.         return LastName;
  480.     }
  481.    
  482.    
  483.     public String getLikes()
  484.     {
  485.         return Likes;
  486.     }
  487.    
  488.    
  489.     public String getDisLikes()
  490.     {
  491.         return DisLikes;
  492.     }
  493.    
  494.    
  495.     public String getBirthDay()
  496.     {
  497.         return BirthDay;
  498.     }
  499.  
  500.    
  501.     public String getBirthMonth()
  502.     {
  503.         return BirthMonth;
  504.     }            
  505. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement