Aggiunta serializzazione
This commit is contained in:
commit
aa7347927f
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@ -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
|
||||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -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
|
||||||
7
.idea/discord.xml
generated
Normal file
7
.idea/discord.xml
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DiscordProjectSettings">
|
||||||
|
<option name="show" value="ASK" />
|
||||||
|
<option name="description" value="" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
7
.idea/encodings.xml
generated
Normal file
7
.idea/encodings.xml
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
21
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
21
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||||
|
<option name="ignoredErrors">
|
||||||
|
<list>
|
||||||
|
<option value="N802" />
|
||||||
|
<option value="N806" />
|
||||||
|
<option value="N801" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</inspection_tool>
|
||||||
|
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
<option name="ignoredIdentifiers">
|
||||||
|
<list>
|
||||||
|
<option value="int.setEnabled" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
||||||
14
.idea/misc.xml
generated
Normal file
14
.idea/misc.xml
generated
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="openjdk-25" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
17
pom.xml
Normal file
17
pom.xml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>JAVA_Ripasso</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>25</maven.compiler.source>
|
||||||
|
<maven.compiler.target>25</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
43
src/main/java/org/example/Cosa.java
Normal file
43
src/main/java/org/example/Cosa.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
49
src/main/java/org/example/Main.java
Normal file
49
src/main/java/org/example/Main.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
||||||
|
// click the <icon src="AllIcons.Actions.Execute"/> 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/main/java/org/example/Pesabile.java
Normal file
5
src/main/java/org/example/Pesabile.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
public interface Pesabile {
|
||||||
|
double getPeso();
|
||||||
|
}
|
||||||
206
src/main/java/org/example/Zaino.java
Normal file
206
src/main/java/org/example/Zaino.java
Normal file
@ -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<Cosa> 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<Cosa> 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<Cosa> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
22
src/main/java/org/example/ZainoScolastico.java
Normal file
22
src/main/java/org/example/ZainoScolastico.java
Normal file
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user