Created
December 2, 2023 16:19
-
-
Save Rizaaal/9d44b6fa31620d5bd395136ec30782c2 to your computer and use it in GitHub Desktop.
CRUD java app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
public class Crud { | |
//legge i dati di una persona | |
public static Persona read(String id){ | |
if (Main.persone.get(id) == null){ | |
System.out.println("read(" + id + "): Persona non trovata"); | |
} | |
return Main.persone.get(id); | |
} | |
//aggiunge una nuova persona | |
public static void create(Persona persona){ | |
String id = "%s%s%s%s%s".formatted( | |
persona.getNome().substring(0, 1), | |
persona.getCognome().substring(0, 1), | |
persona.getAge(), | |
persona.getSesso(), | |
Main.persone.size() + 1); | |
Main.persone.put(id, persona); | |
System.out.println(persona + " added with id: " + id); | |
} | |
//aggiorna un dato di una persona | |
public static void update(String id, String key, String value) { | |
Method method; | |
try { | |
Persona persona = Main.persone.get(id); | |
method = Persona.class.getMethod("set" + key, String.class); | |
method.invoke(persona, value); | |
System.out.printf("\"%s\" aggiornato in \"%s\" con successo.\n", key, value); | |
System.out.println("nuova entry:"); | |
System.out.println(persona); | |
} catch (NoSuchMethodException e) { | |
//se non trova il metodo indicato dà eccezione noSuchMethod | |
System.out.printf( | |
""" | |
update(%s): Il valore "%s" non esiste (utilizzare le maiuscole per indicare il nome del valore) | |
Nessun valore aggiornato. | |
""", id, key); | |
} catch (NullPointerException e) { | |
//se non trova la Persona inserita dà eccezione nullPointer | |
System.out.println("read(" + id + "): la entry che stai cercando probabilmente non esiste"); | |
} catch (IllegalAccessException | InvocationTargetException e){ | |
//altri tipi di errore | |
throw new RuntimeException(e); | |
} | |
} | |
//elimina una persona | |
public static void delete(String id){ | |
if (Main.persone.get(id) == null){ | |
System.out.println("delete(" + id + "): la entry risulta assente, oppure è stata già rimossa"); | |
} else { | |
Main.persone.remove(id); | |
System.out.println("entry con id " + id + " eliminata."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
public class Main { | |
//"database" | |
public static HashMap<String, Persona> persone = new HashMap<>(); | |
public static void main(String[] args) { | |
//operazioni CRUD | |
//create | |
Crud.create(new Persona( | |
"Gabriele", | |
"Di Grazia", | |
"28", | |
"M" | |
)); | |
//read | |
Persona GD28M1 = Crud.read("GD28M1"); | |
System.out.println(GD28M1); | |
//update | |
Crud.update("GD28M1", "Nome", "Lorenzo"); | |
//delete | |
Crud.delete("GD28M1"); | |
Crud.read("GD28M1"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Persona { | |
private String nome; | |
private String cognome; | |
private String age; | |
private String sesso; | |
public Persona(String nome, String cognome, String age, String sesso) { | |
this.nome = nome; | |
this.cognome = cognome; | |
this.age = age; | |
this.sesso = sesso; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public String getCognome() { | |
return cognome; | |
} | |
public void setCognome(String cognome) { | |
this.cognome = cognome; | |
} | |
public String getAge() { | |
return age; | |
} | |
public void setAge(String age) { | |
this.age = age; | |
} | |
public String getSesso() { | |
return sesso; | |
} | |
public void setSesso(String sesso) { | |
this.sesso = sesso; | |
} | |
@Override | |
public String toString() { | |
return "Persona{" + | |
"nome='" + nome + '\'' + | |
", cognome='" + cognome + '\'' + | |
", age=" + age + | |
", sesso='" + sesso + '\'' + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment