├── .classpath ├── .gitignore ├── .project └── src ├── application └── Program.java └── model ├── entities ├── CarRental.java ├── Invoice.java └── Vehicle.java └── services ├── BrazilTaxService.java ├── RentalService.java └── TaxService.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | /bin/ 25 | 26 | # Eclipse 27 | .settings/ 28 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | course 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/application/Program.java: -------------------------------------------------------------------------------- 1 | package application; 2 | 3 | import java.text.ParseException; 4 | import java.time.LocalDateTime; 5 | import java.time.format.DateTimeFormatter; 6 | import java.util.Date; 7 | import java.util.Locale; 8 | import java.util.Scanner; 9 | 10 | import model.entities.CarRental; 11 | import model.entities.Vehicle; 12 | import model.services.BrazilTaxService; 13 | import model.services.RentalService; 14 | 15 | public class Program { 16 | 17 | public static void main(String[] args) throws ParseException { 18 | 19 | Locale.setDefault(Locale.US); 20 | Scanner sc = new Scanner(System.in); 21 | 22 | DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"); 23 | 24 | System.out.println("Entre com os dados do aluguel"); 25 | System.out.print("Modelo do carro: "); 26 | String carModel = sc.nextLine(); 27 | System.out.print("Retirada (dd/MM/yyyy HH:mm): "); 28 | LocalDateTime start = LocalDateTime.parse(sc.nextLine(), fmt); 29 | System.out.print("Retorno (dd/MM/yyyy HH:mm): "); 30 | LocalDateTime finish = LocalDateTime.parse(sc.nextLine(), fmt); 31 | 32 | CarRental cr = new CarRental(start, finish, new Vehicle(carModel)); 33 | 34 | System.out.print("Entre com o preço por hora: "); 35 | double pricePerHour = sc.nextDouble(); 36 | System.out.print("Entre com o preço por dia: "); 37 | double pricePerDay = sc.nextDouble(); 38 | 39 | RentalService rentalService = new RentalService(pricePerDay, pricePerHour, new BrazilTaxService()); 40 | 41 | rentalService.processInvoice(cr); 42 | 43 | System.out.println("FATURA:"); 44 | System.out.println("Pagamento basico: " + String.format("%.2f", cr.getInvoice().getBasicPayment())); 45 | System.out.println("Imposto: " + String.format("%.2f", cr.getInvoice().getTax())); 46 | System.out.println("Pagamento total: " + String.format("%.2f", cr.getInvoice().getTotalPayment())); 47 | 48 | sc.close(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/model/entities/CarRental.java: -------------------------------------------------------------------------------- 1 | package model.entities; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | public class CarRental { 6 | 7 | private LocalDateTime start; 8 | private LocalDateTime finish; 9 | 10 | private Vehicle vehicle; 11 | private Invoice invoice; 12 | 13 | public CarRental() { 14 | } 15 | 16 | public CarRental(LocalDateTime start, LocalDateTime finish, Vehicle vehicle) { 17 | this.start = start; 18 | this.finish = finish; 19 | this.vehicle = vehicle; 20 | } 21 | 22 | public LocalDateTime getStart() { 23 | return start; 24 | } 25 | 26 | public void setStart(LocalDateTime start) { 27 | this.start = start; 28 | } 29 | 30 | public LocalDateTime getFinish() { 31 | return finish; 32 | } 33 | 34 | public void setFinish(LocalDateTime finish) { 35 | this.finish = finish; 36 | } 37 | 38 | public Vehicle getVehicle() { 39 | return vehicle; 40 | } 41 | 42 | public void setVehicle(Vehicle vehicle) { 43 | this.vehicle = vehicle; 44 | } 45 | 46 | public Invoice getInvoice() { 47 | return invoice; 48 | } 49 | 50 | public void setInvoice(Invoice invoice) { 51 | this.invoice = invoice; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/model/entities/Invoice.java: -------------------------------------------------------------------------------- 1 | package model.entities; 2 | 3 | public class Invoice { 4 | 5 | private Double basicPayment; 6 | private Double tax; 7 | 8 | public Invoice() { 9 | } 10 | 11 | public Invoice(Double basicPayment, Double tax) { 12 | this.basicPayment = basicPayment; 13 | this.tax = tax; 14 | } 15 | 16 | public Double getBasicPayment() { 17 | return basicPayment; 18 | } 19 | 20 | public void setBasicPayment(Double basicPayment) { 21 | this.basicPayment = basicPayment; 22 | } 23 | 24 | public Double getTax() { 25 | return tax; 26 | } 27 | 28 | public void setTax(Double tax) { 29 | this.tax = tax; 30 | } 31 | 32 | public Double getTotalPayment() { 33 | return getBasicPayment() + getTax(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/model/entities/Vehicle.java: -------------------------------------------------------------------------------- 1 | package model.entities; 2 | 3 | public class Vehicle { 4 | 5 | private String model; 6 | 7 | public Vehicle() { 8 | } 9 | 10 | public Vehicle(String model) { 11 | this.model = model; 12 | } 13 | 14 | public String getModel() { 15 | return model; 16 | } 17 | 18 | public void setModel(String model) { 19 | this.model = model; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/model/services/BrazilTaxService.java: -------------------------------------------------------------------------------- 1 | package model.services; 2 | 3 | public class BrazilTaxService implements TaxService { 4 | 5 | public double tax(double amount) { 6 | if (amount <= 100.0) { 7 | return amount * 0.2; 8 | } 9 | else { 10 | return amount * 0.15; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/model/services/RentalService.java: -------------------------------------------------------------------------------- 1 | package model.services; 2 | 3 | import java.time.Duration; 4 | 5 | import model.entities.CarRental; 6 | import model.entities.Invoice; 7 | 8 | public class RentalService { 9 | 10 | private Double pricePerDay; 11 | private Double pricePerHour; 12 | 13 | private TaxService taxService; 14 | 15 | public RentalService(Double pricePerDay, Double pricePerHour, TaxService taxService) { 16 | this.pricePerDay = pricePerDay; 17 | this.pricePerHour = pricePerHour; 18 | this.taxService = taxService; 19 | } 20 | 21 | public void processInvoice(CarRental carRental) { 22 | 23 | double minutes = Duration.between(carRental.getStart(), carRental.getFinish()).toMinutes(); 24 | double hours = minutes / 60.0; 25 | 26 | double basicPayment; 27 | if (hours <= 12.0) { 28 | basicPayment = pricePerHour * Math.ceil(hours); 29 | } 30 | else { 31 | basicPayment = pricePerDay * Math.ceil(hours / 24); 32 | } 33 | 34 | double tax = taxService.tax(basicPayment); 35 | 36 | carRental.setInvoice(new Invoice(basicPayment, tax)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/model/services/TaxService.java: -------------------------------------------------------------------------------- 1 | package model.services; 2 | 3 | public interface TaxService { 4 | 5 | double tax(double amount); 6 | } 7 | --------------------------------------------------------------------------------