package org.example;
public class Car extends Veichle {
public Car(String type, Boolean taxClas) {
super(type, taxClas);
}
public Car() {
}
@Override
public String getType() {
return "Car";
}
@Override
public Boolean getTaxClas() {
return true;
}
}
package org.example;
public class Motorcycle extends Veichle{
public Motorcycle(String type, Boolean taxClas) {
super(type, taxClas);
}
public Motorcycle() {
}
@Override
public String getType() {
return "Motorcycle";
}
@Override
public Boolean getTaxClas() {
return true;
}
}
package org.example;
public class Ambulance extends Veichle{
public Ambulance(String type, Boolean taxClas) {
super(type, taxClas);
}
public Ambulance() {
}
@Override
public String getType() {
return "Ambulance";
}
@Override
public Boolean getTaxClas() {
return false;
}
}
package org.example;
import java.util.Objects;
public abstract class Veichle {
private String type;
private Boolean taxClas;
public Veichle(String type, Boolean taxClas) {
this.type = type;
this.taxClas = taxClas;
}
public Veichle(){
}
public abstract String getType();
public abstract Boolean getTaxClas();
@Override
public String toString() {
return "Veichle{" +
"type='" + type + '\'' +
", taxClas=" + taxClas +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Veichle veichle = (Veichle) o;
return type.equals(veichle.type) && taxClas.equals(veichle.taxClas);
}
@Override
public int hashCode() {
return Objects.hash(type, taxClas);
}
}
Thanks for replying and taking your time, above is the current classes iv done.
Now when it comes to the calculation class im sorry i was a bit of topic and didnt really explain it that well. Take a look a current calculation iv done.
package org.example;
import java.text.NumberFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class TaxCalculator {
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH.mm");
public void veichleType(Veichle veichle) {
Boolean tax;
try {
if (!(veichle instanceof Car || veichle instanceof Ambulance)) {
}
}catch (Exception e) {
System.out.println("Not a valid Veichle");
}
tax = veichle.getTaxClas();
var price = calculatePrice(tax);
printMethod(price);
}
private void printMethod(double price) {
NumberFormat.getCurrencyInstance().format(price);
System.out.println("Fee is: ");
System.out.println(NumberFormat.getCurrencyInstance().format(price));
}
private double calculatePrice(boolean tax) {
LocalTime today = LocalTime.now();
String timeString = today.format(formatter);
var newTime = Double.parseDouble(timeString);
System.out.println("Local time passed the taxometer is: ");
System.out.println(newTime);
System.out.println("----------");
if (tax == true) {
var cost = compareTime(newTime);
return cost;
}
return 0;
}
private double compareTime(double hour) {
if (hour >= 24.00 && hour <= 06.00) return 20.00;
else if (hour > 06.00 && hour <= 12.00) return 30.00;
else if (hour > 12.00 && hour <= 18.00) return 40.00;
else if (hour > 18.00) return 30.00;
return 0;
}
}
Main
package org.example;
public class Main {
public static void main(String[] args) {
var carObject = new Car("Car", true);
TaxCalculator calc = new TaxCalculator();
var motorCycleObject = new Motorcycle("Motorcycle", true);
calc.veichleType(motorCycleObject);
calc.veichleType(carObject);
//var ambulanceObject = new Ambulance("Ambulance", false);
//calc.veichleType(ambulanceObject);
//calc.veichleType(ambulanceObject);
}
}
So all im trying to see if the childclass is taxable then use a method to return current time with a value. I think i figured the calculation part, i mean its working? I guess its not really the best library to compare time… in the longer run for more complex calculations, so any ideas on what library to use would be appricated
My org guestion: On the Calculation class I need to know if the instance really are a child right?
But the way its implemented is wrong…
- Im currently not doing anything if its not a Veichle?
-How could this method be improved?
- What if i want to check 10 - 20 types of Veichles, i dont feel im gonna do if statement that is 20 lines long cheecking every instanceof right?
Overall thanks for the help!
Image: