Access.get_user_data (String username) serve per ottenere un oggetto di tipo Entry e all'interno ci sono idati dell'utente

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)
This commit is contained in:
2013-12-17 19:26:06 +01:00
parent 61feceab1d
commit bcbf35b8d3
5 changed files with 136 additions and 10 deletions

View File

@@ -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<Entry> database = new ArrayList<Entry>();
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<Entry>) 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<Entry> database = (ArrayList<Entry>) 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<Entry> database = new ArrayList<Entry>();
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<Entry> database = (ArrayList<Entry>) 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<Entry> database = (ArrayList<Entry>) 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();
}