I'm using the following model in a listview:
public class ListItem {
private int _Id;
private String Name;
public ListItem() {
this._Id = 0;
this.Name = "";
}
public ListItem(int Id, String Name) {
this._Id = Id;
this.Name = Name;
}
public int getId() {
return this._Id;
}
public void setId(int c) {
this._Id = c;
}
public String getName() {
return this.Name;
}
public void setName(String c) {
this.Name = c;
}
}
I have some extra data that will be displaying once they click on the item they want to view. Since I'm pulling this info from a database and I'm not limiting the number of items they put in the database, I was wondering if I could extend my model in another model. Something like (you can correct me if this is the wrong way to write an extend model, if extending models is not a big deal):
public class ItemDetails extends ListItem {
private String itemType, Manufacturer, Qty, Notes;
public ItemDetails () {
this._Id = 0;
this.Name = "";
this.itemType = "";
this.Manufacturer = "";
this.Qty = "";
this.Notes = "";
}
public Ammo(int _Id, String name, String itemType,
String manufacturer, String qty, String notes) {
this._Id = _Id;
this.Name = name;
this.itemType = itemType;
this.Manufacturer = manufacturer;
this.Qty = qty;
this.Notes = notes;
}
// Get Variables //
public int get_Id() {
return this._Id;
}
public String getName() {
return this.Name;
}
public String getItemType() {
return itemType;
}
public String getManufacturer() {
return Manufacturer;
}
public String getQty() {
return Qty;
}
public String getNotes() {
return Notes;
}
// Set Variables //
public void set_Id(int c) {
this._Id(c);
}
public void setName(int c) {
this.Name = c;
}
public void setItemType(String c) {
itemType = c;
}
public void setManufacturer(String c) {
Manufacturer = c;
}
public void setQty(String c) {
Qty = c;
}
public void setNotes(String c) {
Notes = c;
}
}
Would this be a bad idea? Would I be better off just having to different models or one model and only returning the data I need from the List then getting the rest of the data later? I'm trying to write clean code with this, and have as little duplicate code as possible. I am also wanting my code to be efficient and perform well , but I'm not sure if this might be a good idea.
It looks like you're not using most of the code (variables and methods) from the class you are extending from. If you don't explicitly need to get an object of ListItem when you create an object of ItemDetails (like ListItem item = new ItemDetails();) you have actually no reason to extend that class. In that case your code is definitely cleaner, if you make it not to extend.
If you want to write clean code you should understand that the constructor calls the ListItem constructor and already initializes fields "_Id" and "Name" and overriding methods with no need to change them is not recommended.
Related
I have the following class that is created with the usage of a builder:
public class Foo {
private String name;
private int age;
public String getName() { return name; }
public int getAge() { return age; }
private Foo(Builder b) {
name = b.name;
age = b.age;
}
public static final class Builder {
private String name;
private int age;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder age(int age) {
this.age = age;
return this;
}
}
}
Now I want to add a JUnit test for this where if this class were to change (via new field were to be added, some other changes made to this) that would also be reflected once the class got serialized, I want that test to fail to catch that change. I am not aware of any libraries that can do this, how can this be done?
I have a User class and a Pet class. A user can have multiple pets.
I'm trying to retreive a user document and convert it to a user object as follows:
loggedInUser = documentSnapshot.toObject(User.class);
It throws the following exception:
java.lang.RuntimeException: Could not deserialize object. Can't convert object of type com.google.firebase.firestore.DocumentReference to type com.example.pawsibilities.Pet (found in field 'pets.[0]')
Here is an example of a user in firestore. One of its fields is an array of references (pets).
My pet class look like this in Firestore, and it looks like this in Java:
public class Pet {
private String name;
private String type;
private LocalDate birthday;
private String breed;
public enum Gender {
Female,
Male,
Unknown
}
private Gender gender;
private boolean neutered;
private float height;
private float weight;
private String healthNotes;
private boolean lostStatus = false;
private LocalDate lostSince = null;
private ArrayList<LastSeenDetails> lastSeenDetailsList = null;
public Pet() { }
public Pet(String name, String type, LocalDate birthday, String breed, Gender gender, Boolean neutered, float height, float weight, String healthNotes) {
this.name = name;
this.type = type;
this.birthday = birthday;
this.breed = breed;
this.gender = gender;
this.neutered = neutered;
this.height = height;
this.weight = weight;
this.healthNotes = healthNotes;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getBirthday() {
return birthday.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
}
public String getLostSince() {
return lostSince.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
}
public String getBreed() {
return breed;
}
public String getGender() {
return gender.name();
}
public String getHeight() {
return height + " cm";
}
public String getWeight() {
return weight + " kg";
}
public String getHealthNotes() {
return healthNotes;
}
public ArrayList<LastSeenDetails> getLastSeenDetailsList() {
return lastSeenDetailsList;
}
public boolean isLost(){ return lostStatus; }
public String isNeutered() {
if (neutered == true) {
return "Yes";
} else
return "No";
}
public void setLostStatus(boolean lostStatus) {
this.lostStatus = lostStatus;
}
public void setLostSince(LocalDate time) { this.lostSince = time; }
public void setLastSeenDetailsList(ArrayList<LastSeenDetails> lastSeenDetailsList) {
this.lastSeenDetailsList = lastSeenDetailsList;
}
}
It has an empty constructor and all fields have a getter method. I can't seem to find the issue...
It's not necessary to convert document to User, as the retrieved document is an object of type User.
Try this,
User user = documentSnapshot.getData();
or
User user = documentSnapshot.get(String field);
I am trying to learn Realm basics by implementing a simple Android project.
The idea is that user have several items and several item lists and an item can be added to any of these lists and a list can have many items. Therefore, there is a many to many relationship between Item and List objects. Here are my objects.
public class Item extends RealmObject {
#PrimaryKey
private String id;
private String name;
private boolean isDone;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isDone() {
return isDone;
}
public void setDone(boolean done) {
isCollected = done;
}
}
public class List extends RealmObject {
#PrimaryKey
private String id;
private String name;
private RealmList<Item> items;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public RealmList<Item> getItems() {
return items;
}
public void setItems(RealmList<Item> items) {
this.items = items;
}
}
My problem is, the field isDone might be different depending on the item's status in a given list. But when I update this field in a list, all the other items added to different lists get updated too. Since they are using the same object it makes sense but that is not to behavior I want. Is there a Realm way to create a pivot table/object with an extra column/field (in that case isDone) ?
Any help would be appreciated.
The problem is that the isDone property of Item doesn't truly belong to Item. If you can set the same Item in multiple Lists, then the property that a given task is deemed complete within a given List is the property of the List.
public class Item extends RealmObject {
#PrimaryKey
private String id;
private String name;
//private boolean isDone;
#LinkingObjects("completedItems")
private final RealmResults<List> tasksCompletedIn = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//public boolean isDone() {
// return isDone;
//}
//public void setDone(boolean done) {
// isDone = done;
//}
}
public class List extends RealmObject {
#PrimaryKey
private String id;
private String name;
private RealmList<Item> items;
private RealmList<Item> completedItems;
Then you know if it's a completed item if completedItems.contains(item) (where item is a managed object, or overrides equals to check against only id)
I have created a class of Patient object containing patient name and gender and I want to remove it base on Patient name. What is the correct way to do it?
This is my Patient object:
class Patient {
private String name;
private int gender;
public Patient(String name, int gender){
this.name = name;
this.gender = gender;
}
public String getName(){
return this.name;
}
public int getGender(){
return this.gender;
}
public void setName(String name){
this.name = name;
}
public void setGender(int gender){
this.gender = gender;
}
}
This is my Treeset declaration: private TreeSet<Patient> ts = new TreeSet<Patient>(new nameComp());
This is my remove method (I don't know how to start)
void RemovePatient(String patientName) {
}
Just iterating and removing while doing so, will result in a Concurrent Modification Exception. You could temp save the item to remove and remove it later:
For example:
void removePatient(String patientName) {
Person deleteThat;
for (Patient p : ts){
if(p.getName().equals(patientName){
deleteThat = p;
}
}
if(deleteThat != null){
ts.remove(deleteThat);
}
}
I need to update my Country class so that it can store a list of languages, I also need a field for the list, a getter, and a method that allows me to add a language to the collection. I very green when it comes to programing. This is what I have so far.
public class Country {
private int id;
private String name;
private long population;
private double medianAge;
private List<String> languages;
List<String> list = new ArrayList<>();
/**
* Create a Country object with the given properties
*/
public Country(int id, String name, long population, double medianAge) {
this.id = id;
this.name = name;
this.population = population;
this.medianAge = medianAge;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getPopulation() {
return population;
}
public double getMedianAge() {
return medianAge;
}
}
You can remove the member list - cannot see a reason for you to have it.
You constructor can be:
public Country(int id, String name, long population, double medianAge) {
this.id = id;
this.name = name;
this.population = population;
this.medianAge = medianAge;
this.languages = new ArrayList<>();
}
Then you can have a method to add a language to the list:
public void addLanguage (String language) {
languages.add(language);
}
Finally a method to return the list of languages as given below:
public List<String> getLanguages() {
return languages;
}
More information on how ArrayList works
You need a getter method to access languages like below
getLanguages(){
return this.languages;
}
And a method which ads one language to existing list.
addLanguageToList(String language){
this.getLanguages().add(language);
}
Do you mean you want to add an initializer for languages? You can use the list.add() method.
public class Country{
private int id;
private String name;
private long population;
private double medianAge;
private ArrayList<String> languages = new ArrayList<String>();
public Country(int id, String name, long population, double medianAge String language) {
this.id = id;
this.name = name;
this.population = population;
this.medianAge = medianAge;
this.languages.add(laguage);
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getPopulation() {
return population;
}
public double getMedianAge() {
return medianAge;
}
}
Its very simple, Create a getter and two add method, one for adding one language and another add method for adding list of language to existing language list.
import java.util.ArrayList;
import java.util.List;
public class Country {
private int id;
private String name;
private long population;
private double medianAge;
private List<String> languages;
List<String> list = new ArrayList<>();
/**
* Create a Country object with the given properties
*/
public Country(int id, String name, long population, double medianAge) {
this.id = id;
this.name = name;
this.population = population;
this.medianAge = medianAge;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getPopulation() {
return population;
}
public double getMedianAge() {
return medianAge;
}
public List<String> getLanguages() {
return languages;
}
public void addLanguage(String language) {
this.languages.add(language);
}
public void addLanguages(List<String> languages) {
this.languages.addAll(languages);
}
}