I want to parse a YAML-file via Jackson but encounter the problem that one of the properties (let's call it 'Event') has a string called 'type' and a 'properties' object that differs for different Events. My issue is that I need to define the POJOs for this YAML. Therefore, I want to define a Hashmap with VariableObject that can be any of some predefined classes (for brevity, let's say Shipping and Inventory).
How can I implement a Hashmap like that?
public class Event {
private static String type;
private static Map<String, VariableObject> properties;
public static void main(String[] args) {
Inventory inventory = new Inventory("inventoryName", 13);
properties.put("Inventory", inventory);
Shipping shipping = new Shipping("shippingName", true);
properties.put("Shipping", shipping);
}
}
public class Inventory {
private static String name;
private static int someNumber;
public Inventory(String name, int someNumber) {
this.name = name;
this.someNumber = someNumber;
}
}
public class Shipping {
private static String name;
private static boolean someBoolean;
public Shipping(String name, boolean someBoolean) {
this.name = name;
this.someBoolean = someBoolean;
}
}
What you're talking ablut is simple Object. It's the most specific common superclass:
private static Map<String, Object> properties;
Other solution would be to make Inventory and Shipping implement some common interface, for example Named and use it as type parameter in HashMap.
One way to do this is to make Shipping and Inventory implements the same interface (like VariableObject in your cas)
public class Event {
private static String type;
private static Map<String, VariableObject> properties;
public static void main(String[] args) {
Inventory inventory = new Inventory("inventoryName", 13);
properties.put("Inventory", inventory);
Shipping shipping = new Shipping("shippingName", true);
properties.put("Shipping", shipping);
}
}
public interface VariableObject{
//you can define common methods here if you want
}
public class Inventory implements VariableObject{
private static String name;
private static int someNumber;
public Inventory(String name, int someNumber) {
this.name = name;
this.someNumber = someNumber;
}
}
public class Shipping implements VariableObject{
private static String name;
private static boolean someBoolean;
public Shipping(String name, boolean someBoolean) {
this.name = name;
this.someBoolean = someBoolean;
}
}
Related
So I have 4 classes, Testest which has a main method, Phone which extends Product, Product and ProductDB which has a hashmap. When I make a new phone I want the phone to be stored in the database automatically.
public class Product {
protected String productID;
protected String name;
private String description;
private double price;
public Product(){
Product db = new ProductDB();
productID = this.toString();
db.add(productID, this);
}
(Getter and setter methods here...)
}
public class Phone extends Product {
private String make;
private String model;
private int storage;
public Phone(String make, String model, int storage){
this.make = make;
this.model = model;
this.storage = storage;
}
(Getter and setter methods here...)
}
import java.util.HashMap;
public class ProductDB {
private HashMap<String,Product> products = new HashMap<String, Product>();
public void add(String productID, Product product){
products.put(productID, product);
}
public void remove(String productID){
products.remove(productID);
}
public Product find(String productID){
return products.get(productID);
}
public Object showAll(){
return products.values().toArray();
}
}
public class Testest{
public static void main(String[] args){
ProductDB db = new ProductDB();
Phone phone1 = new Phone("Huwawei P30", "HP30", 50000);
Phone phone2 = new Phone("Huwawei P30 Pro", "HP30PRO", 70000);
Phone phone3 = new Phone("Samsung Galaxy SX", "SGSX", 65000);
System.out.println(db.find(phone1.productID));
System.out.println(phone1.productID);
}
}
I want this to return the object when I look for that specific id, but the problem is that the HashMap is empty for some reason
Edit I made productID private. Still nothing
It seems you want your database to include all created phones, in this case instead of creating a database each time which will be useless and also because your database is accessed from several places it will be more consistent to make your database fields and methods static and just access it from where you want:
public class ProductDB {
final private static HashMap<String,Product> products = new HashMap<String, Product>();
public static void add(String productID, Product product){
products.put(productID, product);
}
public static void remove(String productID){
products.remove(productID);
}
public static Product find(String productID){
return products.get(productID);
}
public static Object showAll(){
return products.values().toArray();
}
}
And then in Product constructor just write:
public Product{
productID = this.toString();
ProductDB.add(productID, this);
}
I got the following class:
public class Possibility {
private String name;
public Possibility(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
If I now have many classes that extend "Possibility", how can I find how many instances exist of classes that extend Possibility?
You can use a static field as a counter in Possibility class and use it to increment as the objects are created. This is more efficient and secure than using reflection.
package so;
public class Possibility {
private static int counter = 0;
private String name;
public Possibility(String name) {
counter += 1;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Possibility1 p1 = new Possibility1("p1");
Possibility2 p2 = new Possibility2("p2");
System.out.println(Possibility.counter);
}
}
Possibility1
package so;
public class Possibility1 extends Possibility {
public Possibility1(String name) {
super(name);
}
}
Possibility2:
package so;
public class Possibility2 extends Possibility {
public Possibility2(String name) {
super(name);
}
}
Possibility3
package so;
public class Possibility3 extends Possibility {
public Possibility3(String name) {
super(name);
}
}
The Reflections library provides a pretty easy way to do this:
int numSubTypes = reflections.getSubTypesOf(Possibility.class).size();
you must create an integer attribute in Possibility class and and you can get this integer from another class that extends from Possibility, like this:
class Possibility{
public int someInteger;
//getter
public int getSomeInteger(){
return this.someInteger;
}
}
class someClass extends Possibility{
public void someMethode(){
Possibility possibility = new Possibility("someName");
//get someInteger
possibility.getSomeInteger();
}
}
If for example I had a user class:
public class User{
private String name;
public User(String name){
this.name = name;
}
public String getName(){
return name;
}
And had a listener that is triggered once a player clicks:
public class ClickListener implements Listener{
public void run(String someInt){
}
If these variables were to be used in a different class to be used for some function only once. Would it better to have a:
HashMap<User, String> map = new HashMap<>();
in the listener and then remove the user after the method requiring someInt is completed, or have a:
private String someStr;
public String getSomeStr(){
return someStr;
}
public void setSomeInt(){
this.someStr = someStr;
}
in the User class and then set the value to null after it has been used.
Which is a better way to do this?
I am trying to create program that holds the details of various instruments (e.g. guitars, keyboards). I want it to have information on each item in the Arraylist. I want each instrument to have a Manufacturer and a "description". I am not new to 2D arrays, however i do not know how to use it in Array lists.
I have given it an attempt. (Ps i need to be able to access all of these things to be able to put into a gui. I hope that made sense.)
public class Main {
public static Login form = new Login();
public static ArrayList<ArrayList<String>> instt = new ArrayList<ArrayList<String>>();
public static ArrayList<String> row = new ArrayList<String>();
public static void main(String[] args) {
row.add("Chelo");
row.add("Drums");
row.add("Flute");
row.add("Guitar");
row.add("Harp");
row.add("Piano");
row.add("Recorder");
row.add("Trombone");
row.add("Trumpet");
row.add("Xylophone");
instt.add(row);
form.setVisible(true);
}
}
Any suggestions? Thank you.
The best approach would be to create an Instrument-Class containing all your information.
public static ArrayList<Instrument> row = new ArrayList<Instrument>();
public static void main(String[] args){
row.add(new Instrument("Guitar","Stackoverflow-Instruments","Best guitar!!"));
row.add(new Instrument("Piano","Stackoverflow-Instruments","Best piano!!"));
}
public static class Instrument{
private String name;
private String manufacturer;
private String description;
public String getName() {
return name;
}
public String getManufacturer() {
return manufacturer;
}
public String getDescription() {
return description;
}
public Instrument(String name, String manufacturer, String description) {
this.name = name;
this.manufacturer = manufacturer;
this.description = description;
}
}
I need to write some code which is as follows:
public class Person {
public static final String NAME;
public Person(String NAME) {
this.NAME = NAME;
}
}
public class Player extends Person {
public Peter(String name) {
super(name);
}
}
It's basically, I want the Player class to have a static final field called NAME, that is being initialized somewhere else, without manually writing in every class public static final String NAME = "Peter".
Is it possible?
As it has been said in the comments, you have poorly declared your NAME variable. In actuality, you don't want it to be static (although you can keep the final modifier, if you want). Your code should, instead, be something along the lines of:
public class Person {
public final String name;
public Person(String name) {
this.name = name;
}
}
public class Player extends Person {
public Player(String name) {
super(name);
}
}
Every person should have their own name; you don't want all objects to be sharing one NAME field
I do not know if I fully understand your question, but I think you have a few mistakes in your code. Like declare name of person as static variable, because static variables are often used as variables for the entire class, and if you changed the name, would change the name to the entire class, not for one instance. Also final is wrong, because you cannot set final variable.
I would do something like this:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return String.format("Person: %s", this.getName());
}
}
public class Player extends Person{
public Player(String name) {
super(name);
}
public String toString(){
return String.format("Player: %s", this.getName());
}
}
public class Match {
private Player player_one;
private Player player_two;
public Match(Player player_one, Player player_two) {
this.player_one = player_one;
this.player_two = player_two;
}
public Player getPlayer_one() {
return player_one;
}
public void setPlayer_one(Player player_one) {
this.player_one = player_one;
}
public Player getPlayer_two() {
return player_two;
}
public void setPlayer_two(Player player_two) {
this.player_two = player_two;
}
#Override
public String toString() {
return String.format("Right now are playing %s VS %s",player_one.getName(), player_two.getName());
}
}
public class PlayerTest {
public static void main(String[] args) {
Player peter = new Player("Peter");
Player anna = new Player("Anna");
Match tennisMatch = new Match(peter, anna);
System.out.println(tennisMatch.toString());
}
}
I static field (variable) only exists once for all instances of your class. Therefore what you try does not work by design.
What value would you expect the field to have after you created three different instances of this class using different parameters?
A final variable cannot be changed once it got initialized. For static variables this happens before the first instance of the class is even constructed. At the moment the constructor is executed the field cannot be changed anymore.
To initialize a static final variable you have to assign a value directly at the definition using the = operator or you have to do it in a static initializer which looks like this:
public class FooBar {
public static final String STATIC_VARIABLE;
static {
STATIC_VARIABLE = "Hello World";
}
}
You can make it like this:
private static final NAME;
public Player(String name){
NAME = name;
}
A final varible can be initialized once only if it wasn't initialized yet.
So in this way the constructor is helping you make it.