Enum with Strings - java

This is my enmun class
public enum CSSFont {
RezeptName("-fx-font: 22 calibri;"),
RezeptNameClicked("-fx-font: 22 calibri; -fx-underline: true; -fx-text-fill: purple"),
RezeptTab("-fx-font: 15 calibri;");
private String font;
private CSSFont(String s) {
this.font = s;
}
public String getFont() {
return this.font;
}
}
As you can see I created a getFont() function to get the String of each CSSFont object. Is there a way to directly make String objects in an enum class(I need the String for setStyle() methods in JavaFX), so that I don't have to always write CSSFont.object.getFont() but rather CSSFont.object? I tried to let CSSFont extend String, but obviously enums can only implement interfaces. Or is the only solution to create a class with static (final) String attributes?
EDIT: Thanks everybody, it seems I wasn't really sure when to use enums and when not to, since I have only one attribute(String) and I don't even need enumaration or comparison of these enum objects, I will use a class with static final string attributes ;).

You can use something like this:
public enum MyType {
ONE {
public String toString() {
return "this is one";
}
},
TWO {
public String toString() {
return "this is two";
}
}
}
Test it using:
public class EnumTest {
public static void main(String[] args) {
System.out.println(MyType.ONE);
System.out.println(MyType.TWO);
}
}
Originally taken from here

You can override toString method:
public enum CSSFont {
RezeptName("-fx-font: 22 calibri;"),
RezeptNameClicked("-fx-font: 22 calibri; -fx-underline: true; -fx-text-fill: purple"),
RezeptTab("-fx-font: 15 calibri;");
private String font;
private CSSFont(String s) {
this.font = s;
}
public String getFont() {
return this.font;
}
public String toString(){
return this.font;
}
}
Then you can get font as follows:
CSSFont.RezeptName.toString()

Related

How to add constant to enum?

I have a enum with some links. In those links is the same version number (1.1.1), for now as a String, which I would like to replace with constant. How to do this?
I could edit link while returning it (replace some string with my constant), but it does not seems like clean solution. Thank you for any help!
package test.intro;
import test.version;
public enum LinkType
{
LINK1("test/1.1.1/doc/test1.pdf"),
LINK2("test/1.1.1/doc/test2.pdf"),
LINK3("test/1.1.1/doc/test3.pdf");
public final String href;
//my constant I want to use:
private final String versionName = version.getVersionName();
private LinkType(String href)
{
this.href = href;
}
public String getHref()
{
return href;
}
}
You could do something like this:
package test.intro;
import test.version;
public enum LinkType {
LINK1("test/%s/doc/test1.pdf"),
LINK2("test/%s/doc/test2.pdf"),
LINK3("test/%s/doc/test3.pdf");
public final String hrefTemplate;
private LinkType(String hrefTemplate) {
this.hrefTemplate = hrefTemplate;
}
public String getHrefTemplate() {
return this.hrefTemplate;
}
public String getHref() {
return String.format(this.hrefTemplate, version.getVersionName());
// or return this.hrefTemplate.formatted(version.getVersionName()); if you have Java >= 13
}
}
I guess version.getVersionName() is a static method?
Then you could just write LINK1("test/" + version.getVersionName() + "/doc/test1.pdf") etc.
I put my enum into new class and created an attribute. Because my enum is called only in one method, it was easy to change. The solution from #David Mališ might be cleaner.
package test.intro;
import test.version;
public class TestLinks {
private final static String versionName = version.getVersionName();
public enum LinkType {
LINK1("test/"+versionName+"/doc/test1.pdf"),
//another code
}
}

How to use variable of method from 1 class in 2 class via Java?

public static class One {
#Override
public String interact(String... values) {
String actualTextOne = "test";
return actualTextOne;
}
}
public static class Two {
#Override
public String interact(String... values) {
String actualTextTwo = "test";
/* Here I need to compare actualTextOne and actualTextTwo, but the problem is that I can't find solluction how to use actualTextOne in Two class*/
return actualTextTwo;
}
}
You cannot do that.
Please check variable scope in java.
https://www.codecademy.com/articles/variable-scope-in-java
A possible solution here is to call the method interact from the class One. Something like this
public static class Two {
#Override
public String interact(String... values) {
String actualTextTwo = "test";
One one = new One();
String actualTextOne = one.interact(values);
// compare values here
return actualTextTwo;
}
}
Why in your classes functions have parameters if you dont use it?
You can mark your class with static only if he is nested, else you need do like this:
class Two {
static public String interact(String... values) {
String actualTextTwo = "test";
return actualTextTwo;
}
}
String textOne = One.interact("");
String textTwo = Two.interact("");
System.out.println(textOne==textTwo);

Java Enum : Having Multiple Strings within one Enums

