How can i create Enum in Java? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I already created Enum. But i cannot create new Enum. I wanna create and compare 2 Enums actually. The X value is coming from backend. It's a dynamic value. I wanna make this;
EmployeeType type = new EmployeeType("X")
if (type == EmployeeType.X_PERSONALS) {
Log.i("SAMPLE_TAG", "Yesss!")
}
public enum EmployeeType {
X_PERSONALS("X"),
Y_PERSONALS("Y"),
Z_PERSONALS("Z");
private final String text;
EmployeeType(final String text) {
this.text = text;
}
public String getText() {
return text;
}
}

But i cannot create new Enum
This is literally the point of enums: you can't instantiate them.
You can refer to the existing elements e.g.
EmployeeType type = EmployeeType.X_PERSONALS;

So what you are actually trying to achieve is to find the enum constant that has the same text. You can to that like this:
public enum EmployeeType {
X_PERSONALS("X"),
///...and so on
public static EmployeeType getByText(String x) {
for(EmployeeType t:values()) {
if(t.text.equals(x)){
return t;
}
}
return null;
}
}
and then use the code like this:
EmployeeType type = EmployeeType.getByText("X");
if (type == EmployeeType.X_PERSONALS) {
//and so on

Related

How to return a Map with an Integer key and Set<...>value? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have got this class
public class Motorcycle{
private final Integer year;
private Integer km;
public Motorcycle(Integer year, Integer km) {
super();
this.year= year;
this.km = km;
}
public Integer getKm() {
return km;
}
public void setKm(Integer km) {
this.km = km;
}
public Integer getYear() {
return year;
}
}
And this class
public class Scooter extends Motorcycle{
public Scooter(Integer year, Integer km) {
super(year, km);
}
}
In another class I have got
private Set<Motorcycle>moto;
public Map<Integer, Set<Motorcycle>> getScooterByYear() {}
getScooterByYear is a method which returns a map with an entry whose key is an Integer (the year of registration of the
motorcycles) and by value the set of Scooter-type motorcycles contained in motorcycles whose
year is equal to the key of the corresponding entry.
I have to implement it without parameters.
I suggest that your method gets the base collection as input (that way it doesn't matter where it actually comes from). In that case, your method may also be static.
public static Map<Integer, Set<Motorcycle>> getScooterByYear(Collection<Motorcycle> motorcycles) {
return motorcycles.stream()
.filter(m -> m instanceof Scooter)
.collect(groupingBy(Motorcycle::getYear, toSet()));
}
It should be self-explanatory. First you filter for what you want and then you group the stuff by year and the second argument of groupingBy gives you a Set (where List would be the default).
Disclaimer: if this was some sort of homework, be advised that your teacher may not accept this answer, because it might be too "modern" for school stuff.

Setters on objects Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hello so I'm having a bit of difficulties with a setter method on objects.
public class Company {
private String companyName;
public static int numberOfEmployees;
public Employees employees[];
public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
Employees employee1 = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());
employees[numberOfEmployees] = employee1;
numberOfEmployees++;
}
So basicly this is a method defined in the 'company class' while making an Employees object who's using the parameters for making a 'Programmer'.
But that's not the deal, what I want tot do is by calling this setter method, automaticly create an object. So each time it's used, kind of increment the name of the object it's going to make.
So for example the first time I use it it makes an object called Employee1 and stores it in Employee[0].. second time I want it to store Employee2 into Employee[1].
Maybe I'm making this way too difficult but I'm just trying things out, and can't seem to find a way to make this work.
I suppose that Programmer object is subclass of Employees, or else it will not work. More or less it should look like the following:
public class Company {
private String companyName;
public static int numberOfEmployees;
public static Employees employees[];
public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
numberOfEmployees++;
employees[numberOfEmployees] = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());;
}

Condition for enum values in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I wanted to know how to have an enum in java, containing values that could adjust depending on the condition.
Exemple :
Public enum values{
If (condition){
A("a", 1),
B("b",2);
} else {
C("c", 1);
}
Private string value;
Private int id;
values(string value, int id) {
this.value = value;
this.id = id;
}
}
Thank you for your help
That's not the purpose of Enum. From java docs
An enum type is a special data type that enables for a variable to be
a set of predefined constants. The variable must be equal to one of
the values that have been predefined for it.
You can add the logic in method,
if (condition)
value = values.A;
else
value = values.C;
You can also use filter on enum,
Arrays.stream(values.values())
.filter(condition)
.collect(toList());

Java - refering to an object [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new in Java and I'm trying to create a social network. I have a array of objects "People" which their names, e-mails and so on. I would like to create relations between them (like 'friends') so that program would connect two object.
I though about doing another array inside of every "Person" object, so each person would have a name, e-mail and friend array with all friends inside of it. I don't know how to refer to an other object of type People inside of object of this type.
I don't know if it's clear..
Hope it is!
Thank You in advance!
Here's one way:
public class Person {
private String name;
private String email;
private List<Person> friends;
// Leave the rest for you.
public void addFriend(Person p) {
if (p != null) {
this.friends.add(p);
}
}
public void removeFriend(Person p) {
this.friends.remove(p);
}
public boolean isFriend(Person p) {
return this.friends.contains(p);
}
}
You'll want constructors and a way to add and remove Person from your friend List.

which class should I use to collect and store different data types? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My Sqlite Database has several columns with different data types. Now, I want to retrieve a row which has different data types and store it in one object.
Which data type or class should I use?
You can make a list of objects:
int numberOfObjects = 10;
ArrayList<Obj> lst = new ArrayList<Obj>(numberOfObjects);
That depends on how you are going to use it later.. The
resultset
can hold all the datatypes.Later you can retreive every coloumn value from it as a string.
Cursor and a Custom Object is just fine.
class YourDataClass {
int id;
String data;
byte[] rawdata;
public String getData() { return data; }
public String setData(String data) { this.data = data; }
// .... other getters and setters
}
Cursor yourCursor = database.rawQuery("select * from blub");
if (yourCursor != null & yourCursor.size() > 0 && yourCursor.moveToFirst) {
YourDataClass yourObject = new YourObject;
yourObject.setId(cursor.getInt(cursor.getColumnIndex("id"));
yourObject.setData(cursor.getString(cursor.getColumnIndex("data"));
... whatever.
}

Categories

Resources