CIS355A landscaping calculator tabbed program (Java)
package wk7; import java.awt.*; import java.awt.event.*; import java.io.*; import java.text.DecimalFormat; import javax.swing.*; public class LandscapingCalculator extends JFrame { private static final long serialVersionUID = 1L; private JTabbedPane tab; private JPanel topPanel; final JPanel Customers; final JPanel Contractors; final JPanel HotTubs; public LandscapingCalculator() { super(" Landscaping Calculator"); tab = new JTabbedPane(); getRootPane().add(tab); // add tabs to tab tab.addTab("Customers", new Customers()); tab.addTab("Contractors", new Contractors()); tab.addTab("Pools", new Pools()); tab.addTab("Hot Tubs", new HotTubs()); topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); getContentPane().add(topPanel); topPanel.add(tab, BorderLayout.CENTER); setSize(425, 250); setBackground(Color.blue); setVisible(true); } public static void main(String[] args) { LandscapingCalculator calcFrame = new LandscapingCalculator(); calcFrame.setSize(425, 250); calcFrame.setVisible(true); calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class Customers extends JPanel { private static final long serialVersionUID = 1L; public Customers() { JTextField custName; JTextField custCity; JTextField custState; JTextField custZip; JTextField custPhone; JTextField custAdd; final JTextArea custArea = new JTextArea(6, 30); final JTextArea custMessage; JTextArea custAddMessage; // Add customer button JButton addCustomer = new JButton("Add Customer"); addCustomer.setMnemonic(0); // Customer Panel JPanel custPanel = new JPanel(); custArea.setText("Select Add Customer to add customer. Select Refresh to refresh this pane."); custArea.setForeground(Color.orange); custArea.setLineWrap(true); custArea.setWrapStyleWord(true); JButton custRefButton = new JButton("Refresh"); custRefButton.setMnemonic(0); custMessage = new JTextArea(2, 30); custMessage.setLineWrap(true); custMessage.setWrapStyleWord(true); addCustomer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // action if button is // used new Customer("Customer"); }// end actionPerformed()//end action performed }); // end performed action custPanel.add(custArea); custPanel.add(addCustomer); custPanel.add(custRefButton); custPanel.add(custMessage); custRefButton.setMnemonic('R'); custRefButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { custMessage.setText(""); try { File custOpen = new File("customer.txt"); FileReader custAreaIn = new FileReader(custOpen); custArea.read(custAreaIn, custOpen.toString()); custMessage .setText("The file exists and can be read from."); } catch (IOException e3) { custMessage.setText("The file could not be read. " + e3.getMessage()); } } }); add(custPanel); } class Customer extends JPanel { private static final long serialVersionUID = 1L; private String[] states = { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" }; private JComboBox StateList = new JComboBox(states); private JTextField NameText = new JTextField(25); private JTextField AddressText = new JTextField(25); private JTextField CityText = new JTextField(25); private JTextField ZipText = new JTextField(9); private JTextField PhoneText = new JTextField(10); private JTextField PopMessageText = new JTextField(30); private AddCustButtonHandler addCusHandler = new AddCustButtonHandler(); public Customer(String name) { popUpWindow(name); } // Customer and Contractor Pop Up Window public void popUpWindow(final String name) { // add components to contractor Pop up window final JFrame popWindow; popWindow = new JFrame(name); popWindow.setSize(425, 350); popWindow.setLocation(100, 100); popWindow.setVisible(true); //popWindow.setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = new Container(); popWindow.add(c); c.setLayout(new FlowLayout()); JPanel one = new JPanel(); JPanel two = new JPanel(); JPanel three = new JPanel(); JPanel four = new JPanel(); JPanel five = new JPanel(); JPanel six = new JPanel(); one.add(new JLabel(name + " Name ")); one.add(NameText); two.add(new JLabel("Address ")); two.add(AddressText); three.add(new JLabel("City ")); three.add(CityText); four.add(new JLabel("State ")); StateList.setSelectedIndex(0); four.add(StateList); four.add(new JLabel("ZIP")); four.add(ZipText); four.add(new JLabel("Phone")); four.add(PhoneText); JButton addName = new JButton("Add " + name); addName.setMnemonic('A'); JButton close = new JButton("Close"); close.setMnemonic('C'); JButton deleteFile = new JButton("Delete File"); deleteFile.setMnemonic('D'); five.add(addName); five.add(close); five.add(deleteFile); PopMessageText.setEditable(false); PopMessageText.setHorizontalAlignment(JTextField.CENTER); // message.setOpaque(false); six.add(PopMessageText); c.add(one); c.add(two); c.add(three); c.add(four); c.add(five); c.add(six); deleteFile.setToolTipText("Delete File"); addName.setToolTipText("Add " + name); close.setToolTipText("Close"); if (name == "Customer") addName.addActionListener(addCusHandler); // registers listener close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { NameText.setText(""); AddressText.setText(""); CityText.setText(""); ZipText.setText(""); PhoneText.setText(""); PopMessageText.setText(""); popWindow.dispose(); } }); deleteFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { PopMessageText.setText(""); if (name == "Customer") { File file = new File("Customer.txt"); boolean cusFileDeleted = file.delete(); if (cusFileDeleted) { PopMessageText .setText("Customer file has been deleted"); } else { PopMessageText .setText("There was an erron in deleting file"); } } } }); }// end Pop up window // Class handler to add customer information to a file class AddCustButtonHandler implements ActionListener { public void actionPerformed(ActionEvent addCusHandler) { int StateIndex; try { File file = new File("Customer.txt"); boolean success = file.createNewFile(); if (success) { PopMessageText .setText("Customer.txt file created file added"); } else if (file.canWrite()) { PopMessageText .setText("Writing data to Customer.txt, file added"); } else { PopMessageText .setText("Cannot create file: Customer.txt"); } try { FileWriter fileW = new FileWriter("Customer.txt", true); fileW.write(NameText.getText()); fileW.write(","); fileW.write(AddressText.getText()); fileW.write(","); fileW.write(CityText.getText()); fileW.write(","); StateIndex = StateList.getSelectedIndex(); fileW.write(states[StateIndex]); fileW.write(","); fileW.write(ZipText.getText()); fileW.write(","); fileW.write(PhoneText.getText()); fileW.write("\r\n"); fileW.close(); PopMessageText .setText("A new Customer has been added!"); FileReader fileR = new FileReader("Customer.txt"); BufferedReader buffIn = new BufferedReader(fileR); buffIn.readLine(); buffIn.close(); } // Error message if unable to write to file catch (IOException e1) { JOptionPane.showMessageDialog(null, e1.getMessage(), "ERROR", 2); } NameText.setText(""); AddressText.setText(""); CityText.setText(""); ZipText.setText(""); PhoneText.setText(""); } catch (IOException e1) { } }// end actionPerformed method }// end customer btnHandler } } class Contractors extends JPanel { private static final long serialVersionUID = 1L; // contractor variables JTextField contName; JTextField contCity; JTextField contState; JTextField contZip; JTextField contPhone; JTextField contAdd; public Contractors() { setLayout(new FlowLayout()); final JTextArea contArea = new JTextArea(6, 30); final JTextArea contMessage; JTextArea contAddMessage; JButton addContractor = new JButton("Add Contractor"); addContractor.setMnemonic('a'); // Contractor Panel JPanel contPanel2 = new JPanel(); contArea.setText("Select Add Contractor to add contractor. Select Refresh to refresh this pane."); contArea.setForeground(Color.orange); contArea.setLineWrap(true); contArea.setWrapStyleWord(true); JButton contRefButton = new JButton("Refresh"); contMessage = new JTextArea(2, 30); contMessage.setLineWrap(true); contMessage.setWrapStyleWord(true); addContractor.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // action if button is // used new Contractors(); }// end actionPerformed()//end action performed }); // end performed action contPanel2.add(contArea); contPanel2.add(addContractor); contPanel2.add(contRefButton); contPanel2.add(contMessage); contRefButton.setMnemonic('R'); contRefButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { contMessage.setText(""); try { File contOpen = new File("contractor.txt"); FileReader contAreaIn = new FileReader(contOpen); contArea.read(contAreaIn, contOpen.toString()); contMessage .setText("The file exists and can be read from."); } catch (IOException e3) { contMessage.setText("The file could not be read. " + e3.getMessage()); } } }); add(contPanel2); } class createContractor extends JPanel { private String[] states = { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" }; private JComboBox StateList = new JComboBox(states); private JTextField NameText = new JTextField(25); private JTextField AddressText = new JTextField(25); private JTextField CityText = new JTextField(25); private JTextField ZipText = new JTextField(9); private JTextField PhoneText = new JTextField(10); private JTextField PopMessageText = new JTextField(30); private static final long serialVersionUID = 1L; private AddContButtonHandler addContHandler = new AddContButtonHandler(); public createContractor(String name) { popUpWindow(name); } // Customer and Contractor Pop Up Window public void popUpWindow(final String name) { // add components to contractor Pop up window final JFrame popWindow; popWindow = new JFrame(name); popWindow.setSize(425, 350); popWindow.setLocation(100, 100); popWindow.setVisible(true); Container c = new Container(); popWindow.add(c); c.setLayout(new FlowLayout()); JPanel one = new JPanel(); JPanel two = new JPanel(); JPanel three = new JPanel(); JPanel four = new JPanel(); JPanel five = new JPanel(); JPanel six = new JPanel(); one.add(new JLabel(name + " Name ")); one.add(NameText); two.add(new JLabel("Address ")); two.add(AddressText); three.add(new JLabel("City ")); three.add(CityText); four.add(new JLabel("State ")); StateList.setSelectedIndex(0); four.add(StateList); four.add(new JLabel("ZIP")); four.add(ZipText); four.add(new JLabel("Phone")); four.add(PhoneText); JButton addName = new JButton("Add " + name); addName.setMnemonic('A'); JButton close = new JButton("Close"); close.setMnemonic('C'); JButton deleteFile = new JButton("Delete File"); deleteFile.setMnemonic('D'); five.add(addName); five.add(close); five.add(deleteFile); PopMessageText.setEditable(false); PopMessageText.setHorizontalAlignment(JTextField.CENTER); // message.setOpaque(false); six.add(PopMessageText); c.add(one); c.add(two); c.add(three); c.add(four); c.add(five); c.add(six); deleteFile.setToolTipText("Delete File"); addName.setToolTipText("Add " + name); close.setToolTipText("Close"); if (name == "Contractor") addName.addActionListener(addContHandler); // registers listener close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { NameText.setText(""); AddressText.setText(""); CityText.setText(""); ZipText.setText(""); PhoneText.setText(""); PopMessageText.setText(""); popWindow.dispose(); } }); deleteFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { PopMessageText.setText(""); if (name == "Contractor") { File file = new File("Contractor.txt"); boolean contFileDeleted = file.delete(); if (contFileDeleted) { PopMessageText .setText("Contractor file has been deleted"); } else { PopMessageText .setText("There was an erron in deleting file"); } } } }); } // end Pop up window // Class handler to add contractor information to a file class AddContButtonHandler implements ActionListener { public void actionPerformed(ActionEvent addContHandler) { int StateIndex; try { File file = new File("Contractor.txt"); boolean success = file.createNewFile(); if (success) { PopMessageText .setText("Contractor.txt file created file added"); } else if (file.canWrite()) { PopMessageText .setText("Writing data to Contractor.txt, file added"); } else { PopMessageText .setText("Cannot create file: Contractor.txt"); } try { FileWriter fileW = new FileWriter("Contractor.txt", true); fileW.write(NameText.getText()); fileW.write(","); fileW.write(AddressText.getText()); fileW.write(","); fileW.write(CityText.getText()); fileW.write(","); StateIndex = StateList.getSelectedIndex(); fileW.write(states[StateIndex]); fileW.write(","); fileW.write(ZipText.getText()); fileW.write(","); fileW.write(PhoneText.getText()); fileW.write("\r\n"); fileW.close(); PopMessageText .setText("A new Contractor has been added!"); FileReader fileR = new FileReader("Contractor.txt"); BufferedReader buffIn = new BufferedReader(fileR); buffIn.readLine(); buffIn.close(); } catch (IOException e1) { JOptionPane.showMessageDialog(null, e1.getMessage(), "ERROR", 2); // Will display error message if // unable // to write to file } NameText.setText(""); AddressText.setText(""); CityText.setText(""); ZipText.setText(""); PhoneText.setText(""); } catch (IOException e1) { } } } } }// end of Constructor class abstract class Pools extends JPanel{ private static final long serialVersionUID = 1L; JButton calcVolume; JButton quit; JPanel Pools; JTextField lengthText, widthText, depthText, volumeText; public Pools() { setLayout(new FlowLayout()); Pools = new JPanel(); Pools.setLayout(null); JLabel lengthLabel = new JLabel( "Enter the pool's length(ft): "); lengthLabel.setBounds(20, 15, 160, 20); Pools.add(lengthLabel); lengthText = new JTextField(); lengthText.setBounds(170, 15, 150, 20); Pools.add(lengthText); JLabel widthLabel = new JLabel( "Enter the pool's width(ft): "); widthLabel.setBounds(20, 60, 160, 20); Pools.add(widthLabel); widthText = new JTextField(); widthText.setBounds(170, 60, 150, 20); Pools.add(widthText); JLabel depthLabel = new JLabel( "Enter the pool's depth(ft): "); depthLabel.setBounds(20, 100, 160, 20); Pools.add(depthLabel); depthText = new JTextField(); depthText.setBounds(170, 100, 150, 20); Pools.add(depthText); JLabel volumeLabel = new JLabel( "The pool volume is:(ft ^3): "); volumeLabel.setBounds(20, 180, 160, 20); Pools.add(volumeLabel); volumeText = new JTextField(); volumeText.setBounds(170, 180, 150, 20); volumeText.setEditable(false); Pools.add(volumeText); JButton calcVolume = new JButton("Calculate Volume"); calcVolume.setMnemonic(0); calcVolume.setBounds(80, 130, 150, 30); calcVolume.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0){ //calc_volume method for Pools String lengthString, widthString, depthString; int length = 0; int width = 0; int depth = 0; lengthString = lengthText.getText(); widthString = widthText.getText(); depthString = depthText.getText(); if (lengthString.length() < 1 || widthString.length() < 1 || depthString.length() < 1) { volumeText.setText("Error! Must enter in all three numbers!!"); return; } length = Integer.parseInt(lengthString); width = Integer.parseInt(widthString); depth = Integer.parseInt(depthString); if (length != 0 || width != 0 || depth != 0) { volumeText.setText((length * width * depth) + ""); } else { volumeText.setText("Error! Must Enter in all three numbers!!"); return; } } }); JButton exit = new JButton("Exit"); exit.setMnemonic(1); exit.setBounds(250, 130, 80, 30); exit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { System.exit(0); } }); Pools.add(calcVolume); Pools.add(exit); }// end Pools constructor }// end Pools Class class HotTubs extends JPanel { private static final long serialVersionUID = 1L; private JButton calcVolume; private JButton quit; private JPanel HotTubs; JTextField lengthText, widthText, depthText, volumeText; public HotTubs() { setLayout(new FlowLayout()); HotTubs = new JPanel(); HotTubs.setLayout(null); final JTextArea labelTubStatus = new JTextArea(6, 30); final JTextArea textFieldTubResult = new JTextArea(6, 30); final JTextArea textFieldTubWidth = new JTextArea(6, 30); final JRadioButton rdbtnRoundTub = new JRadioButton("RoundTub"); rdbtnRoundTub.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textFieldTubWidth.setEditable(false); } }); rdbtnRoundTub.setSelected(true); rdbtnRoundTub.setBounds(79, 7, 109, 23); HotTubs.add(rdbtnRoundTub); JRadioButton rdbtnOvalTub = new JRadioButton("Oval Tub"); rdbtnOvalTub.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textFieldTubWidth.setEditable(true); } }); rdbtnOvalTub.setBounds(201, 7, 109, 23); HotTubs.add(rdbtnOvalTub); ButtonGroup radioBtnGroup = new ButtonGroup(); radioBtnGroup.add(rdbtnRoundTub); radioBtnGroup.add(rdbtnOvalTub); JButton btnCalculateVlmn = new JButton("Calculate Volume"); btnCalculateVlmn.setMnemonic('C'); btnCalculateVlmn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { double width = 0, length = 0, depth = 0, volume = 0; try { if (rdbtnRoundTub.isSelected()) { volume = Math.PI * Math.pow(length / 2.0, 2) * depth; } else { volume = Math.PI * Math.pow(length * width, 2) * depth; } // double volume = calculate volume; DecimalFormat formatter = new DecimalFormat("#,###,###.###"); textFieldTubResult.setText("" + formatter.format(volume)); } catch (NumberFormatException e) { labelTubStatus .setText("Please make sure that all fields are filled and contain valid input!"); } } }); btnCalculateVlmn.setBounds(47, 115, 141, 23); HotTubs.add(btnCalculateVlmn); } }// end HotTub Class