import
import
import
import
java.io.BufferedReader;
java.io.RandomAccessFile;
java.io.IOException;
java.io.InputStreamReader;
public class EsercizioP7 {
public
public
public
public
final
final
final
final
static
static
static
static
String
String
String
String
USER = "pippo";
PWD = "topolino";
FILE = "rubrica.tsv";
DELIM = "-";
public static void main(String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
UserLogin ulAllow = new UserLogin(USER, PWD);
UserLogin ul = new UserLogin(br.readLine(), DELIM);
ul.checkAccount(ulAllow);
DataBaseFile dbf = new DataBaseFile(FILE);
boolean fine = false;
do{
System.out.println("w: write, r: read, e: exit \t");
char choice = br.readLine().toCharArray()[0];
switch (choice) {
case 'r': System.out.println("Account ID: "); dbf.query(0,br.readLine());
break;
case 'w': System.out.println("Account (id - nome - cognome - via - città
- telefono fisso - telefono cellulare): "); dbf.write(br.readLine().split(DELIM)); break;
case 'e': dbf.close(); fine = true; break;
default: break;
}
} while (!fine);
} catch (UserFormatException ufe) {
ufe.getMessage();
} catch (WrongPasswordException wpe) {
wpe.getMessage();
} catch (UserNotFoundException unfe) {
unfe.getMessage();
} catch (IOException ioe) {
ioe.getMessage();
}
}
}
import
import
import
import
java.io.File;
java.io.FileNotFoundException;
java.io.IOException;
java.io.RandomAccessFile;
public class DataBaseFile {
private RandomAccessFile raf;
public DataBaseFile(File f) throws FileNotFoundException {
raf = new RandomAccessFile(f,"rw");
}
public DataBaseFile(String f) throws FileNotFoundException {
raf = new RandomAccessFile(f,"rw");
}
public String[] query(int pos, String value) throws IOException {
String record;
raf.seek(0);
while ((record = raf.readLine()) != null) {
if (record.split("\t")[pos].trim().equals(value)) {
return record.split("\t");
}
}
return null;
}
public void write(String[] record) throws IOException {
raf.seek(raf.length());
for (int i = 0; i < record.length - 1; i++) {
raf.write((record[i].trim() + "\t").getBytes());
}
raf.write((record[record.length].trim()+"\r\n").getBytes());
}
public void close() throws IOException {
raf.close();
}
}
public class UserLogin {
String user, pwd;
public UserLogin(String userpwd, String delim) throws UserFormatException {
String[] up;
if ((up = userpwd.split(delim)).length != 2)
throw new UserFormatException();
this.user = up[0];
this.pwd = up[1];
}
public boolean checkAccount(UserLogin ul) throws UserNotFoundException, WrongPasswordException {
if (!(ul.getUser().equals(this.user)))
throw new UserNotFoundException();
if (!(ul.getPwd().equals(this.pwd)))
throw new WrongPasswordException();
return true;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public boolean equals(UserLogin ul) {
return(ul.getUser().equals(this.user) && ul.getPwd().equals(this.pwd));
}
}
public class UserFormatException extends Exception {
private static final long serialVersionUID = 1L;
public UserFormatException() {
}
public UserFormatException(String arg0) {
super(arg0);
}
public UserFormatException(Throwable arg0) {
super(arg0);
}
public UserFormatException(String arg0, Throwable arg1) {
super(arg0, arg1);
}
}
public class UserNotFoundException extends Exception {
private static final long serialVersionUID = 1L;
public UserNotFoundException() {
}
public UserNotFoundException(String arg0) {
super(arg0);
}
public UserNotFoundException(Throwable arg0) {
super(arg0);
}
public UserNotFoundException(String arg0, Throwable arg1) {
super(arg0, arg1);
}
}
public class WrongPasswordException extends Exception {
private static final long serialVersionUID = -6421527090757575027L;
public WrongPasswordException() {
}
public WrongPasswordException(String message) {
super(message);
}
public WrongPasswordException(Throwable cause) {
super(cause);
}
public WrongPasswordException(String message, Throwable cause) {
super(message, cause);
}
}