Create unique ID with constructor - java

I want to create objects with a name and a unique ID number that increments with the creation of each user.
class user {
static int uid = 0;
String name;
public user (String name){
User.uid = uid++;
this.name = name;
}
}
When creating user objects in a main method and printing out their ID they all return 0. I think there is a simply fix to this but can't seem to find it elsewhere online.

Your code has several problems:
A User doesn't have any ID. All you have is a static ID, thus shared by all users
You're incrementing the static ID, and then assigning its previous value to the ID right after.
You're not respecting the Java naming conventions.
The code should be
class User {
private static int uid = 0;
private String name;
private int id;
public User(String name) {
uid++;
this.id = uid;
this.name = name;
}
// getters
}
or, if you want the IDs to start at 0:
class User {
private static int uid = 0;
private String name;
private int id;
public User(String name) {
this.id = uid++;
this.name = name;
}
// getters
}

Related

Refactoring in Java: Duplicated attributes

I am supposed to refactor duplicated attributes in Student class. I have Student and Professor classes as below. I am really confused about how to do refactoring with attributes. Should I add a new class, or made modifications in one of the classes. If so, how? I could not understand how to proceed with this to-do.
private final String matrNr;
private final String name;
private final int age;
private int semester;
private final String email;
public Student(String name, int age, String email, String matrNr, int semester) {
this.matrNr = matrNr;
this.name = name;
this.age = age;
this.semester = semester;
this.email = email;
}
public String getEmail() {
return email;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getSemester() {
return semester;
}
public String getMatrNr() {
return matrNr;
}
public void increaseSemester(){
semester = semester + 1;
}
}
And the professor is a like:
private final String persNr;
private final String name;
private final int age;
private final String email;
public Professor(String name, int age, String email, String persNr) {
this.persNr = persNr;
this.name = name;
this.age = age;
this.email = email;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getPersNr() {
return persNr;
}
}
Thanks for any kind of helps!
Your goal is to refactor duplicated attributes in the Student and Professor classes. The way to do this is to create a parent class which defines the common attributes (like "name"), and modify Student and Professor classes to extend the common parent class. In this way, both Students and Professors can have a "name", even though you have defined "name" only once in the common parent.
Below shows how you could do this with a common "Human" parent class, how the constructors would work, and how you could define a Student-only attribute (semester).
Here is a simple version a common Human class:
common "Human" class
each Human has a "name"
the name is set in the constructor (so when you're creating an object) and cannot be changed later ("name" is final; also no "setHuman()")
class Human {
private final String name;
public Human(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Here's a simple Professor class:
by definition, a Professor is a Human (Professor extends Human)
when creating a Professor, you must specify the "name" (which is then passed to the Human constructor)
once you have a Professor, you can call getName() (which is defined on the Human class)
class Professor extends Human {
public Professor(String name) {
super(name);
}
}
Here's a simple Student class:
Student is a little different - in addition to a name, it also has a "semester"
when creating a Student, the constructor requires a name and semester, and the Student class itself keeps track of "semester" – so it's fine to have semester defined on Student, and name defined on Human.
you can call getName() (defined on Human)
you can call getSemester() (defined on Student)
class Student extends Human {
private final int semester;
public Student(String name, int semester) {
super(name);
this.semester = semester;
}
public int getSemester() {
return semester;
}
}

How can I set data in a Constructor if all parameters are private?

I have to write a code for example
public class Student {
private int id;
private String name;
public Student (int id, String name) {
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now, in MAIN CLASS I have to get all values from user
How am I supposed to set value in constructor??
like this?
String name = input.nextLine();
int id = input.nextInt();
Student student = new Student (id, name);
"THE ISSUE WITH THIS METHOD IS THAT, WE CAN'T KEEP THE ATTRIBUTES PRIVATE THEN, BECAUSE THEY ARE NOW LOCAL VARIABLES"
OR
I create a constructor with no parameters and use that object to get values and set in fully parametrized constructor.
Student st = new Student();
student.setName(input.nextLine());
student.setID(input.nextInt());
Student student = new Student (st.getId, st.getName);
I don't know if I was able to explain what I want, but if you get that, do let me know
I would use this approach
Student st = new Student();
st.setName(input.nextLine());
st.setID(input.nextInt());
But both are fine . You don't really need to create the second student object in the second example. Just use the one you created upfront
To access the name and ID later you can do
String name = st.getName();
int id = st.getID();
(Edit - this assumes you do make a no arg constructor)

What is the best way of declaring and implementing two java classes?

I have two classes one is called Modifiers and the other is called Options. Modifiers has an id, name and a group of options associated with it. While Options (or each option) has a name and price. Each option is associated with only one Modifier. What is the best way of Writing these classes to take into account their relationship. In my application, the user has to enter the name of the Modifier, then they have to enter the name and price of each option. Each modifier can have as many options as necessary. Here are the classes and how I am using them for now:the user has to enter the name of the Modifier, then they have to enter the name and price of each option. Each modifier can have as many options as necessary. Here are the classes and how I am using them for now:
public class Modifier {
int id;
String name;
Options [] options;
public Modifier() {
}
public Modifier(String name, Options[] options) {
this.name = name;
this.options = options;
}
public Modifier(int id, String name, Options[] options) {
this.id = id;
this.name = name;
this.options = options;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Options[] getOptions() {
return options;
}
public void setOptions(Options[] options) {
this.options = options;
}
}
I am currently add the group of Options as an array. Here is the Options class.
public class Options {
int id;
String name;
public Options() {
}
public Options(String name, long price) {
this.name = name;
this.price = price;
}
public Options(int id, String name, long price) {
this.id = id;
this.name = name;
this.price = price;
}
long price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getPrice() {
return price;
}
public void setPrice(long price) {
this.price = price;
}
}
And in my main:
Options sausage = new Options("sausage",23);
Options olives = new Options("olives", 24);
Options bacon = new Options("bacon", 25);
Options [] toppings = {sausage, olives, bacon};
Modifier pizza_toppings = new Modifier(1, "Pizza toppings",toppings);
I plan on using an SQLIte DB so I need to find an efficient way of doing this.
you could add one field to Options class which save modifier id, then with sqlite you will create two tables one for modifiers and one for options, and when you retrieve data from it, you use foreign key to build your object. whoever your classes now looks fine, you may just save modifiers ids in options table and the vice versa like this:
modifier table [modifierId, name] - option table [optionId, name, modifierId]

create unique id without using constructure and setters

class person {
private int id ;
private String name;
private boolean gender;
public person() {
}
public AtomicLong getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
}
I want to create unique id in this class without using constructors and setters.
To construct a person instance, the field initializer will be copied into the constructor. Assuming that's okay, you could use an AtomicInteger and something like,
private static AtomicInteger ai = new AtomicInteger(0);
private int id = ai.incrementAndGet();
you could add:
private static int ID_GENERATOR = 0;
then, in the constructor, you will use:
public person() {
id = ID_GENERATOR++;
}

How to not request certain properties when creating an object through SOAP?

Assume I have an User object:
public class User {
private Long id;
private String name;
private String surname;
// getters + setters
}
When creating a User through SOAP, I want the user to input only the name and surname but not the id. In this case, should I create something like UserDTO object? For example:
public class UserDTO {
private String name;
private String surname;
// getters + setters
}
In my service bean, I will have the following method:
public void updateUser (Long id, UserDTO dto) {
User user = new User(id, dto.getName(), dto.getSurname());
service.updateUser(user);
}
Will this be a correct approach to my question? Thank you
I'm not sure if I understand your question right but why don't you just provide second constructor for your User class?
It will be:
public class User {
private Long id;
private String name;
private String surname;
//in case of creating user manually
public User(Long id, String name, String surname) {
this.id = id;
this.name = name;
this.surname = surname;
}
//in case of creating user via soap:
public User(String name, String surname) {
this.id = generateId(); //or some other logic to specify users id
this.name = name;
this.surname = surname;
}
}

Categories

Resources