Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how do I display customer info

Posted on 2007-04-01
3
Medium Priority
?
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

705 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