You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.1 KiB

package model;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by Gondr on 1/06/2017.
*/
public class User {
protected int studentNumber;
protected String email;
protected String password;
protected String address;
protected String homephone;
protected String mobilephone;
protected AccountType accountType;
transient protected Image photo;
private byte[] image;
protected String ID;
protected transient ArrayList<SharedTrip> bookings;
public enum AccountType{
PASSENGER("Passenger"), DRIVER("Driver");
public String name;
AccountType(String name){
this.name = name;
}
public static AccountType getValueOf(String s){
switch(s){
case "Passenger":
return PASSENGER;
case "Driver":
return DRIVER;
default:
return null;
}
}
}
public User(int studentNumber, String email, String password, String address, String homephone, String mobilephone,
Image photo){
this.studentNumber = studentNumber;
this.ID = String.valueOf(studentNumber);
this.email = email;
this.password = password;
this.address = address;
this.homephone = homephone;
this.mobilephone = mobilephone;
this.photo = photo;
this.accountType = AccountType.PASSENGER;
bookings = new ArrayList<>();
}
public boolean checkPassword(String p){
return p.equals(password);
}
public int getStudentNumber() {
return studentNumber;
}
public String getID(){
return ID;
}
public void book(SharedTrip trip){
bookings.add(trip);
}
public ArrayList<SharedTrip> getBookings() {
return bookings;
}
public void serialise(){
try {
BufferedImage bi = SwingFXUtils.fromFXImage(photo, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bi, "png", out);
out.flush();
image = out.toByteArray();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void populateBookings(){
bookings = new ArrayList<>();
for (SharedTrip s: Session.session.getDataManager().getAllSharedTrips()){
for (User u: s.userBookings){
if (u != null) {
if (u.getStudentNumber() == this.studentNumber) {
this.bookings.add(s);
}
}
}
}
}
public void deserialise(){
ByteArrayInputStream in = new ByteArrayInputStream(image);
BufferedImage bi = null;
try {
bi = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
photo = SwingFXUtils.toFXImage(bi, null);
}
}