dghf
dghf
JCHJava Community | Help. Code. Learn.
Created by dghf on 3/2/2024 in #java-help
I need to rewrite this Java code while retaining same functionality
bump
7 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 3/2/2024 in #java-help
I need to rewrite this Java code while retaining same functionality
Does this achieve the same thing?
7 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 3/2/2024 in #java-help
I need to rewrite this Java code while retaining same functionality
Any advice
7 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
And I don't know what lambda exp. is
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
Multithreading is for next asssignment
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
in which file?
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
but i want it to be functionally the same, that's to say it should work
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
which one are you talking about now
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
but apart from the comments
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
yeah
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
import java.io.*;
import java.net.*;

class HttpRequest {
private String request;

public HttpRequest(InputStream inputStream) throws IOException {
StringBuilder requestBuilder = new StringBuilder();
int byteRead = inputStream.read();
while (byteRead != '\n') {
requestBuilder.append((char) byteRead);
}
request = requestBuilder.toString();
}

// Getter method
public String getRequest() {
return request;
}

// Method to parse request parameters
public String[] getRequestParams() {
String[] requestLines = request.split("\\r?\\n");
return requestLines[0].split("[?\\s]");
}
}
import java.io.*;
import java.net.*;

class HttpRequest {
private String request;

public HttpRequest(InputStream inputStream) throws IOException {
StringBuilder requestBuilder = new StringBuilder();
int byteRead = inputStream.read();
while (byteRead != '\n') {
requestBuilder.append((char) byteRead);
}
request = requestBuilder.toString();
}

// Getter method
public String getRequest() {
return request;
}

// Method to parse request parameters
public String[] getRequestParams() {
String[] requestLines = request.split("\\r?\\n");
return requestLines[0].split("[?\\s]");
}
}
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
import java.io.*;
import java.net.*;

// Class to handle HTTP responses
class HttpResponse {
private String response;

// Constructor
public HttpResponse(String response) {
this.response = response;
}

// Method to send response to client
public void send(DataOutputStream out) throws IOException {
out.writeBytes(response);
}
}
import java.io.*;
import java.net.*;

// Class to handle HTTP responses
class HttpResponse {
private String response;

// Constructor
public HttpResponse(String response) {
this.response = response;
}

// Method to send response to client
public void send(DataOutputStream out) throws IOException {
out.writeBytes(response);
}
}
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
Is this better?
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 2/24/2024 in #java-help
Can my code be improved?
@Code@szatkus@HAO@tjoener
34 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 6/4/2023 in #java-help
Need help with Strategy pattern for discounts in POS system
But I am not sure if that'd break encapsulation or violate OOP principles and bring bad code smells
23 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 6/4/2023 in #java-help
Need help with Strategy pattern for discounts in POS system
So I'd have to move the createSaleAndApplyDiscount method from the Controller class to the View class, and call it after adding the products to the sale? So
public void simulateSaleProcess() {

cont.initializeNewSale();
getProductById(firstProductId);
getProductById(secondProductId);
getProductById(firstProductId);
cont.finalizeSaleAndUpdateSystems();
sendProductInfoToDisplay();
// Call the createSaleAndApplyDiscount method here
createSaleAndApplyDiscount();
cont.processPaymentAndReturnChange(Payment.FIRST_PAYMENT);

cont.initializeNewSale();
getProductById(firstProductId);
getProductById(firstProductId);
getProductById(121);
cont.finalizeSaleAndUpdateSystems();
sendProductInfoToDisplay();
// Call the createSaleAndApplyDiscount method here
createSaleAndApplyDiscount();
cont.processPaymentAndReturnChange(Payment.SECOND_PAYMENT);
}

// Move the createSaleAndApplyDiscount method from the Controller class to the View class
private void createSaleAndApplyDiscount() {
// Set the discounter field to a new instance of QuantityDiscounter with a parameter of 3
Discounter discounter = new QuantityDiscounter(3);
cont.setDiscounter(discounter);
// Call the applyDiscount method on the sale object and print the discounted price
BigDecimal amount = cont.getSaleDTO().getPaymentDTO().getTotalAmount();
BigDecimal discountedPrice = cont.applyDiscount(amount);
System.out.println("The discounted price is: " + discountedPrice);
}
public void simulateSaleProcess() {

cont.initializeNewSale();
getProductById(firstProductId);
getProductById(secondProductId);
getProductById(firstProductId);
cont.finalizeSaleAndUpdateSystems();
sendProductInfoToDisplay();
// Call the createSaleAndApplyDiscount method here
createSaleAndApplyDiscount();
cont.processPaymentAndReturnChange(Payment.FIRST_PAYMENT);

cont.initializeNewSale();
getProductById(firstProductId);
getProductById(firstProductId);
getProductById(121);
cont.finalizeSaleAndUpdateSystems();
sendProductInfoToDisplay();
// Call the createSaleAndApplyDiscount method here
createSaleAndApplyDiscount();
cont.processPaymentAndReturnChange(Payment.SECOND_PAYMENT);
}

// Move the createSaleAndApplyDiscount method from the Controller class to the View class
private void createSaleAndApplyDiscount() {
// Set the discounter field to a new instance of QuantityDiscounter with a parameter of 3
Discounter discounter = new QuantityDiscounter(3);
cont.setDiscounter(discounter);
// Call the applyDiscount method on the sale object and print the discounted price
BigDecimal amount = cont.getSaleDTO().getPaymentDTO().getTotalAmount();
BigDecimal discountedPrice = cont.applyDiscount(amount);
System.out.println("The discounted price is: " + discountedPrice);
}
23 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 6/4/2023 in #java-help
Need help with Strategy pattern for discounts in POS system
You think the issue is that createSaleAndApplyDiscount creates an empty sale and doesn't add any products to it?
23 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 6/4/2023 in #java-help
Need help with Strategy pattern for discounts in POS system
The code that calculates the discount is in the Sale class, in the applyDiscount method. Here is the code:
// A method that uses the strategy object to apply the discount and handles any exceptions that may occur
public BigDecimal applyDiscount(BigDecimal amount) {
try {
// Use the discounter object to apply the discount
// Convert the double value to a BigDecimal value
// For example, you can use the BigDecimal.valueOf method
BigDecimal payAmount = BigDecimal.valueOf(payment.getPayAmount());
return discounter.applyDiscount(payAmount);
} catch (DatabaseFailureException e) {
// Handle the exception here
// For example, you can print or log an error message for developers and users
System.out.println("Developer log: " + e.getMessage());
System.out.println("User message: There was an error when fetching discounts from database.");
// You can also return a default value or do something else
// For example, you can return the original amount without any discount
return amount;
}
}
// A method that uses the strategy object to apply the discount and handles any exceptions that may occur
public BigDecimal applyDiscount(BigDecimal amount) {
try {
// Use the discounter object to apply the discount
// Convert the double value to a BigDecimal value
// For example, you can use the BigDecimal.valueOf method
BigDecimal payAmount = BigDecimal.valueOf(payment.getPayAmount());
return discounter.applyDiscount(payAmount);
} catch (DatabaseFailureException e) {
// Handle the exception here
// For example, you can print or log an error message for developers and users
System.out.println("Developer log: " + e.getMessage());
System.out.println("User message: There was an error when fetching discounts from database.");
// You can also return a default value or do something else
// For example, you can return the original amount without any discount
return amount;
}
}
23 replies
JCHJava Community | Help. Code. Learn.
Created by dghf on 6/4/2023 in #java-help
Need help with Strategy pattern for discounts in POS system
This is my entire code
23 replies