-
Notifications
You must be signed in to change notification settings - Fork 66
Lab3 #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lab3 #31
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| target/ | ||
| /.idea/ | ||
| .env |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,40 @@ | ||
| package com.example; | ||
|
|
||
| import io.github.cdimascio.dotenv.Dotenv; | ||
| import javafx.application.Platform; | ||
| import javafx.collections.FXCollections; | ||
| import javafx.collections.ObservableList; | ||
| import tools.jackson.databind.ObjectMapper; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain
The import Update the import to match the actual package provided by your Jackson version (most likely 🏁 Script executed: #!/bin/bash
# Examine the HelloModel.java file and check dependencies
echo "=== HelloModel.java imports and class structure ==="
cat src/main/java/com/example/HelloModel.java 2>/dev/null || echo "File not found at expected path"
echo ""
echo "=== Searching for pom.xml to check dependencies ==="
find . -name "pom.xml" -type f | head -5
echo ""
echo "=== Searching for build.gradle to check dependencies ==="
find . -name "build.gradle*" -type f | head -5Length of output: 3772 🏁 Script executed: #!/bin/bash
# Check pom.xml for Jackson dependencies
echo "=== pom.xml Jackson dependencies ==="
cat pom.xmlLength of output: 3021
The import <groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>The groupId is Additionally, Fix: Update <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>Then update the import to 🤖 Prompt for AI Agents |
||
|
|
||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Model layer: encapsulates application data and business logic. | ||
| */ | ||
| public class HelloModel { | ||
|
|
||
| private final String hostName; | ||
| private final HttpClient http = HttpClient.newHttpClient(); | ||
| private final ObjectMapper mapper = new ObjectMapper(); | ||
| private final ObservableList<NtfyMessageDto> messages = FXCollections.observableArrayList(); | ||
|
|
||
| private boolean senderMe=false; | ||
|
|
||
| public HelloModel() { | ||
| Dotenv dotenv = Dotenv.load(); | ||
| hostName = Objects.requireNonNull(dotenv.get("HOST_NAME")); | ||
| receiveMessage(); | ||
| } | ||
|
|
||
| public ObservableList<NtfyMessageDto> getMessages() { | ||
| return messages; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a greeting based on the current Java and JavaFX versions. | ||
| */ | ||
|
|
@@ -12,4 +43,52 @@ public String getGreeting() { | |
| String javafxVersion = System.getProperty("javafx.version"); | ||
| return "Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + "."; | ||
| } | ||
|
|
||
| public void sendMessage(String text) { | ||
|
|
||
| senderMe=true; | ||
|
|
||
| long now = System.currentTimeMillis() / 1000; | ||
| NtfyMessageDto myMsg = new NtfyMessageDto("local", now, "message", "me", text); | ||
|
|
||
| Platform.runLater(() -> messages.add(myMsg)); | ||
|
|
||
| HttpRequest httpRequest= HttpRequest.newBuilder() | ||
| .POST(HttpRequest.BodyPublishers.ofString(text)) | ||
| .uri(URI.create(hostName + "/mytopic")) | ||
| .build(); | ||
| http.sendAsync(httpRequest, HttpResponse.BodyHandlers.discarding()) | ||
| .exceptionally(ex -> { | ||
| System.out.println("Error sending message: " + ex.getMessage()); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| public void receiveMessage(){ | ||
| HttpRequest httpRequest = HttpRequest.newBuilder() | ||
| .GET() | ||
| .uri(URI.create(hostName + "/mytopic/json")) | ||
| .build(); | ||
|
|
||
| http.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofLines()) | ||
| .thenAccept(response -> response.body() | ||
| .map(s -> { | ||
| try { | ||
| return mapper.readValue(s, NtfyMessageDto.class); | ||
| } catch (Exception e) { | ||
| System.err.println("Failed to parse message: " + e.getMessage()); | ||
| return null; | ||
| } | ||
| }) | ||
| .filter(Objects::nonNull) | ||
| .filter(msg -> "message".equals(msg.event())) | ||
| .forEach(msg->{ | ||
| if (senderMe) { | ||
| senderMe=false; | ||
| return; | ||
| } | ||
| Platform.runLater(() -> messages.add(msg)); | ||
| })); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||
| package com.example; | ||||||
|
|
||||||
| public class ManyParameters { | ||||||
|
|
||||||
| public ManyParameters(String computerName, int timeout, String method, int size, byte[] data) { | ||||||
|
|
||||||
| } | ||||||
|
Comment on lines
+5
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constructor body is empty - parameters are not stored. The constructor accepts five parameters but the body is empty, meaning all parameters are discarded. The class also has no fields to store this data. This renders the class non-functional. Add fields and assign the constructor parameters: public class ManyParameters {
+ private final String computerName;
+ private final int timeout;
+ private final String method;
+ private final int size;
+ private final byte[] data;
public ManyParameters(String computerName, int timeout, String method, int size, byte[] data) {
-
+ this.computerName = computerName;
+ this.timeout = timeout;
+ this.method = method;
+ this.size = size;
+ this.data = data;
}
🤖 Prompt for AI Agents |
||||||
|
|
||||||
| static void main () { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect main method signature. The method signature should be Apply this diff: - static void main () {
+ public static void main(String[] args) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
| ManyParametersBuilder builder= new ManyParametersBuilder(); | ||||||
| builder | ||||||
| .setComputerName("localhost") | ||||||
| .setTimeout(10) | ||||||
| .setSize(0) | ||||||
| .createManyParameters(); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.example; | ||
|
|
||
| public class ManyParametersBuilder { | ||
| private String computerName; | ||
| private int timeout = 0; | ||
| private String method; | ||
| private int size = 0; | ||
| private byte[] data = null; | ||
|
|
||
| public ManyParametersBuilder setComputerName(String computerName) { | ||
| this.computerName = computerName; | ||
| return this; | ||
| } | ||
|
|
||
| public ManyParametersBuilder setTimeout(int timeout) { | ||
| this.timeout = timeout; | ||
| return this; | ||
| } | ||
|
|
||
| public ManyParametersBuilder setMethod(String method) { | ||
| this.method = method; | ||
| return this; | ||
| } | ||
|
|
||
| public ManyParametersBuilder setSize(int size) { | ||
| this.size = size; | ||
| return this; | ||
| } | ||
|
|
||
| public ManyParametersBuilder setData(byte[] data) { | ||
| this.data = data; | ||
| return this; | ||
| } | ||
|
Comment on lines
+30
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing defensive copy for mutable byte array. The Apply this diff to create a defensive copy: public ManyParametersBuilder setData(byte[] data) {
- this.data = data;
+ this.data = data != null ? data.clone() : null;
return this;
}🤖 Prompt for AI Agents |
||
|
|
||
| public ManyParameters createManyParameters() { | ||
| return new ManyParameters(computerName, timeout, method, size, data); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.example; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
|
||
| import java.text.SimpleDateFormat; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Date; | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| public record NtfyMessageDto(String id, long time, String event, String topic, String message){ | ||
| private static final DateTimeFormatter date = | ||
| DateTimeFormatter.ofPattern("HH:mm"); | ||
| @Override | ||
| public String toString(){ | ||
| String timeStr = new SimpleDateFormat("HH:mm").format(new Date(time * 1000)); | ||
| return timeStr+ " " + message ; | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.example; | ||
|
|
||
| public class Singelton { | ||
|
|
||
| private final static Singelton instance= new Singelton(); | ||
|
|
||
| public static Singelton getInstance(){ | ||
| return instance; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,22 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <?import javafx.scene.layout.StackPane?> | ||
| <?import javafx.scene.control.Label?> | ||
|
|
||
| <StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.HelloController"> | ||
| <children> | ||
| <Label fx:id="messageLabel" text="Hello, JavaFX!" /> | ||
| </children> | ||
| </StackPane> | ||
| <?import javafx.geometry.*?> | ||
| <?import javafx.scene.control.*?> | ||
| <?import javafx.scene.layout.*?> | ||
|
|
||
| <VBox prefHeight="482.0" prefWidth="248.0" spacing="5.0" xmlns="http://javafx.com/javafx/17.0.12" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.HelloController"> | ||
| <Label fx:id="messageLabel" /> | ||
| <ListView fx:id="messageView" prefHeight="373.0" prefWidth="248.0" VBox.vgrow="ALWAYS" /> | ||
|
|
||
| <HBox spacing="5.0"> | ||
| <Button fx:id="emojiButton" onAction="#emojis" prefHeight="25.0" prefWidth="34.0" text="😀" /> | ||
| <TextField fx:id="messageField" prefHeight="50.0" prefWidth="154.0" promptText="Type your message..." HBox.hgrow="ALWAYS"> | ||
| <HBox.margin> | ||
| <Insets bottom="5.0" /> | ||
| </HBox.margin></TextField> | ||
| <Button onAction="#sendMessage" prefHeight="34.0" prefWidth="43.0" text="Send"> | ||
| <HBox.margin> | ||
| <Insets right="2.0" /> | ||
| </HBox.margin></Button> | ||
| </HBox> | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </VBox> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Label text from FXML is overridden by greeting
initialize()setsmessageLabelfrommodel.getGreeting(), so the"CHAT LAB3"text configured inhello-view.fxmlwill never be visible. If the intent is to show the “CHAT LAB3” header, consider removing this assignment or using a separate label for the greeting/version info.🤖 Prompt for AI Agents