userexit
userexit
JCHJava Community | Help. Code. Learn.
Created by userexit on 11/3/2024 in #java-help
How to include a thymeleaf template into another one, to include header, footer etc
Title is self explanatory -
63 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 11/2/2024 in #java-help
I set incrment to be 10, and I set initial value to be 100 but its not working, did it with hibernat
No description
242 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 10/30/2024 in #java-help
How to create custom id generator for primary key in springboot
very clear question in title
302 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 10/30/2024 in #java-help
How to choose between using @Enumerated and a converter ? or @PostLoad and @PrePersist ?
Which one is better ? or does it depend, and if it depends, in what does it depend ?
4 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 10/28/2024 in #java-help
Enable live reload with Spring Boot devtools
help with the title
7 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 10/28/2024 in #java-help
Need help with this error in vscodium
No description
8 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 10/28/2024 in #java-help
Syntax questions
Id like to know what these syntaxes mean: javadoc -d doc Hello.java jar -cf hello.jar Hello.class java -cp hello.jar Hello
36 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/24/2024 in #java-help
Is this good composition class ?
public class ThreadComposition implements Runnable {
private String name;
private final Thread thread;

public ThreadComposition(String name) {
this.name = name;
thread = new Thread(this);
}

@Override
public void run() {
for (int i=0; i<10; i++) {
for (int j=0; j<50000; j++) {
System.out.println("Thread " + name + " running");
}
}
}

public void start() {
thread.start();
}
}
public class ThreadComposition implements Runnable {
private String name;
private final Thread thread;

public ThreadComposition(String name) {
this.name = name;
thread = new Thread(this);
}

@Override
public void run() {
for (int i=0; i<10; i++) {
for (int j=0; j<50000; j++) {
System.out.println("Thread " + name + " running");
}
}
}

public void start() {
thread.start();
}
}
4 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/21/2024 in #java-help
Help decoupling a presenter class in multiple classes
No description
4 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/21/2024 in #java-help
Is it good practice to have 2 DTO's. one for database, one for application.
Im having an error as my DTO constructor can throw exceptions if the data introduced to it is invalid, but doing so means I can't create DTO's in DAO without handling these exceptions, but the DAO layer is not supposed to handle exceptions
4 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/21/2024 in #java-help
Should I decouple my presenter in multiple ones ?
6 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/20/2024 in #java-help
Help improving this code
ok so when i query a favorite row i know its id because its already been inserted, problem arises when i try inserting a favoritedto that i made in the program, i cant know what the id will be for it, but i dont have to either because the add method of my dao will just insert it and sql will generate a key for it. problem is when im trying to save a favorite ride i need to pass a favoritedto as argument but a favoritedto requires a id to be constructed, so i just put garbage number to construct it, there is no issue then all fine. problem is i dont think this is good written and i would refer to this as spaghetti code
195 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/20/2024 in #java-help
How could I simplify this
public interface Dao<K, S extends Key<K>> { void update(S item); void insert(S item); void delete(K key); S select(K key); List<S> selectAll(); } public interface Dao<K, T, S extends Key<K, T>> { void update(S item); void insert(S item); void delete(K key1, T key2); S select(K key1, T key2); List<S> selectAll(); } public interface Dao<K, T, I S extends Key<K, T, I>> { void update(S item); void insert(S item); void delete(K key1, T key2, I key3); S select(K key1, T key2, I key3); List<S> selectAll(); }
87 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/19/2024 in #java-help
HELP HANDLING ERRORS IN MVP
Design Pattern: MVP -> MODEL-VIEW-PRESENTER Interaction with user - presenter calls logic on model - model updates presenter - presenter updates view and/or calls new logic on model now assume there is code in model that throws errors (parameter validation for example): in presenter: try { model.doSomething(); } catch (error) { view.showError(blabla); } is it better this way, or is it better like this: in presenter: model.doSomething(); in model: if (name.length < 2) { notifyObservers(ModelEvent.NOT_ENOUGH_NAME_LENGTH); return; } then in presenter: update(ModelEvent event) { switch (event) { case NOT_ENOUGH_NAME_LENGTH: view.showError(blabla); break; } }
50 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/18/2024 in #java-help
Help Project MVP Layout
I was thinking of this layout: Presenter class that will handle interaction between model and view ViewController class with methods to interact with the UI Model package with: - Repository that is going to contact the database and get me the needed information - Dijkstra class that is going to handle calculating shortest path between 2 nodes. Instantiating View => generating the UI Instantiating Presenter => initializing the View with data that I get from the model, adding presenter as a button handler for view and adding presenter as an observer of Model Instantiating Model => creating an instance of dijkstra and repository. any idea how to optimize this ?
4 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/18/2024 in #java-help
Help optimize this dijkstra algorithm:
public static void dijkstra(Set<Node> graph, Node start, Node end) {
start.setDistance(0);
Set<Node> visited = new HashSet<>();
dijkstraHelper(graph, visited, start, end);
}

