Files
project-unisa-for-shop-mana…/it/unisa/info13d/Login/Access.java
Simone Argenziano 9f73adac65 Completati tutti i metodi per il Cliente e l'admin.
-Resta da implementare esclusivvaemnte l'ordinamento per datascadenza e id, e la condizione delle cene nella zona dell'utente.
2013-12-18 17:09:37 +01:00

255 lines
8.3 KiB
Java

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 pemette di capire se fare un Login o una Registrazione
*
* @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;
choice = reader.nextLine();
for ( ; !(choice.equals("1")) && !(choice.equals("2")) ;){
System.out.println("Scelta Errata. Riprovare");
choice = reader.nextLine();
}
if (choice.equals("1")){
return login(set_user_logged_here);
}
return register(set_user_logged_here);
}
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);
}
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("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("Registered and Logged");
set_user_logged_here.logged_user = name;
return true;
}
/**
*
* @param in_name
* @param in_password
* @return true if math found, false elsewhere
*/
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<Entry> 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;
}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;
}
}
/**
*
* @param in_name
* @return type of account (admin or user)
*/
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<Entry> database = (ArrayList<Entry>) 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;
}
/**
* save data to user_db
*
* @param in_nome
* @param in_password
*/
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,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<Entry> database = new ArrayList<Entry>();
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();
}
}
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();
}
}