0

I have a entity class:

Product

public String email;
public int age;
public double price;
public double discount;

And I want to create service class for it to create some calculations

Do I need to extend this Product class to consume variables like email, age, price so I can do calculations?

And also do I need to create object from service or I can do it somehow with my Entity object but still use methods on it?

I have this in main for example

Service service = new Service("email", 1, 30)

You see its service, while I want to create object from entity and then on that object consume methods.

So it would be

Product product = new Product ("email", 1, 30)

Main

public class Main {

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    TaxService taxService = new TaxService("The Little Prince", 12345, 20.25);

    int option = 0;
    do {
        menu();
        option = scanner.nextInt();
        switch (option) {
            case 1:
                taxService.tax();
                break;
            case 2:
                System.out.println(
                        "\nThank you for using the program. Goodbye!\n");
                System.exit(0);

                break;
            default:
                System.out.println("Invalid input");
                break;
        }
    } while (option != 9);
}


public static void menu() {
    System.out.println("Choose action");
    System.out.println("1. Tax calculate\n");
    System.out.println("2: Exit program");


    System.out.print("Enter your selection: ");
}

}

Entity

public class Product {
public String name;
public int UPC;
public double price;
public double tax;


public Product() {
}

public Product(String name, int UPC, double price) {
    this.name = name;
    this.UPC = UPC;
    this.price = price;
    this.tax = 20;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getUPC() {
    return UPC;
}

public void setUPC(int UPC) {
    this.UPC = UPC;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public double getTax() {
    return tax;
}

public void setTax(double tax) {
    this.tax = tax;
}

}

Service

public class TaxService extends Product {

Scanner scanner = new Scanner(System.in);

public TaxService(String name, int UPC, double price) {
    super(name, UPC, price);
}

public void tax() {
    System.out.println("Do you want to change tax percentage? (y / n)");
    String answer = scanner.nextLine();

    if (answer.equalsIgnoreCase("y")) {
        System.out.println("Please enter a new tax percentage: ");
        double newTaxPercentage = scanner.nextInt();
        scanner.nextLine();
        if (newTaxPercentage < 0) {
            System.out.println("Wrong input, try again.");
        } else {
            double calculateTax = (price * (newTaxPercentage / 100));
            double result = calculateTax + price;
            System.out.println("Calculation finished");
            System.out.printf("Product price reported as $%.2f before tax and $%.2f after %.0f%% tax.\n", price, result, newTaxPercentage);
        }
    } else {
        double calculateTax = (price * (tax / 100));
        double result = calculateTax + price;
        System.out.println("Calculation finished");
        System.out.printf("Product price reported as $%.2f before tax and $%.2f after %.0f%% tax.\n", price, result, tax);
    }
}

private boolean isTaxValid() {
    return tax < 0;
}

}

  • everything depends on how you write your code. – Stultuske Aug 09 '22 at 11:07
  • Can you give me some idea? I think its bad to use my service class for creating objects, I want to create entity from which will I create object and on that object I will consume methods from service – Sefan Jankovic Aug 09 '22 at 11:09
  • makes little sense to me, maybe I just don't understand you correctly. Can you put an actual example in your question of what you mean? – Stultuske Aug 09 '22 at 11:11
  • I edited question with full code so far – Sefan Jankovic Aug 09 '22 at 11:12
  • TaxService extends Product... that makes no sense at all. There is no "IS-A" relationship between the two classes. Create the products in your runner class, and pass them as parameters to your tax method – Stultuske Aug 09 '22 at 11:13
  • maybe I just don't understand you correctly. Can you put an actual example in your answer of what you mean? – Sefan Jankovic Aug 09 '22 at 11:19
  • just delete the "extends Product". Then, in your runner do Product p = new Product (/* parameters */ ); let your taxService methods accept a parameter of type Product, and pass p as parameter when you call them – Stultuske Aug 09 '22 at 11:29
  • Yeah, I did that, in my main I have now Product object but how I can use my methods on that object? That is my problem right now, when I call `taxService.tax()` its empty, like method doesnt use variables from my entity – Sefan Jankovic Aug 09 '22 at 11:32
  • How to achieve this `let your taxService methods accept a parameter of type Product` – Sefan Jankovic Aug 09 '22 at 11:32
  • I manage to fix it, thank you! – Sefan Jankovic Aug 09 '22 at 11:36

1 Answers1

0

You should separate the responsibilities of the TaxService and Product. The idea is that the TaxService is working with products, it is not a product or a parent class of a product. So inside TaxService class you should not extend Product.

To work with a product you can accept in the methods signatures of TaxService Product as argument:

public void tax(Product product) {
...// some code
// Everywhere when you need properties from the product just call the getters
double result = product.getCalculateTax() + product.getPrice();

Now on creation of the service you don't need to add values in the constructor. So in the main you should have:

Product product = new Product("The Little Prince", 12345, 20.25);
TaxService taxService = new TaxService(scanner); // below I explain why we pass scanner here.
..... // some code
taxService.tax(product);

By the way it is not good practice to open many Scanners for the same resource. You can see here: Java Multiple Scanners

What I will do it to pass the Scanner from the main to the TaxService when I create tax service in the constructor

Level_Up
  • 789
  • 5
  • 19
  • Okay, what about other part of code anyway? Is it good, or is there a way to reduce it? – Sefan Jankovic Aug 09 '22 at 12:17
  • This is another question but normally you are putting directly the tax 20.25 and in the service you are asking if the user want to change it. Better ask in the main. Make another case and put custom tax value. Before executing the service you can set the new tax value in product with `product.setTax(newTax)`. Now in the service you don't need to execute two times the same things. See what you are doing in the `if` and in the `else` part – Level_Up Aug 09 '22 at 12:27
  • Also in java when you are working with currency and you want to be exact this value you should use BigDecimal not double. See this for more info: https://stackoverflow.com/questions/3413448/double-vs-bigdecimal – Level_Up Aug 09 '22 at 12:31