Results 1 to 2 of 2
  1. #1
    crazydeo is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Question 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.

    Java 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("");
    		}
    	}
    }

  2. #2
    crazydeo is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Convert a vector to a string array
    By orchid in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 03:31 AM
  2. How i add string array in vector
    By susan in forum New To Java
    Replies: 7
    Last Post: 02-12-2009, 02:32 PM
  3. String []Array
    By Warren in forum New To Java
    Replies: 4
    Last Post: 12-01-2007, 09:03 AM
  4. Help with string and array in java
    By zoe in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:12 AM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •