Sistema di Login teoricamente completo. Nel main la classe LoginData si occupa di fare tutto. Per richiamare chi è loggato basta chiamare il metodo .getUserName. Per richiamre il tipo di account .getType. Per stamapre le info di chi è loggato .getUserDataInfo
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package it.unisa.info13d.Login;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
@@ -16,7 +17,7 @@ public class Access {
|
||||
* @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 (String set_user_logged_here){
|
||||
public static boolean get_access (LoggedUser set_user_logged_here) throws FileNotFoundException,IOException,ClassNotFoundException{
|
||||
|
||||
System.out.println("Benvenuto");
|
||||
Scanner reader = new Scanner(System.in);
|
||||
@@ -40,7 +41,7 @@ public class Access {
|
||||
|
||||
|
||||
|
||||
protected static boolean login(String 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;
|
||||
@@ -52,7 +53,7 @@ public class Access {
|
||||
password = reader.nextLine();
|
||||
|
||||
for (; !(search_username (name,password)) ;){
|
||||
System.out.println("Dati non trovati. Vuoi Registrarti ?: Y/N");
|
||||
System.out.println("Username e/o password non trovati. Vuoi Registrarti ?: Y/N");
|
||||
|
||||
String choice;
|
||||
choice = reader.nextLine();
|
||||
@@ -72,12 +73,13 @@ public class Access {
|
||||
password = reader.nextLine();
|
||||
}
|
||||
|
||||
set_user_logged_here = name;
|
||||
set_user_logged_here.logged_user = name;
|
||||
System.out.println("Logged");
|
||||
return getAccountType(name);
|
||||
|
||||
}
|
||||
|
||||
protected static boolean register( String set_user_logged_here ){
|
||||
protected static boolean register ( LoggedUser set_user_logged_here ) throws FileNotFoundException,IOException,ClassNotFoundException{
|
||||
|
||||
Scanner reader = new Scanner(System.in);
|
||||
String name;
|
||||
@@ -92,8 +94,8 @@ public class Access {
|
||||
store_data(name,password);
|
||||
System.out.println("Registered and Logged");
|
||||
|
||||
set_user_logged_here = name;
|
||||
return false;
|
||||
set_user_logged_here.logged_user = name;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
@@ -103,11 +105,28 @@ public class Access {
|
||||
* @param in_password
|
||||
* @return true if math found, false elsewhere
|
||||
*/
|
||||
protected static boolean search_username (String in_name, String in_password){
|
||||
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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,9 +134,24 @@ public class Access {
|
||||
* @param in_name
|
||||
* @return type of account (admin or user)
|
||||
*/
|
||||
protected static boolean getAccountType(String in_name){
|
||||
protected static boolean getAccountType(String in_name) throws FileNotFoundException,IOException,ClassNotFoundException{
|
||||
|
||||
return true;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,9 +160,31 @@ public class Access {
|
||||
* @param in_nome
|
||||
* @param in_password
|
||||
*/
|
||||
protected static void store_data (String in_nome, String in_password){
|
||||
protected static void store_data (String in_nome, String in_password) 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));
|
||||
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));
|
||||
database.add(new Entry(in_nome,in_password,true));
|
||||
|
||||
ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(f_data));
|
||||
writer.writeObject(database);
|
||||
writer.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
35
it/unisa/info13d/Login/Entry.java
Normal file
35
it/unisa/info13d/Login/Entry.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package it.unisa.info13d.Login;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 17/12/13
|
||||
* Time: 0.38
|
||||
*/
|
||||
public class Entry implements Serializable{
|
||||
|
||||
public Entry (String in_user, String in_password, boolean in_type) {
|
||||
user = in_user;
|
||||
password = in_password;
|
||||
type = in_type;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public boolean getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
private String user;
|
||||
private String password;
|
||||
private boolean type;
|
||||
|
||||
}
|
||||
11
it/unisa/info13d/Login/LoggedUser.java
Normal file
11
it/unisa/info13d/Login/LoggedUser.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package it.unisa.info13d.Login;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 17/12/13
|
||||
* Time: 1.41
|
||||
*/
|
||||
public class LoggedUser {
|
||||
public String logged_user = "null";
|
||||
}
|
||||
49
it/unisa/info13d/Login/LoginData.java
Normal file
49
it/unisa/info13d/Login/LoginData.java
Normal file
@@ -0,0 +1,49 @@
|
||||
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
|
||||
*/
|
||||
public class LoginData {
|
||||
|
||||
public LoginData () throws FileNotFoundException,IOException,ClassNotFoundException{
|
||||
|
||||
LoggedUser logged_user = new LoggedUser();
|
||||
boolean AccountType = true;
|
||||
AccountType = Access.get_access(logged_user);
|
||||
|
||||
type = convert_type (AccountType);
|
||||
username = logged_user.logged_user;
|
||||
|
||||
}
|
||||
|
||||
protected String convert_type (boolean value){
|
||||
if (value == false)
|
||||
return "Admin";
|
||||
return "Client";
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void getUserDataInfo (){
|
||||
|
||||
System.out.println("***************************************");
|
||||
System.out.println("Utente Loggato : " + username);
|
||||
System.out.println("AccoutType : " + type );
|
||||
System.out.println("***************************************");
|
||||
}
|
||||
|
||||
private String username;
|
||||
private String type;
|
||||
}
|
||||
@@ -1,13 +1,19 @@
|
||||
package it.unisa.info13d;
|
||||
|
||||
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;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws FileNotFoundException,IOException,ClassNotFoundException {
|
||||
|
||||
LoginData login = new LoginData();
|
||||
login.getUserDataInfo();
|
||||
|
||||
String logged_user = "null";
|
||||
Access.get_access(logged_user);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user