Solved

how do I display customer info

Posted on 2007-04-01
3
397 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:mayankeagle
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now