From bcbf35b8d38d81f05e65002754a3651ab85e7dea Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Tue, 17 Dec 2013 19:26:06 +0100 Subject: [PATCH] Access.get_user_data (String username) serve per ottenere un oggetto di tipo Entry e all'interno ci sono idati dell'utente MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Access.replace_data(String username, Entry new_data) serve per scrivere nel database i nuovi dati dellutente dopo aver modificato loggetto Entry ottenuto con il metodo precedente. Per settare un nuovo bilancio basta invocare il metodo setBalance(double value) sull'oggetto Entry ottenuto. Per settare un nuovo acquisto fatto dell'utente bisogna prima chiamare getStorico() sull'oggetto entry che restituira un arraylist ed in seguito eseguire un add sull'arraylist per aggiungere un nuovo oggetto di tipo StoricoItem (questo conterrĂ  la descrizione, il prezzo al momento dell'acquisto e la data) --- .../GestioneCatalogo/AdminSession.java | 1 - it/unisa/info13d/Login/Access.java | 77 +++++++++++++++++-- it/unisa/info13d/Login/Entry.java | 24 +++++- it/unisa/info13d/Login/StoricoItem.java | 42 ++++++++++ it/unisa/info13d/Main.java | 2 +- 5 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 it/unisa/info13d/Login/StoricoItem.java diff --git a/it/unisa/info13d/GestioneCatalogo/AdminSession.java b/it/unisa/info13d/GestioneCatalogo/AdminSession.java index bdcc893..ad58a62 100644 --- a/it/unisa/info13d/GestioneCatalogo/AdminSession.java +++ b/it/unisa/info13d/GestioneCatalogo/AdminSession.java @@ -40,7 +40,6 @@ public class AdminSession { { case "1": catalogo.nuovoProdotto(); - break; case "2": catalogo.cancellaProdotto(); diff --git a/it/unisa/info13d/Login/Access.java b/it/unisa/info13d/Login/Access.java index 604f464..a566a1a 100644 --- a/it/unisa/info13d/Login/Access.java +++ b/it/unisa/info13d/Login/Access.java @@ -84,14 +84,18 @@ public class Access { Scanner reader = new Scanner(System.in); String name; String password; + String location; 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); + + store_data(name,password,location); System.out.println("Registered and Logged"); set_user_logged_here.logged_user = name; @@ -123,10 +127,33 @@ public class Access { } } 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; + } + } /** @@ -160,14 +187,14 @@ public class Access { * @param in_nome * @param in_password */ - protected static void store_data (String in_nome, String in_password) throws FileNotFoundException,IOException,ClassNotFoundException{ + 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)); + database.add(new Entry(in_nome,in_password,true,in_location)); reader.close(); ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(f_data)); writer.writeObject(database); @@ -177,8 +204,8 @@ public class Access { //crea nuovo database e poi memorizza ArrayList database = new ArrayList(); - database.add(new Entry("admin","admin",false)); - database.add(new Entry(in_nome,in_password,true)); + 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); @@ -189,6 +216,42 @@ public class Access { } + 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; // You should not be here or your database is corrupted + } + + + 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 index 5a9b858..ce77b81 100644 --- a/it/unisa/info13d/Login/Entry.java +++ b/it/unisa/info13d/Login/Entry.java @@ -1,6 +1,7 @@ package it.unisa.info13d.Login; import java.io.Serializable; +import java.util.ArrayList; /** * Created with MONSTER. @@ -10,10 +11,12 @@ import java.io.Serializable; */ public class Entry implements Serializable{ - public Entry (String in_user, String in_password, boolean in_type) { + 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(); } public String getUser() { @@ -28,8 +31,27 @@ public class Entry implements Serializable{ return type; } + public String getLocation() { + return location; + } + + public double getBalance() { + return balance; + } + + public void setBalance(double balance) { + this.balance = balance; + } + + public ArrayList getStorico() { + return storico; + } + private String user; private String password; + private String location; + private double balance = 0; private boolean type; + private ArrayList storico; } diff --git a/it/unisa/info13d/Login/StoricoItem.java b/it/unisa/info13d/Login/StoricoItem.java new file mode 100644 index 0000000..e2af91b --- /dev/null +++ b/it/unisa/info13d/Login/StoricoItem.java @@ -0,0 +1,42 @@ +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 { + + public StoricoItem (String in_description, GregorianCalendar in_data_acquisto, double in_prezzo){ + description = in_description; + data_acquisto = in_data_acquisto; + prezzo = in_prezzo; + + } + + + public String getDescription() { + return description; + } + + public GregorianCalendar getData_acquisto() { + return data_acquisto; + } + + 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 index 62aad84..47f5166 100644 --- a/it/unisa/info13d/Main.java +++ b/it/unisa/info13d/Main.java @@ -21,7 +21,7 @@ public class Main { ReShow r = new ReShow(); for (;r.reshow;) - load_catalogo.showMenu(login.getType(),r); + load_catalogo.showMenu(login.getType(),r); }