简体   繁体   中英

It show the error that Customer is already defined.Please let me know whats wrong and how to correct it

There is a compliation error stating that class name is already define i can't find the way to resolve it

further the class name are declared only once and can't find the place where the things are going wrong

package practo;
import java.io.*;
import java.lang.*;
import java.util.*;

@SuppressWarnings("unused")
class Customer     /* compilation error occurs here */
{
    private int id;
    private String name;
    private String email;
    private String address;
    void setid(int id)
    {
        this.id=id;
    }
    int getid()
    {
        return id;
    }
    void setname(String name)
    {
        this.name=name;
    }
    String getname()
    {
        return name;
    }
    void setemail(String email)
    {
        this.email=email;
    }
    String getemail()
    {
        return email;
    }
    void setaddress(String address)
    {
        this.address=address;
    }
    String getaddress()
    {
        return address;
    }
    class PhoneNumber
    {
        private String phoneNumber;
        private String heldFromDate;
        private String heldToDate;
        void setphoneNumber(String phoneNumber)
        {
            this.phoneNumber=phoneNumber;
        }
        String getphoneNumber()
        {
            return phoneNumber;
        }

        void setheldToDate(String heldToDate)
        {
            this.heldToDate=heldToDate;
        }
        String getheldToDate()
        {
            return heldToDate;
        }

        public String getHeldFromDate() {
            return heldFromDate;
        }
        public void setHeldFromDate(String heldFromDate) {
            this.heldFromDate = heldFromDate;
        }
        class NumberType
        {
            private String code;
            private String description;
            void setcode(String code)
            {
                this.code=code;
            }
            void setdescription(String description)
            {
                this.description=description;
            }
            String getcode()
            {
                return code;
            }
            String getdescription()
            {
                return description;
            }
        }
    }
}

class x1
{
    public void main(String args[])
    {
        @SuppressWarnings("resource")
        Scanner s=new Scanner(System.in);
        Customer c=new Customer();
        Customer.PhoneNumber p=c.new PhoneNumber();
        Customer.PhoneNumber.NumberType n=p.new NumberType();
        System.out.println("Enter the customer details");
        System.out.println("Enter the  id :");
        int id=s.nextInt();
        c.setid(id);
        System.out.println(c.getid());
        System.out.println("Enter the name :");
        String name=s.nextLine();
        c.setname(name);
        System.out.println(c.getname());
        System.out.println("Enter the email :");
        String email=s.nextLine();
        c.setemail(email);
        System.out.println(c.getemail());
        System.out.println("Enter the address :");
        String address=s.nextLine();
        c.setaddress(address);
        System.out.println(c.getaddress());
        System.out.println("Enter the customer contact details");
        System.out.println("Enter the phone number :");
        String phoneNumber=s.nextLine();
        p.setphoneNumber(phoneNumber);
        System.out.println(p.getphoneNumber());
        System.out.println("Enter the held from date (dd/MM/yyyy)  :");
        String heldFromDate=s.next();
        p.setHeldFromDate(heldFromDate);
        System.out.println(p.getHeldFromDate());
        System.out.println("Enter the held to date (dd/MM/yyyy)  :");
        String heldToDate=s.next();
        p.setheldToDate(heldToDate);
        System.out.println(p.getheldToDate());
        System.out.println("Enter number type code :");
        String code=s.next();
        n.setcode(code);
        System.out.println(n.getcode());
        System.out.println("Enter number type description");
        String description=s.next();
        n.setdescription(description);
        System.out.println(n.getdescription());
    }

}

Can you please verify Customer class is not duplicate ? If it is not there, can you choose Clean from the Project menu, it might fix these errors. Sometime eclipse trouble us.

Check if you have another class called Customer in the package practo . That would cause a name conflict.

Your class does not give me any compilation error. You might try making the class public ie public class Customer and file name having the name Customer.java . It may happen that the package practo already contains a class named Customer .

Lots of recommendations for improvement:

  1. Open public class per file, and a file for each class. Your arrangement is confusing.
  2. Learn and follow the Java coding standards.
  3. Using a Date for a date instead of a String is a better design, especially with JDK 8 and the java.time package.
  4. Learn JUnit instead of that x1.main.

These are examples of how your classes should look.

Customer.java

    package practo;

/**
 * Created by Michael
 * Creation date 5/29/2016.
 * @link https://stackoverflow.com/questions/37511168/it-show-the-error-that-customer-is-already-defined-please-let-me-know-whats-wron
 */
public class Customer {

    private int id;
    private String name;
    private String email;
    private String address;

    public Customer() {
        this(0, "", "", "");
    }

    public Customer(int id, String name, String email, String address) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

PhoneNumber.java:

package practo;

/**
 * Created by Michael
 * Creation date 5/29/2016.
 * @link https://stackoverflow.com/questions/37511168/it-show-the-error-that-customer-is-already-defined-please-let-me-know-whats-wron
 */
public class PhoneNumber {

    private String phoneNumber;
    private String heldFromDate;  // Bad design.  This ought to be a Date, not a String
    private String heldToDate;    // Bad design.  This ought to be a Date, not a String

    public PhoneNumber() {
        this("", "", "");
    }

    public PhoneNumber(String phoneNumber, String heldFromDate, String heldToDate) {
        this.phoneNumber = phoneNumber;
        this.heldFromDate = heldFromDate;
        this.heldToDate = heldToDate;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getHeldFromDate() {
        return heldFromDate;
    }

    public void setHeldFromDate(String heldFromDate) {
        this.heldFromDate = heldFromDate;
    }

    public String getHeldToDate() {
        return heldToDate;
    }

    public void setHeldToDate(String heldToDate) {
        this.heldToDate = heldToDate;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM