From 0015d7dce432520f61561735c964383d5dddca05 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Tue, 14 Jan 2014 00:30:49 +0100 Subject: [PATCH] =?UTF-8?q?Partenza=20dopo=20aver=20duplicato=20il=20primo?= =?UTF-8?q?=20progetto=20in=20cui=20c''erano=20gi=C3=A0=20le=20classi=20an?= =?UTF-8?q?che=20della=20seconda=20parte.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- it/unisa/info13d/Articoli/BeniDiConsumo.java | 120 +++ it/unisa/info13d/Articoli/BeniDiConsumo2.java | 150 +++ .../info13d/Articoli/CeneInRistoranti.java | 163 +++ .../info13d/Articoli/CeneInRistoranti2.java | 188 ++++ it/unisa/info13d/Articoli/Global.java | 50 + .../info13d/Articoli/PrestazioniDopera2.java | 98 ++ it/unisa/info13d/Articoli/Utilizzabile.java | 17 + it/unisa/info13d/Articoli/Utilizzabile2.java | 21 + it/unisa/info13d/Articoli/Vacanze.java | 123 +++ it/unisa/info13d/Articoli/Vacanze2.java | 139 +++ .../GestioneCatalogo/AdminSession.java | 66 ++ .../GestioneCatalogo/AdminSession2.java | 66 ++ .../info13d/GestioneCatalogo/Catalogo.java | 921 ++++++++++++++++ .../info13d/GestioneCatalogo/Catalogo2.java | 982 ++++++++++++++++++ .../GestioneCatalogo/ClientSession.java | 65 ++ .../GestioneCatalogo/ClientSession2.java | 65 ++ it/unisa/info13d/Login/Access.java | 279 +++++ it/unisa/info13d/Login/Entry.java | 98 ++ it/unisa/info13d/Login/LoggedUser.java | 15 + it/unisa/info13d/Login/LoginData.java | 74 ++ it/unisa/info13d/Login/StoricoItem.java | 58 ++ it/unisa/info13d/Main.java | 34 + it/unisa/info13d/Main2.java | 34 + it/unisa/info13d/Utility/IdCounter.java | 57 + it/unisa/info13d/Utility/ReShow.java | 16 + 25 files changed, 3899 insertions(+) create mode 100644 it/unisa/info13d/Articoli/BeniDiConsumo.java create mode 100644 it/unisa/info13d/Articoli/BeniDiConsumo2.java create mode 100644 it/unisa/info13d/Articoli/CeneInRistoranti.java create mode 100644 it/unisa/info13d/Articoli/CeneInRistoranti2.java create mode 100644 it/unisa/info13d/Articoli/Global.java create mode 100644 it/unisa/info13d/Articoli/PrestazioniDopera2.java create mode 100644 it/unisa/info13d/Articoli/Utilizzabile.java create mode 100644 it/unisa/info13d/Articoli/Utilizzabile2.java create mode 100644 it/unisa/info13d/Articoli/Vacanze.java create mode 100644 it/unisa/info13d/Articoli/Vacanze2.java create mode 100644 it/unisa/info13d/GestioneCatalogo/AdminSession.java create mode 100644 it/unisa/info13d/GestioneCatalogo/AdminSession2.java create mode 100644 it/unisa/info13d/GestioneCatalogo/Catalogo.java create mode 100644 it/unisa/info13d/GestioneCatalogo/Catalogo2.java create mode 100644 it/unisa/info13d/GestioneCatalogo/ClientSession.java create mode 100644 it/unisa/info13d/GestioneCatalogo/ClientSession2.java create mode 100644 it/unisa/info13d/Login/Access.java create mode 100644 it/unisa/info13d/Login/Entry.java create mode 100644 it/unisa/info13d/Login/LoggedUser.java create mode 100644 it/unisa/info13d/Login/LoginData.java create mode 100644 it/unisa/info13d/Login/StoricoItem.java create mode 100644 it/unisa/info13d/Main.java create mode 100644 it/unisa/info13d/Main2.java create mode 100644 it/unisa/info13d/Utility/IdCounter.java create mode 100644 it/unisa/info13d/Utility/ReShow.java diff --git a/it/unisa/info13d/Articoli/BeniDiConsumo.java b/it/unisa/info13d/Articoli/BeniDiConsumo.java new file mode 100644 index 0000000..51d3e8b --- /dev/null +++ b/it/unisa/info13d/Articoli/BeniDiConsumo.java @@ -0,0 +1,120 @@ +package it.unisa.info13d.Articoli; + +import java.io.Serializable; +import java.util.GregorianCalendar; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 16/12/13 + * Time: 18.46 + */ + +/** + * + * Classe che rappresenta un bene di consumo all'interno del catalogo. + * + */ +public class BeniDiConsumo implements Utilizzabile, Serializable{ + + /** + * Costruttore che inizializza i valori del bene di consumo + * + * @param idBene + * @param descrizioneBene + * @param prezzoBene + * @param beniInStock + */ + public BeniDiConsumo ( int idBene, String descrizioneBene, double prezzoBene, int beniInStock ){ + this.idBene=idBene; + this.descrizioneBene=descrizioneBene; + this.prezzoBene=prezzoBene; + this.beniInStock=beniInStock; + } + + @Override + public boolean eAcquistabile() { + if(beniVenduticeneVendute)) + return false; + return true; + } + + /** + * @return - l'id della cena + */ + public int getIdCena() { + return idCena; + } + + /** + * @return - in nome del ristorante + */ + public String getNomeRistorante() { + return nomeRistorante; + } + + /** + * @return - l'ubicazione del ristorante + */ + public String getLuogo() { + return luogo; + } + + /** + * @return - descrizione della cena + */ + public String getDescrizione_cena() { + return descrizione_cena; + } + + /** + * @return - il costo per ogni singola persona + */ + public double getCostoPerPersona() { + return costoPerPersona; + } + + /** + * @return - data di scadenza dell'offerta + */ + public GregorianCalendar getDataScadenzaOffertaCena() { + return dataScadenzaOffertaCena; + } + + /** + * @return - il numero di cene ancora disponibili per la vendita + */ + public int getNumCenedaVendere() { + return numCenedaVendere; + } + + /** + * @return - il numero di cene vendute + */ + public int getCeneVendute() { + return ceneVendute; + } + + /** + * @param nomeRistorante - setta il nome del ristorante + */ + public void setNomeRistorante(String nomeRistorante) { + this.nomeRistorante = nomeRistorante; + } + + /** + * @param luogo - setta il l'ubicazione del ristorante + */ + public void setLuogo(String luogo) { + this.luogo = luogo; + } + + /** + * @param descrizione_cena - setta la descrizione della cena (Esempio: pizza margherita, patate e dolce) + */ + public void setDescrizione_cena(String descrizione_cena) { + this.descrizione_cena = descrizione_cena; + } + + /** + * @param costoPerPersona - setta il costo per singola persona + */ + public void setCostoPerPersona(double costoPerPersona) { + this.costoPerPersona = costoPerPersona; + } + + /** + * @param dataScadenzaOffertaCena - setta la data di scadenza per l'offerta + */ + public void setDataScadenzaOffertaCena(GregorianCalendar dataScadenzaOffertaCena) { + this.dataScadenzaOffertaCena = dataScadenzaOffertaCena; + } + + /** + * @param numCenedaVendere - setta il numero di cene da vendere + */ + public void setNumCenedaVendere(int numCenedaVendere) { + this.numCenedaVendere = numCenedaVendere; + } + + /** + * Questo metodo all'atto dell'acquisto da parte di un utente, decrementa le cene da vendere ed incrementa quelle vendute + */ + public void setCeneVendute() + { + numCenedaVendere--; + ceneVendute++; + } + + private int idCena; + private String nomeRistorante; + private String luogo; + private String descrizione_cena; + private double costoPerPersona; + private GregorianCalendar dataScadenzaOffertaCena; + private int numCenedaVendere; + private int ceneVendute; +} diff --git a/it/unisa/info13d/Articoli/CeneInRistoranti2.java b/it/unisa/info13d/Articoli/CeneInRistoranti2.java new file mode 100644 index 0000000..ed03e14 --- /dev/null +++ b/it/unisa/info13d/Articoli/CeneInRistoranti2.java @@ -0,0 +1,188 @@ +package it.unisa.info13d.Articoli; + +import java.io.Serializable; +import java.util.GregorianCalendar; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 16/12/13 + * Time: 18.47 + */ + +/** + * + * Classe che rappresenta una cena all'interno del catalogo. + * + */ +public class CeneInRistoranti2 extends Utilizzabile2 implements Serializable{ + + /** + * + * @param idCena - l'id della cena + * @param nomeRistorante - il ristorante in cui usufruire dell'offerta + * @param luogo - Ubicazione del ristorante + * @param descrizione_cena - descrizione della cena (cosa viene offerto dal ristorante) + * @param costoPerPersona - costo per singola persona + * @param dataScadenzaOffertaCena - scadenza dell'offerta + * @param numCenedaVendere - offerte disponibile per la cena + */ + + public CeneInRistoranti2(int idCena, String nomeRistorante, String luogo, String descrizione_cena, double costoPerPersona, GregorianCalendar dataScadenzaOffertaCena, int numCenedaVendere){ + this.idCena=idCena; + this.nomeRistorante=nomeRistorante; + this.luogo=luogo; + this.descrizione_cena=descrizione_cena; + this.costoPerPersona=costoPerPersona; + this.dataScadenzaOffertaCena=dataScadenzaOffertaCena; + this.numCenedaVendere=numCenedaVendere; + } + + @Override + public boolean eAcquistabile() { + if((dataScadenzaOffertaCena.before(new GregorianCalendar())) && (numCenedaVendere>ceneVendute)) + return false; + return true; + } + + /** + * @return - l'id della cena + */ + public int getIdCena() { + return idCena; + } + + /** + * @return - in nome del ristorante + */ + public String getNomeRistorante() { + return nomeRistorante; + } + + /** + * @return - l'ubicazione del ristorante + */ + public String getLuogo() { + return luogo; + } + + /** + * @return - descrizione della cena + */ + public String getDescrizione_cena() { + return descrizione_cena; + } + + /** + * @return - il costo per ogni singola persona + */ + public double getCostoPerPersona() { + return costoPerPersona; + } + + /** + * @return - data di scadenza dell'offerta + */ + public GregorianCalendar getDataScadenzaOffertaCena() { + return dataScadenzaOffertaCena; + } + + /** + * @return - il numero di cene ancora disponibili per la vendita + */ + public int getNumCenedaVendere() { + return numCenedaVendere; + } + + /** + * @return - il numero di cene vendute + */ + public int getCeneVendute() { + return ceneVendute; + } + + /** + * @param nomeRistorante - setta il nome del ristorante + */ + public void setNomeRistorante(String nomeRistorante) { + this.nomeRistorante = nomeRistorante; + } + + /** + * @param luogo - setta il l'ubicazione del ristorante + */ + public void setLuogo(String luogo) { + this.luogo = luogo; + } + + /** + * @param descrizione_cena - setta la descrizione della cena (Esempio: pizza margherita, patate e dolce) + */ + public void setDescrizione_cena(String descrizione_cena) { + this.descrizione_cena = descrizione_cena; + } + + /** + * @param costoPerPersona - setta il costo per singola persona + */ + public void setCostoPerPersona(double costoPerPersona) { + this.costoPerPersona = costoPerPersona; + } + + /** + * @param dataScadenzaOffertaCena - setta la data di scadenza per l'offerta + */ + public void setDataScadenzaOffertaCena(GregorianCalendar dataScadenzaOffertaCena) { + this.dataScadenzaOffertaCena = dataScadenzaOffertaCena; + } + + /** + * @param numCenedaVendere - setta il numero di cene da vendere + */ + public void setNumCenedaVendere(int numCenedaVendere) { + this.numCenedaVendere = numCenedaVendere; + } + + /** + * Questo metodo all'atto dell'acquisto da parte di un utente, decrementa le cene da vendere ed incrementa quelle vendute + */ + public void setCeneVendute() + { + numCenedaVendere--; + ceneVendute++; + } + + @Override + /** + * Questo metodo stabilisce se � possibile applicare o meno uno sconto alle cene. + * @return Restitisce il prezzo originale se la politica di socnto non � applicabile, altrienti restituisce il prezzo scontato del 10%. + */ + public double get_prezzo_scontato() { + GregorianCalendar dataOdierna = new GregorianCalendar(); + + long milliseconds1 = dataOdierna.getTimeInMillis(); + long milliseconds2 = dataScadenzaOffertaCena.getTimeInMillis(); + + long diff = milliseconds2 - milliseconds1; + long diffGiorni = diff / (24 * 60 * 60 * 1000); //differenza in giorni + + if( diffGiorni<=7 ) + { + if( ceneVendute > ( numCenedaVendere/2 ) ) + return costoPerPersona - (costoPerPersona* ( 0.1 )) - (costoPerPersona* ( 0.1 )) ; //generico sconto ultima sett + se 50% vendute + else + return costoPerPersona - (costoPerPersona * ( 0.1 )); //generico sconto ultima sett + } + else + return costoPerPersona; + } + + private int idCena; + private String nomeRistorante; + private String luogo; + private String descrizione_cena; + private double costoPerPersona; + private GregorianCalendar dataScadenzaOffertaCena; + private int numCenedaVendere; + private int ceneVendute; +} \ No newline at end of file diff --git a/it/unisa/info13d/Articoli/Global.java b/it/unisa/info13d/Articoli/Global.java new file mode 100644 index 0000000..fb24045 --- /dev/null +++ b/it/unisa/info13d/Articoli/Global.java @@ -0,0 +1,50 @@ +package it.unisa.info13d.Articoli; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 16/12/13 + * Time: 19.22 + */ + + +/** + * + * Classe che raccoglie tutti i metodi statici (globali) da usare nel progetto + * + * + */ +public class Global { + /** + * + * @return il seguente id disponibile all'aggiunta di un nuovo prodotto + */ + public static int get_next_id (){ + general_counter++; + return general_counter; + } + + /** + * Questo metodo setta il contatore generale degli id dei prodotti + * + * @param general_counter - contatore id + */ + public static void setGeneral_counter(int general_counter) { + Global.general_counter = general_counter; + } + + /** + * + * @return il contatore generale degli id. + */ + public static int getGeneral_counter() { + return general_counter; + } + + /** + * E' il contatore statico privato + */ + private static int general_counter; + + +} diff --git a/it/unisa/info13d/Articoli/PrestazioniDopera2.java b/it/unisa/info13d/Articoli/PrestazioniDopera2.java new file mode 100644 index 0000000..7745f40 --- /dev/null +++ b/it/unisa/info13d/Articoli/PrestazioniDopera2.java @@ -0,0 +1,98 @@ +package it.unisa.info13d.Articoli; + +import java.util.GregorianCalendar; + +public class PrestazioniDopera2 extends Utilizzabile2 { + + + + + public PrestazioniDopera2(int in_id, String in_localita, String in_descrizione, String in_fornitore, int in_giudizio, double in_prezzo ){ + + idPrOpera = in_id; + Localita = in_localita; + Descrizione = in_descrizione; + Fornitore = in_fornitore; + giudizioFornitore = in_giudizio; + prezzoPrestazione = in_prezzo; + + } + + + @Override + public boolean eAcquistabile() { + return true; + } + + @Override + public double get_prezzo_scontato() { + + return prezzoPrestazione; //Nessuno sconto applicabile + } + + + + public int getIdPrOpera() { + return idPrOpera; + } + + public void setIdPrOpera(int idPrOpera) { + this.idPrOpera = idPrOpera; + } + + public int getGiudizioFornitore() { + return giudizioFornitore; + } + + public void setGiudizioFornitore(int giudizioFornitore) { + this.giudizioFornitore = giudizioFornitore; + } + + public int getPrestazioniVendute() { + return prestazioniVendute; + } + + public void setPrestazioniVendute(int prestazioniVendute) { + this.prestazioniVendute = prestazioniVendute; + } + + public double getPrezzoPrestazione() { + return prezzoPrestazione; + } + + public void setPrezzoPrestazione(double prezzoPrestazione) { + this.prezzoPrestazione = prezzoPrestazione; + } + + public String getLocalita() { + return Localita; + } + + public void setLocalita(String localita) { + Localita = localita; + } + + public String getDescrizione() { + return Descrizione; + } + + public void setDescrizione(String descrizione) { + Descrizione = descrizione; + } + + public String getFornitore() { + return Fornitore; + } + + public void setFornitore(String fornitore) { + Fornitore = fornitore; + } + + private int idPrOpera; + private int giudizioFornitore; // varia da 1 a 5 + private int prestazioniVendute = 0; + private double prezzoPrestazione; + private String Localita; + private String Descrizione; + private String Fornitore; +} \ No newline at end of file diff --git a/it/unisa/info13d/Articoli/Utilizzabile.java b/it/unisa/info13d/Articoli/Utilizzabile.java new file mode 100644 index 0000000..1e82f1e --- /dev/null +++ b/it/unisa/info13d/Articoli/Utilizzabile.java @@ -0,0 +1,17 @@ +package it.unisa.info13d.Articoli; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 16/12/13 + * Time: 18.53 + */ +public interface Utilizzabile { + /** + * Verifica se un metodo e' acquistabile + * + * @return se un bene e' acquistabile + */ + boolean eAcquistabile(); + +} diff --git a/it/unisa/info13d/Articoli/Utilizzabile2.java b/it/unisa/info13d/Articoli/Utilizzabile2.java new file mode 100644 index 0000000..d6f8628 --- /dev/null +++ b/it/unisa/info13d/Articoli/Utilizzabile2.java @@ -0,0 +1,21 @@ +package it.unisa.info13d.Articoli; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 09/01/14 + * Time: 18.11 + */ + + +public abstract class Utilizzabile2 { + + public abstract boolean eAcquistabile(); + /** + * + * @return metodo per l'applicazione delle politiche di sconto relative ad un articolo. + */ + public abstract double get_prezzo_scontato(); + + +} diff --git a/it/unisa/info13d/Articoli/Vacanze.java b/it/unisa/info13d/Articoli/Vacanze.java new file mode 100644 index 0000000..987e85c --- /dev/null +++ b/it/unisa/info13d/Articoli/Vacanze.java @@ -0,0 +1,123 @@ +package it.unisa.info13d.Articoli; + +import java.io.Serializable; +import java.util.GregorianCalendar; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 16/12/13 + * Time: 18.45 + */ +public class Vacanze implements Utilizzabile, Serializable{ + + + /** + * Costruttore per inizialiazzare una Vacanza + * + * @param idViaggio + * @param localitaViaggio + * @param dataPartenzaViaggio + * @param scadenzaOfferta + * @param prezzoPSingola + */ + public Vacanze(int idViaggio, String localitaViaggio, GregorianCalendar dataPartenzaViaggio, GregorianCalendar scadenzaOfferta, double prezzoPSingola){ + this.idViaggio = idViaggio; + this.localitaViaggio = localitaViaggio; + this.dataPartenzaViaggio = dataPartenzaViaggio; + this.scadenzaOfferta = scadenzaOfferta; + this.prezzoPSingola = prezzoPSingola; + } + + @Override + public boolean eAcquistabile() { + if(scadenzaOfferta.before(new GregorianCalendar())) //L'offerta della vacanza scade se la data sua scadenza e successiva a quella della data odierna + return false; + return true; + } + + /** + * @return - id del viaggio + */ + public int getIdViaggio() { + return idViaggio; + } + + /** + * @return - localita' del viaggio + */ + public String getLocalitaViaggio() { + return localitaViaggio; + } + + /** + * @return - data di partenza + */ + public GregorianCalendar getDataPartenzaViaggio() { + return dataPartenzaViaggio; + } + + /** + * @return - data di scadenza dell'offerta + */ + public GregorianCalendar getScadenzaOfferta() { + return scadenzaOfferta; + } + + /** + * @return - il prezzo per singola persona + */ + public double getPrezzoPSingola() { + return prezzoPSingola; + } + + /** + * @return - il numero di viaggi venduti + */ + public int getViaggiVenduti() { + return viaggiVenduti; + } + + /** + * @param localitaViaggio - setta la localit' del viaggio + */ + public void setLocalitaViaggio(String localitaViaggio) { + this.localitaViaggio = localitaViaggio; + } + + /** + * @param dataPartenzaViaggio - setta la data di patenza del viaggio + */ + public void setDataPartenzaViaggio(GregorianCalendar dataPartenzaViaggio) { + this.dataPartenzaViaggio = dataPartenzaViaggio; + } + + /** + * @param scadenzaOfferta - setta la data di scadenza dell'offerta della vacanza + */ + public void setScadenzaOfferta(GregorianCalendar scadenzaOfferta) { + this.scadenzaOfferta = scadenzaOfferta; + } + + /** + * @param prezzoPSingola - setta il prezzo per singola persona + */ + public void setPrezzoPSingola(double prezzoPSingola) { + this.prezzoPSingola = prezzoPSingola; + } + + /** + * Questo metodo incrementa, dopo ogni acquisto, il numero di viaggi venduti + */ + public void setViaggiVenduti() + { + viaggiVenduti++; + } + + private int idViaggio; + private String localitaViaggio; + private GregorianCalendar dataPartenzaViaggio; + private GregorianCalendar scadenzaOfferta; + private double prezzoPSingola; + private int viaggiVenduti; +} diff --git a/it/unisa/info13d/Articoli/Vacanze2.java b/it/unisa/info13d/Articoli/Vacanze2.java new file mode 100644 index 0000000..a183e4e --- /dev/null +++ b/it/unisa/info13d/Articoli/Vacanze2.java @@ -0,0 +1,139 @@ +package it.unisa.info13d.Articoli; + +import java.io.Serializable; +import java.util.GregorianCalendar; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 16/12/13 + * Time: 18.45 + */ +public class Vacanze2 extends Utilizzabile2 implements Serializable{ + + /** + * Costruttore per inizialiazzare una Vacanza + * + * @param idViaggio + * @param localitaViaggio + * @param dataPartenzaViaggio + * @param scadenzaOfferta + * @param prezzoPSingola + */ + public Vacanze2(int idViaggio, String localitaViaggio, GregorianCalendar dataPartenzaViaggio, GregorianCalendar scadenzaOfferta, double prezzoPSingola){ + this.idViaggio = idViaggio; + this.localitaViaggio = localitaViaggio; + this.dataPartenzaViaggio = dataPartenzaViaggio; + this.scadenzaOfferta = scadenzaOfferta; + this.prezzoPSingola = prezzoPSingola; + } + + @Override + public boolean eAcquistabile() { + if(scadenzaOfferta.before(new GregorianCalendar())) //L'offerta della vacanza scade se la data sua scadenza e successiva a quella della data odierna + return false; + return true; + } + + /** + * @return - id del viaggio + */ + public int getIdViaggio() { + return idViaggio; + } + + /** + * @return - localita' del viaggio + */ + public String getLocalitaViaggio() { + return localitaViaggio; + } + + /** + * @return - data di partenza + */ + public GregorianCalendar getDataPartenzaViaggio() { + return dataPartenzaViaggio; + } + + /** + * @return - data di scadenza dell'offerta + */ + public GregorianCalendar getScadenzaOfferta() { + return scadenzaOfferta; + } + + /** + * @return - il prezzo per singola persona + */ + public double getPrezzoPSingola() { + return prezzoPSingola; + } + + /** + * @return - il numero di viaggi venduti + */ + public int getViaggiVenduti() { + return viaggiVenduti; + } + + /** + * @param localitaViaggio - setta la localit' del viaggio + */ + public void setLocalitaViaggio(String localitaViaggio) { + this.localitaViaggio = localitaViaggio; + } + + /** + * @param dataPartenzaViaggio - setta la data di patenza del viaggio + */ + public void setDataPartenzaViaggio(GregorianCalendar dataPartenzaViaggio) { + this.dataPartenzaViaggio = dataPartenzaViaggio; + } + + /** + * @param scadenzaOfferta - setta la data di scadenza dell'offerta della vacanza + */ + public void setScadenzaOfferta(GregorianCalendar scadenzaOfferta) { + this.scadenzaOfferta = scadenzaOfferta; + } + + /** + * @param prezzoPSingola - setta il prezzo per singola persona + */ + public void setPrezzoPSingola(double prezzoPSingola) { + this.prezzoPSingola = prezzoPSingola; + } + + /** + * Questo metodo incrementa, dopo ogni acquisto, il numero di viaggi venduti + */ + public void setViaggiVenduti() + { + viaggiVenduti++; + } + + @Override + public double get_prezzo_scontato() { + GregorianCalendar dataOdierna = new GregorianCalendar(); + + long milliseconds1 = dataOdierna.getTimeInMillis(); + long milliseconds2 = scadenzaOfferta.getTimeInMillis(); + + long diff = milliseconds2 - milliseconds1; + long diffGiorni = diff / (24 * 60 * 60 * 1000); //differenza in giorni + + if( diffGiorni<=7 ) + return prezzoPSingola - (prezzoPSingola*0.10); //Prezzo scontato + else + return prezzoPSingola; //Nessuno sconto applicabile + } + + private int idViaggio; + private String localitaViaggio; + private GregorianCalendar dataPartenzaViaggio; + private GregorianCalendar scadenzaOfferta; + private double prezzoPSingola; + private int viaggiVenduti; + +} diff --git a/it/unisa/info13d/GestioneCatalogo/AdminSession.java b/it/unisa/info13d/GestioneCatalogo/AdminSession.java new file mode 100644 index 0000000..0971763 --- /dev/null +++ b/it/unisa/info13d/GestioneCatalogo/AdminSession.java @@ -0,0 +1,66 @@ +package it.unisa.info13d.GestioneCatalogo; + +import it.unisa.info13d.Articoli.Utilizzabile; +import it.unisa.info13d.Utility.ReShow; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Scanner; + +/** + * Created with PC-STATION. + * User: lebon + * Date: 17/12/13 + * Time: 10.55 + */ +public class AdminSession { + + /** + * Questo metodo visualizza il menu per gli Amministratori + * @throws IOException + * @throws ParseException + * @throws ClassNotFoundException + * @throws FileNotFoundException + */ + public static void showAdminMenu(Catalogo catalogo, ReShow r, String username) throws FileNotFoundException, ClassNotFoundException, ParseException, IOException { + System.out.println("------------ Admin Menu ------------"); + System.out.println("1 --> Nuovo Prodotto"); //Inserisce un nuovo prodotto in vendita nel catalogo + System.out.println("2 --> Cancella Prodotto"); //Cancella un prodotto in offerta nel catalogo + System.out.println("3 --> Offerte attive"); //Visualizza le offerte attive nel catalogo + System.out.println("4 --> Offerte scadute"); //Visualizza le offerte non piu acquistabili + System.out.println("5 --> Esci"); + + String sceltaMenu; + System.out.print("\nOperazione: "); + Scanner inputData = new Scanner(System.in); + sceltaMenu = inputData.nextLine(); + //Controllo input. La scelta deve essere obbligatoriamente compresa tra 1 e 4 + for ( ; !(sceltaMenu.equals("1"))&&!(sceltaMenu.equals("2"))&&!(sceltaMenu.equals("3"))&&!(sceltaMenu.equals("4"))&&!(sceltaMenu.equals("5")); ){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("Operazione: "); + sceltaMenu = inputData.nextLine(); + } + + switch(sceltaMenu) + { + case "1": + catalogo.nuovoProdotto(); + break; + case "2": + catalogo.cancellaProdotto(); + break; + case "3": + catalogo.offerteAttive(username); + break; + case "4": + catalogo.offerteScadute(username); + break; + case "5": + r.reshow = false; + break; + } + + } +} diff --git a/it/unisa/info13d/GestioneCatalogo/AdminSession2.java b/it/unisa/info13d/GestioneCatalogo/AdminSession2.java new file mode 100644 index 0000000..40584ee --- /dev/null +++ b/it/unisa/info13d/GestioneCatalogo/AdminSession2.java @@ -0,0 +1,66 @@ +package it.unisa.info13d.GestioneCatalogo; + +import it.unisa.info13d.Articoli.Utilizzabile; +import it.unisa.info13d.Utility.ReShow; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Scanner; + +/** + * Created with PC-STATION. + * User: lebon + * Date: 17/12/13 + * Time: 10.55 + */ +public class AdminSession2 { + + /** + * Questo metodo visualizza il menu per gli Amministratori + * @throws IOException + * @throws ParseException + * @throws ClassNotFoundException + * @throws FileNotFoundException + */ + public static void showAdminMenu(Catalogo2 catalogo, ReShow r, String username) throws FileNotFoundException, ClassNotFoundException, ParseException, IOException { + System.out.println("------------ Admin Menu ------------"); + System.out.println("1 --> Nuovo Prodotto"); //Inserisce un nuovo prodotto in vendita nel catalogo + System.out.println("2 --> Cancella Prodotto"); //Cancella un prodotto in offerta nel catalogo + System.out.println("3 --> Offerte attive"); //Visualizza le offerte attive nel catalogo + System.out.println("4 --> Offerte scadute"); //Visualizza le offerte non piu acquistabili + System.out.println("5 --> Esci"); + + String sceltaMenu; + System.out.print("\nOperazione: "); + Scanner inputData = new Scanner(System.in); + sceltaMenu = inputData.nextLine(); + //Controllo input. La scelta deve essere obbligatoriamente compresa tra 1 e 4 + for ( ; !(sceltaMenu.equals("1"))&&!(sceltaMenu.equals("2"))&&!(sceltaMenu.equals("3"))&&!(sceltaMenu.equals("4"))&&!(sceltaMenu.equals("5")); ){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("Operazione: "); + sceltaMenu = inputData.nextLine(); + } + + switch(sceltaMenu) + { + case "1": + catalogo.nuovoProdotto(); + break; + case "2": + catalogo.cancellaProdotto(); + break; + case "3": + catalogo.offerteAttive(username); + break; + case "4": + catalogo.offerteScadute(username); + break; + case "5": + r.reshow = false; + break; + } + + } +} diff --git a/it/unisa/info13d/GestioneCatalogo/Catalogo.java b/it/unisa/info13d/GestioneCatalogo/Catalogo.java new file mode 100644 index 0000000..cd29a4d --- /dev/null +++ b/it/unisa/info13d/GestioneCatalogo/Catalogo.java @@ -0,0 +1,921 @@ +package it.unisa.info13d.GestioneCatalogo; +import java.util.*; + +import it.unisa.info13d.Articoli.BeniDiConsumo; +import it.unisa.info13d.Articoli.CeneInRistoranti; +import it.unisa.info13d.Articoli.Global; +import it.unisa.info13d.Articoli.Utilizzabile; +import it.unisa.info13d.Articoli.Vacanze; +import it.unisa.info13d.Login.Access; +import it.unisa.info13d.Login.Entry; +import it.unisa.info13d.Utility.IdCounter; +import it.unisa.info13d.Utility.ReShow; +import it.unisa.info13d.Login.Access; +import it.unisa.info13d.Login.LoggedUser; +import it.unisa.info13d.Login.LoginData; +import it.unisa.info13d.Login.StoricoItem; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +/** + * Created with PC-STATION. + * User: lebon + * Date: 17/12/13 + * Time: 10.45 + */ +public class Catalogo{ + + /** + * Il costruttore definisce un nuovo catalogo nel caso non sia gia presente. + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + public Catalogo() throws FileNotFoundException,IOException,ClassNotFoundException + { + File f_data = new File("cat_db"); + if ( (f_data.exists())){ + ObjectInputStream reader = new ObjectInputStream(new FileInputStream(f_data)); + catalogoOfferte = (ArrayList) reader.readObject(); + reader.close(); + } else { + catalogoOfferte = new ArrayList(); + } + } + + /** + * Questo metodo si occupa di serializzare il catalogo (per garantire un minimo di sicurezza) e salvarlo su disco + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + void store_data()throws FileNotFoundException,IOException,ClassNotFoundException{ + File f_data = new File("cat_db"); + ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(f_data)); + writer.writeObject(catalogoOfferte); + writer.close(); + } + + + /** + * Questo metodo seleziona in Menu da visualizzare in base al login effettuato dall'utente + * + * @param userType is the type of user obtained from the main + * @throws IOException + * @throws ParseException + * @throws ClassNotFoundException + * @throws FileNotFoundException + */ + public void showMenu(String userType,String username, ReShow r) throws FileNotFoundException, ClassNotFoundException, ParseException, IOException + { + if(userType.equals("Admin")) + AdminSession.showAdminMenu(this,r, username); + else + ClientSession.showClientMenu(this,r, username); + } + + /** + * Questo metodo permette l'inserimento di un nuovo deal all'interno del catalogo + * @throws ParseException + * @throws IOException + * @throws ClassNotFoundException + * @throws FileNotFoundException + */ + public void nuovoProdotto() throws ParseException, FileNotFoundException, ClassNotFoundException, IOException + { + System.out.println("--------- Aggiunta nuovo Prodotto ----------"); + System.out.println("1 --> Vacanza"); + System.out.println("2 --> Cena"); + System.out.println("3 --> Bene di consumo "); + + String sceltaMenu; + System.out.print("Prodotto: "); + Scanner inputData = new Scanner(System.in); + sceltaMenu = inputData.nextLine(); + //Controllo input. La scelta deve essere obbligatoriamente compresa tra 1 e 4 + for ( ; !(sceltaMenu.equals("1"))&&!(sceltaMenu.equals("2"))&&!(sceltaMenu.equals("3")) ; ){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("Prodotto: "); + sceltaMenu = inputData.nextLine(); + } + + switch(sceltaMenu) + { + case "1": + System.out.println("---------- Nuova Offerta Vacanza ----------"); + System.out.print("Localita: "); + String localita = inputData.nextLine(); + + System.out.print("Data Partenza (GG/MM/AAAA): "); + String data = inputData.nextLine(); + String temp[]; + String marcatore="/"; + temp = data.split(marcatore); + GregorianCalendar dataPartenza = new GregorianCalendar((Integer.parseInt(temp[2])),(Integer.parseInt(temp[1]))-1,(Integer.parseInt(temp[0]))); + + System.out.print("Data Scadenza offerta (GG/MM/AAAA): "); + String data2 = inputData.nextLine(); + String temp2[]; + temp2 = data2.split(marcatore); + GregorianCalendar dataScad = new GregorianCalendar((Integer.parseInt(temp2[2])),(Integer.parseInt(temp2[1]))-1,(Integer.parseInt(temp2[0]))); + + System.out.print("Prezzo singola persona: "); + String prezzo = inputData.nextLine(); + double prezzoPSing = Double.parseDouble(prezzo); + + catalogoOfferte.add(new Vacanze(Global.get_next_id(), localita, dataPartenza, dataScad, prezzoPSing)); + store_data(); + break; + case "2": + System.out.println("---------- Nuova Offerta Cena ----------"); + System.out.print("Ristorante: "); + String ristorante = inputData.nextLine(); + + System.out.print("Localita: "); + String localitaCena = inputData.nextLine(); + + System.out.print("Descrizione: "); + String descrizioneCena = inputData.nextLine(); + + System.out.print("Costo a Persona: "); + String prezzoCena = inputData.nextLine(); + double prezzoPSingCena = Double.parseDouble(prezzoCena); + + System.out.print("Data Scadenza offerta (GG/MM/AAAA): "); + String data3 = inputData.nextLine(); + String temp3[]; + String marcatore2="/"; + temp3 = data3.split(marcatore2); + GregorianCalendar dataScadCena = new GregorianCalendar((Integer.parseInt(temp3[2])),(Integer.parseInt(temp3[1]))-1,(Integer.parseInt(temp3[0]))); + + System.out.print("Cene da Vendere: "); + String nCene = inputData.nextLine(); + int ceneDaVendere = Integer.parseInt(nCene); + + catalogoOfferte.add(new CeneInRistoranti(Global.get_next_id(), ristorante, localitaCena, descrizioneCena, prezzoPSingCena, dataScadCena, ceneDaVendere)); + store_data(); + break; + case "3": + System.out.println("---------- Nuova Offerta Bene ----------"); + System.out.print("Descrizione: "); + String descrizioneBene = inputData.nextLine(); + + System.out.print("Prezzo Bene: "); + String prezzoBene = inputData.nextLine(); + int prezzo_bene = Integer.parseInt(prezzoBene); + + System.out.print("Quantita in magazzino: "); + String qntMag = inputData.nextLine(); + int qnt_mag = Integer.parseInt(prezzoBene); + + catalogoOfferte.add(new BeniDiConsumo(Global.get_next_id(), descrizioneBene, prezzo_bene, qnt_mag)); + store_data(); + break; + } + IdCounter.save_counter(); + } + + /** + * Questo metodo permette la cancellazione di un prodotto dal catalogo + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws IOException + */ + public void cancellaProdotto() throws FileNotFoundException, ClassNotFoundException, IOException + { + System.out.println("------ Lista Prodotti -----\n--"); + for(Utilizzabile deal: catalogoOfferte) + { + if(deal instanceof Vacanze) + { + Vacanze dealVacanza = (Vacanze)deal; + visualizzaVacanza(dealVacanza); + + } + else if(deal instanceof BeniDiConsumo) + { + BeniDiConsumo dealBene = (BeniDiConsumo)deal; + visualizzaBene(dealBene); + } + else if(deal instanceof CeneInRistoranti) + { + CeneInRistoranti dealCena = (CeneInRistoranti)deal; + visualizzaCena(dealCena); + } + } + + System.out.print("Elimina prodotto [Inserisci id]: "); + Scanner inputData = new Scanner(System.in); + String id = inputData.nextLine(); + int idDel = Integer.parseInt(id); + System.out.println(idDel); + for(Utilizzabile deal: catalogoOfferte) + { + if(deal instanceof Vacanze && ((Vacanze)deal).getIdViaggio()==idDel) + { + catalogoOfferte.remove(deal); + store_data(); + break; + } + if(deal instanceof BeniDiConsumo && ((BeniDiConsumo) deal).getIdBene()==idDel) + { + catalogoOfferte.remove(deal); + store_data(); + break; + } + if(deal instanceof CeneInRistoranti && ((CeneInRistoranti) deal).getIdCena()==idDel) + { + catalogoOfferte.remove(deal); + store_data(); + break; + } + } + } + + /** + * + * Questo metodo velocizza la stampa dell informazioni scorrenndo l'array. + * + * @param a + */ + public void print_cat_admin (ArrayList a){ + for(Utilizzabile deal: a) + { + if(deal instanceof Vacanze) + { + Vacanze dealVacanza = (Vacanze)deal; + if((dealVacanza.eAcquistabile())) + { + visualizzaVacanza(dealVacanza); + } + continue; + } + if(deal instanceof BeniDiConsumo) + { + BeniDiConsumo dealBene = (BeniDiConsumo)deal; + if((dealBene.eAcquistabile())) + { + visualizzaBene(dealBene); + } + continue; + } + if(deal instanceof CeneInRistoranti) + { + CeneInRistoranti dealCena = (CeneInRistoranti)deal; + + if((dealCena.eAcquistabile())) + { + visualizzaCena(dealCena); + } + continue; + } + } + } + + /** + * Stamapa la lista degli oggetti utilizzabile escludendo le cene con location diversa da quelle dell'utente + * + * @param a + * @param location_user_to_compare + */ + public void print_cat_user (ArrayList a, String location_user_to_compare){ + for(Utilizzabile deal: a) + { + if(deal instanceof Vacanze) + { + Vacanze dealVacanza = (Vacanze)deal; + if((dealVacanza.eAcquistabile())) + { + visualizzaVacanza(dealVacanza); + } + continue; + } + if(deal instanceof BeniDiConsumo) + { + BeniDiConsumo dealBene = (BeniDiConsumo)deal; + if((dealBene.eAcquistabile())) + { + visualizzaBene(dealBene); + } + continue; + } + if(deal instanceof CeneInRistoranti) + { + CeneInRistoranti dealCena = (CeneInRistoranti)deal; + + if((dealCena.eAcquistabile() && dealCena.getLuogo().equals(location_user_to_compare ))) + { + visualizzaCena(dealCena); + } + continue; + } + } + } + + /** + * + * @param in - data da convertire in millisecondi + * @return - il numero in millisencodi della data passata alla funzione. + */ + protected long convert_calendar_to_int (GregorianCalendar in){ + return in.getTimeInMillis(); + } + + /** + * Questo metodo visualizza le offerte attive del catalogo. Viene differenziata la visualizzazione in base all'utente. + * - L'amministratore avra' la possibilita' di scegliere l'ordinamento in base dalla data di scadenza o ID prodotti + * - L'utente visualizzera' le offerte attive senza possibilita' di ordinamento + * + * @param user - username utente + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws IOException + */ + public void offerteAttive(String user) throws FileNotFoundException, ClassNotFoundException, IOException + { + Entry loggedUser = Access.get_user_data(user); + if(!(loggedUser.getType())) + { + Scanner reader = new Scanner(System.in); + + + System.out.println("1 --> Ordina Per Data di Scadenza"); + System.out.println("2 --> Ordina Per ID"); + + String choice; + System.out.print("Scelta operazione: "); + choice = reader.nextLine(); + for ( ; !(choice.equals("1")) && !(choice.equals("2")) ;){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("Scelta operazione: "); + choice = reader.nextLine(); + } + + if (choice.equals("1")){ + + class CompareDataScadenza implements Comparator { + + @Override + public int compare(Utilizzabile e1, Utilizzabile e2) { + + if (e1 instanceof BeniDiConsumo){ + //beni vs + if (e2 instanceof Vacanze){ + //beni vs vacanze + if (((BeniDiConsumo)e1).getScadenza().getTimeInMillis() < ((Vacanze)e2).getScadenzaOfferta().getTimeInMillis()) + return -1; + return 1; + + }else{ + if (e2 instanceof BeniDiConsumo){ + //bene vs //bene + + if (((BeniDiConsumo)e1).getScadenza().getTimeInMillis() < ((BeniDiConsumo)e2).getScadenza().getTimeInMillis()) + return -1; + return 1; + + }else{ + //bene vs cena + + if (((BeniDiConsumo)e1).getScadenza().getTimeInMillis() < ((CeneInRistoranti)e2).getDataScadenzaOffertaCena().getTimeInMillis()) + return -1; + return 1; + } + } + }else{ + if (e1 instanceof Vacanze){ + //vacanze vs + if (e2 instanceof BeniDiConsumo){ + //vacanze vs bene + if (((Vacanze)e1).getScadenzaOfferta().getTimeInMillis() < ((BeniDiConsumo)e2).getScadenza().getTimeInMillis()) + return -1; + return 1; + + }else{ + if (e2 instanceof Vacanze){ + //vacabnza vs vacanza + + if (((Vacanze)e1).getScadenzaOfferta().getTimeInMillis() < ((Vacanze)e2).getScadenzaOfferta().getTimeInMillis()) + return -1; + return 1; + + + }else{ + //vacanza vs cena + + if (((Vacanze)e1).getScadenzaOfferta().getTimeInMillis() < ((CeneInRistoranti)e2).getDataScadenzaOffertaCena().getTimeInMillis()) + return -1; + return 1; + + } + } + + }else{ + //Cene vs + if (e2 instanceof Vacanze){ + //cena vs vacanza + + if (((CeneInRistoranti)e1).getDataScadenzaOffertaCena().getTimeInMillis() < ((Vacanze)e2).getScadenzaOfferta().getTimeInMillis()) + return -1; + return 1; + + }else{ + if (e2 instanceof CeneInRistoranti){ + //cena vs cena + + if (((CeneInRistoranti)e1).getDataScadenzaOffertaCena().getTimeInMillis() < ((CeneInRistoranti)e2).getDataScadenzaOffertaCena().getTimeInMillis()) + return -1; + return 1; + }else{ + //cena vs bene + + if (((CeneInRistoranti)e1).getDataScadenzaOffertaCena().getTimeInMillis() < ((BeniDiConsumo)e2).getScadenza().getTimeInMillis()) + return -1; + return 1; + } + } + + } + } + + } + } + + Collections.sort(catalogoOfferte,new CompareDataScadenza()); + print_cat_admin (catalogoOfferte); + }else{ + + class CompareID implements Comparator { + + @Override + public int compare(Utilizzabile e1, Utilizzabile e2) { + + if (e1 instanceof BeniDiConsumo){ + //beni vs + if (e2 instanceof Vacanze){ + //beni vs vacanze + if (((BeniDiConsumo)e1).getIdBene() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + }else{ + if (e2 instanceof BeniDiConsumo){ + //bene vs //bene + + if (((BeniDiConsumo)e1).getIdBene() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + + }else{ + //bene vs cena + + if (((BeniDiConsumo)e1).getIdBene() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + } + } + }else{ + if (e1 instanceof Vacanze){ + //vacanze vs + if (e2 instanceof BeniDiConsumo){ + //vacanze vs bene + if (((Vacanze)e1).getIdViaggio() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + + }else{ + if (e2 instanceof Vacanze){ + //vacabnza vs vacanza + + if (((Vacanze)e1).getIdViaggio() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + + }else{ + //vacanza vs cena + + if (((Vacanze)e1).getIdViaggio() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + + } + } + + }else{ + //Cene vs + if (e2 instanceof Vacanze){ + //cena vs vacanza + + if (((CeneInRistoranti)e1).getIdCena() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + }else{ + if (e2 instanceof CeneInRistoranti){ + //cena vs cena + + if (((CeneInRistoranti)e1).getIdCena() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + }else{ + //cena vs bene + + if (((CeneInRistoranti)e1).getIdCena() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + } + } + + } + } + + } + } + + + Collections.sort(catalogoOfferte,new CompareID()); + print_cat_admin (catalogoOfferte); + + } + + + } else{ + + class CompareIDUser implements Comparator { + + @Override + public int compare(Utilizzabile e1, Utilizzabile e2) { + + if (e1 instanceof BeniDiConsumo){ + //beni vs + if (e2 instanceof Vacanze){ + //beni vs vacanze + if (((BeniDiConsumo)e1).getIdBene() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + }else{ + if (e2 instanceof BeniDiConsumo){ + //bene vs //bene + + if (((BeniDiConsumo)e1).getIdBene() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + + }else{ + //bene vs cena + + if (((BeniDiConsumo)e1).getIdBene() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + } + } + }else{ + if (e1 instanceof Vacanze){ + //vacanze vs + if (e2 instanceof BeniDiConsumo){ + //vacanze vs bene + if (((Vacanze)e1).getIdViaggio() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + + }else{ + if (e2 instanceof Vacanze){ + //vacabnza vs vacanza + + if (((Vacanze)e1).getIdViaggio() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + + }else{ + //vacanza vs cena + + if (((Vacanze)e1).getIdViaggio() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + + } + } + + }else{ + //Cene vs + if (e2 instanceof Vacanze){ + //cena vs vacanza + + if (((CeneInRistoranti)e1).getIdCena() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + }else{ + if (e2 instanceof CeneInRistoranti){ + //cena vs cena + + if (((CeneInRistoranti)e1).getIdCena() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + }else{ + //cena vs bene + + if (((CeneInRistoranti)e1).getIdCena() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + } + } + + } + } + + } + } + + + Collections.sort(catalogoOfferte,new CompareIDUser()); + print_cat_user(catalogoOfferte,loggedUser.getLocation()); + + } + } + + /** + * Questo metodo visualizza le offerte scadute del catalogo + * @throws IOException + * @throws ClassNotFoundException + * @throws FileNotFoundException + */ + public void offerteScadute(String user) throws FileNotFoundException, ClassNotFoundException, IOException + { + Entry loggedUser = Access.get_user_data(user); + System.out.println("------ Offerte Scadute -----"); + for(Utilizzabile deal: catalogoOfferte) + { + if(deal instanceof Vacanze) + { + Vacanze dealVacanza = (Vacanze)deal; + if(!(dealVacanza.eAcquistabile())) {visualizzaVacanza(dealVacanza);continue;} + } + if(deal instanceof BeniDiConsumo) + { + BeniDiConsumo dealBene = (BeniDiConsumo)deal; + if(!(dealBene.eAcquistabile())) {visualizzaBene(dealBene);continue;} + } + if(deal instanceof CeneInRistoranti) + { + CeneInRistoranti dealCena = (CeneInRistoranti)deal; + if(!(dealCena.eAcquistabile())) {visualizzaCena(dealCena);continue;} + } + } + } + /** + * Questa metodo aggiunge credito al conto dell'utente + * @param user + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws IOException + */ + public void aggiungiCredito(String user) throws FileNotFoundException, ClassNotFoundException, IOException + { + Entry loggedUser = Access.get_user_data(user); + System.out.println("----- Acquisto Credito -----"); + System.out.println("Saldo Attuale:"+ loggedUser.getBalance()); + System.out.println("Aggiungi importo da ricaricare:"); + Scanner inputData = new Scanner(System.in); + String euro = inputData.nextLine(); + loggedUser.setBalance(Double.parseDouble(euro)); + Access.replace_data(user, loggedUser); + System.out.println("Saldo attuale: "+loggedUser.getBalance()); + } + + /** + * Questo metodo permette all'utente di effettuare un acquisto + * + * @param user + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws IOException + */ + public void acquistaProdotto(String user) throws FileNotFoundException, ClassNotFoundException, IOException + { + Entry loggedUser = Access.get_user_data(user); + System.out.println("----- Shop ----"); + System.out.println("### Prodotti ###"); + + offerteAttive(user); + + System.out.println("Offerta da acquistare[inserisci id]: "); + Scanner inputData = new Scanner(System.in); + String idProd = inputData.nextLine(); + int idDel = Integer.parseInt(idProd); + + boolean trovato=false; + ArrayList storico = loggedUser.getStorico(); + + for(Utilizzabile dealsc: catalogoOfferte) + { + if((dealsc instanceof Vacanze) && ((Vacanze)dealsc).getIdViaggio()==idDel) + { + Vacanze dealVacanza = (Vacanze)dealsc; + if(dealVacanza.eAcquistabile() && loggedUser.getBalance()>=dealVacanza.getPrezzoPSingola()) + { + trovato=true; + storico.add(new StoricoItem("Viaggio: "+dealVacanza.getLocalitaViaggio(), new GregorianCalendar() ,dealVacanza.getPrezzoPSingola())); + dealVacanza.setViaggiVenduti(); + loggedUser.setBalance((dealVacanza.getPrezzoPSingola() * (-1))); + store_data(); + Access.replace_data(user, loggedUser); + System.out.println("************************************\nAcquisto completato con successo! \n************************************\nSaldo Attuale:" + loggedUser.getBalance()); + } + else + { + System.out.println("Il prodotto non e' acquistabile e/o Credito insufficiente, ricaricare"); + trovato=true; + break; + } + } + if((dealsc instanceof BeniDiConsumo) && ((BeniDiConsumo) dealsc).getIdBene()==idDel) + { + BeniDiConsumo dealBene = (BeniDiConsumo)dealsc; + if(dealBene.eAcquistabile() && loggedUser.getBalance()>=dealBene.getPrezzoBene()) + { + trovato=true; + storico.add(new StoricoItem("Prodotto: "+dealBene.getDescrizioneBene(), new GregorianCalendar() ,dealBene.getPrezzoBene())); + dealBene.setBeniVenduti(); + loggedUser.setBalance((dealBene.getPrezzoBene() * (-1))); + Access.replace_data(user, loggedUser); + store_data(); + System.out.println("************************************\nAcquisto completato con successo! \n************************************\nSaldo Attuale:" + loggedUser.getBalance()); + } + else + { + System.out.println("Il prodotto non e' acquistabile e/o Credito insufficiente, ricaricare"); + trovato=true; + break; + } + + } + if( (dealsc instanceof CeneInRistoranti) && ((CeneInRistoranti) dealsc).getIdCena()==idDel) + { + CeneInRistoranti dealCena = (CeneInRistoranti)dealsc; + if(dealCena.eAcquistabile() && loggedUser.getBalance()>=dealCena.getCostoPerPersona()) + { + trovato=true; + storico.add(new StoricoItem("Cena ristorante "+dealCena.getNomeRistorante()+" "+dealCena.getDescrizione_cena(), new GregorianCalendar() ,dealCena.getCostoPerPersona())); + dealCena.setCeneVendute(); + loggedUser.setBalance((dealCena.getCostoPerPersona() * (-1))); + Access.replace_data(user, loggedUser); + store_data(); + System.out.println("*************************************\nAcquisto completato con successo! \n************************************\nSaldo Attuale:" + loggedUser.getBalance()); + } + else + { + System.out.println("Il prodotto non e' acquistabile e/o Credito insufficiente, ricaricare"); + trovato=true; + break; + } + } + } + if(!trovato) System.out.println("### Prodotto non presente nel catalogo"); + } + + /** + * Questo metodo visualizza lo storico degli ordini effettuati dall'utente. rende possibile l'ordinamento in base alla data di scadenza o per prezzo. + * + * @param user + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws IOException + */ + public void visualizzaStorico(String user) throws FileNotFoundException, ClassNotFoundException, IOException + { + Scanner reader = new Scanner(System.in); + + System.out.println("1 --> Ordina Per Data di Acquisto"); + System.out.println("2 --> Ordina Per Costo Prodotto"); + + String choice; + System.out.print("Opzione: "); + choice = reader.nextLine(); + for ( ; !(choice.equals("1")) && !(choice.equals("2")) ;){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("Opzione: "); + choice = reader.nextLine(); + } + + Entry userLogged = Access.get_user_data(user); + System.out.println("---------- STORICO ----------"); + ArrayList storico = userLogged.getStorico(); + + if (choice.equals("1")){ + class CompareDataAcquisto implements Comparator { + + @Override + public int compare(StoricoItem e1, StoricoItem e2) { + + if (e1.getData_acquisto().getTimeInMillis() < e2.getData_acquisto().getTimeInMillis()) + return -1; + return 1; + } + } + + Collections.sort(storico,new CompareDataAcquisto()); + print_storico(storico); + + } else { + + class ComparePrezzo implements Comparator { + + @Override + public int compare(StoricoItem e1, StoricoItem e2) { + + if (e1.getPrezzo() < e2.getPrezzo()) + return -1; + return 1; + } + } + Collections.sort(storico,new ComparePrezzo()); + print_storico (storico); + } + } + + protected void print_storico (ArrayList in){ + + for(StoricoItem record: in) + { + GregorianCalendar partenza = record.getData_acquisto(); + int giornoP = partenza.get(Calendar.DAY_OF_MONTH); + int meseP = partenza.get(Calendar.MONTH); + int annoP = partenza.get(Calendar.YEAR); + System.out.println("| deal: "+record.getDescription()+"| Data Acquisto: "+giornoP+"/"+(meseP+1)+"/"+annoP+"| Prezzo: "+record.getPrezzo()); + } + + } + + + /** + * Questo metodo visualizza una vacanza, descrivendone i suoi dati. + * + * @param dealVacanza - oggetto di tipo Vacanze che rappresenta l'offerta della Vacanza da visualizzare + */ + public void visualizzaVacanza(Vacanze dealVacanza) + { + GregorianCalendar partenza = dealVacanza.getDataPartenzaViaggio(); + int giornoP = partenza.get(Calendar.DAY_OF_MONTH); + int meseP = partenza.get(Calendar.MONTH); + int annoP = partenza.get(Calendar.YEAR); + GregorianCalendar scadenza = dealVacanza.getScadenzaOfferta(); + int giornoS = scadenza.get(Calendar.DAY_OF_MONTH); + int meseS = scadenza.get(Calendar.MONTH); + int annoS = scadenza.get(Calendar.YEAR); + + System.out.println("#Vacanza-"+dealVacanza.getIdViaggio()+"# Scadenza: "+giornoS+"/"+(meseS+1)+"/"+annoS+"| Localita: "+dealVacanza.getLocalitaViaggio()+"| Partenza: "+giornoP+"/"+(meseP+1)+"/"+annoP+"| Prezzo Pers. Singola: "+dealVacanza.getPrezzoPSingola()+"\n--"); + } + + /** + * Questo metodo visualizza un Bene di consumo, descrivendone i suoi dati. + * + * @param dealBene - oggetto di tipo beniDiConsumo che rappresenta l'offerta del bene da visualizzare + */ + + public void visualizzaBene(BeniDiConsumo dealBene) + { + System.out.println("#Articolo-"+dealBene.getIdBene()+"# Descrizione: "+dealBene.getDescrizioneBene()+" Prezzo: "+dealBene.getPrezzoBene()+"| Quantita in magazzino: "+dealBene.getBeniInStock()+"\n--"); + } + + + /** + * + * Questo metodo visualizza una cena, descrivendone i suoi dati. + * @param dealCena - oggetto di tipo CeneInRistoranti che rappresenta l'offerta della cena da visualizzare + */ + public void visualizzaCena(CeneInRistoranti dealCena) + { + GregorianCalendar scadenzaCena = dealCena.getDataScadenzaOffertaCena(); + int giornoSC = scadenzaCena.get(Calendar.DAY_OF_MONTH); + int meseSC = scadenzaCena.get(Calendar.MONTH); + int annoSC = scadenzaCena.get(Calendar.YEAR); + System.out.println("#Cena-"+dealCena.getIdCena()+"# Scadenza:"+giornoSC+"/"+(meseSC+1)+"/"+annoSC+"| Ristorante: "+dealCena.getNomeRistorante()+"| Indirizzo: "+dealCena.getLuogo()+"| Descrizione: "+dealCena.getDescrizione_cena()+"| Costo: "+dealCena.getCostoPerPersona()+"| Disponibilita:"+dealCena.getNumCenedaVendere()+"cene"+"\n--"); + } + + /** + * + * @return - restituisce il catalogo delle offerte + */ + + public ArrayList getCatalogo() { + return catalogoOfferte; + } + + + + private ArrayList catalogoOfferte; + +} diff --git a/it/unisa/info13d/GestioneCatalogo/Catalogo2.java b/it/unisa/info13d/GestioneCatalogo/Catalogo2.java new file mode 100644 index 0000000..7764585 --- /dev/null +++ b/it/unisa/info13d/GestioneCatalogo/Catalogo2.java @@ -0,0 +1,982 @@ +package it.unisa.info13d.GestioneCatalogo; +import java.util.*; + +import it.unisa.info13d.Articoli.BeniDiConsumo; +import it.unisa.info13d.Articoli.BeniDiConsumo2; +import it.unisa.info13d.Articoli.CeneInRistoranti; +import it.unisa.info13d.Articoli.Global; +import it.unisa.info13d.Articoli.PrestazioniDopera2; +import it.unisa.info13d.Articoli.Utilizzabile; +import it.unisa.info13d.Articoli.Utilizzabile2; +import it.unisa.info13d.Articoli.Vacanze; +import it.unisa.info13d.Login.Access; +import it.unisa.info13d.Login.Entry; +import it.unisa.info13d.Utility.IdCounter; +import it.unisa.info13d.Utility.ReShow; +import it.unisa.info13d.Login.Access; +import it.unisa.info13d.Login.LoggedUser; +import it.unisa.info13d.Login.LoginData; +import it.unisa.info13d.Login.StoricoItem; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +/** + * Created with PC-STATION. + * User: lebon + * Date: 17/12/13 + * Time: 10.45 + */ +public class Catalogo2{ + + /** + * Il costruttore definisce un nuovo catalogo nel caso non sia gia presente. + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + public Catalogo2() throws FileNotFoundException,IOException,ClassNotFoundException + { + File f_data = new File("cat_db"); + if ( (f_data.exists())){ + ObjectInputStream reader = new ObjectInputStream(new FileInputStream(f_data)); + catalogoOfferte = (ArrayList) reader.readObject(); + reader.close(); + } else { + catalogoOfferte = new ArrayList(); + } + } + + /** + * Questo metodo si occupa di serializzare il catalogo (per garantire un minimo di sicurezza) e salvarlo su disco + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + void store_data()throws FileNotFoundException,IOException,ClassNotFoundException{ + File f_data = new File("cat_db"); + ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(f_data)); + writer.writeObject(catalogoOfferte); + writer.close(); + } + + + /** + * Questo metodo seleziona in Menu da visualizzare in base al login effettuato dall'utente + * + * @param userType is the type of user obtained from the main + * @throws IOException + * @throws ParseException + * @throws ClassNotFoundException + * @throws FileNotFoundException + */ + public void showMenu(String userType,String username, ReShow r) throws FileNotFoundException, ClassNotFoundException, ParseException, IOException + { + if(userType.equals("Admin")) + AdminSession2.showAdminMenu(this,r, username); + else + ClientSession2.showClientMenu(this,r, username); + } + + /** + * Questo metodo permette l'inserimento di un nuovo deal all'interno del catalogo + * @throws ParseException + * @throws IOException + * @throws ClassNotFoundException + * @throws FileNotFoundException + */ + public void nuovoProdotto() throws ParseException, FileNotFoundException, ClassNotFoundException, IOException + { + System.out.println("--------- Aggiunta nuovo Prodotto ----------"); + System.out.println("1 --> Vacanza"); + System.out.println("2 --> Cena"); + System.out.println("3 --> Bene di consumo "); + + String sceltaMenu; + System.out.print("Prodotto: "); + Scanner inputData = new Scanner(System.in); + sceltaMenu = inputData.nextLine(); + //Controllo input. La scelta deve essere obbligatoriamente compresa tra 1 e 4 + for ( ; !(sceltaMenu.equals("1"))&&!(sceltaMenu.equals("2"))&&!(sceltaMenu.equals("3")) ; ){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("Prodotto: "); + sceltaMenu = inputData.nextLine(); + } + + switch(sceltaMenu) + { + case "1": + System.out.println("---------- Nuova Offerta Vacanza ----------"); + System.out.print("Localita: "); + String localita = inputData.nextLine(); + + System.out.print("Data Partenza (GG/MM/AAAA): "); + String data = inputData.nextLine(); + String temp[]; + String marcatore="/"; + temp = data.split(marcatore); + GregorianCalendar dataPartenza = new GregorianCalendar((Integer.parseInt(temp[2])),(Integer.parseInt(temp[1]))-1,(Integer.parseInt(temp[0]))); + + System.out.print("Data Scadenza offerta (GG/MM/AAAA): "); + String data2 = inputData.nextLine(); + String temp2[]; + temp2 = data2.split(marcatore); + GregorianCalendar dataScad = new GregorianCalendar((Integer.parseInt(temp2[2])),(Integer.parseInt(temp2[1]))-1,(Integer.parseInt(temp2[0]))); + + System.out.print("Prezzo singola persona: "); + String prezzo = inputData.nextLine(); + double prezzoPSing = Double.parseDouble(prezzo); + + catalogoOfferte.add(new Vacanze(Global.get_next_id(), localita, dataPartenza, dataScad, prezzoPSing)); + store_data(); + break; + case "2": + System.out.println("---------- Nuova Offerta Cena ----------"); + System.out.print("Ristorante: "); + String ristorante = inputData.nextLine(); + + System.out.print("Localita: "); + String localitaCena = inputData.nextLine(); + + System.out.print("Descrizione: "); + String descrizioneCena = inputData.nextLine(); + + System.out.print("Costo a Persona: "); + String prezzoCena = inputData.nextLine(); + double prezzoPSingCena = Double.parseDouble(prezzoCena); + + System.out.print("Data Scadenza offerta (GG/MM/AAAA): "); + String data3 = inputData.nextLine(); + String temp3[]; + String marcatore2="/"; + temp3 = data3.split(marcatore2); + GregorianCalendar dataScadCena = new GregorianCalendar((Integer.parseInt(temp3[2])),(Integer.parseInt(temp3[1]))-1,(Integer.parseInt(temp3[0]))); + + System.out.print("Cene da Vendere: "); + String nCene = inputData.nextLine(); + int ceneDaVendere = Integer.parseInt(nCene); + + catalogoOfferte.add(new CeneInRistoranti(Global.get_next_id(), ristorante, localitaCena, descrizioneCena, prezzoPSingCena, dataScadCena, ceneDaVendere)); + store_data(); + break; + case "3": + System.out.println("---------- Nuova Offerta Bene ----------"); + System.out.print("Descrizione: "); + String descrizioneBene = inputData.nextLine(); + + System.out.print("Prezzo Bene: "); + String prezzoBene = inputData.nextLine(); + int prezzo_bene = Integer.parseInt(prezzoBene); + + System.out.print("Quantita in magazzino: "); + String qntMag = inputData.nextLine(); + int qnt_mag = Integer.parseInt(prezzoBene); + + catalogoOfferte.add(new BeniDiConsumo(Global.get_next_id(), descrizioneBene, prezzo_bene, qnt_mag)); + store_data(); + break; + } + IdCounter.save_counter(); + } + + /** + * Questo metodo permette la cancellazione di un prodotto dal catalogo + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws IOException + */ + public void cancellaProdotto() throws FileNotFoundException, ClassNotFoundException, IOException + { + System.out.println("------ Lista Prodotti -----\n--"); + for(Utilizzabile deal: catalogoOfferte) + { + if(deal instanceof Vacanze) + { + Vacanze dealVacanza = (Vacanze)deal; + visualizzaVacanza(dealVacanza); + + } + else if(deal instanceof BeniDiConsumo) + { + BeniDiConsumo dealBene = (BeniDiConsumo)deal; + visualizzaBene(dealBene); + } + else if(deal instanceof CeneInRistoranti) + { + CeneInRistoranti dealCena = (CeneInRistoranti)deal; + visualizzaCena(dealCena); + } + } + + System.out.print("Elimina prodotto [Inserisci id]: "); + Scanner inputData = new Scanner(System.in); + String id = inputData.nextLine(); + int idDel = Integer.parseInt(id); + System.out.println(idDel); + for(Utilizzabile deal: catalogoOfferte) + { + if(deal instanceof Vacanze && ((Vacanze)deal).getIdViaggio()==idDel) + { + catalogoOfferte.remove(deal); + store_data(); + break; + } + if(deal instanceof BeniDiConsumo && ((BeniDiConsumo) deal).getIdBene()==idDel) + { + catalogoOfferte.remove(deal); + store_data(); + break; + } + if(deal instanceof CeneInRistoranti && ((CeneInRistoranti) deal).getIdCena()==idDel) + { + catalogoOfferte.remove(deal); + store_data(); + break; + } + } + } + + /** + * + * Questo metodo velocizza la stampa dell informazioni scorrenndo l'array. + * + * @param a + */ + public void print_cat_admin (ArrayList a){ + for(Utilizzabile deal: a) + { + if(deal instanceof Vacanze) + { + Vacanze dealVacanza = (Vacanze)deal; + if((dealVacanza.eAcquistabile())) + { + visualizzaVacanza(dealVacanza); + } + continue; + } + if(deal instanceof BeniDiConsumo) + { + BeniDiConsumo dealBene = (BeniDiConsumo)deal; + if((dealBene.eAcquistabile())) + { + visualizzaBene(dealBene); + } + continue; + } + if(deal instanceof CeneInRistoranti) + { + CeneInRistoranti dealCena = (CeneInRistoranti)deal; + + if((dealCena.eAcquistabile())) + { + visualizzaCena(dealCena); + } + continue; + } + } + } + + /** + * Stamapa la lista degli oggetti utilizzabile escludendo le cene con location diversa da quelle dell'utente + * + * @param a + * @param location_user_to_compare + */ + public void print_cat_user (ArrayList a, String location_user_to_compare){ + for(Utilizzabile deal: a) + { + if(deal instanceof Vacanze) + { + Vacanze dealVacanza = (Vacanze)deal; + if((dealVacanza.eAcquistabile())) + { + visualizzaVacanza(dealVacanza); + } + continue; + } + if(deal instanceof BeniDiConsumo) + { + BeniDiConsumo dealBene = (BeniDiConsumo)deal; + if((dealBene.eAcquistabile())) + { + visualizzaBene(dealBene); + } + continue; + } + if(deal instanceof CeneInRistoranti) + { + CeneInRistoranti dealCena = (CeneInRistoranti)deal; + + if((dealCena.eAcquistabile() && dealCena.getLuogo().equals(location_user_to_compare ))) + { + visualizzaCena(dealCena); + } + continue; + } + } + } + + /** + * + * @param in - data da convertire in millisecondi + * @return - il numero in millisencodi della data passata alla funzione. + */ + protected long convert_calendar_to_int (GregorianCalendar in){ + return in.getTimeInMillis(); + } + + /** + * Questo metodo visualizza le offerte attive del catalogo. Viene differenziata la visualizzazione in base all'utente. + * - L'amministratore avra' la possibilita' di scegliere l'ordinamento in base dalla data di scadenza o ID prodotti + * - L'utente visualizzera' le offerte attive senza possibilita' di ordinamento + * + * @param user - username utente + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws IOException + */ + public void offerteAttive(String user) throws FileNotFoundException, ClassNotFoundException, IOException + { + Entry loggedUser = Access.get_user_data(user); + if(!(loggedUser.getType())) + { + Scanner reader = new Scanner(System.in); + + + System.out.println("1 --> Ordina Per Data di Scadenza"); + System.out.println("2 --> Ordina Per ID"); + + String choice; + System.out.print("Scelta operazione: "); + choice = reader.nextLine(); + for ( ; !(choice.equals("1")) && !(choice.equals("2")) ;){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("Scelta operazione: "); + choice = reader.nextLine(); + } + + if (choice.equals("1")){ + + class CompareDataScadenza implements Comparator { + + @Override + public int compare(Utilizzabile e1, Utilizzabile e2) { + + if (e1 instanceof BeniDiConsumo){ + //beni vs + if (e2 instanceof Vacanze){ + //beni vs vacanze + if (((BeniDiConsumo)e1).getScadenza().getTimeInMillis() < ((Vacanze)e2).getScadenzaOfferta().getTimeInMillis()) + return -1; + return 1; + + }else{ + if (e2 instanceof BeniDiConsumo){ + //bene vs //bene + + if (((BeniDiConsumo)e1).getScadenza().getTimeInMillis() < ((BeniDiConsumo)e2).getScadenza().getTimeInMillis()) + return -1; + return 1; + + }else{ + //bene vs cena + + if (((BeniDiConsumo)e1).getScadenza().getTimeInMillis() < ((CeneInRistoranti)e2).getDataScadenzaOffertaCena().getTimeInMillis()) + return -1; + return 1; + } + } + }else{ + if (e1 instanceof Vacanze){ + //vacanze vs + if (e2 instanceof BeniDiConsumo){ + //vacanze vs bene + if (((Vacanze)e1).getScadenzaOfferta().getTimeInMillis() < ((BeniDiConsumo)e2).getScadenza().getTimeInMillis()) + return -1; + return 1; + + }else{ + if (e2 instanceof Vacanze){ + //vacabnza vs vacanza + + if (((Vacanze)e1).getScadenzaOfferta().getTimeInMillis() < ((Vacanze)e2).getScadenzaOfferta().getTimeInMillis()) + return -1; + return 1; + + + }else{ + //vacanza vs cena + + if (((Vacanze)e1).getScadenzaOfferta().getTimeInMillis() < ((CeneInRistoranti)e2).getDataScadenzaOffertaCena().getTimeInMillis()) + return -1; + return 1; + + } + } + + }else{ + //Cene vs + if (e2 instanceof Vacanze){ + //cena vs vacanza + + if (((CeneInRistoranti)e1).getDataScadenzaOffertaCena().getTimeInMillis() < ((Vacanze)e2).getScadenzaOfferta().getTimeInMillis()) + return -1; + return 1; + + }else{ + if (e2 instanceof CeneInRistoranti){ + //cena vs cena + + if (((CeneInRistoranti)e1).getDataScadenzaOffertaCena().getTimeInMillis() < ((CeneInRistoranti)e2).getDataScadenzaOffertaCena().getTimeInMillis()) + return -1; + return 1; + }else{ + //cena vs bene + + if (((CeneInRistoranti)e1).getDataScadenzaOffertaCena().getTimeInMillis() < ((BeniDiConsumo)e2).getScadenza().getTimeInMillis()) + return -1; + return 1; + } + } + + } + } + + } + } + + Collections.sort(catalogoOfferte,new CompareDataScadenza()); + print_cat_admin (catalogoOfferte); + }else{ + + class CompareID implements Comparator { + + @Override + public int compare(Utilizzabile e1, Utilizzabile e2) { + + if (e1 instanceof BeniDiConsumo){ + //beni vs + if (e2 instanceof Vacanze){ + //beni vs vacanze + if (((BeniDiConsumo)e1).getIdBene() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + }else{ + if (e2 instanceof BeniDiConsumo){ + //bene vs //bene + + if (((BeniDiConsumo)e1).getIdBene() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + + }else{ + //bene vs cena + + if (((BeniDiConsumo)e1).getIdBene() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + } + } + }else{ + if (e1 instanceof Vacanze){ + //vacanze vs + if (e2 instanceof BeniDiConsumo){ + //vacanze vs bene + if (((Vacanze)e1).getIdViaggio() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + + }else{ + if (e2 instanceof Vacanze){ + //vacabnza vs vacanza + + if (((Vacanze)e1).getIdViaggio() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + + }else{ + //vacanza vs cena + + if (((Vacanze)e1).getIdViaggio() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + + } + } + + }else{ + //Cene vs + if (e2 instanceof Vacanze){ + //cena vs vacanza + + if (((CeneInRistoranti)e1).getIdCena() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + }else{ + if (e2 instanceof CeneInRistoranti){ + //cena vs cena + + if (((CeneInRistoranti)e1).getIdCena() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + }else{ + //cena vs bene + + if (((CeneInRistoranti)e1).getIdCena() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + } + } + + } + } + + } + } + + + Collections.sort(catalogoOfferte,new CompareID()); + print_cat_admin (catalogoOfferte); + + } + + + } else{ + + class CompareIDUser implements Comparator { + + @Override + public int compare(Utilizzabile e1, Utilizzabile e2) { + + if (e1 instanceof BeniDiConsumo){ + //beni vs + if (e2 instanceof Vacanze){ + //beni vs vacanze + if (((BeniDiConsumo)e1).getIdBene() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + }else{ + if (e2 instanceof BeniDiConsumo){ + //bene vs //bene + + if (((BeniDiConsumo)e1).getIdBene() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + + }else{ + //bene vs cena + + if (((BeniDiConsumo)e1).getIdBene() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + } + } + }else{ + if (e1 instanceof Vacanze){ + //vacanze vs + if (e2 instanceof BeniDiConsumo){ + //vacanze vs bene + if (((Vacanze)e1).getIdViaggio() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + + }else{ + if (e2 instanceof Vacanze){ + //vacabnza vs vacanza + + if (((Vacanze)e1).getIdViaggio() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + + }else{ + //vacanza vs cena + + if (((Vacanze)e1).getIdViaggio() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + + } + } + + }else{ + //Cene vs + if (e2 instanceof Vacanze){ + //cena vs vacanza + + if (((CeneInRistoranti)e1).getIdCena() < ((Vacanze)e2).getIdViaggio()) + return -1; + return 1; + + }else{ + if (e2 instanceof CeneInRistoranti){ + //cena vs cena + + if (((CeneInRistoranti)e1).getIdCena() < ((CeneInRistoranti)e2).getIdCena()) + return -1; + return 1; + }else{ + //cena vs bene + + if (((CeneInRistoranti)e1).getIdCena() < ((BeniDiConsumo)e2).getIdBene()) + return -1; + return 1; + } + } + + } + } + + } + } + + + Collections.sort(catalogoOfferte,new CompareIDUser()); + print_cat_user(catalogoOfferte,loggedUser.getLocation()); + + } + } + + /** + * Questo metodo visualizza le offerte scadute del catalogo + * @throws IOException + * @throws ClassNotFoundException + * @throws FileNotFoundException + */ + public void offerteScadute(String user) throws FileNotFoundException, ClassNotFoundException, IOException + { + Entry loggedUser = Access.get_user_data(user); + System.out.println("------ Offerte Scadute -----"); + for(Utilizzabile deal: catalogoOfferte) + { + if(deal instanceof Vacanze) + { + Vacanze dealVacanza = (Vacanze)deal; + if(!(dealVacanza.eAcquistabile())) {visualizzaVacanza(dealVacanza);continue;} + } + if(deal instanceof BeniDiConsumo) + { + BeniDiConsumo dealBene = (BeniDiConsumo)deal; + if(!(dealBene.eAcquistabile())) {visualizzaBene(dealBene);continue;} + } + if(deal instanceof CeneInRistoranti) + { + CeneInRistoranti dealCena = (CeneInRistoranti)deal; + if(!(dealCena.eAcquistabile())) {visualizzaCena(dealCena);continue;} + } + } + } + /** + * Questa metodo aggiunge credito al conto dell'utente + * @param user + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws IOException + */ + public void aggiungiCredito(String user) throws FileNotFoundException, ClassNotFoundException, IOException + { + Entry loggedUser = Access.get_user_data(user); + System.out.println("----- Acquisto Credito -----"); + System.out.println("Saldo Attuale:"+ loggedUser.getBalance()); + System.out.println("Aggiungi importo da ricaricare:"); + Scanner inputData = new Scanner(System.in); + String euro = inputData.nextLine(); + loggedUser.setBalance(Double.parseDouble(euro)); + Access.replace_data(user, loggedUser); + System.out.println("Saldo attuale: "+loggedUser.getBalance()); + } + + /** + * Questo metodo permette all'utente di effettuare un acquisto + * + * @param user + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws IOException + */ + public void acquistaProdotto(String user) throws FileNotFoundException, ClassNotFoundException, IOException + { + Entry loggedUser = Access.get_user_data(user); + System.out.println("----- Shop ----"); + System.out.println("### Prodotti ###"); + + offerteAttive(user); + + System.out.println("Offerta da acquistare[inserisci id]: "); + Scanner inputData = new Scanner(System.in); + String idProd = inputData.nextLine(); + int idDel = Integer.parseInt(idProd); + + boolean trovato=false; + ArrayList storico = loggedUser.getStorico(); + + for(Utilizzabile dealsc: catalogoOfferte) + { + if((dealsc instanceof Vacanze) && ((Vacanze)dealsc).getIdViaggio()==idDel) + { + Vacanze dealVacanza = (Vacanze)dealsc; + if(dealVacanza.eAcquistabile() && loggedUser.getBalance()>=dealVacanza.getPrezzoPSingola()) + { + trovato=true; + storico.add(new StoricoItem("Viaggio: "+dealVacanza.getLocalitaViaggio(), new GregorianCalendar() ,dealVacanza.getPrezzoPSingola())); + dealVacanza.setViaggiVenduti(); + loggedUser.setBalance((dealVacanza.getPrezzoPSingola() * (-1))); + store_data(); + Access.replace_data(user, loggedUser); + System.out.println("************************************\nAcquisto completato con successo! \n************************************\nSaldo Attuale:" + loggedUser.getBalance()); + } + else + { + System.out.println("Il prodotto non e' acquistabile e/o Credito insufficiente, ricaricare"); + trovato=true; + break; + } + } + if((dealsc instanceof BeniDiConsumo) && ((BeniDiConsumo) dealsc).getIdBene()==idDel) + { + BeniDiConsumo dealBene = (BeniDiConsumo)dealsc; + if(dealBene.eAcquistabile() && loggedUser.getBalance()>=dealBene.getPrezzoBene()) + { + trovato=true; + storico.add(new StoricoItem("Prodotto: "+dealBene.getDescrizioneBene(), new GregorianCalendar() ,dealBene.getPrezzoBene())); + dealBene.setBeniVenduti(); + loggedUser.setBalance((dealBene.getPrezzoBene() * (-1))); + Access.replace_data(user, loggedUser); + store_data(); + System.out.println("************************************\nAcquisto completato con successo! \n************************************\nSaldo Attuale:" + loggedUser.getBalance()); + } + else + { + System.out.println("Il prodotto non e' acquistabile e/o Credito insufficiente, ricaricare"); + trovato=true; + break; + } + + } + if( (dealsc instanceof CeneInRistoranti) && ((CeneInRistoranti) dealsc).getIdCena()==idDel) + { + CeneInRistoranti dealCena = (CeneInRistoranti)dealsc; + if(dealCena.eAcquistabile() && loggedUser.getBalance()>=dealCena.getCostoPerPersona()) + { + trovato=true; + storico.add(new StoricoItem("Cena ristorante "+dealCena.getNomeRistorante()+" "+dealCena.getDescrizione_cena(), new GregorianCalendar() ,dealCena.getCostoPerPersona())); + dealCena.setCeneVendute(); + loggedUser.setBalance((dealCena.getCostoPerPersona() * (-1))); + Access.replace_data(user, loggedUser); + store_data(); + System.out.println("*************************************\nAcquisto completato con successo! \n************************************\nSaldo Attuale:" + loggedUser.getBalance()); + } + else + { + System.out.println("Il prodotto non e' acquistabile e/o Credito insufficiente, ricaricare"); + trovato=true; + break; + } + } + } + if(!trovato) System.out.println("### Prodotto non presente nel catalogo"); + } + + /** + * Questo metodo visualizza lo storico degli ordini effettuati dall'utente. rende possibile l'ordinamento in base alla data di scadenza o per prezzo. + * + * @param user + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws IOException + */ + public void visualizzaStorico(String user) throws FileNotFoundException, ClassNotFoundException, IOException + { + Scanner reader = new Scanner(System.in); + + System.out.println("1 --> Ordina Per Data di Acquisto"); + System.out.println("2 --> Ordina Per Costo Prodotto"); + + String choice; + System.out.print("Opzione: "); + choice = reader.nextLine(); + for ( ; !(choice.equals("1")) && !(choice.equals("2")) ;){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("Opzione: "); + choice = reader.nextLine(); + } + + Entry userLogged = Access.get_user_data(user); + System.out.println("---------- STORICO ----------"); + ArrayList storico = userLogged.getStorico(); + + if (choice.equals("1")){ + class CompareDataAcquisto implements Comparator { + + @Override + public int compare(StoricoItem e1, StoricoItem e2) { + + if (e1.getData_acquisto().getTimeInMillis() < e2.getData_acquisto().getTimeInMillis()) + return -1; + return 1; + } + } + + Collections.sort(storico,new CompareDataAcquisto()); + print_storico(storico); + + } else { + + class ComparePrezzo implements Comparator { + + @Override + public int compare(StoricoItem e1, StoricoItem e2) { + + if (e1.getPrezzo() < e2.getPrezzo()) + return -1; + return 1; + } + } + Collections.sort(storico,new ComparePrezzo()); + print_storico (storico); + } + } + + protected void print_storico (ArrayList in){ + + for(StoricoItem record: in) + { + GregorianCalendar partenza = record.getData_acquisto(); + int giornoP = partenza.get(Calendar.DAY_OF_MONTH); + int meseP = partenza.get(Calendar.MONTH); + int annoP = partenza.get(Calendar.YEAR); + System.out.println("| deal: "+record.getDescription()+"| Data Acquisto: "+giornoP+"/"+(meseP+1)+"/"+annoP+"| Prezzo: "+record.getPrezzo()); + } + + } + + + /** + * Questo metodo visualizza una vacanza, descrivendone i suoi dati. + * + * @param dealVacanza - oggetto di tipo Vacanze che rappresenta l'offerta della Vacanza da visualizzare + */ + public void visualizzaVacanza(Vacanze dealVacanza) + { + GregorianCalendar partenza = dealVacanza.getDataPartenzaViaggio(); + int giornoP = partenza.get(Calendar.DAY_OF_MONTH); + int meseP = partenza.get(Calendar.MONTH); + int annoP = partenza.get(Calendar.YEAR); + GregorianCalendar scadenza = dealVacanza.getScadenzaOfferta(); + int giornoS = scadenza.get(Calendar.DAY_OF_MONTH); + int meseS = scadenza.get(Calendar.MONTH); + int annoS = scadenza.get(Calendar.YEAR); + + System.out.println("#Vacanza-"+dealVacanza.getIdViaggio()+"# Scadenza: "+giornoS+"/"+(meseS+1)+"/"+annoS+"| Localita: "+dealVacanza.getLocalitaViaggio()+"| Partenza: "+giornoP+"/"+(meseP+1)+"/"+annoP+"| Prezzo Pers. Singola: "+dealVacanza.getPrezzoPSingola()+"\n--"); + } + + /** + * Questo metodo visualizza un Bene di consumo, descrivendone i suoi dati. + * + * @param dealBene - oggetto di tipo beniDiConsumo che rappresenta l'offerta del bene da visualizzare + */ + + public void visualizzaBene(BeniDiConsumo dealBene) + { + System.out.println("#Articolo-"+dealBene.getIdBene()+"# Descrizione: "+dealBene.getDescrizioneBene()+" Prezzo: "+dealBene.getPrezzoBene()+"| Quantita in magazzino: "+dealBene.getBeniInStock()+"\n--"); + } + + + /** + * + * Questo metodo visualizza una cena, descrivendone i suoi dati. + * @param dealCena - oggetto di tipo CeneInRistoranti che rappresenta l'offerta della cena da visualizzare + */ + public void visualizzaCena(CeneInRistoranti dealCena) + { + GregorianCalendar scadenzaCena = dealCena.getDataScadenzaOffertaCena(); + int giornoSC = scadenzaCena.get(Calendar.DAY_OF_MONTH); + int meseSC = scadenzaCena.get(Calendar.MONTH); + int annoSC = scadenzaCena.get(Calendar.YEAR); + System.out.println("#Cena-"+dealCena.getIdCena()+"# Scadenza:"+giornoSC+"/"+(meseSC+1)+"/"+annoSC+"| Ristorante: "+dealCena.getNomeRistorante()+"| Indirizzo: "+dealCena.getLuogo()+"| Descrizione: "+dealCena.getDescrizione_cena()+"| Costo: "+dealCena.getCostoPerPersona()+"| Disponibilita:"+dealCena.getNumCenedaVendere()+"cene"+"\n--"); + } + + /** + * + * @return - restituisce il catalogo delle offerte + */ + + public ArrayList getCatalogo() { + return catalogoOfferte; + } + + /** + * + * Questo emtodo restituisce tutti gli articoli con scadenza (vacanze, cene ristorante) che hanno scadenza tra una data iniziale e una data finale presi come parametri. + * @param dataStart - data iniziale + * @param dataEnd - data finale + * @return un ArrayList contenente gli articoli compresi tra le due date inserite dall'utente + */ + public ArrayList getDataExpire(GregorianCalendar dataStart, GregorianCalendar dataEnd) + { + ArrayList arrayData = new ArrayList(); + + for(Utilizzabile articolo: catalogoOfferte) + { + if(articolo instanceof Vacanze) + { + Vacanze dealVacanza = (Vacanze)articolo; + if( ( dealVacanza.getScadenzaOfferta().before(dataEnd) ) && ( dealVacanza.getScadenzaOfferta().after(dataStart) ) ) + { + arrayData.add(dealVacanza); + } + } + else if(articolo instanceof CeneInRistoranti) + { + CeneInRistoranti dealCena = (CeneInRistoranti)articolo; + if( ( dealCena.getDataScadenzaOffertaCena().before(dataEnd) ) && ( dealCena.getDataScadenzaOffertaCena().after(dataStart) ) ) + { + arrayData.add(dealCena); + } + } + } + + return arrayData; + } + + /** + * Questo metodo restituisce tutti gli articoli senza scadenza (beni, prestazioni) che sono offerti da fornitori con un giudizio superiore ad un valore preso come parametro. + * @param votazione + * @return arrayDealNotExpire - Ritorna un arrayList contente tutti gli articoli senza scadenza + */ + public ArrayList getDealNotExpire(int votazione) + { + ArrayList arrayDealNotExpire = new ArrayList(); + + for(Utilizzabile articolo: catalogoOfferte) + { + if(articolo instanceof BeniDiConsumo) + { + BeniDiConsumo2 dealBene = (BeniDiConsumo2)articolo; + if( dealBene.getGiudizioFornitore()>votazione ) + arrayDealNotExpire.add(dealBene); + } + else if(articolo instanceof PrestazioniDopera2) + { + PrestazioniDopera2 dealPrestazione = (PrestazioniDopera2)articolo; + if( dealPrestazione.getGiudizioFornitore()>votazione ) + arrayDealNotExpire.add(dealPrestazione); + } + } + return arrayDealNotExpire; + } + + private ArrayList catalogoOfferte; +} \ No newline at end of file diff --git a/it/unisa/info13d/GestioneCatalogo/ClientSession.java b/it/unisa/info13d/GestioneCatalogo/ClientSession.java new file mode 100644 index 0000000..d20abfa --- /dev/null +++ b/it/unisa/info13d/GestioneCatalogo/ClientSession.java @@ -0,0 +1,65 @@ +package it.unisa.info13d.GestioneCatalogo; + +import it.unisa.info13d.Articoli.Utilizzabile; +import it.unisa.info13d.Login.Access; +import it.unisa.info13d.Login.Entry; +import it.unisa.info13d.Utility.ReShow; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; +/** + * Created with PC-STATION. + * User: lebon + * Date: 17/12/13 + * Time: 11.10 + */ +public class ClientSession { + /** + * Questo metodo visualizza il menu per i Clienti + * @throws IOException + * @throws ClassNotFoundException + * @throws FileNotFoundException + */ + public static void showClientMenu(Catalogo catalogo, ReShow r, String username) throws FileNotFoundException, ClassNotFoundException, IOException { + Entry loggedUser = Access.get_user_data(username); + System.out.println("------------ Menu operazioni ------------"); + System.out.println("-- Salto totale:"+loggedUser.getBalance()); + System.out.println("1 --> Acquista Credito"); //Si acquista credito per l'acquisto dei prodotti + System.out.println("2 --> Visualizza Offerte"); //Visualizza le offerte acquistabili + System.out.println("3 --> Acquista"); //Acquisto di un offerta + System.out.println("4 --> Storico acquisti"); //Visualizza lo storico degli acquisti dell'utente + System.out.println("5 --> Esci"); + + String sceltaMenu; + System.out.print("Operazione: "); + Scanner inputData = new Scanner(System.in); + sceltaMenu = inputData.nextLine(); + //Controllo input. La scelta deve essere obbligatoriamente compresa tra 1 e 4 + for ( ; !(sceltaMenu.equals("1"))&&!(sceltaMenu.equals("2"))&&!(sceltaMenu.equals("3"))&&!(sceltaMenu.equals("4"))&&!(sceltaMenu.equals("5")) ;){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("Operazione: "); + sceltaMenu = inputData.nextLine(); + } + + switch(sceltaMenu) + { + case "1": + catalogo.aggiungiCredito(username); + break; + case "2": + catalogo.offerteAttive(username); + break; + case "3": + catalogo.acquistaProdotto(username); + break; + case "4": + catalogo.visualizzaStorico(username); + break; + case "5": + r.reshow = false; + break; + } + } +} diff --git a/it/unisa/info13d/GestioneCatalogo/ClientSession2.java b/it/unisa/info13d/GestioneCatalogo/ClientSession2.java new file mode 100644 index 0000000..dc47131 --- /dev/null +++ b/it/unisa/info13d/GestioneCatalogo/ClientSession2.java @@ -0,0 +1,65 @@ +package it.unisa.info13d.GestioneCatalogo; + +import it.unisa.info13d.Articoli.Utilizzabile; +import it.unisa.info13d.Login.Access; +import it.unisa.info13d.Login.Entry; +import it.unisa.info13d.Utility.ReShow; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; +/** + * Created with PC-STATION. + * User: lebon + * Date: 17/12/13 + * Time: 11.10 + */ +public class ClientSession2 { + /** + * Questo metodo visualizza il menu per i Clienti + * @throws IOException + * @throws ClassNotFoundException + * @throws FileNotFoundException + */ + public static void showClientMenu(Catalogo2 catalogo, ReShow r, String username) throws FileNotFoundException, ClassNotFoundException, IOException { + Entry loggedUser = Access.get_user_data(username); + System.out.println("------------ Menu operazioni ------------"); + System.out.println("-- Salto totale:"+loggedUser.getBalance()); + System.out.println("1 --> Acquista Credito"); //Si acquista credito per l'acquisto dei prodotti + System.out.println("2 --> Visualizza Offerte"); //Visualizza le offerte acquistabili + System.out.println("3 --> Acquista"); //Acquisto di un offerta + System.out.println("4 --> Storico acquisti"); //Visualizza lo storico degli acquisti dell'utente + System.out.println("5 --> Esci"); + + String sceltaMenu; + System.out.print("Operazione: "); + Scanner inputData = new Scanner(System.in); + sceltaMenu = inputData.nextLine(); + //Controllo input. La scelta deve essere obbligatoriamente compresa tra 1 e 4 + for ( ; !(sceltaMenu.equals("1"))&&!(sceltaMenu.equals("2"))&&!(sceltaMenu.equals("3"))&&!(sceltaMenu.equals("4"))&&!(sceltaMenu.equals("5")) ;){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("Operazione: "); + sceltaMenu = inputData.nextLine(); + } + + switch(sceltaMenu) + { + case "1": + catalogo.aggiungiCredito(username); + break; + case "2": + catalogo.offerteAttive(username); + break; + case "3": + catalogo.acquistaProdotto(username); + break; + case "4": + catalogo.visualizzaStorico(username); + break; + case "5": + r.reshow = false; + break; + } + } +} diff --git a/it/unisa/info13d/Login/Access.java b/it/unisa/info13d/Login/Access.java new file mode 100644 index 0000000..d992a92 --- /dev/null +++ b/it/unisa/info13d/Login/Access.java @@ -0,0 +1,279 @@ +package it.unisa.info13d.Login; + +import java.io.*; +import java.util.ArrayList; +import java.util.Scanner; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 16/12/13 + * Time: 19.45 + */ +public class Access { + /** + * Questo metodo avvia la procedura di login o di registrazione dell'utente in base alla scelta fatta nel menu. + * + * @param set_user_logged_here dove memorizzare il nome dell'utente loggato o registrato + * @return false se Amministratore, true se Utente + */ + public static boolean get_access (LoggedUser set_user_logged_here) throws FileNotFoundException,IOException,ClassNotFoundException{ + + System.out.println("---------- Benvenuto ----------"); + Scanner reader = new Scanner(System.in); + + + System.out.println("1 --> Login"); + System.out.println("2 --> Registrazione"); + + String choice; + System.out.print("\nScelta operazione: "); + choice = reader.nextLine(); + for ( ; !(choice.equals("1")) && !(choice.equals("2")) ;){ + System.out.println("Scelta Errata. Riprovare"); + System.out.print("\nScelta operazione: "); + choice = reader.nextLine(); + } + + if (choice.equals("1")){ + System.out.println("---------- Login ----------"); + return login(set_user_logged_here); + } + return register(set_user_logged_here); + } + + /** + * + * @param set_user_logged_here - nome dell'utente loggato o registrato + * @return - Se avviene una registrazione viene restituito il valore true, poiche solo gli utenti possono registrarsi. Se viene effettuato un login, viene restituito il tipo di utente appena loggato. + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + protected static boolean login(LoggedUser set_user_logged_here) throws FileNotFoundException,IOException,ClassNotFoundException{ + + Scanner reader = new Scanner(System.in); + String name; + String password; + + System.out.print("Username: "); + name = reader.nextLine(); + System.out.print("Password: "); + password = reader.nextLine(); + + for (; !(search_username (name,password)) ;){ + System.out.println("Username e/o password non trovati. Vuoi Registrarti ?: Y/N"); + + String choice; + choice = reader.nextLine(); + for ( ; !(choice.equals("Y")) && !(choice.equals("N")) ;){ + System.out.println("Scelta Errata. Riprovare"); + choice = reader.nextLine(); + } + + if (choice.equals("Y")){ + return register(set_user_logged_here); + } + System.out.println("Reinserisci i dati di Login "); + + System.out.print("Username: "); + name = reader.nextLine(); + System.out.print("Password: "); + password = reader.nextLine(); + } + + set_user_logged_here.logged_user = name; + System.out.println("Logged"); + return getAccountType(name); + + } + + /** + * + * @param set_user_logged_here - nome dell'utente loggato o registrato + * @return - true poiche' e' possibile la registrazione solo degli utenti. + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + protected static boolean register ( LoggedUser set_user_logged_here ) throws FileNotFoundException,IOException,ClassNotFoundException{ + + Scanner reader = new Scanner(System.in); + String name; + String password; + String location; + System.out.println("---------- Registrazione ----------"); + System.out.println("Inserisci i dati di Registrazione"); + System.out.print("Username: "); + name = reader.nextLine(); + System.out.print("Password: "); + password = reader.nextLine(); + System.out.print("Location: "); + location = reader.nextLine(); + + + store_data(name,password,location); + System.out.println("Registrato e login effettuato"); + + set_user_logged_here.logged_user = name; + return true; + + } + + /** + * Questo metodo si occupa della ricerca dei dati dell'utente all'interno del "database". + * + * @param in_name - username dell'utente + * @param in_password - password dell'utente + * @return true se la ricerca ha avuto esito, false altrimenti + */ + protected static boolean search_username (String in_name, String in_password) throws FileNotFoundException,IOException,ClassNotFoundException{ + + File f_data = new File("user_db"); + if ( (f_data.exists())){ + ObjectInputStream reader = new ObjectInputStream(new FileInputStream(f_data)); + ArrayList database = (ArrayList) reader.readObject(); + for (int i = 0 ; i < database.size();++i){ + if ( + database.get(i).getUser().equals( in_name) && + database.get(i).getPassword().equals(in_password) + ) + { + reader.close(); + return true; + } + } + reader.close(); + return false; + + }else{ + ArrayList database = new ArrayList(); + database.add(new Entry("admin","admin",false,"UNISA")); + + ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(f_data)); + writer.writeObject(database); + writer.close(); + + ObjectInputStream reader = new ObjectInputStream(new FileInputStream(f_data)); + database = (ArrayList) reader.readObject(); + for (int i = 0 ; i < database.size();++i){ + if ( + database.get(i).getUser().equals( in_name) && + database.get(i).getPassword().equals(in_password) + ) + { + reader.close(); + return true; + } + } + reader.close(); + return false; + } + } + + /** + * Questo metodo si occupa di restituite la tipologia dell'account dell'utente connesso + * + * @param in_name - username utente + * @return tipo di account (admin o Client) + */ + protected static boolean getAccountType(String in_name) throws FileNotFoundException,IOException,ClassNotFoundException{ + + File f_data = new File("user_db"); + if ( (f_data.exists())){ + ObjectInputStream reader = new ObjectInputStream(new FileInputStream(f_data)); + ArrayList database = (ArrayList) reader.readObject(); + for (int i = 0 ; i < database.size();++i){ + if ( database.get(i).getUser().equals( in_name) ) + { + reader.close(); + return database.get(i).getType(); + + } + } + reader.close(); + } + return false; + } + + /** + * Questo metodo si occupa di salvare i dati su disco dell'utente. + * + * @param in_nome - username utente + * @param in_password - password utente + */ + protected static void store_data (String in_nome, String in_password, String in_location) throws FileNotFoundException,IOException,ClassNotFoundException{ + + File f_data = new File("user_db"); + if ( (f_data.exists())){ + //Aggiungi dati al database + ObjectInputStream reader = new ObjectInputStream(new FileInputStream(f_data)); + ArrayList database = (ArrayList) reader.readObject(); + database.add(new Entry(in_nome,in_password,true,in_location)); + reader.close(); + ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(f_data)); + writer.writeObject(database); + writer.close(); + }else{ + //Crea nuovo database e poi memorizza + ArrayList database = new ArrayList(); + database.add(new Entry("admin","admin",false,"UNISA")); + database.add(new Entry(in_nome,in_password,true,in_location)); + + ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(f_data)); + writer.writeObject(database); + writer.close(); + } + } + + /** + * Questo emtodo si occupa di prelevare i dati dell'utente dal disco + * + * @param in_nome - username utente + * @return un Entry contenente tutti i dati dell'utente + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + public static Entry get_user_data (String in_nome)throws FileNotFoundException,IOException,ClassNotFoundException{ + + File f_data = new File("user_db"); + ObjectInputStream reader = new ObjectInputStream(new FileInputStream(f_data)); + ArrayList database = (ArrayList) reader.readObject(); + for (int i = 0 ; i < database.size();++i){ + if ( database.get(i).getUser().equals( in_nome) ) + { + reader.close(); + return database.get(i); + } + } + return null; // Non puoi essere qui o altriemnti il tuo database e' corrotto + } + + /** + * Questo metodo scrive su disco le modifiche apportate agli attributi di un utente. (Ad esempio bilancio incrementato) + * + * @param in_nome - username utente + * @param new_entry - Oggetto di tipo Entry + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + public static void replace_data (String in_nome, Entry new_entry )throws FileNotFoundException,IOException,ClassNotFoundException{ + File f_data = new File("user_db"); + ObjectInputStream reader = new ObjectInputStream(new FileInputStream(f_data)); + ArrayList database = (ArrayList) reader.readObject(); + for (int i = 0 ; i < database.size();++i){ + if ( database.get(i).getUser().equals( in_nome) ) + { + reader.close(); + database.set(i,new_entry); + break; + + } + } + ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(f_data)); + writer.writeObject(database); + writer.close(); + } +} diff --git a/it/unisa/info13d/Login/Entry.java b/it/unisa/info13d/Login/Entry.java new file mode 100644 index 0000000..321dbb8 --- /dev/null +++ b/it/unisa/info13d/Login/Entry.java @@ -0,0 +1,98 @@ +package it.unisa.info13d.Login; + +import java.io.Serializable; +import java.util.ArrayList; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 17/12/13 + * Time: 0.38 + */ +/** + * + * Classe che rappresenta un utente all'interno del "database" su disco. + * + */ +public class Entry implements Serializable{ + + /** + * Il costruttore della classe si occupa di inizializzare i dati dell'utente + * @param in_user - username utente + * @param in_password - password utente + * @param in_type - tipologia di utente + * @param in_location - luogo abitazione utente + */ + public Entry (String in_user, String in_password, boolean in_type, String in_location) { + user = in_user; + password = in_password; + type = in_type; + location = in_location; + storico = new ArrayList(); + } + + /** + * + * @return - username of user + */ + public String getUser() { + return user; + } + + /** + * + * @return - password of user + */ + public String getPassword() { + return password; + } + + /** + * + * @return - il tipo di utente + */ + public boolean getType() { + return type; + } + + /** + * + * @return - il luogo di abitazione dell'utente + */ + public String getLocation() { + return location; + } + + /** + * + * @return - il saldo dell'utente + */ + public double getBalance() { + return balance; + } + + /** + * + * Questo metodo aggiugne credito al saldo dell'utente. + * @param balance - valore della ricarica del saldo + */ + public void setBalance(double balance) { + this.balance += balance; + + } + /** + * + * @return - un ArrayList che rappresenta lo storico degli acquisti dell'utente + */ + public ArrayList getStorico() { + return storico; + } + + private String user; + private String password; + private String location; + private double balance; + private boolean type; + private ArrayList storico; + +} diff --git a/it/unisa/info13d/Login/LoggedUser.java b/it/unisa/info13d/Login/LoggedUser.java new file mode 100644 index 0000000..58c23f5 --- /dev/null +++ b/it/unisa/info13d/Login/LoggedUser.java @@ -0,0 +1,15 @@ +package it.unisa.info13d.Login; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 17/12/13 + * Time: 1.41 + * + * + * Classe che rappresenta l'utente connesso al sistema. + * + */ +public class LoggedUser { + public String logged_user = "null"; +} diff --git a/it/unisa/info13d/Login/LoginData.java b/it/unisa/info13d/Login/LoginData.java new file mode 100644 index 0000000..99b78c8 --- /dev/null +++ b/it/unisa/info13d/Login/LoginData.java @@ -0,0 +1,74 @@ +package it.unisa.info13d.Login; + +import java.io.FileNotFoundException; +import java.io.IOException; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 17/12/13 + * Time: 1.47 + * + * Questa classe si occupa della gestione del login dell'utente e dell'amministratore + * + * + */ +public class LoginData { + + /** + * Il costruttore si occupa di prelevare i dati dell'utente appena loggato. Tipo di account e username + * + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + public LoginData () throws FileNotFoundException,IOException,ClassNotFoundException{ + + LoggedUser logged_user = new LoggedUser(); + boolean AccountType = true; + AccountType = Access.get_access(logged_user); //chiama al form di login e/o registrazione + + type = convert_type (AccountType); + username = logged_user.logged_user; + } + + /** + * Questo metodo si occupa di convertire un valore bolleano nel tipo di account dll'utente connesso + * + * @param value - tipo di account (Admin o Client) ottenuto dalla chiamata alla funzione Access.get_access(LoggedUser set_user_logged_here) + * @return restituisce un valore booleano, che rappresenta il tipo di utente connesso, false=>Admin true=>Client + */ + protected String convert_type (boolean value){ + if (value == false) + return "Admin"; + return "Client"; + } + + /** + * @return restituisce l'username dell'utente connesso + */ + public String getUsername() { + return username; + } + + /** + * @return restituisce il tipo di account associato all'utente connesso + */ + public String getType() { + return type; + } + + /** + * Stampa le informazioni dell'utente connesso (username e tipo di account) + */ + public void getUserDataInfo (){ + + System.out.println("\n***************************************"); + System.out.println("Utente Loggato : " + username); + System.out.println("AccoutType : " + type ); + System.out.println("***************************************\n"); + } + + private String username; + private String type; +} diff --git a/it/unisa/info13d/Login/StoricoItem.java b/it/unisa/info13d/Login/StoricoItem.java new file mode 100644 index 0000000..7316e90 --- /dev/null +++ b/it/unisa/info13d/Login/StoricoItem.java @@ -0,0 +1,58 @@ +package it.unisa.info13d.Login; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 17/12/13 + * Time: 18.27 + */ + +import java.io.Serializable; +import java.util.GregorianCalendar; + +/** + * Questa classe rappresenta un singolo acquisto fatto dall'utente. + */ +public class StoricoItem implements Serializable { + + /** + * + * @param in_description - descrizione articolo acquistato + * @param in_data_acquisto - data dell'acquisto (Generata al momento dell'acquisto) + * @param in_prezzo - prezzo dell'articolo + */ + public StoricoItem (String in_description, GregorianCalendar in_data_acquisto, double in_prezzo){ + description = in_description; + data_acquisto = in_data_acquisto; + prezzo = in_prezzo; + + } + + /** + * + * @return - descrizione deall'articolo acquistato + */ + public String getDescription() { + return description; + } + + /** + * + * @return - la data dell'acquisto dell'oggeto + */ + public GregorianCalendar getData_acquisto() { + return data_acquisto; + } + /** + * + * @return - il prezzo dell'oggetto acquistato + */ + public double getPrezzo() { + return prezzo; + } + + private String description; + private GregorianCalendar data_acquisto; + private double prezzo; + +} diff --git a/it/unisa/info13d/Main.java b/it/unisa/info13d/Main.java new file mode 100644 index 0000000..4a7b5d6 --- /dev/null +++ b/it/unisa/info13d/Main.java @@ -0,0 +1,34 @@ +package it.unisa.info13d; + +import it.unisa.info13d.GestioneCatalogo.Catalogo; +import it.unisa.info13d.Login.Access; +import it.unisa.info13d.Login.LoggedUser; +import it.unisa.info13d.Login.LoginData; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.text.ParseException; + +import it.unisa.info13d.Utility.IdCounter; +import it.unisa.info13d.Utility.ReShow; +/** + * Questa classe rappresenta il Main dell'applicazione dalla quale si avvieranno le procedure per la gestione dell'utente e del catalogo. + * + * @author Simone Argenziano e Giovanni Di Grezia + * + */ +public class Main { + + public static void main(String[] args) throws FileNotFoundException,IOException,ClassNotFoundException, ParseException { + + LoginData login = new LoginData(); + login.getUserDataInfo(); + + IdCounter.restore_counter(); + Catalogo load_catalogo = new Catalogo(); + + ReShow r = new ReShow(); + for (;r.reshow;) + load_catalogo.showMenu(login.getType(),login.getUsername(),r); + } +} diff --git a/it/unisa/info13d/Main2.java b/it/unisa/info13d/Main2.java new file mode 100644 index 0000000..578a095 --- /dev/null +++ b/it/unisa/info13d/Main2.java @@ -0,0 +1,34 @@ +package it.unisa.info13d; + +import it.unisa.info13d.GestioneCatalogo.*; +import it.unisa.info13d.Login.LoginData; +import it.unisa.info13d.Utility.IdCounter; +import it.unisa.info13d.Utility.ReShow; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.text.ParseException; + + + +/** + * Questa classe rappresenta il Main dell'applicazione dalla quale si avvieranno le procedure per la gestione dell'utente e del catalogo. + * + * @author Simone Argenziano e Giovanni Di Grezia + * + */ +public class Main2 { + + public static void main(String[] args) throws FileNotFoundException,IOException,ClassNotFoundException, ParseException { + + LoginData login = new LoginData(); + login.getUserDataInfo(); + + IdCounter.restore_counter(); + Catalogo2 load_catalogo = new Catalogo2(); + + ReShow r = new ReShow(); + for (;r.reshow;) + load_catalogo.showMenu(login.getType(),login.getUsername(),r); + } +} diff --git a/it/unisa/info13d/Utility/IdCounter.java b/it/unisa/info13d/Utility/IdCounter.java new file mode 100644 index 0000000..6f0fd38 --- /dev/null +++ b/it/unisa/info13d/Utility/IdCounter.java @@ -0,0 +1,57 @@ +package it.unisa.info13d.Utility; + +import it.unisa.info13d.Articoli.Global; +import it.unisa.info13d.Login.Entry; + +import java.io.*; +import java.util.ArrayList; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 18/12/13 + * Time: 00:41 + */ + +/** + * + * Classe che rappresenta il contatore generale dell'id di ogni articolo presente in catalogo + * + */ +public class IdCounter { + + /** + * + * Questo metodo salva il contatore dell'id sul disco. + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + public static void save_counter() throws FileNotFoundException,IOException,ClassNotFoundException{ + File f_data = new File("id_counter"); + Integer data = Integer.valueOf(Global.getGeneral_counter()); + ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(f_data)); + writer.writeObject(data); + writer.close(); + } + + + /** + * Questo metodo legge il valore del contatore e lo setta. + * + * @throws FileNotFoundException + * @throws IOException + * @throws ClassNotFoundException + */ + public static void restore_counter() throws FileNotFoundException,IOException,ClassNotFoundException{ + File f_data = new File("id_counter"); + if ( (f_data.exists())){ + ObjectInputStream reader = new ObjectInputStream(new FileInputStream(f_data)); + Integer data = (Integer) reader.readObject(); + Global.setGeneral_counter(data.intValue()); + }else{ + + Global.setGeneral_counter(0); + } + } +} diff --git a/it/unisa/info13d/Utility/ReShow.java b/it/unisa/info13d/Utility/ReShow.java new file mode 100644 index 0000000..4d34eb9 --- /dev/null +++ b/it/unisa/info13d/Utility/ReShow.java @@ -0,0 +1,16 @@ +package it.unisa.info13d.Utility; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 17/12/13 + * Time: 18.06 + */ + +/** + * Envelop di una variabile booleana, necessaria per passare la varabiale reshow in profondita' + * + */ +public class ReShow { + public boolean reshow = true; +}