I am new student java learning its basics
My objective is to create a Enum which contains various categories like Emails, Username, Passwords, MaterialType etc.
Further I wanted that within one Category I can declare various strings and my sample code is as below:
public enum MyEnums {
Usernames
{
public String toString()
{
return "This is a GmailUsername";
}
/*public String toString()
{
return "This is a GalleryComment";
}*/
},
Password
{
public String toString()
{
return "This is a public password";
}
/* public String GmailPassword()
{
return "This is a Gmail Password";
} */
},
Emails
{
public String toString()
{
return "This is a public contact email address";
}
/* public String EmailAccount()
{
return "This is a public Email Account address";
} */
},
PhoneNumbers
{
public String toString()
{
return "This is Phonenumber";
}
/* public String Phone()
{
return "This is a phone number";
}*/
}
}
and I call the code as
public static void main (String args[])
{
System.out.println(MyEnums.Emails);
System.out.println(MyEnums.Usernames );
System.out.println(MyEnums.PhoneNumbers);
System.out.println(MyEnums.Password);
}
My question is why on using second string type function it is giving error, example In the password category for GmailPassword() why it is not working.
Is there is any other way to declare multiple strings in enum in category wise manner like
public Enum myEnum{
Category1
{
"String 1","String2",......."String N"
}
.......
.......
.......
.......
CategoryN
{
"String 1","String2",......."String N"
}
maybe this helps?
public enum MyEnum {
Emails("mail1", "mail2", "mail3"),
Usernames("username1", "username2"),
CategoryN("a", "b", "c");
private String[] strings;
private MyEnum(String... strings) {
this.strings = strings;
}
#Override
public String toString() {
return Arrays.toString(strings);
}
public String getString(int index) {
return strings[index];
}
}
Main
public static void main(String[] args) {
System.out.println(MyEnum.Emails); //[mail1, mail2, mail3]
System.out.println(MyEnum.Emails.getString(1)); //mail2
}
You can create an enum with Objects instead of just Strings. That way you can access all the properties of those objects in a clean manner.
It seems to me that instead of an Enum with categories, you should have an interface that you make each category enum implement. That way you have N enums that each implement the category interface, and inside each of those you have the strings as the enum constants.

How to use enum to define a bunch of string consts

any example of using enum to define a bunch of string consts? The string can contains special charaters like - / etc?
enum MyConstants {
STR1("some text"),
STR2("some other text");
private String value;
private MyConstants(String str) {
this.value = str;
}
public String getValue() {
return value;
}
}
then use it like this:
MyConstants.STR1.getValue();
String [] messages = {"maybe you", "better go with", "an array?"};
System.out.println (messages[1]);
Without further knowledge - why do you like to use enums at all?
I think this page will be helpful:
http://javahowto.blogspot.com/2006/10/custom-string-values-for-enum.html
In short:
public enum MyType {
ONE {
public String toString() {
return "this is one";
}
},
TWO {
public String toString() {
return "this is two";
}
}
}

Implementing toString on Java enums

It seems to be possible in Java to write something like this:
private enum TrafficLight {
RED,
GREEN;
public String toString() {
return //what should I return here if I want to return
//"abc" when red and "def" when green?
}
}
Now, I'd like to know if it possible to returnin the toString method "abc" when the enum's value is red and "def" when it's green. Also, is it possible to do like in C#, where you can do this?:
private enum TrafficLight {
RED = 0,
GREEN = 15
...
}
I've tried this but it but I'm getting compiler errors with it.
Thanks
You can do it as follows:
private enum TrafficLight {
// using the constructor defined below
RED("abc"),
GREEN("def");
// Member to hold the name
private String string;
// constructor to set the string
TrafficLight(String name){string = name;}
// the toString just returns the given name
#Override
public String toString() {
return string;
}
}
You can add as many methods and members as you like. I believe you can even add multiple constructors. All constructors must be private.
An enum in Java is basically a class that has a set number of instances.
Ans 1:
enum TrafficLight {
RED,
GREEN;
#Override
public String toString() {
switch(this) {
case RED: return "abc";
case GREEN: return "def";
default: throw new IllegalArgumentException();
}
}
}
Ans 2:
enum TrafficLight {
RED(0),
GREEN(15);
int value;
TrafficLight(int value) { this.value = value; }
}
Also if You need to get lowercase string value of enum ("red", "green") You can do it as follows:
private enum TrafficLight {
RED,
GREEN;
#Override
public String toString() {
return super.toString().toLowerCase();
}
}
I liked this approach for selective alternate toString() if it's useful for anyone out there :
private enum TrafficLight {
RED,
GREEN {
#Override
public String toString() {
return "GREEN-ISH";
}
}
}

Categories

Resources