Skip to content
Snippets Groups Projects
Commit 4ee90c73 authored by Antoine Maille's avatar Antoine Maille
Browse files

fin tp3

parent 6001e98c
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ import java.util.logging.Logger; ...@@ -16,7 +16,7 @@ import java.util.logging.Logger;
*/ */
public class Main { public class Main {
// Base URI the Grizzly HTTP server will listen on // Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/myapp/"; public static final String BASE_URI = "http://localhost:8080/api/v1/";
/** /**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application. * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
...@@ -58,7 +58,7 @@ public class Main { ...@@ -58,7 +58,7 @@ public class Main {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
final HttpServer server = startServer(); final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at " System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI)); + "%s\nHit enter to stop it...", BASE_URI));
System.in.read(); System.in.read();
server.shutdownNow(); server.shutdownNow();
} }
......
package fr.ulille.iut.tva.dto;
import fr.ulille.iut.tva.service.CalculTva;
import fr.ulille.iut.tva.service.TauxTva;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class InfoPrixDto extends InfoTauxDto {
private double montantTotal;
private double somme;
private double montantTva;
public InfoPrixDto() {
}
public InfoPrixDto(String label, double somme, double taux) {
super(label, taux);
this.somme = somme;
CalculTva calculTva = new CalculTva();
this.montantTotal = calculTva.calculerMontant(TauxTva.valueOf(label), somme);
this.montantTva = montantTotal - somme;
}
public double getMontantTotal() {
return montantTotal;
}
public void setMontantTotal(double montantTotal) {
this.montantTotal = montantTotal;
}
public double getSomme() {
return somme;
}
public void setSomme(double sommme) {
this.somme = sommme;
}
public double getMontantTva() {
return montantTva;
}
public void setMontantTva(double montantTva) {
this.montantTva = montantTva;
}
}
package fr.ulille.iut.tva.dto;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class InfoTauxDto {
private String label;
private double taux;
public InfoTauxDto() {}
public InfoTauxDto(String label, double taux) {
this.label = label;
this.taux = taux;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public double getTaux() {
return taux;
}
public void setTaux(double taux) {
this.taux = taux;
}
}
\ No newline at end of file
package fr.ulille.iut.tva.ressource;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
public class NiveauTvaInexistantException extends WebApplicationException {
private static final long serialVersionUID = 939875418210403804L;
public NiveauTvaInexistantException() {
super(Response.status(Response.Status.NOT_ACCEPTABLE).entity("Niveau de TVA inexistant").build());
}
}
package fr.ulille.iut.tva.ressource; package fr.ulille.iut.tva.ressource;
import java.util.ArrayList;
import java.util.List;
import fr.ulille.iut.tva.dto.InfoPrixDto;
import fr.ulille.iut.tva.dto.InfoTauxDto;
import fr.ulille.iut.tva.service.CalculTva; import fr.ulille.iut.tva.service.CalculTva;
import fr.ulille.iut.tva.service.TauxTva; import fr.ulille.iut.tva.service.TauxTva;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
/** /**
* TvaRessource * TvaRessource
*/ */
@Path("tva")
public class TvaRessource { public class TvaRessource {
private CalculTva calculTva = new CalculTva(); private CalculTva calculTva = new CalculTva();
@GET
@Path("tauxpardefaut")
public double getValeurTauxParDefaut() {
return TauxTva.NORMAL.taux;
}
@GET
@Path("valeur/{niveauTva}")
public double getValeurTaux(@PathParam("niveauTva") String niveau) {
try {
return TauxTva.valueOf(niveau.toUpperCase()).taux;
}
catch ( Exception ex ) {
throw new NiveauTvaInexistantException();
}
}
@GET
@Path("{niveauTva}")
public double getMontantTotal(@PathParam("niveauTva") String niveau, @QueryParam("somme") int somme) {
try {
return calculTva.calculerMontant(TauxTva.valueOf(niveau), somme);
}
catch ( Exception ex ) {
throw new NiveauTvaInexistantException();
}
}
@GET
@Path("lestaux")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public List<InfoTauxDto> getInfoTaux() {
ArrayList<InfoTauxDto> result = new ArrayList<InfoTauxDto>();
for ( TauxTva t : TauxTva.values() ) {
result.add(new InfoTauxDto(t.name(), t.taux));
}
return result;
}
@GET
@Path("details/{niveauTva}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public InfoPrixDto getDetail(@PathParam("niveauTva") String niveau, @QueryParam("somme") int somme) {
try {
return new InfoPrixDto(niveau.toUpperCase(), somme, TauxTva.valueOf(niveau.toUpperCase()).taux);
}
catch ( Exception ex ) {
throw new NiveauTvaInexistantException();
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment