Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by xDTatoDx ( 14 years ago )
package tareabdvendedetodo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class UI {
private static boolean error = false;
private static ResultSet resultSet;//Variable para que no arroje error nada mas xD(NO SIRVE)
private static int tamañoVarCharNomOApel = 50;//se Modifica dependiendo del Varchar Ocupado
private static int tamañoVarCharCalleComunaCiudadEmail = 50;
public static void main(String[] args) {
// TODO code application logic here
int opcion = 0;
BufferedReader tcld = new BufferedReader(new InputStreamReader(System.in));
System.out.println("=================================");
System.out.println(" Menu ");
System.out.println("=================================\n");
System.out.println("1.- Listar Vendedores.");
System.out.println("2.- Buscar Vendedor.");
System.out.println("3.- Listar Clientes.");
System.out.println("4.- Agregar Cliente.");
System.out.println("5.- Modificar un Cliente.");
System.out.println("6.- Eliminar un Cliente.");
System.out.println("7.- Listar productos.");
System.out.println("8.- Buscar producto por Descripción.");
System.out.println("9.- Producto más vendido entre Fechas.");
System.out.println("10.- Producto menos vendido entre Fechas.");
System.out.println("11.- Producto más vendido por Oficina.");
System.out.println("12.- Producto menos vendido por Oficina.");
System.out.println("13.- Vendedor con más Ventas.");
System.out.println("14.- vendedor con menos Ventas.\n");
System.out.println("Ingrese la Opción que desea realizar:");
do{
error=false;
try {
opcion = Integer.parseInt(tcld.readLine());
} catch (IOException ex) {error=true;System.out.println("Error, ingrese el numero nuevamente.");}
catch(NumberFormatException e)
{error = true;System.out.println("Error, formato de numero invalido.\nIntentelo nuevamente.");}
}while(error);
switch(opcion){
case 1:
System.out.println("Los Vendedores son:\n.");
imprimir(resultSet);//ResultSet de los Vendedores
break;
case 2:
int op=0;;
System.out.println("Ingrese Filtro:\n1.- Buscar por Nombre.\n2.- Buscar por Apellido.\n");
do{
error=false;
try {
op = Integer.parseInt(tcld.readLine());
} catch (IOException ex) {error=true;System.out.println("Error, ingrese el numero nuevamente.");}
catch(NumberFormatException e)
{error = true;System.out.println("Error, formato de numero invalido.\nIntentelo nuevamente.");}
}while(error);
String aux="";
switch(op){
case 1:
System.out.println("Ingrese el Nombre del Vendedor a buscar:");
do{
error = false;
try {
aux = tcld.readLine();
} catch (IOException ex)
{System.out.println("Ha ocurrido un error al momento de ingresar el Nombre del Vendedor.\nIntentelo nuevamente.\n");}
}while(error);
imprimir(resultSet);//Resultset de la Busqueda por nombre
break;
case 2:
System.out.println("Ingrese el Apellido del Vendedor a buscar:");
do{
error = false;
try {
aux = tcld.readLine();
} catch (IOException ex)
{System.out.println("Ha ocurrido un error al momento de ingresar el Apellido del Vendedor.\nIntentelo nuevamente.\n");}
}while(error);
imprimir(resultSet);//ResultSet de la Busqueda por Apellido
break;
default:
System.out.println("Ingrese una opcion valida.");
}//Fin switch buscar nombre o apellido
break;
case 3:
System.out.println("Los clientes son: \n");
imprimir(resultSet);//resultSet de los Clientes
break;
case 4:
String nombre="",apaterno="",amaterno="",rut="",calle="";
String ciudad="",comuna="",email="",telefono="",numero="";
System.out.println("Ingrese los Datos del cliente:\n");
//Ingresar Nombre
System.out.print("Nombre: ");
try{
nombre = tcld.readLine();
while(!nombreOApellidoValido(nombre)){
System.out.println("Nombre invalido. Intente nuevamente. \nNombre: ");
nombre = tcld.readLine();
}
//Ingresar Apaterno
System.out.print("Apellido Paterno: ");
apaterno = tcld.readLine();
while(!nombreOApellidoValido(apaterno)){
System.out.println("Apellido invalido. Intente nuevamente. \nApellido Paterno: ");
apaterno = tcld.readLine();
}
//Ingresar AMaterno
System.out.print("Apellido Materno: ");
amaterno = tcld.readLine();
while(!nombreOApellidoValido(amaterno)){
System.out.println("Apellido Materno invalido. Intente nuevamente. \nApellido Materno: ");
amaterno = tcld.readLine();
}
//Ingresar Rut
System.out.print("Rut: ");
rut = tcld.readLine();
while(!rutValido(rut)){
System.out.println("Rut invalido. El Rut debe ser sin puntos ni digito verificador.\nIntentelo nuevamente. \nRut: ");
rut= tcld.readLine();
}
//Ingresar Calle Comuna o Ciudad
System.out.print("Calle: ");
calle = tcld.readLine();
while(!validarCalleComunaCiudadEmail(calle)){
System.out.println("Calle invalida.\nIntentelo nuevamente. \nCalle: ");
calle= tcld.readLine();
}
System.out.print("Comuna: ");
comuna = tcld.readLine();
while(!validarCalleComunaCiudadEmail(comuna)){
System.out.println("Comuna invalida.\nIntentelo nuevamente. \nComuna: ");
comuna = tcld.readLine();
}
System.out.print("Ciudad: ");
ciudad = tcld.readLine();
while(!validarCalleComunaCiudadEmail(ciudad)){
System.out.println("Ciudad invalida.\nIntentelo nuevamente. \nCiudad: ");
ciudad = tcld.readLine();
}
//eMail
System.out.print("email: ");
email = tcld.readLine();
while(!validarCalleComunaCiudadEmail(email)){
System.out.println("Email invalido.\nIntentelo nuevamente. \nEmail: ");
email = tcld.readLine();
}
//OPCIONALIDAD DE NUMERO Y TELEFONO
op=0;
System.out.println("Posee Numero Telefonico:\n1.- Si.\n2.- No.\n");
do{
error=false;
try {
op = Integer.parseInt(tcld.readLine());
}catch(NumberFormatException e)
{error = true;System.out.println("Error, formato de numero invalido.\nIntentelo nuevamente.");}
}while(error);
switch(op){
case 1:
System.out.print("Telefono: ");
telefono = tcld.readLine();
while(!validarNumeroTelefono(telefono)){
System.out.println("Telefono invalido.\nIntentelo nuevamente. \nTelefono: ");
telefono = tcld.readLine();
}
break;
case 2://No hay caso 2 peor lo agrego para que al apretar 2 no lo tome el default
break;
default:
System.out.println("Ingrese una opcion Valida.");
break;
}
//Numero no lo agrego?
}catch (IOException ex) {System.out.println("Error al leer los Datos.");}
break;
case 5:
System.out.println("Modificar un Cliente.");
System.out.println("Ingrese lo que quiere modificar de algun cliente: ");
System.out.println("1.- Nombre.\n2.- Apellido Paterno.\n3.- Apellido Materno.");
System.out.println("4.- Rut.\n5.- Telefono.\n6.- Numero\n7.- Ciudad.\n8.- Comuna.");
System.out.println("9.- Calle.");
int opM = 0;
do{
error=false;
try {
opM = Integer.parseInt(tcld.readLine());
}catch(NumberFormatException e)
{error = true;System.out.println("Error, formato de numero invalido.\nIntentelo nuevamente.");}
catch (IOException ex) {
Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
}
}while(error);
switch(opM){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
default:
break;
}
break;
case 6:
int opE =0;
System.out.println("Ingrese la opcion para diferenciar la eliminacion : ");
System.out.println("1.- Nombre.\n2.- Apellido Paterno.\n3.- Apellido Materno.");
System.out.println("4.- Rut.\n5.- Telefono.\n6.- Numero.");
do{
error=false;
try {
opE = Integer.parseInt(tcld.readLine());
}catch(NumberFormatException e)
{error = true;System.out.println("Error, formato de numero invalido.\nIntentelo nuevamente.");}
catch (IOException ex) {
Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
}
}while(error);
switch(opE){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
default:
break;
}
break;
case 7:
System.out.println("Productos.");
imprimir(resultSet);//ResultSet son lo Productos
break;
case 8:
System.out.println("Ingrese la Descripcion del producto a buscar: ");
try {
String desc = tcld.readLine();
imprimir(BUSCARPRODUCTO(desc));//AGREGAR ESTA FUNCION la cual debe RETORNAR UN RESULTSET
} catch (IOException ex) {Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);}
break;
case 9:
String fInicio="",fFin="";
System.out.println("Ingrese las Fechas en el siguiente formato: \tdd-mm-aaaa");
try{
do{
System.out.print("Fecha de Inicio: ");
fInicio = tcld.readLine();
}while(!fechaValida(fInicio));
do{
System.out.print("Fecha de Finalizacion: ");
fFin = tcld.readLine();
}while(!fechaValida(fFin));
}catch (IOException ex) {Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);}
imprimir(productoMasVendido(fInicio,fFin));//La funcion retorna un RESULTSET con el producto mas vendido
break;
case 10:
System.out.println("Ingrese las Fechas en el siguiente formato: \tdd-mm-aaaa");
try{
do{
System.out.print("Fecha de Inicio: ");
fInicio = tcld.readLine();
}while(!fechaValida(fInicio));
do{
System.out.print("Fecha de Finalizacion: ");
fFin = tcld.readLine();
}while(!fechaValida(fFin));
}catch (IOException ex) {Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);}
imprimir(productoMenosVendido(fInicio,fFin));//La funcion retorna un RESULTSET con el producto menos vendido
break;
case 11:
String oficina="";
System.out.print("Ingrese el numero de la Oficina: ");
try{
do{
oficina = tcld.readLine();
}while(!validarNumero(oficina));
}catch (IOException ex) {Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);}
imprimir(productoMasVendido(oficina));//La funcion retorna un RESULTSET con el producto mas vendido por oficina
break;
case 12:
System.out.print("Ingrese el numero de la Oficina: ");
try{
do{
oficina = tcld.readLine();
}while(!validarNumero(oficina));
}catch (IOException ex) {Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);}
imprimir(productoMenosVendido(oficina));//La funcion retorna un RESULTSET con el producto mas vendido por oficina
break;
case 13:
System.out.println("El Vendedor con más ventas es: ");
imprimir(vendedorConMasVentas());//Retorna un RESULTSET PARA IMPRIMIR
break;
case 14:
System.out.println("El Vendedor con menos ventas es: ");
imprimir(vendedorConMenosVentas());//Retorna un RESULTSET PARA IMPRIMIR
break;
default:
System.out.println("Ingrese una opcion valida.");
}
}
private static void imprimir(ResultSet x) {
try {
int columnas = getColumnas(x);
do{
System.out.print(x.getRow()+".- ");
for(int i=1;i<=columnas;i++)
System.out.print(x.getString(i)+" ");
System.out.println();
}while(x.next());
} catch (SQLException ex) {System.out.println("Error, "+ex.toString());}
}
private static int getColumnas(ResultSet x) {
int cont =0;
boolean exito=false;
String aux="";
for(cont=1;!exito;cont++){
try{
aux=x.getString(cont);
}catch(SQLException s){exito=true;}
}
return cont-2;
}
private static boolean nombreOApellidoValido(String n) {
return n.length()<= tamañoVarCharNomOApel && n.trim().length() > 4;// >4 para que no sea vacia ni letras raras xD ahi se cambia
}
private static boolean rutValido(String rut) {
boolean esNumero=true;
try{
Integer.parseInt(rut);
}catch(NumberFormatException ex){esNumero=false;}
return (rut.trim().length()==8 || rut.trim().length()==7) && esNumero;
}
private static boolean validarCalleComunaCiudadEmail(String objt) {
return objt.length()<=tamañoVarCharCalleComunaCiudadEmail || objt.trim().length() >4;
}
private static boolean validarNumeroTelefono(String telefono) {
boolean esNumero=true;
try{
Integer.parseInt(telefono);
}catch(NumberFormatException ex){esNumero=false;}
return (telefono.trim().length()>5 && telefono.trim().length()<10) && esNumero;
}
private static boolean fechaValida(String fInicio) {
throw new UnsupportedOperationException("VALIDAR FECHA");
}
private static boolean validarNumero(String n) {
boolean esNumero=true;
try{
Integer.parseInt(n);
}catch(NumberFormatException ex){esNumero=false;}
return esNumero;
}
}
Revise this Paste