commit aa7347927f8485266c2572286a60e1aca0362241 Author: unknown Date: Thu Oct 16 09:39:03 2025 +0200 Aggiunta serializzazione diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..30bab2a --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..186d49a --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,21 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..de5c651 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..74fff88 --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + JAVA_Ripasso + 1.0-SNAPSHOT + + + 25 + 25 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/org/example/Cosa.java b/src/main/java/org/example/Cosa.java new file mode 100644 index 0000000..0c7a095 --- /dev/null +++ b/src/main/java/org/example/Cosa.java @@ -0,0 +1,43 @@ +package org.example; + +import java.io.Serializable; + +public class Cosa implements Pesabile, Serializable { + private String descrizione; + private int volume; + private int peso; + + public Cosa(String descrizione, double volume, double peso) { + this.descrizione = descrizione; + this.volume = Zaino.doubleToInt(volume); + this.peso = Zaino.doubleToInt(peso); + } + + public String toString() { + return descrizione + "," + volume + "," + peso; + } + + public String getDescrizione() { + return descrizione; + } + + public double getVolume() { + return Zaino.intToDouble(volume); + } + + public double getPeso() { + return Zaino.intToDouble(peso); + } + + /* Restituisce il volume in ml*/ + public int getRawVolume() { + return this.volume; + } + + /* Restituisce il peso in ml*/ + public int getRawPeso() { + return this.peso; + } + + +} diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java new file mode 100644 index 0000000..23d3cd6 --- /dev/null +++ b/src/main/java/org/example/Main.java @@ -0,0 +1,49 @@ +package org.example; + +import java.io.*; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + ZainoScolastico zs = new ZainoScolastico("The Nord Face", "blu", 15.50, 0.4, false, 5, true); + Zaino z = new Zaino("Salewa", "giallo fluo", 25.00, 0.8, true, 4); + + zs.inserisci(new Cosa("tablet apple", 0.2, 0.8)); + System.out.println(zs.getVolumeOccupato()); + zs.inserisci(new Cosa("quaderno matematica", 1.2, 2)); + System.out.println(zs.getRawVolumeLibero()); + zs.inserisci(new Cosa("quaderno inglese", 0.8, 2)); + System.out.println(zs.getRawVolumeLibero()); + + + + // TEST SERIALIZZAZIONE + + String filename = (new File("target\\z1.ser")).getAbsolutePath(); + + Zaino z1; + z.inserisci(new Cosa("Caschetto arrampicata", 5, 1)); + + try { + z.salva(filename); + } catch (IOException e) { + System.out.println("Errore serializzazione"); + } + + + try { + z1 = Zaino.carica(filename); + System.out.println("Oggetto deserializzato"); + System.out.println("A: "+ z); + System.out.println("B: "+ z1); + System.out.println("A: "+ z.getPeso()); + System.out.println("B: "+ z1.getPeso()); + } catch (IOException e) { + System.out.println("Errore lettura"); + } catch (ClassNotFoundException e) { + System.out.println("Classe non trovata"); + } + + } +} diff --git a/src/main/java/org/example/Pesabile.java b/src/main/java/org/example/Pesabile.java new file mode 100644 index 0000000..46f82ce --- /dev/null +++ b/src/main/java/org/example/Pesabile.java @@ -0,0 +1,5 @@ +package org.example; + +public interface Pesabile { + double getPeso(); +} diff --git a/src/main/java/org/example/Zaino.java b/src/main/java/org/example/Zaino.java new file mode 100644 index 0000000..736841a --- /dev/null +++ b/src/main/java/org/example/Zaino.java @@ -0,0 +1,206 @@ +package org.example; + +import java.io.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class Zaino implements Pesabile, Serializable { + private final String marca; + private String colore; + private final int capacitaLitri; // in ml + private boolean impermeabile; + private int numeroScomparti; + private final String erroreScomparti = "Il numero di scomparti non può essere inferiore a 1"; + + private int pesoVuoto; // in g + private final List cose = new ArrayList<>(); + + // Costruttori + + public Zaino(String marca, String colore, double capacitaLitri, double pesoVuoto) { + this.marca = marca; + this.colore = colore; + this.capacitaLitri = doubleToInt(capacitaLitri); + this.pesoVuoto = doubleToInt(pesoVuoto); + this.impermeabile = false; + this.numeroScomparti = 2; + } + + public Zaino(String marca, String colore, double capacitaLitri, double pesoVuoto, boolean impermeabile, int numeroScomparti) { + this.marca = marca; + this.colore = colore; + this.capacitaLitri = doubleToInt(capacitaLitri); + this.pesoVuoto = doubleToInt(pesoVuoto); + this.impermeabile = impermeabile; + this.numeroScomparti = numeroScomparti; + } + + // Getters + public String getMarca() { + return marca; + } + + public String getColore() { + return colore; + } + + public double getCapacitaLitri() { + return intToDouble(getRawCapacitaLitri()); + } + + public int getRawCapacitaLitri() { + return capacitaLitri; + } + + public boolean isImpermeabile() { + return impermeabile; + } + + public int getNumeroScomparti() { + return numeroScomparti; + } + + public int getRawPesoVuoto() { + return pesoVuoto; + } + + public double getPesoVuoto() { + return intToDouble(this.getRawPesoVuoto()); + } + + /* Restituisce il peso dello zaino, in g */ + public int getRawPeso() { + Iterator iterator = cose.iterator(); + int pesoTotale = 0; + if (iterator.hasNext()) { + pesoTotale += iterator.next().getRawPeso(); + } + return pesoTotale + getRawPesoVuoto(); + } + + public double getPeso() { + return intToDouble(getRawPeso()); + } + + public int getRawVolumeOccupato() { + Iterator iterator = cose.iterator(); + int volumeOccupato = 0; + while (iterator.hasNext()) { + volumeOccupato += iterator.next().getRawVolume(); + } + return volumeOccupato; + } + + public double getVolumeOccupato() { + return intToDouble(getRawVolumeOccupato()); + } + + public int getRawVolumeLibero() { + return getRawCapacitaLitri() - getRawVolumeOccupato(); + } + + public double getVolumeLibero() { + return intToDouble(getRawVolumeLibero()); + } + + // Setters + + public void setColore(String colore) { + this.colore = colore; + } + + public void setImpermeabile(boolean impermeabile) { + this.impermeabile = impermeabile; + } + + public void setNumeroScomparti(int numeroScomparti) { + if (numeroScomparti > 0) { + this.numeroScomparti = numeroScomparti; + } else { + throw new IllegalArgumentException(erroreScomparti); + } + } + + + // To string + /* Restituisce le informazioni riguardo alla macchina nel formato: + marca,colore,capacitaLitri,impermeabile,numeroScomparti + * */ + public String toString() { + return getMarca() + "," + getColore() + "," + getCapacitaLitri() + "," + isImpermeabile() + "," + getNumeroScomparti(); + } + + + // Misc + + public void aggiungiScomparto() { + numeroScomparti++; + } + + public void rimuoviScomparto() { + if (numeroScomparti > 0) { + numeroScomparti--; + } else { + throw new IllegalArgumentException(erroreScomparti); + } + } + + public boolean equals(Object o) { + return o instanceof Zaino && ((Zaino) o).getMarca().equals(this.getMarca()) && ((Zaino) o).getCapacitaLitri() == this.getCapacitaLitri(); + } + + + public int confrontaCapacita(Zaino altroZaino) { + double thisCapacita = this.getCapacitaLitri(); + double thatCapacita = altroZaino.getCapacitaLitri(); + if (thisCapacita > thatCapacita) return 1; + if (thisCapacita < thatCapacita) return -1; + return 0; + } + + + public void inserisci(Cosa cosa) throws IllegalArgumentException { + if (this.getRawPeso() + cosa.getRawPeso() > 25000) { + throw new IllegalArgumentException("Il peso supera i 25kg. Peso attuale: " + + String.format("%.2f", this.getPeso()) + ". Peso oggetto: " + String.format("%.2f", this.getPeso())); + } else if (this.getRawVolumeLibero() < cosa.getRawVolume()) { + throw new IllegalArgumentException("Lo zaino e' pieno. Volume zaino: " + + String.format("%.2f", this.getCapacitaLitri()) + ". Volume libero: " + String.format("%.2f", this.getVolumeLibero()) + + ". Volume cosa: " + String.format("%.2f", cosa.getVolume()) + ); + } else { + this.cose.add(cosa); + } + } + + + public void salva(String filename) throws IOException { + FileOutputStream file = new FileOutputStream(filename); + ObjectOutputStream out = new ObjectOutputStream(file); + out.writeObject(this); + out.close(); + file.close(); + } + + // Metodi statici + + public static Zaino carica(String filename) throws IOException, ClassNotFoundException { + FileInputStream file = new FileInputStream(filename); + ObjectInputStream in = new ObjectInputStream(file); + Zaino output = (Zaino) in.readObject(); + in.close(); + file.close(); + return output; + } + + public static int doubleToInt(double i) { + return (int) (i * 1000); + } + + public static double intToDouble(int f) { + return (double) f / 1000; + } + + +} diff --git a/src/main/java/org/example/ZainoScolastico.java b/src/main/java/org/example/ZainoScolastico.java new file mode 100644 index 0000000..6ab5883 --- /dev/null +++ b/src/main/java/org/example/ZainoScolastico.java @@ -0,0 +1,22 @@ +package org.example; + +public class ZainoScolastico extends Zaino implements Pesabile { + private boolean hasScompartoPerTablet; + + public ZainoScolastico(String marca, String colore, double capacitaLitri, double pesoVuoto, boolean impermeabile, int numeroScomparti, boolean hasScompartoPerTablet) { + super(marca, colore, capacitaLitri, pesoVuoto, impermeabile, numeroScomparti); + this.hasScompartoPerTablet = hasScompartoPerTablet; + } + + public void inserisci(Cosa cosa) throws IllegalArgumentException { + String descrizioneCosa = cosa.getDescrizione(); + if (descrizioneCosa.contains("libro") || descrizioneCosa.contains("quaderno") || descrizioneCosa.contains("tablet")) { + super.inserisci(cosa); + + } else { + throw new IllegalArgumentException("La cosa non è un libro, un quaderno o un tablet"); + } + } + + +}