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:
@@ -40,7 +40,6 @@ public class AdminSession {
|
||||
{
|
||||
case "1":
|
||||
catalogo.nuovoProdotto();
|
||||
|
||||
break;
|
||||
case "2":
|
||||
catalogo.cancellaProdotto();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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<StoricoItem>();
|
||||
}
|
||||
|
||||
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<StoricoItem> getStorico() {
|
||||
return storico;
|
||||
}
|
||||
|
||||
private String user;
|
||||
private String password;
|
||||
private String location;
|
||||
private double balance = 0;
|
||||
private boolean type;
|
||||
private ArrayList<StoricoItem> storico;
|
||||
|
||||
}
|
||||
|
||||
42
it/unisa/info13d/Login/StoricoItem.java
Normal file
42
it/unisa/info13d/Login/StoricoItem.java
Normal file
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user