Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2008, 12:48 AM
Member
 
Join Date: May 2008
Posts: 20
crazydeo is on a distinguished road
Need Help with String Array
I need help with my project.... Everything seems to work except for when I add a new bagel and the program checks for duplicates it doesn't recognize egg or rye and can be added twice. Where as any other bagel option doesn't allow to be entered twice. Attached is my code.

Code:
import java.text.*; import javax.swing.*; import java.awt.event.*; public class Ch6Assignment7 extends JFrame implements ItemListener, ActionListener { //create the main panel, radio buttons, and text fields for the panel JPanel mainPanel = new JPanel(); JPanel itemPanel = new JPanel(); String bagelNameString [] = {"Plain", "Egg", "Rye", "Salt", "Blueberry", "Garlic", "Onion", "Sesame", "Poppy Seed", "The Works"}; JComboBox bagelComboBox = new JComboBox(bagelNameString); JTextField bagelQuantityTextField = new JTextField("0",5); JCheckBox creamCheeseButton = new JCheckBox("Cream Cheese"); String creamCheeseNameString [] = {"Plain", "Herb", "Garlic"}; JComboBox creamCheeseComboBox = new JComboBox(creamCheeseNameString); JTextField creamCheeseQuantityTextField = new JTextField("0",5); JTextField newItemTextField = new JTextField(10); JRadioButton bagelRadioButton = new JRadioButton("Bagel"); JRadioButton creamRadioButton = new JRadioButton("Cream Cheese"); JRadioButton itemInvisibleRadioButton = new JRadioButton(""); ButtonGroup itemButtonGroup = new ButtonGroup(); JButton purchaseButton = new JButton("Purchase Items"); JButton clearButton = new JButton("Clear Items"); JButton summaryButton = new JButton("Summary of Sales"); JButton addButton = new JButton("Add an item"); JButton deleteButton = new JButton("Delete an item"); JTextArea outputTextArea = new JTextArea(20,30); JScrollPane mainScrollPane = new JScrollPane(outputTextArea); public static void main(String[] args) { // Create an object of the class Ch6Assignment7 myBagelShop = new Ch6Assignment7(); myBagelShop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public Ch6Assignment7() { //setting title, size of the designFrame designFrame(); add(mainPanel); setTitle("Bagel Shop"); setSize(350,600); setVisible(true); } public void designFrame() { //add items to the panels itemButtonGroup.add(bagelRadioButton); itemButtonGroup.add(creamRadioButton); itemButtonGroup.add(itemInvisibleRadioButton); itemPanel.add(bagelRadioButton); itemPanel.add(creamRadioButton); mainPanel.add(new JLabel("Bagel Variety")); mainPanel.add(bagelComboBox); mainPanel.add(new JLabel("Bagel Quantity")); mainPanel.add(bagelQuantityTextField); creamCheeseComboBox.setEnabled(false); creamCheeseQuantityTextField.setEditable(false); mainPanel.add(creamCheeseButton); mainPanel.add(creamCheeseComboBox); mainPanel.add(new JLabel("Quantity")); mainPanel.add(creamCheeseQuantityTextField); mainPanel.add(itemPanel); mainPanel.add(new JLabel(" New Item to Add")); mainPanel.add(newItemTextField); mainPanel.add(purchaseButton); mainPanel.add(clearButton); mainPanel.add(summaryButton); mainPanel.add(addButton); mainPanel.add(deleteButton); mainPanel.add(mainScrollPane); newItemTextField.addActionListener(this); purchaseButton.addActionListener(this); clearButton.addActionListener(this); summaryButton.addActionListener(this); addButton.addActionListener(this); deleteButton.addActionListener(this); creamCheeseButton.addItemListener(this); bagelRadioButton.addItemListener(this); creamRadioButton.addItemListener(this); } //Different actions performed when the buttons are pushed public void actionPerformed(ActionEvent evt) { Object sourceObject = evt.getSource(); //if purchase button pushed everything sent to total class for calculation if(sourceObject == purchaseButton) { total(); } //if summary button pushed everything thing is sent to summary for calculation else if(sourceObject == summaryButton) { summary(); } //if clear button pushed everything thing is cleared else if(sourceObject == clearButton) { clear(); } //if bagel radio button is selected and add button is pushed items are added if( bagelRadioButton.isSelected()&& sourceObject == addButton ) { addItem(); } //if bagel radio button is selected and delete button is pushed items are deleted else if(bagelRadioButton.isSelected() && sourceObject == deleteButton) { deleteItem(); } //if cream radio button is selected and add button is pushed items are added if( creamRadioButton.isSelected() && sourceObject == addButton ) { add2Item(); } //if cream radio button is selected and delete button is pushed items are deleted else if(creamRadioButton.isSelected() && sourceObject == deleteButton) { delete2Item(); } } public void addItem() { String inputString; boolean foundBagelBoolean; inputString = newItemTextField.getText(); //Check if the text field is not blank if((!(inputString.equals("")))) { //checking for duplicate foundBagelBoolean = checkInput(inputString); displayBagelFound(foundBagelBoolean, inputString); } else { JOptionPane.showMessageDialog(null,"Enter a bagel type to add"); newItemTextField.requestFocus(); } } public void deleteItem() { int indexInteger, resultInteger; indexInteger = bagelComboBox.getSelectedIndex(); String deleteString, outputString; //checking to make sure object exists to delete if(indexInteger != -1 ) { deleteString = (String) bagelComboBox.getItemAt(indexInteger); outputString = "Do wish to delete " + deleteString; resultInteger = JOptionPane.showConfirmDialog(null,outputString, "Confirmation",JOptionPane.YES_NO_OPTION); if(resultInteger == JOptionPane.YES_OPTION) { bagelComboBox.removeItemAt(indexInteger); } } else { JOptionPane.showMessageDialog(null,"Select an item to remove"); bagelComboBox.requestFocus(); } } public void add2Item() { String inputString; boolean foundCreamBoolean; inputString = newItemTextField.getText(); //Check if the text field is not blank if((!(inputString.equals("")))) { //checking for duplicate foundCreamBoolean = checkInput(inputString); displayCreamFound(foundCreamBoolean, inputString); } else { JOptionPane.showMessageDialog(null,"Enter a type of Cream Cheese to add"); newItemTextField.requestFocus(); } } public void delete2Item() { int indexInteger, resultInteger; indexInteger = creamCheeseComboBox.getSelectedIndex(); String deleteString, outputString; //checking to make sure object exists to delete if(indexInteger != -1 ) { deleteString = (String) creamCheeseComboBox.getItemAt(indexInteger); outputString = "Do wish to delete " + deleteString; resultInteger = JOptionPane.showConfirmDialog(null,outputString, "Confirmation",JOptionPane.YES_NO_OPTION); if(resultInteger == JOptionPane.YES_OPTION) { creamCheeseComboBox.removeItemAt(indexInteger); } } else { JOptionPane.showMessageDialog(null,"Select an item to remove"); creamCheeseComboBox.requestFocus(); } } //getting input from user and sending to calculation public void total() { int bagelQuantityInteger, creamCheeseQuantityInteger; double bagelDouble; bagelQuantityInteger = Integer.parseInt(bagelQuantityTextField.getText()); creamCheeseQuantityInteger = Integer.parseInt(creamCheeseQuantityTextField. getText()); Calculation myCalcs = new Calculation(bagelQuantityInteger, creamCheeseQuantityInteger); bagelDouble = myCalcs.getBagelCharge(); displayOutput(bagelQuantityInteger, creamCheeseQuantityInteger, bagelDouble); } //clearing all values public void clear() { itemInvisibleRadioButton.setSelected(true); bagelQuantityTextField.setText("0"); creamCheeseQuantityTextField.setText("0"); creamCheeseQuantityTextField.setEditable(false); newItemTextField.setText(""); creamCheeseComboBox.setEnabled(false); creamCheeseButton.setSelected(false); outputTextArea.setText(""); bagelComboBox.requestFocus(); } //different options for when the Cream Cheese button is selected public void itemStateChanged(ItemEvent evt) { if(creamCheeseButton.isSelected()) { creamCheeseComboBox.setEnabled(true); creamCheeseQuantityTextField.setEditable(true); } else if(!creamCheeseButton.isSelected()) { creamCheeseComboBox.setEnabled(false); creamCheeseQuantityTextField.setEditable(false); } } //checking to insure the object is found and no duplicates exist public boolean checkInput(String userInputString) { boolean foundBoolean = false; int indexInteger = 0; String inputString = new String(); while(!foundBoolean && indexInteger < bagelComboBox.getItemCount()) { inputString = (String) bagelComboBox.getItemAt(indexInteger); if(userInputString.equalsIgnoreCase(inputString)) { foundBoolean = true; } else { indexInteger++; } while(!foundBoolean && indexInteger < creamCheeseComboBox.getItemCount()) { inputString = (String) creamCheeseComboBox.getItemAt(indexInteger); if(userInputString.equalsIgnoreCase(inputString)) { foundBoolean = true; } else { indexInteger++; } }} return foundBoolean; } //display output of totals public void displayOutput(int bagelQuantityInteger, int creamCheeseQuantityInteger, double bagleChargeDouble) { Calculation myCalcs = new Calculation(); DecimalFormat formatDecimal = new DecimalFormat("$0.00"); DecimalFormat formatNumber = new DecimalFormat("0"); String outputString; outputString = "Type of Bagel: " + bagelComboBox.getSelectedItem() + '\n' + "Quantity:" + formatNumber.format(bagelQuantityInteger)+ '\n'+ "Cream Cheese: " + creamCheeseComboBox.getSelectedItem()+ '\n' + "Quantity:" + formatNumber.format(creamCheeseQuantityInteger)+ '\n' + "Bagels: " + formatDecimal.format(myCalcs.getBagelTotal())+ '\n' + "Cream Cheese:" + formatDecimal.format(myCalcs.getCreamCheeseTotal())+ '\n' + "Total: " + formatDecimal.format(myCalcs.getGrandTotalDouble()) + '\n'; outputTextArea.append(outputString); } public void summary() { Calculation myCalculations = new Calculation(); DecimalFormat formatDecimal = new DecimalFormat("$0.00"); DecimalFormat formatNumber = new DecimalFormat("0"); JOptionPane.showMessageDialog(null,"Total Revenue from Bagels:" + formatDecimal.format(myCalculations.getBagelTotal())+'\n'+ "Total Revenue from Cream Cheese: " + formatDecimal.format (myCalculations.getCreamCheeseTotal())+'\n'+ "Total Orders Placed: " + formatNumber.format(myCalculations.getGrandTotalInteger())+'\n'+ "Total Bagels " + "Ordered: " + formatNumber.format(myCalculations.getTotalNumberOfBagelsInteger()) +'\n' + "Total Cream Cheese Ordered: " + formatNumber.format (myCalculations.getTotalNumberOfCreamCheeseInteger())+'\n'); } //validation for bagels public void displayBagelFound(boolean bagelBoolean, String inputString) { if(bagelBoolean) { JOptionPane.showMessageDialog(null,"This bagel is already in the list"); newItemTextField.selectAll(); newItemTextField.requestFocus(); } else { bagelComboBox.addItem(inputString); JOptionPane.showMessageDialog(null,"This bagel is added to the list"); newItemTextField.setText(""); } } //validation for cream cheese public void displayCreamFound(boolean creamBoolean, String inputString) { if(creamBoolean) { JOptionPane.showMessageDialog(null,"This Cream Cheese is already in the list"); newItemTextField.selectAll(); newItemTextField.requestFocus(); } else { creamCheeseComboBox.addItem(inputString); JOptionPane.showMessageDialog(null,"This Cream Cheese is added to the list"); newItemTextField.setText(""); } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-13-2008, 10:18 AM
Member
 
Join Date: May 2008
Posts: 20
crazydeo is on a distinguished road
Any help would help a lot!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
String []Array Warren New To Java 4 12-01-2007 09:03 AM
Help with string and array in java zoe New To Java 1 08-07-2007 07:12 AM
I can't seem to pass the value of a string variable into a string array mathias Java Applets 1 08-03-2007 11:52 AM
How i add string array in vector susan New To Java 1 07-16-2007 06:44 AM
Convert a vector to a string array orchid New To Java 2 05-06-2007 08:14 AM


All times are GMT +3. The time now is 05:08 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org