Hi peeps... I'm trying to write a program that enables the user to write and retrieve memos with calendar dates. Here is what I got so far...
//Test class import javax.swing.JFrame.*; public class CalendarTest{ public static void main(String[] args) { userInterface uI = new userInterface(); uI.setSize(600, 300); uI.setLocationRelativeTo(null); uI.setVisible(true); uI.getDefaultCloseOperation(); } }//user interface class import java.awt.*; import java.awt.event.*; import javax.swing.*; public class userInterface extends JFrame implements ActionListener { private JButton save, retrieve; private JLabel sidl, namel, instl; private JTextField sidtf, nametf, insttf; private JLabel commentl; private JTextArea commentta; private JScrollPane commentsp; public userInterface() { super.setTitle("Memo Generator"); buildTop(); buildBottom(); buildRight(); pack(); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } private void buildTop() { Dimension dim = new Dimension(60,25); JPanel tp = new JPanel(); sidl = new JLabel("Month"); namel = new JLabel("Day"); instl = new JLabel("Year"); sidtf = new JTextField(8); nametf = new JTextField(8); insttf = new JTextField(8); sidl.setPreferredSize(dim); namel.setPreferredSize(dim); instl.setPreferredSize(dim); sidl.setHorizontalAlignment(SwingConstants.RIGHT); namel.setHorizontalAlignment(SwingConstants.RIGHT); instl.setHorizontalAlignment(SwingConstants.RIGHT); tp.add(sidl); tp.add(sidtf); tp.add(namel); tp.add(nametf); tp.add(instl); tp.add(insttf); add(tp, BorderLayout.NORTH); } private void buildBottom() { Dimension dim = new Dimension(90,25); JPanel bp1 = new JPanel(); save = new JButton("Save"); retrieve = new JButton("Retrieve"); save.setPreferredSize(dim); retrieve.setPreferredSize(dim); bp1.add(save); bp1.add(retrieve); JPanel bp3 = new JPanel(new GridLayout(2,1)); bp3.add(bp1); add(bp3, BorderLayout.SOUTH); } private void buildRight() { JPanel rp = new JPanel(); rp.setLayout(new BorderLayout()); rp.setBorder(BorderFactory.createEmptyBorder(0,10,0,10)); commentl = new JLabel("Message: "); commentl.setHorizontalAlignment(SwingConstants.CENTER); rp.add(commentl, BorderLayout.NORTH); commentta = new JTextArea(6,30); commentsp = new JScrollPane(commentta); rp.add(commentsp, BorderLayout.CENTER); add(rp, BorderLayout.CENTER); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }
These two seem to work as intended. Its this last class where the I/O is supposed to take place that is giving me difficulty...
import java.io.*; public class CalendarManger { public userInterface uI = new userInterface(); public CalendarManger() { /*Set the base directory where the calendar manager’s data files will reside Create a file object for the base directory and make sure that the directory exists, and if it doesn’t, use the mkdir method on the file object to create it.*/ File fileDir = new File ("c:\\temp\\"); } private void getFile(String month, String year) { /*This method trys to get the string array from the specified file Formulate the filename from the base directory, month, and year – make sure you use double backslashes after the base directory Create a file object using the filename If the file does not exist, create a new array of 31 strings for the member variable else create a FileInputStream and ObjectInputStream on the file and use the readObject method of the object input stream to get an array of strings from the file – remember to cast the value returned by the readObject method to a String [ ] and assign it to the string array member variable. Close the streams. If an exception occurs, create a new array of 31 strings for the member variable*/ } public void getEntry(String month, String day, String year) { /*Call the getFile method to try to get the string array from the file specified by the month and year. Convert the day string to an integer Remember, days go from 1 to 31, while the string array contains entries 0 thru 30 Get the entry from the string array member variable If it is not null, return that string Else return an error string indicating that the entry does not exist*/ } public void saveEntry(String month, String day, String year, String entry) { /*Call the getFile method to try to get the string array from the file specified by the month and year. Convert the day string to an integer Remember, days go from 1 to 31, while the string array contains entries 0 thru 30 Save the entry to the string array member variable Create a FileOutputStream object using the base directory, month, and year. Create an ObjectOutputStream object on top of the file output stream. Use the writeObject method to write the string array member variable to the file. Close the object stream and the file stream object. If everything goes well, return a string indicating success. If there was an exception, return a string indicating an error occurred.*/ } }
I have pseudo code that was given to help guide me but I'm still stuck. Any help will be greatly appreciated.