solucion de la practica
DESCRIPTION
practica dirigidaTRANSCRIPT
Prof. Ing. Alberto Moreno
Solucion de la practica
Cliente.java Detalle.java facturas.java
Paquete Beans
package beans;
public class Cliente {
private String codc;
private String nomc;
public Cliente(String codc, String nomc) {
this.codc = codc;
this.nomc = nomc;
}
public String getCodc() {
return codc;
}
public void setCodc(String codc) {
this.codc = codc;
}
public String getNomc() {
return nomc;
}
public void setNomc(String nomc) {
this.nomc = nomc;
}
}
Facturas.java
package beans;
public class facturas {
private String codf;
private String fecha;
private String igv;
Prof. Ing. Alberto Moreno
public facturas() {
}
public facturas(String codf, String fecha, String igv) {
this.codf = codf;
this.fecha = fecha;
this.igv =igv;
}
public String getCodf() {
return codf;
}
public void setCodf(String codf) {
this.codf = codf;
}
public String getFecha() {
return fecha;
}
public void setFecha(String fecha) {
this.fecha = fecha;
}
public String getIgv() {
return igv;
}
public void setIgv(String igv) {
this.igv = igv;
}
}
Detalle.java
package beans;
public class Detalle {
private String detacod;
private String descrip;
private double precio;
private int cant;
public double total(){
return getCant()*getPrecio();
}
Prof. Ing. Alberto Moreno
public Detalle() {
}
public Detalle(String detacod, String descrip, double
precio, int cant) {
this.detacod = detacod;
this.descrip = descrip;
this.precio = precio;
this.cant = cant;
}
public String getDetacod() {
return detacod;
}
public void setDetacod(String detacod) {
this.detacod = detacod;
}
public String getDescrip() {
return descrip;
}
public void setDescrip(String descrip) {
this.descrip = descrip;
}
public double getPrecio() {
return precio;
}
public void setPrecio(double precio) {
this.precio = precio;
}
public int getCant() {
return cant;
}
public void setCant(int cant) {
this.cant = cant;
}
}
Prof. Ing. Alberto Moreno
En el paquete dao
Clase Negocio.java package dao;
import beans.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Negocio {
public List<Cliente> LisCli(){
List<Cliente> lista=new ArrayList();
String sql="select cli_cod,cli_nom from clientes";
try{
PreparedStatement
st=Conexion.Conecta().prepareStatement(sql);
ResultSet rs=st.executeQuery();
while(rs.next()){
Cliente a=new Cliente(rs.getString(1),rs.getString(2));
lista.add(a);
}
}catch(SQLException ex){
ex.printStackTrace();
}
return lista;
}
public List<facturas> LisFact(String cod){
List<facturas> lista=new ArrayList();
String sql="select fac_num,fac_fec,fac_igv from fac_cabe
where cli_cod=?";
try{
PreparedStatement
st=Conexion.Conecta().prepareStatement(sql);
st.setString(1, cod);
ResultSet rs=st.executeQuery();
while(rs.next()){
facturas a=new
facturas(rs.getString(1),rs.getString(2),rs.getString(3));
lista.add(a);
}
}catch(SQLException ex){
ex.printStackTrace();
}
Prof. Ing. Alberto Moreno
return lista;
}
public List<Detalle> LisDeta(String fac){
List<Detalle> lista=new ArrayList();
String sql="select
A.art_cod,A.art_nom,A.art_pre,A.art_pre,D.art_can from
fac_deta D, articulos A "
+ " where d.art_cod=A.art_cod and D.fac_num=?";
try{
PreparedStatement
st=Conexion.Conecta().prepareStatement(sql);
st.setString(1, fac);
ResultSet rs=st.executeQuery();
while(rs.next()){
Detalle a=new
Detalle(rs.getString(1),rs.getString(2),rs.getDouble(3),rs.ge
tInt(4));
lista.add(a);
}
}catch(SQLException ex){
ex.printStackTrace();
}
return lista;
}
}
En el paquete JSF
Controla3.java package JSF;
import beans.*;
import dao.*;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
//import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
//import javax.faces.bean.RequestScoped;
@ManagedBean
@ViewScoped
//@SessionScoped
Prof. Ing. Alberto Moreno
public class Controla3 {
private List<Cliente> liscli;
private List<facturas> lisfact;
private List<Detalle> lisdeta;
private String nombre;
private String fac;
private double total;
public Controla3 () {
liscli=new ArrayList();
}
@PostConstruct
public void init(){
setLiscli(new Negocio().LisCli());
}
public void elige(Cliente c){
lisfact= new ArrayList();
setLisfact(new Negocio().LisFact(c.getCodc()));
setNombre(c.getNomc());
}
public void deta(facturas f){
lisdeta= new ArrayList();
setLisdeta(new Negocio().LisDeta(f.getCodf()));
// System.out.println(f.getCodf());
setFac(f.getCodf());
setTotal(0);
for(Detalle x:lisdeta)
setTotal(getTotal() + x.total());
}
public List<Cliente> getLiscli() {
return liscli;
}
public void setLiscli(List<Cliente> liscli) {
this.liscli = liscli;
}
public List<facturas> getLisfact() {
return lisfact;
}
public void setLisfact(List<facturas> lisfact) {
this.lisfact = lisfact;
}
public String getNombre() {
return nombre;
Prof. Ing. Alberto Moreno
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getFac() {
return fac;
}
public void setFac(String fac) {
this.fac = fac;
}
public List<Detalle> getLisdeta() {
return lisdeta;
}
public void setLisdeta(List<Detalle> lisdeta) {
this.lisdeta = lisdeta;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
}
En la vista en la pagina xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<center>
<h1>Listado de Clientes</h1>
Prof. Ing. Alberto Moreno
<h:form id="fr1">
<p:dataTable value="#{controla3.liscli}"
paginator="true" rows="5"
var="cli">
<p:column headerText="Codigo">
<p:outputLabel value="#{cli.codc}"/>
</p:column>
<p:column headerText="Nombre">
<p:outputLabel value="#{cli.nomc}"/>
</p:column>
<p:column headerText="ver">
<p:commandLink value="facturas"
action="#{controla3.elige(cli)}"
update=":fr2"/>
</p:column>
</p:dataTable>
</h:form>
<h:form id="fr2">
<p:panelGrid columns="1">
<p:outputLabel value="nombre
seleccionado:#{controla3.nombre}" style="color: blue;font-size:x-
large"/>
</p:panelGrid>
<p:dataTable value="#{controla3.lisfact}"
var="fc">
<p:column headerText="Factura">
<p:outputLabel value="#{fc.codf}"/>
</p:column>
<p:column headerText="Fecha">
<p:outputLabel value="#{fc.fecha}"/>
</p:column>
<p:column headerText="IGV">
<p:outputLabel value="#{fc.igv}"/>
</p:column>
<p:column headerText="ver">
<p:commandLink value="Detalle"
action="#{controla3.deta(fc)}"
update=":fr3"/>
</p:column>
</p:dataTable>
</h:form>
<h:form id="fr3">
<p:panelGrid columns="1">
<p:outputLabel
value="factura:#{controla3.fac}" style="color: blue;font-size:x-
large"/>
</p:panelGrid>
<p:dataTable value="#{controla3.lisdeta}"
var="det">
Prof. Ing. Alberto Moreno
<p:column headerText="codigo">
<p:outputLabel value="#{det.detacod}"/>
</p:column>
<p:column headerText="Descripcion">
<p:outputLabel value="#{det.descrip}"/>
</p:column>
<p:column headerText="precio">
<p:outputLabel value="#{det.precio}"/>
</p:column>
<p:column headerText="cantidad">
<p:outputLabel value="#{det.cant}"/>
</p:column>
<p:column headerText="total">
<p:outputLabel value="#{det.total()}"/>
</p:column>
<p:columnGroup type="footer">
<p:row>
<p:column colspan="4"
footerText="Total pago:"/>
<p:column
footerText="#{controla3.total}"/>
</p:row>
</p:columnGroup>
</p:dataTable>
</h:form>
</center>
</h:body>
</html>
Prof. Ing. Alberto Moreno