public static void dijkstraHelper(Set<Node> unvisited, Set<Node> visited, Node currentNode, Node end) {
if (visited.contains(end)) {
Node prevNode = end;
System.out.println(prevNode);
while (prevNode.getPreviousNode() != null) {
System.out.println(prevNode.getPreviousNode());
prevNode = prevNode.getPreviousNode();
}
return;
}

Map<Node, Integer> neighbours = currentNode.getAdjacentNodes();
neighbours.forEach((node, distance) -> {
if (unvisited.contains(node)) {
int computeDistance = distance + currentNode.getDistance();
if (computeDistance <= node.getDistance()) {
node.setDistance(computeDistance);
node.setPrevNode(currentNode);
}
}
});

unvisited.remove(currentNode);
visited.add(currentNode);

AtomicReference<Node> nextNode = new AtomicReference<>();
neighbours.forEach((node, integer) -> {
if (nextNode.get() == null && unvisited.contains(node)) {
nextNode.set(node);
} else if (unvisited.contains(node) && node.getDistance() < nextNode.get().getDistance()){
nextNode.set(node);
}
});

dijkstraHelper(unvisited, visited, nextNode.get(), end);
}
public static void dijkstra(Set<Node> graph, Node start, Node end) {
start.setDistance(0);
Set<Node> visited = new HashSet<>();
dijkstraHelper(graph, visited, start, end);
}

public static void dijkstraHelper(Set<Node> unvisited, Set<Node> visited, Node currentNode, Node end) {
if (visited.contains(end)) {
Node prevNode = end;
System.out.println(prevNode);
while (prevNode.getPreviousNode() != null) {
System.out.println(prevNode.getPreviousNode());
prevNode = prevNode.getPreviousNode();
}
return;
}

Map<Node, Integer> neighbours = currentNode.getAdjacentNodes();
neighbours.forEach((node, distance) -> {
if (unvisited.contains(node)) {
int computeDistance = distance + currentNode.getDistance();
if (computeDistance <= node.getDistance()) {
node.setDistance(computeDistance);
node.setPrevNode(currentNode);
}
}
});

unvisited.remove(currentNode);
visited.add(currentNode);

AtomicReference<Node> nextNode = new AtomicReference<>();
neighbours.forEach((node, integer) -> {
if (nextNode.get() == null && unvisited.contains(node)) {
nextNode.set(node);
} else if (unvisited.contains(node) && node.getDistance() < nextNode.get().getDistance()){
nextNode.set(node);
}
});

dijkstraHelper(unvisited, visited, nextNode.get(), end);
}
9 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/17/2024 in #java-help
Help implementing DJIKSTRA ALGORITHM
I have a Graph class that contains a Set of nodes, I define a node to have a name and adjacent nodes. The only thing im struggling with is: lets say we have this: A ---- B ---- C | | D ---- E Node D has name "D" and its adjacent nodes are E and A. How can I adapt my class to hold the distance to node E and A ? I have thought of modifying my node class to have a attribute distance to Parent, then Node E distance to parent is 1 but what if it has multiple parents as in what if a node F was connected to it
4 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/17/2024 in #java-help
How can I do this MVP model view presenter
Trying to implement this design pattern but need some help, my view class extends Application and has a start method inside it how can I access this and launch the view through my main class ?
58 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/17/2024 in #java-help
Can anyone explain this error message
Explain this error message please:
27 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 5/16/2024 in #java-help
What is the benefit of using the Singleton pattern like this:
public static ConfigManager getInstance() {
return ConfigManagerHolder.INSTANCE;
}

private static class ConfigManagerHolder {

private static final ConfigManager INSTANCE = new ConfigManager();
}
}
public static ConfigManager getInstance() {
return ConfigManagerHolder.INSTANCE;
}

private static class ConfigManagerHolder {

private static final ConfigManager INSTANCE = new ConfigManager();
}
}
What is the benefit of having an internal class compared to just returning a new ConfigManager()
33 replies