https://www.arquitecturajava.com/java-polimorfismo-herencia-y-simplicidad/
Categoría: Java
API Rest y JPA 1 to 1
Enlaces interesantes:
API Rest:
https://www.arquitecturajava.com/api-rest-estilos-y-homogeneidad/
JPA 1 to 1:
https://www.arquitecturajava.com/jpa-onetoone-y-relaciones-1-a-1/
Java Object reference
Enlaces de Java
Java Hibernate
Hosting para java
http://picodotdev.github.io/blog-bitix/2015/04/nueva-visita-a-5-plus-opciones-de-hosting-para-aplicaciones/
Acceso a datos JDBC
import java.math.BigDecimal;
import java.sql.*;
public class Derby {
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/JavaTunesDB", "guest",
"password");) {
// rest of code
String sql = "SELECT ITEM_ID, Title, Price FROM ITEM";
// conn is connection as from previous slide
// create a Statement object
Statement stmt = conn.createStatement();
// execute the SQL with it
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
long id = rs.getLong("ITEM_ID");
String title = rs.getString("Title");
BigDecimal price = rs.getBigDecimal("Price");
System.out.println(id + "," + title + "," + price);
} // Do something with data (not shown)
sql = "SELECT Title, Price FROM ITEM " +
"WHERE ITEM_ID = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
// set the ? - they are numbered starting at 1
pstmt.setInt(1, 10);
// execute the PreparedStatement and get a ResultSet
rs = pstmt.executeQuery();
rs.next();
String title = rs.getString("Title");
Double price = rs.getDouble("Price");
System.out.println(title + "," + price);
} catch (SQLException sqle) {
System.out.println(sqle.getMessage());
}
}
}
Clase Bolsa Genérica
import java.util.ArrayList;
import java.util.Iterator;
public class Bolsa<T> implements Iterable<T>{
private ArrayList<T> objetos = new ArrayList<T>();
private int capacidad = 10;
public Bolsa() {
}
public Bolsa(int capacidad) {
this.capacidad=capacidad;
}
/**
* @return the objetos
*/
public ArrayList<T> getObjetos() {
return objetos;
}
/**
* @param objetos
* the objetos to set
*/
public void setObjetos(ArrayList<T> objetos) {
this.objetos = objetos;
}
public boolean meteObjeto(T objeto){
if (objetos.size()<capacidad){
objetos.add(objeto);
return true;
}
return false;
}
public boolean sacaObjeto(){
if (objetos.size()>0){
objetos.remove(objetos.size()-1);
return true;
}
return false;
}
public boolean sacaObjeto(T objeto){
return objetos.remove(objeto);
}
public boolean cambiaObjeto(T objeto1, T objeto2){
if (objetos.remove(objeto1)){
meteObjeto(objeto2);
return true;
}
return false;
}
public boolean cambiaObjetoR(T objeto1, T objeto2){
int pos=objetos.indexOf(objeto1);
if (pos>=0){
objetos.set(pos, objeto2);
return true;
}
return false;
}
/**
* @return the capacidad
*/
public int getCapacidad() {
return capacidad;
}
public String toString(){
String res=capacidad + " | ";
for(T s:objetos){
res+=s+" | ";
}
return res;
}
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
}
Clase Bolsa
import java.util.ArrayList;
public class Bolsa {
private ArrayList<String> objetos = new ArrayList<String>();
private int capacidad = 10;
public Bolsa() {
}
public Bolsa(int capacidad) {
this.capacidad=capacidad;
}
/**
* @return the objetos
*/
public ArrayList<String> getObjetos() {
return objetos;
}
/**
* @param objetos
* the objetos to set
*/
public void setObjetos(ArrayList<String> objetos) {
this.objetos = objetos;
}
public boolean meteObjeto(String objeto){
if (objetos.size()<capacidad){
objetos.add(objeto);
return true;
}
return false;
}
public boolean sacaObjeto(){
if (objetos.size()>0){
objetos.remove(objetos.size()-1);
return true;
}
return false;
}
public boolean sacaObjeto(String objeto){
return objetos.remove(objeto);
}
public boolean cambiaObjeto(String objeto1, String objeto2){
if (objetos.remove(objeto1)){
meteObjeto(objeto2);
return true;
}
return false;
}
public boolean cambiaObjetoR(String objeto1, String objeto2){
int pos=objetos.indexOf(objeto1);
if (pos>=0){
objetos.set(pos, objeto2);
return true;
}
return false;
}
/**
* @return the capacidad
*/
public int getCapacidad() {
return capacidad;
}
public String toString(){
String res=capacidad + " | ";
for(String s:objetos){
res+=s+" | ";
}
return res;
}
}
Soluciones ejercicio Excepciones
Creo una clase nueva:
public class ExceptionMarcaIncorrecta extends Exception{
public ExceptionMarcaIncorrecta() {
// TODO Auto-generated constructor stub
}
public ExceptionMarcaIncorrecta(String mensaje) {
super(mensaje);
}
}
Añado esa excepción en setMarcas:
public void setMarca(String marca) throws ExceptionMarcaIncorrecta {
for (String m : VALID_BRANDS) {
if (marca.equals(m)) {
this.marca = marca;
return;
}
}
throw new ExceptionMarcaIncorrecta("Marca incorrecta");
}
Modifico los constructores para que sean específicos:
public Television(String marca, int volumen, String proveedor) throws ExceptionMarcaIncorrecta {
setMarca(marca);
setVolume(volumen);
setProveedor(proveedor);
}
public Television(String proveedor) throws ExceptionMarcaIncorrecta {
this("LG", 6, proveedor);
}
Ahora puedo usarla en la prueba:
try {
Television tv1 = new Television("ww", 4, "Miro");
tv1.setVolume(-5);
tv1.setMarca("Sony");
tv1.setModelo("weweew");
System.out.println(tv1);
} catch (ExceptionMarcaIncorrecta e) {
System.out.println(e.getMessage());
}
catch (Exception e) {
System.out.println(e.getMessage());
}