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;
}
}