9 Replies - 514 Views - Last Post: 12 November 2011 - 01:00 PM Rate Topic: -----

#1 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 27-October 09

Enumeration

Posted 09 November 2011 - 01:10 PM

Is enum same thing as regular switch case in C++? I'm having trouble linking enum to a print out. This is what I'm trying to do:

Create an application that contains an enumeration that represents the days of the week. Display a list of the days, then prompt the user for a day. Display business hours for the chosen day. Assume that the business is open from 11 to 5 on Sunday, 9 to 9 on weekdays, and 9 to 6 on Saturday.

This is what I have:
import javax.swing.JOptionPane;


public class DayOfWeek {
	Day day;
	
	public void Day(Day day) {
		this.day = day;
	}
	
	public void businessHours() {
		switch (day) {
			case SATURDAY: System.out.println("Open from 9 to 6.");
		     			 break;
		     			 
			case SUNDAY: System.out.println("Open from 11 to 5.");
					     break;
					     
			default:	 System.out.println("Open from 9 to 9.");
					     break;
		}
	}
	
	public static void main(String[] args) {
		
		String dayInput = JOptionPane.showInputDialog("Please input a day: ");
		
		EnumDay sixthDay = new EnumDay(Day.SATURDAY);
		sixthDay.businessHours();
		EnumDay seventhDay = new EnumDay(Day.SUNDAY);
		seventhDay.businessHours();
		
		if (dayInput == "Saturday")
		{
		JOptionPane.showMessageDialog(null, sixthDay.businessHours());
		System.exit(0);
		}
		
		else if (dayInput == "Sunday")
		{
			JOptionPane.showMessageDialog(null, seventhDay.businessHours());
			System.exit(0);
		}
		else
		{
			JOptionPane.showMessageDialog(null, default.businessHours());
			System.exit(0);
		}
	}
}



and for enum class:
public enum Day {
	
	SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
    THURSDAY, FRIDAY, SATURDAY 

}


This post has been edited by alex71385: 09 November 2011 - 01:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Enumeration

#2 Veitch  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 11

Re: Enumeration

Posted 09 November 2011 - 01:17 PM

Use equals to compare strings:
if (string1.equals(string2))

string1 == string2 will only result into true if both Strings are of the same instance. Mostly that is not what you want to know. You want to test, whether the strings have the same content, so you have to use equals.

Edit:
You might also consider using equalsIgnoreCase(). So it won't matter if the user writes "FRIDAY" or "friday" or "Friday" ...

This post has been edited by Veitch: 09 November 2011 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Enumeration

Posted 09 November 2011 - 06:53 PM

Why building an enum if you compare with Strings ???
Was This Post Helpful? 0
  • +
  • -

#4 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 27-October 09

Re: Enumeration

Posted 11 November 2011 - 01:12 PM

View Postpbl, on 09 November 2011 - 06:53 PM, said:

Why building an enum if you compare with Strings ???


Because that's the assignment :(
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Enumeration

Posted 11 November 2011 - 05:55 PM

So the enum is useless

Or you use an enum that gives int values to your possible different values
or you compare with the String

assignment or not it is one or the other

I think you missed the asignment goal or purpose
Was This Post Helpful? 0
  • +
  • -

#6 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 27-October 09

Re: Enumeration

Posted 12 November 2011 - 10:31 AM

View Postpbl, on 11 November 2011 - 05:55 PM, said:

So the enum is useless

Or you use an enum that gives int values to your possible different values
or you compare with the String

assignment or not it is one or the other

I think you missed the asignment goal or purpose


Missed the assignment goal?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9029
  • View blog
  • Posts: 33,489
  • Joined: 27-December 08

Re: Enumeration

Posted 12 November 2011 - 10:34 AM

Enums work almost exactly like classes. You can still have instance fields, constructors, getter/setter methods, other methods, etc.

A basic example:
public enum RPS{

    ROCK("ROCK"), 
    PAPER("PAPER"),
    SCISSORS("SCISSORS");
  
    private String name;

    RPS(String name){ this.name = name; }

}


This post has been edited by macosxnerd101: 12 November 2011 - 11:30 AM
Reason for edit:: Removed public keyword

Was This Post Helpful? 1
  • +
  • -

#8 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 993
  • Posts: 2,203
  • Joined: 05-April 11

Re: Enumeration

Posted 12 November 2011 - 11:28 AM

macosxnerd101 I believe enum's can only have private constructors :) at least that makes most sense
Was This Post Helpful? 1
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9029
  • View blog
  • Posts: 33,489
  • Joined: 27-December 08

Re: Enumeration

Posted 12 November 2011 - 11:31 AM

No access modifier seems to work, but public does not. My bad. Good catch! :)
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,776
  • Joined: 20-September 08

Re: Enumeration

Posted 12 November 2011 - 01:00 PM

You should try to keep String out of the picture. Use the following, which will also obviate any validation problems (line 1 of the code could come in useful later - otherwise you can get rid of it):


	Day[] days = Day.values();
	Day dayChosen = (Day)JOptionPane.showInputDialog(
                    null,
                    "Please choose a day:",
                    "",
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    days,
                    Day.SUNDAY);


This post has been edited by g00se: 12 November 2011 - 01:02 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1