Solved

how do I display customer info

Posted on 2007-04-01
3
403 Views
Last Modified: 2013-11-23
I am new to Java and I need Help with displaying customer info
here is my code
public class Customer
{
      // attribute definitions
       private String name;
       private String address;
       private String phoneNo;
            
      // constructor with parameters
      public Customer(String aName, String anAddress, String aPhoneNo)
      {
            // invoke setters to populate attributes
            setName(aName);
            setAddress(anAddress);
            setPhoneNo(aPhoneNo);
      }
      
      // getters
       public String getName()
             { return name;}
      public String getAddress()
             { return address;}
      public String getPhoneNo()
             { return phoneNo;}

      // setters
      public void setName(String newName)
            { name = newName;}
      public void setAddress(String newAddress)
            { address = newAddress;}
      public void setPhoneNo(String newPhoneNo)
            { phoneNo = newPhoneNo;}
      
      public String tellAboutSelf()
            {
                  String info;
                  info = "Customer name = "
                              + getName() + ", Address = "
                              + getAddress() + ", Phone Number = "
                              + getPhoneNo();
                  return info;
            }
}
and my display class
import java.awt.*;                   // Font, Color, Layout managers            
import javax.swing.*;             // GUI components            
import java.awt.event.*;       // ActionEvent, ActionListener

public class DisplayCustomer extends JFrame implements ActionListener
{
      // variables needing class scope
      JTextField customerNameText, customerAddressText, customerPhoneText;
      JButton acceptButton, clearButton, closeButton;
      Customer aCustomer;       
      String customerName, customerAddress, customerPhone;         

      public static void main(String args[])
      {      // create instance of Frame
            DisplayCustomer frame = new DisplayCustomer();
      }
      // constructor
      public DisplayCustomer()
      {
            Container c = this.getContentPane();
            c.setLayout(new GridLayout(3,1));
            JPanel centerPanel = new JPanel(new GridLayout(3,2));
            JPanel lowerPanel = new JPanel(new FlowLayout());

            // create logo
            JLabel logoLabel = new JLabel(" ",SwingConstants.CENTER);
            logoLabel.setForeground(Color.red);
            logoLabel.setFont(new Font("TimesRoman", Font.ITALIC,36));
            logoLabel.setText("Bradshaw Marina");
            c.add(logoLabel);       

            // create TextFields for name, address & phone
            customerNameText = new JTextField();
            customerAddressText = new JTextField();            
            customerPhoneText = new JTextField();
            // add labels and TextFields to the panels
            centerPanel.add(new JLabel("Name: ", SwingConstants.RIGHT));
            centerPanel.add(customerNameText);
            centerPanel.add(new JLabel("Address: ", SwingConstants.RIGHT));
            centerPanel.add(customerAddressText);            
            centerPanel.add(new JLabel("Phone: ", SwingConstants.RIGHT));
            centerPanel.add(customerPhoneText);            
            c.add(centerPanel);
            // create & add Buttons to bottom panel
            acceptButton = new JButton("Accept");
            clearButton = new JButton("Clear");
            closeButton = new JButton("Close");
            lowerPanel.add(acceptButton);
            lowerPanel.add(clearButton);
            lowerPanel.add(closeButton);
            c.add(lowerPanel);             
            
            // register frame as listener for button events
            acceptButton.addActionListener(this);
            clearButton.addActionListener(this);            
            closeButton.addActionListener(this);            

            this.setSize(300,200);    
            this.setTitle("Display A Customer");
            this.setVisible(true);

            // create anonymous inner class to handle window closing event
            this.addWindowListener(new WindowAdapter()
                  {
                        public void windowClosing(WindowEvent event)
                                 {shutDown();}
                  }       
             );
   }
   // actionPerformed is invoked when a Button is clicked
   public void actionPerformed(ActionEvent e)
   {
            // see which button was clicked
            if(e.getSource() == acceptButton)
                 {displayCustomer();}
              if(e.getSource() == clearButton)
                 {clearForm();}
            if(e.getSource() == closeButton)
                  {shutDown();}
   }
      private void displayCustomer()
      {
            customerName = customerNameText.getText();      
            customerAddress = customerAddressText.getText();            
            customerPhone = customerPhoneText.getText();      
            
            if(customerName.length() == 0 || customerAddress.length() == 0 || customerPhone.length() == 0)
               JOptionPane.showMessageDialog(this, "Please Enter All Data ");
         else  
                  {
                     aCustomer = new Customer(customerName, customerAddress, customerPhone);
                   JOptionPane.showMessageDialog(this, /*System.out.print*/(aCustomer));
                   System.out.print(aCustomer);
                        
                        clearForm();
                  }

      }
      private void clearForm()
      {
            customerNameText.setText("");      
            customerAddressText.setText("");            
            customerPhoneText.setText("");
            customerNameText.requestFocus();
      }
      
       public void shutDown()
            { System.exit(0); }
}
0
Comment
Question by:jhelp_1
3 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18833138
What's the problem you are facing/
0
 

Author Comment

by:jhelp_1
ID: 18833355
I want to display name, address and phone using print ln & a message box
JOptionPane.showMessageDialog(this, " Name " + customerName + "\n" + " Address " + customerAddress +
      "\n" + " Phone " + customerPhone);

this is how i am doing it now or
System.out.print(" Name " + customerNameText.getText() + "\n" +
                   " Address " + customerAddressText.getText() + "\n" + " Phone " + customerPhoneText.getText()); also works
but  
JoptionPane.showMessageDialog(System.out.print(" Name " + customerNameText.getText() + "\n" +
                   " Address " + customerAddressText.getText() + "\n" + " Phone " + customerPhoneText.getText()));
gives me an error      
                                           clearForm();
here iss the error
DisplayCustomer.java:103: 'void' type not allowed here
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 500 total points
ID: 18834319
the method signature for joptionPane.showMessageDialog takes a component and a object..
you are passing neither of these..

the code needs to be changed as

JOptionPane.showMessageDialog(this, " Name " + customerName + "\n" + " Address " + customerAddress +
      "\n" + " Phone " + customerPhone);

not a system.out.println inside the method..
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question