Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java add constructor to enum

package cakes;

public class EnumDemo {

	public enum Food {
		HAMBURGER(7), FRIES(2), HOTDOG(3), ARTICHOKE(4);

		Food(int price) {
			this.price = price;
		}

		private final int price;

		public int getPrice() {
			return price;
		}
	}

	public static void main(String[] args) {
		for (Food f : Food.values()) {
			System.out.print("Food: " + f + ", ");

			if (f.getPrice() >= 4) {
				System.out.print("Expensive, ");
			} else {
				System.out.print("Affordable, ");
			}

			switch (f) {
			case HAMBURGER:
				System.out.println("Tasty");
				continue;
			case ARTICHOKE:
				System.out.println("Delicious");
				continue;
			default:
				System.out.println("OK");
			}
		}

	}

}
Comment

PREVIOUS NEXT
Code Example
Java :: encode file to utf-8 in java 
Java :: java change hashmap value 
Java :: java list change element position 
Java :: Java @Deprecated annotation 
Java :: stringbuilder in java 
Java :: maths trivia in java 
Java :: Java How to use Deque? 
Java :: round to the next multiple of 5 
Java :: android click button programmatically 
Java :: The Longest Substring 
Java :: unresolved reference activity_main 
Java :: Passing data from an activity to a fragment 
Java :: java to kotlin converter android studio 
Java :: GridLayout 
Java :: use custom font java 
Java :: declaring java variables 
Java :: No enclosing instance of type Foo is accessible. Must qualify the allocation with an enclosing instance of type Foo (e.g. x.new A() where x is an instance of Foo 
Java :: Add Elements in java map 
Java :: java pass by reference 
Java :: fabricmc concat text 
Java :: jenkins password decrypt online 
Java :: netbens setdefaultbutton 
Java :: teimpo en segundos java 
Java :: java method overloading 
Java :: worldedit api copy schematic 
Java :: gson to class 
Java :: polymorphism array with abstract class java 
Java :: trémaux’ methode 
Java :: java package keyword 
Java :: java platform runlater keeps running 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =