I've built a Java application for, globally, mange computers database at my job. At first, I've been told that we needed a tab called 'Users', which would contain first name, last name and email. But now, the technician wants to add other fields such as address, phone, etc. He asked me if he could add himself these fields. The problem is, he's not a programmer. He wants to add these fields with a GUI. I have built the application with static fields, and here is my User Class.
public class User {
private String firstname;
private String lastname;
private String email;
private int id;
public User(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public User(int id, String firstname, String lastname, String email) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
public User(String firstname, String lastname, String email) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public String toString() {
return firstname + " " + lastname;
}
}
Now, I wonder if there's a way to modify this class using the GUI or if I have to rebuild entirely the software and stop using object classes like that. There are also classes like "Software" and "Operating Systems" that have static fields but need to be modified if necessary.
I don't know what options exactly I have to get the job done:
Let the tech modify the database and do something like "for each column in database, add this column in the GUI". (Which would cause to rebuild the entire software.)
others?
Any reads/tutorials on that kind of issues?
Thanks.
If the requirement is that a normal user should be able to add additonal fields to existing objects, propably the best way would be to store the information in a map.
So instead of:
public class User {
private String firstname;
private String lastname;
private String email;
private int id;
}
you would have:
public class User {
private int id;
private Map<String,String> properties;
public User(String firstname, String lastname) {
properties = new HashMap<String,String>();
properties.put("firstname",firstname);
properties.put("lastname",lastname);
}
etc.
Related
I have a users database in firebase that contains user's first name and last name
So, i want to take only the head name (i.e) Stephen, Steve, Tony e.t.c.
And i want to show them as a list in a recycler view.
Is this possible?
This is my java object
public class Users {
String firstName, lastName;
public Users(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
and i have created my adapter that extends RecyclerView.Adapter<>
PS: The names are not always same as the first name.
Actually i am implementing one program in which i am retrieving information from database and i am setting that info to One Object using setters and i just want to print that Object parameters without using system.out.println(object.getHeadCount());
I just want like if i am giving system.out.println(object); so using this code it should print data in Json format or any other readable format.
How to do it.Because my object is containing 30 fields so it is very hectic to write 30 getters to print data.
You have to override toString()
Normal way
class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString(){
return this.firstName + " " + this.lastName;
}
}
Using Apache Commons
class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString(){
return new ToStringBuilder(this)
.append("firstName", firstName)
.append("lastName", lastName)
.toString();
}
}
Using Google Guava
class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString(){
return MoreObjects.toStringHelper(this)
.add("firstName", firstName)
.add("lastName", lastName)
.toString();
}
}
You simply have to override the toString() method of the class, read about it in java doc for Object class. Most IDE support it's automatic generation for all the fields of your class or for a number of them. Just for example:
class User {
private String name;
private String surname;
User(String name, String surname)
{
this.name = name;
this.surname = surname;
}
#Override
public String toString()
{
return this.name+" "+this.surname;
}
}
You should override toString() method. if you are using eclipse then you can try right-click within the editor, you'll find it under Source -> Generate toString()
To make a toString method you can simply just add one like so.
public String toString(){
return "" + getValue();
}
The toString method is a part of
java.lang.Object
So it is implemented in every class.
Hello I was wondering how I can make the most dynamic get set methods in a java program I am working on? The program has multiple fields and each field needs to have its own get and set method but I want only one get set method that can get and set any field in the class I want.
package kalsi;
public class ContestantInformation {
String firstName, lastName, city, province, postalCode, streetName, streetNumber, phoneNum, birthDate;
public ContestantInformation() {
}
public ContestantInformation(String firstName, String lastName, int streetNumber, String streetName, String city,
String province, String postalCode, int phoneNum, int birthDate) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = "" + birthDate;
this.streetNumber = "" + streetNumber;
this.streetName = streetName;
this.city = city;
this.postalCode = postalCode;
this.phoneNum = "" + phoneNum;
}
public void setName(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
You can use your IDE to generate getter/setter methods. If you are using eclipse then you can do it by going to Source>generate methods.
Or alternatively you can use lombok generate getter setter method dynamically. In this case you don't even need to write your getter/setter methods. Look at the example -
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
public class Person {
#Getter #Setter private String name;
#Getter #Setter private int age = 10;
}
I am trying to figure out how integration between google endpoint, objectify and javscripts works. I made a real simple class named 'User'. All I am trying is to fetch a record from a Data Store and return this object back and use this in javascript.
However, the object does not seem to flow back properly. I do not see any details around this object using Chrome's developer tools... Any ideas? FYI, the record exists in the data store as I can see it while using the development console. I also can see the information as I've logged this info to the console as well. Thanks.
** JAVA CLASS **
package com.Backend;
import com.googlecode.objectify.annotation.*;
#Entity
public class User {
String firstName;
String lastName;
#Id String email;
public User(){
super();
}
public User(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
** GOOGLE ENDPOINT **
public class UserEndpoint {
private static final Logger LOG = Logger.getLogger(UserEndpoint.class.getName());
static {
ObjectifyService.register(User.class);
}
#ApiMethod(name = "getUser")
public User getUser() {
User u = ofy().load().type(User.class).id("johndoe#domain.com").now();
return u;
}
}
Finally found out what the issue is. It has to do with how the object was returned... It did not have any return value.
I have an embedded document based OrientDB database with records and I can't read back the saved entities. A get the right number of elements, but the attributes aren't mapped to my pojo's fields.
What am I doing wrong? Any tips?
Thanks!
OObjectDatabaseTx db = new OObjectDatabaseTx("local:db");
if (db.exists()) {
db = new OObjectDatabaseTx("local:db").open("admin", "admin");
} else {
db.create();
}
db.getEntityManager().registerEntityClass(User.class);
long cnt = db.countClass(User.class);
System.out.println(cnt); // OK
User user = db.newInstance(User.class, "Firstname", "Lastname", "email#example.com");
db.begin();
db.save(user); // it's OK, the DB contains this document, I can select it with the servers console
db.commit();
for (User usr : db.browseClass(User.class)) {
System.out.println(usr); // User object with null fields, why?
}
The User class:
public class User {
private String firstname;
private String lastname;
private String email;
public User() {
}
public User(String firstname, String lastname, String email) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public String toString() {
return firstname + " " + lastname + " " + email;
}
}
Java objects retrieved directly from OrientDB (as you're doing in this example) are proxied transparently to their underlying records, allowing lazy loading of their fields. In your scenario, you'll have to use the User object get methods in order to trigger the population of the corresponding fields from the DB. The fields have default values (all null in your case) until loaded.
See also: "How It Works" in OrientDB Object2RecordJavaBinding
As referenced in the Binding section of the Orientdb docs, you must provide an empty constructor for each java object you wish to bind. Source : http://orientdb.com/docs/2.0/orientdb.wiki/Object-2-Record-Java-Binding.html