Results 1 to 3 of 3
- 12-12-2012, 11:42 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 22
- Rep Power
- 0
Linking Problems between Java methods
Here is my java code for my GUI.
Here is my OrderManagerClassJava Code:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.*; /** * @author Prabhdeep Singh * @param args * This is panel which is then displayed on J panel with all the different options allowed. */ public class Panel extends JPanel // Extend JPanel { JPanel containerPanel,subPanel1,subPanel2,bouquetSizePanel,msgPanel; JPanel deliveryPanel,totalCostPanel,buttonPanel,inputPanel; JLabel lNameLabel, fNameLabel,streetLabel,cityLabel,stateLabel,zipLabel; JTextField lNameField, fNameField,streetField,cityField,stateField,zipField,msgField,totalField; JLabel lNameLabel2, fNameLabel2,streetLabel2,cityLabel2,stateLabel2,zipLabel2,inputLabel; JTextField lNameField2, fNameField2,streetField2,cityField2,stateField2,zipField2; JRadioButton small,medium,large,standard,holiday; JCheckBox beforeNoon; JButton submitButton,clearButton,writeButton,exitButton,scheduleButton; JComboBox nameBox; private int bsize,toZ,fromZ, ship; private String toLn, toFn, toStr, toC, toSt,fromLn, fromFn, fromStr, fromC, fromSt,msg,d; private boolean beforeN; public String[] names = { "select a standard or Holiday", "May 8", "May 9", "May 10", "May 11", "May 12"}; OrderManager manager = new OrderManager(); int orderCount = 0; /** * Constructor for the Panel Class. */ public Panel() { //create all necessaries panels buttonPanel = new JPanel(); containerPanel = new JPanel(); subPanel1 = new JPanel(); subPanel2 = new JPanel(); bouquetSizePanel = new JPanel(); /* * LABELS AND TEXTFIELDS PANEL */ //FIRST SUBPANEL subPanel1= new JPanel(); //set the size of the panel subPanel1.setPreferredSize(new Dimension (210,130)); //set the panel to the left subPanel1.setLayout(new FlowLayout(FlowLayout.LEFT)); //add the border around the panel and set a title subPanel1.setBorder(BorderFactory.createTitledBorder("To")); //LABELS lNameLabel = new JLabel("Last Name"); fNameLabel = new JLabel("First Name"); streetLabel = new JLabel("Street"); cityLabel = new JLabel("City"); stateLabel = new JLabel("State"); zipLabel = new JLabel("Zip"); //TEXTFIELDS lNameField = new JTextField(12); fNameField = new JTextField(12); streetField = new JTextField(12); cityField = new JTextField(12); stateField = new JTextField(12); zipField = new JTextField(12); //gridLayout subPanel1.setLayout(new GridLayout(6,1)); //add text field and label to the sub panel subPanel1.add(lNameLabel); subPanel1.add(lNameField); subPanel1.add(fNameLabel); subPanel1.add(fNameField); subPanel1.add(streetLabel); subPanel1.add(streetField); subPanel1.add(cityLabel); subPanel1.add(cityField); subPanel1.add(stateLabel); subPanel1.add(stateField); subPanel1.add(zipLabel); subPanel1.add(zipField); //FIRST SUBPANEL subPanel2= new JPanel(); //set the size of the panel subPanel2.setPreferredSize(new Dimension (240,150)); //set the panel to the left subPanel2.setLayout(new FlowLayout(FlowLayout.RIGHT)); //add the border around the panel and set a title subPanel2.setBorder(BorderFactory.createTitledBorder("FROM")); //LABELS lNameLabel2 = new JLabel("Last Name"); fNameLabel2 = new JLabel("First Name"); streetLabel2 = new JLabel("Street"); cityLabel2 = new JLabel("City"); stateLabel2 = new JLabel("State"); zipLabel2 = new JLabel("Zip"); //TEXTFIELDS lNameField2 = new JTextField(14); fNameField2 = new JTextField(14); streetField2 = new JTextField(14); cityField2 = new JTextField(14); stateField2 = new JTextField(14); zipField2 = new JTextField(14); subPanel2.setLayout(new GridLayout(6,1)); //add text field and label to the sub panel subPanel2.add(lNameLabel2); subPanel2.add(lNameField2); subPanel2.add(fNameLabel2); subPanel2.add(fNameField2); subPanel2.add(streetLabel2); subPanel2.add(streetField2); subPanel2.add(cityLabel2); subPanel2.add(cityField2); subPanel2.add(stateLabel2); subPanel2.add(stateField2); subPanel2.add(zipLabel2); subPanel2.add(zipField2); /* * BOUQUET SIZE PANEL */ //create first panel bouquetSizePanel = new JPanel(); //set the size of the panel bouquetSizePanel.setPreferredSize(new Dimension (10,150)); //set the panel to the left bouquetSizePanel.setLayout(new FlowLayout(FlowLayout.LEFT)); // create the radio buttons small = new JRadioButton("Small - $19", true); medium = new JRadioButton("Medium - $35"); large = new JRadioButton(" Large - $49"); // group the radio buttons ButtonGroup bg = new ButtonGroup(); bg.add(small); bg.add(medium); bg.add(large); //add the border around the panel and set a title bouquetSizePanel.setBorder(BorderFactory.createTitledBorder("bouquet Size")); bouquetSizePanel.setLayout(new GridLayout(2,1)); //add the radio button to the panel bouquetSizePanel.add(small); bouquetSizePanel.add(medium); bouquetSizePanel.add(large); /* * MESSAGE PANEL */ msgPanel= new JPanel(); //set the size of the panel msgPanel.setPreferredSize(new Dimension (60,150)); //set the panel to the left msgPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); //add the border around the panel and set a title msgPanel.setBorder(BorderFactory.createTitledBorder("Message")); //TEXTFIELDS msgField = new JTextField(16); msgField.setText("Type message here"); //add text field and label to the sub panel msgPanel.add(msgField); /* * DELIVERY PANEL */ deliveryPanel= new JPanel(); //set the size of the panel deliveryPanel.setPreferredSize(new Dimension (140,150)); //set the panel to the left deliveryPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); //add the border around the panel and set a title deliveryPanel.setBorder(BorderFactory.createTitledBorder("Delivery Options")); //create the radio buttons standard = new JRadioButton("Standard"); holiday = new JRadioButton("Holiday"); //create the check boxes beforeNoon = new JCheckBox("Before Noon"); //combo boxes nameBox = new JComboBox(names); //set the grid layout deliveryPanel.setLayout(new GridLayout(2,1)); // group the radio buttons ButtonGroup bg1 = new ButtonGroup(); bg1.add(standard); bg1.add(holiday); //bg1.add(beforeNoon); //add the radio button to the panel deliveryPanel.add(standard); deliveryPanel.add(holiday); deliveryPanel.add(beforeNoon); deliveryPanel.add(nameBox); /* * TOTAL COST PANEL */ totalCostPanel= new JPanel(); //set the size of the panel totalCostPanel.setPreferredSize(new Dimension (140,150)); //set the panel to the left totalCostPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); //add the border around the panel and set a title totalCostPanel.setBorder(BorderFactory.createTitledBorder("Total Cost")); //TEXTFIELDS totalField = new JTextField(12); totalField.setEditable(false); //add text field and label to the sub panel totalCostPanel.add(totalField); /* * BUTTONS PANEL */ //create buttons submitButton = new JButton("Submit Order"); submitButton.setToolTipText("Click to submit your order"); writeButton = new JButton("Print Message Cards"); writeButton.setToolTipText("Click to print your Message Cards"); scheduleButton = new JButton("Delivery Schedule"); scheduleButton.setToolTipText("Click for your Delivery Schedule"); clearButton = new JButton("Clear"); clearButton.setToolTipText("Click to clear your order"); exitButton = new JButton("Exit"); exitButton.setToolTipText("Click to exit"); //Mnemonic exitButton.setMnemonic(KeyEvent.VK_E); submitButton.setMnemonic(KeyEvent.VK_S); clearButton.setMnemonic(KeyEvent.VK_C); writeButton.setMnemonic(KeyEvent.VK_P); scheduleButton.setMnemonic(KeyEvent.VK_D); //register Buttons submitButton.addActionListener(new ButtonListener()); writeButton.addActionListener(new ButtonListener()); scheduleButton.addActionListener(new ButtonListener()); clearButton.addActionListener(new ButtonListener()); exitButton.addActionListener(new ButtonListener()); //add buttons to the panel buttonPanel.add(submitButton); buttonPanel.add(writeButton); buttonPanel.add(scheduleButton); buttonPanel.add(clearButton); buttonPanel.add(exitButton); /* * INPUT PANEL */ inputPanel = new JPanel(); //create empty label inputLabel = new JLabel("Numbers Of Orders "+ orderCount); inputPanel.add(inputLabel); // //ADD COMPONENTS //grid layout // containerPanel.setLayout(new GridLayout(3, 3)); //add the components to the content pane add(containerPanel); add(buttonPanel, BorderLayout.SOUTH); add(inputPanel, BorderLayout.PAGE_END); //add each individual panel to the container panel containerPanel.add(subPanel1); containerPanel.add(subPanel2); containerPanel.add(bouquetSizePanel); containerPanel.add(msgPanel); containerPanel.add(deliveryPanel); containerPanel.add(totalCostPanel); } /** * Prints the file using JFileChooser. */ public void printFile() { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); try { BufferedWriter out = new BufferedWriter(new FileWriter(selectedFile)); out.write(manager.printMessageCards()); out.close(); } catch (IOException e1) { } } } /** * Prints the schedule from a file. */ public void printSchedule() { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); try { BufferedWriter out = new BufferedWriter(new FileWriter(selectedFile)); out.write(manager.deliverySchedule()); out.close(); } catch (IOException e1) { } } } /** * Action Listener class * * */ private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource()==exitButton) { System.exit(0); } if(e.getSource()==writeButton) { printFile(); } if(e.getSource()==scheduleButton) { printSchedule(); } if (e.getSource()==submitButton) { orderCount++; inputLabel.setText("Numbers Of Orders "+ orderCount); toLn = fNameField.getText(); toFn = lNameField.getText(); toStr = cityField.getText(); toC = streetField.getText(); toSt = stateField.getText(); toZ = Integer.parseInt(zipField.getText()); fromLn = fNameField2.getText(); fromFn = lNameField2.getText(); fromStr = cityField2.getText(); fromC = streetField2.getText(); fromSt = stateField2.getText(); fromZ = Integer.parseInt(zipField2.getText()); beforeN = false; d = (String) nameBox.getSelectedItem(); if(beforeNoon.isSelected()) { beforeN = true; } if(!(beforeNoon.isSelected())) { beforeN = false; } System.out.println(beforeN); if (small.isSelected()) { bsize =19; if (standard.isSelected()) { ship = 7; d = (String) nameBox.getSelectedItem(); //nameBox.removeAllItems(); } else if (holiday.isSelected()) { ship = 10; } } else if (medium.isSelected() ) { bsize=35; if (standard.isSelected()) { ship = 10; d = (String) nameBox.getSelectedItem(); } else if (holiday.isSelected()) { ship = 15; d = (String) nameBox.getSelectedItem(); } } else if(large.isSelected()) { bsize=49; if (standard.isSelected()) { ship = 15; d = (String) nameBox.getSelectedItem(); } else if (holiday.isSelected()) { ship = 20; d = (String) nameBox.getSelectedItem(); } } //read the message msg = msgField.getText(); //debugger System.out.print(toLn+toFn+toStr+toC+toSt+toZ+fromLn+fromFn+fromStr+fromC+fromSt+fromZ+beforeN+d+ship+bsize+msg+"\n"); //add info to addOrder manager.addOrder(toLn, toFn, toStr, toC, toSt, toZ, fromLn, fromFn, fromStr, fromC, fromSt, fromZ, beforeN, d, ship, bsize, msg); beforeN = false; } } } }
Here is my Order class codeJava Code:import java.text.DecimalFormat; import java.util.ArrayList; /** * @author Prabhdeep Singh * OrderManager which implements the Inteface */ public class OrderManager implements OrderManagerInterface { private ArrayList<Comparable> orders; private int numOrders; /** * intializes number of orders to zero and orders arraylist */ public OrderManager() { orders=new ArrayList<Comparable>(); numOrders=0; } /** * Sorts all orders by date and then * prints out the message cards for each order in the following format: </br> * Dear Mom </br> * {message} </br> * Love {first name of FROM} </br> * {blank line} </br> * {blank line} </br> * @return String of all message cards from all orders */ public String printMessageCards() { Sorting.selectionSort(orders); String out=""; Order temp; for(Comparable x: orders) { temp=(Order)x; out+="Dear Mom\n"+temp.getMessage()+"\nLove "+temp.getFromFirst()+"\n\n\n"; } return out; } /** * * @param toLn Last Name of TO * @param toFn First Name of TO * @param toStr Street of TO * @param toC City of TO * @param toSt State of TO * @param toZ Zip of TO * @param fromLn Last Name of FROM * @param fromFn First Name of FROM * @param fromStr Street of FROM * @param fromC City of FROM * @param fromSt State of FROM * @param fromZ Zip of FROM * @param beforeN boolean - true if before noon delivery * @param d date of delivery in form of string * @param ship ship type (1=standard, 2=holiday) * @param bSize bouquet size (1=small, 2=medium, 3=big) * @param msg message from FROM * @return total cost of the bouquet */ public double addOrder(String toLn, String toFn, String toStr, String toC, String toSt, int toZ, String fromLn, String fromFn, String fromStr, String fromC, String fromSt, int fromZ, boolean beforeN, String d, int ship, int bSize, String msg) { Order temp=new Order(toLn, toFn, toStr, toC, toSt, toZ, fromLn, fromFn, fromStr, fromC, fromSt, fromZ, beforeN, d,ship, bSize, msg); orders.add(temp); numOrders++; return temp.calculateCost(); } /** * Sorts the order items by the date string. Returns a string * in the following format: </br> * Delivery Schedule </br> * {blank} </br> * {blank} </br> * {Date} </br> * *{TO Street}, {TO City} - {TO Last Name}, {TO First Name} (a before noon delivery, preceeded by *)</br> * {TO Street}, {TO City} - {TO Last Name}, {TO First Name} (an after noon delivery)</br> * { . . . the rest for this date} </br> * {blank} </br> * {blank} </br> * {next Date} </br> * { . . .} </br> * @return the delivery schedule */ public String deliverySchedule() { Sorting.selectionSort(orders); String sched="Delivery Schedule\n\n\nMay 8\n"; String currentDate="May 8"; Order temp; for(Comparable x: orders) { temp=(Order)x; if (temp.getDate().compareTo(currentDate)!=0) { currentDate=temp.getDate(); sched+="\n\n"+currentDate+"\n"; } if (temp.getBeforeNoon()) { sched+="*"; } sched+=temp.getToStr()+", "+temp.getToC()+" - "+temp.getToLn()+", "+temp.getToFn()+"\n"; } return sched; } /** * @return the current number of orders held by the manager */ public int getNumOrders() { return numOrders; } }
My problem - unable to display the cost returned by the CalculateCost() method, which is called through the OrderManager method add(order), in the totalCost textfield in my panel class.Java Code:/** * * @author Prabhdeep Singh * Order class implements the Comparable Interface */ public class Order implements Comparable<Order> { private int size, toZ, fromZ, ship; private boolean beforeN; private String toLn, toFn, toStr, toC, toSt, fromLn, fromFn, fromStr, fromC, fromSt, date, msg; /** * * @param toLn last name of recipient * @param toFn first name of recipient * @param toStr street address of recipient * @param toC city of recipient * @param toSt state of recipient * @param toZ zip code of recipient * @param fromLn last name of sender * @param fromFn first name of sender * @param fromStr street address of sender * @param fromC city of sender * @param fromSt state of sender * @param fromZ zip code of sender * @param beforeN is the delivery before noon * @param d the date of delivery * @param ship the type of shipping(1 for standard, 2 for holiday) * @param bSize the size of the bouquet(1 for small, 2 for medium, 3 for large) * @param msg the personal message attached to be sent to a loving mother */ public Order(String toLn, String toFn, String toStr, String toC, String toSt, int toZ, String fromLn, String fromFn, String fromStr, String fromC, String fromSt, int fromZ, boolean beforeN, String d, int ship, int bSize, String msg) { this.toLn=toLn; this.toFn=toFn; this.toStr=toStr; this.toC=toC; this.toSt=toSt; this.toZ=toZ; this.fromLn=fromLn; this.fromFn=fromFn; this.fromStr=fromStr; this.fromC=fromC; this.fromSt=fromSt; this.fromZ=fromZ; this.beforeN=beforeN; date=d; this.ship=ship; size=bSize; this.msg=msg; } public String getDate() { return date; } public void setDate(String date) { this.date=date; } public boolean getBeforeNoon() { return beforeN; } public void setBeforeNoon(boolean beforeN) { this.beforeN=beforeN; } public String getMessage() { return msg; } public void setMessage(String msg) { this.msg= msg; } public String getFromLast() { return fromLn; } public void setFromLast(String fromLn) { this.fromLn=fromLn; } public String getFromFirst() { return fromFn; } public void setFromFirst(String fromFn) { this.fromFn=fromFn; } public String getToFn() { return toFn; } public void setToFn(String toFn) { this.toFn=toFn; } public String getToLn() { return toLn; } public void setToLn(String toLn) { this.toLn=toLn; } public String getToStr() { return toStr; } public void setToStr(String toStr) { this.toStr=toStr; } public String getToC() { return toC; } public void setToC(String toC) { this.toC=toC; } /** * * @return the total cost of the order */ public double calculateCost() { double cost=0; switch (size) { case 1: if (ship==1) cost=7+19; else cost=10+19; if(beforeN) cost+=5; break; case 2: if (ship==1) cost=10+35; else cost=15+35; if(beforeN) cost+=7; break; case 3: if (ship==1) cost=15+49; else cost=20+49; if(beforeN) cost+=10; break; } return cost; } /** * @param other the Order to be compared to * @return Compares Orders by date. */ public int compareTo(Order other) { int compare; compare=Integer.parseInt(date.substring(4))-Integer.parseInt(other.getDate().substring(4)); if (compare==0) { if ((beforeN && other.getBeforeNoon())|| (!beforeN && !other.getBeforeNoon())) compare=0; else if(beforeN) compare=-1; else compare=1; } return compare; } }
What i have tried. ToStringing the cost and then setting the textfield to that string - did not work, tried creating other methods, still did not work.
Any Help would be appreciated - to all the mods, i have done my homework, i am not asking anyone to do it for me. i just pointers and help regarding what i should do with the code. Please, anyone who is repsonding, dont try to confuse me even more.
- 12-13-2012, 12:47 AM #2
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: Linking Problems between Java methods
1. In your button listener class, where you call addOrder() you need to catch the value that is returned by addOrder() and then convert it to a String and call setText() for the totalField. This is a start, but there is another bug. See number 2 below.
2. In calculateCost() I found that the cost returned was zero because the size of the package was 19. In your switch statement, you should have a default case so that you'll always be alerted when none of the cases apply. See the code below.
3. I noticed that you are using a class to sort your collection of orders. Consider using the following to sort. It comes with the Java API and it's already debugged:
Collections.sort(orders);
4. Consider using the following for your list of orders:
ArrayList<Order> orders = new ArrayList<Order>();
This is better than using an ArrayList of Comparables, because then you can more easily get the orders out without type-casting to an Order.
5. I noticed that if I leave a zip code blank that I get a NumberFormatException and the program terminates. I would be good to use a try-catch and catch these exceptions and give the user an alert telling them why they can't submit the order and giving them a chance to enter the data.
That's all the suggestions that I have for you.
Java Code:/** * * @return the total cost of the order */ public double calculateCost() { double cost=0; switch (size) { case 1: if (ship==1) cost=7+19; else cost=10+19; if(beforeN) cost+=5; break; case 2: if (ship==1) cost=10+35; else cost=15+35; if(beforeN) cost+=7; break; case 3: if (ship==1) cost=15+49; else cost=20+49; if(beforeN) cost+=10; break; default: System.err.println("Size " + size + " is not recognized."); cost = 0; break; } return cost; }
- 12-13-2012, 01:09 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 22
- Rep Power
- 0
Re: Linking Problems between Java methods
@kaydell2
Thank you very much for that reccomendation and for fixing the errors. The problem i am having now is getting the bouquetsize to fit into the calculateCost(). whenever i selected the choices from my in gui for small, medium, or large, it says size not recognized, is there a way to fix that?
Similar Threads
-
Problems with morphing Methods
By wallyll in forum New To JavaReplies: 1Last Post: 11-13-2012, 04:34 PM -
problems with multiple methods and paramaters...
By silafirion in forum New To JavaReplies: 13Last Post: 12-07-2010, 01:10 AM -
Having problems with methods
By JavatastesGood in forum New To JavaReplies: 2Last Post: 10-02-2010, 10:57 PM -
Problems With Array/Methods
By blueduiker in forum New To JavaReplies: 4Last Post: 01-19-2010, 01:49 AM -
Problems implementing Java string methods
By everlast88az in forum New To JavaReplies: 8Last Post: 11-06-2008, 04:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks