7

Picking java back up and trying to familiarize myself with enum types. I'm trying to create an address book where user can create contacts. I've created everything already, but am hung up on setting a contact type (Family, Friends, Business, etc...) I've set up an enum class in a separate java class.

public class ContactType
{
    public enum contactType
    {
        Family,
        Church,
        Friend,
        BusninessColleague,
        ServicePerson,
        Customer,
        Other
    }
}

With my Contacts class looking like:

public class Contacts
{
        private contactType contact;
    private String name;                    
    private String streetAddress;           
    private String city;                    
    private String state;                   
    private String zipCode;                 
    private String phone;                   
    private String email;                   
    private String photo;                   


    public Contacts ( )
    {
        contact = null;
        name = "XXX XXX";
        streetAddress = "XXX";
        state = "XX";
        zipCode = "00000";
        phone = "XXX-XXXX";
        email = "XXX@XXX.COM";
        photo = "XXX.jpg";
    }


    public Contacts (ContactType contactType, String name, String streetAddress, String city,
                        String state, String zipCode, String phone, String email, String photo)
    {

        this.contact = contactType;
        this.name = name;
        this.streetAddress = streetAddress;
        this.city = city;
        this.state = state;
        this.zipCode = zipCode;
        this.phone = phone;
        this.email = email;
        this.photo = photo;
    }



    public ContactType getContactType ( )
    {
        return contact;
    }


    public void setContactType (ContactType input)
    {
        this.contact = input;
    }

  //rest of code

and finally my driver, it includes a menu (everything else works except for setting the contact type so i've just included that snippet for the sake of keeping it short):

switch (iSelection)
  {
    case 1: 
          c1 = new Contacts();      //creates a new contact
          break;
    case 2:
          strContactType = JOptionPane.showInputDialog ("Please enter contact type (Family, Church, BusinessColleague, ServicePerson, Customer, or Other)");
          contactType.valueOf(strContactType);
          JOptionPane.showMessageDialog (null, strContactType);
          c1.setContactType (strContactType);
          break;

I know i've done something wrong here c1.setContactType(strContactType); because i'm getting "The method setContactType(ContactType) in the type Contacts is not applicable for the arguments (String)" due to complete ignorance I don't know how to fix it to set the contactType to whatever the user inputs.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    I've deleted your [tag:javascript] tag as your question has nothing to do with use of this language. Please understand that these are two completely different programming languages, about as closely related as ham is to hamburger, that if you mis-tag your question you will not get the right experts in to review it, and that this may hurt your chances of getting decent help. – Hovercraft Full Of Eels Feb 24 '15 at 19:31

2 Answers2

4

Your problem is that you call valueOf(...) but you discard the ContactType object that is returned by this method call.... i.e., you never assign the returned object to a variable.

First get rid of your unnecessary wrapper by changing this:

public class ContactType
{
    public enum contactType
    {
        Family,
        Church,
        Friend,
        BusninessColleague,
        ServicePerson,
        Customer,
        Other
    }
}

to this:

    public enum ContactType
    {
        Family,
        Church,
        Friend,
        BusninessColleague,
        ServicePerson,
        Customer,
        Other
    }

Next use the valueOf to set a ContactType variable.

      strContactType = JOptionPane.showInputDialog ("Please enter contact type (Family, Church, BusinessColleague, ServicePerson, Customer, or Other)");
      ContactType contactType = ContactType.valueOf(strContactType);
      JOptionPane.showMessageDialog (null, strContactType);
      c1.setContactType (contactType);

Note, I would try to make it more idiot-proof by limiting the selections to a ContactType. For example:

  Object selection = JOptionPane.showInputDialog(null,
        "Choose a contact type", "Contact Type",
        JOptionPane.INFORMATION_MESSAGE, null,
        ContactType.values(), ContactType.values()[0]);

  // check that selection is not null before using
  System.out.println(selection);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
-1

Call ContactType.valueOf(strContactType) to convert string name to Enum value

Zielu
  • 8,312
  • 4
  • 28
  • 41