Skip to content

Commit

Permalink
phase 3;
Browse files Browse the repository at this point in the history
no,
i am the one who knocks!
  • Loading branch information
moamdavoodi committed Jul 24, 2020
1 parent 1ea9dc8 commit 33e5b11
Show file tree
Hide file tree
Showing 28 changed files with 160 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void goPaymentAction(ActionEvent actionEvent) throws IOException {
String response = in.readUTF();
receiveMessageId.setText(response);
if (response.equals("done successfully")) {
out.writeUTF("get_size " + LoginPanelController.token);
out.writeUTF("get_size " + LoginPanelController.getToken());
out.flush();
int size = Integer.parseInt(in.readUTF());
for (int i = 0; i < size; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package client.graphicView.userRegion.loginPanel;

import server.controller.LoginPageController;
import server.controller.ValidationController;
import server.exception.UsernameExistsException;
import server.exception.UsernameNotExistsException;
import server.exception.WrongPasswordException;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
Expand All @@ -14,57 +10,54 @@
import javafx.scene.media.MediaPlayer;
import server.model.account.Account;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.net.Socket;
import java.net.URL;
import java.util.HashMap;
import java.util.ResourceBundle;

public class LoginPanelController implements Initializable {
// pass this to next scene for adding properties
private static Account loggedInAccount;

public static void setLoggedInAccount(Account loggedInAccount) {
LoginPanelController.loggedInAccount = loggedInAccount;
}

public static Account getLoggedInAccount() {
return loggedInAccount;
}

public static String token;

private static String token;
final ObservableList<String> accountTypesList = FXCollections.observableArrayList("Head Manager",
"Seller",
"Purchaser");
private final int PORT = 9050;
private final String IP = "127.0.0.1";
private DataOutputStream out;
private DataInputStream in;
@FXML
private ComboBox<String> accountTypes = new ComboBox<>();

// check if clicked, then can login
@FXML
private RadioButton agreeButton = new RadioButton();

@FXML
private TextField loginUsernameField = new TextField();

@FXML
private TextField registerUsernameField = new TextField();

@FXML
private PasswordField loginPasswordField = new PasswordField();

@FXML
private PasswordField registerPasswordField = new PasswordField();

@FXML
private Label loginMessageText = new Label();

@FXML
private Label registerMessageText = new Label();

@FXML
private TextField registerEmailField = new TextField();

public static String getToken() {
return token;
}

public static Account getLoggedInAccount() {
return loggedInAccount;
}

public static void setLoggedInAccount(Account loggedInAccount) {
LoginPanelController.loggedInAccount = loggedInAccount;
}

// after hit the buttons, call this
void resetFields() {
registerEmailField.setText("");
Expand Down Expand Up @@ -101,28 +94,14 @@ private void processRegister() {
return;
}
if (accountTypes.getValue() == null) {
registerMessageText.setText("Select server.main.Main type first");
registerMessageText.setText("Select server.Main type first");
return;
}
try {
ValidationController.checkUsernameForRegistration(registerUsernameField.getText());
LoginPanel.window.close();
if (accountTypes.getValue().equals("Head Manager")) {
ManagerInfoSetPanel.display(registerUsernameField.getText(),
registerPasswordField.getText(),
registerEmailField.getText());
} else if (accountTypes.getValue().equals("Seller")) {
SellerInfoSetPanel.display(registerUsernameField.getText(),
registerPasswordField.getText(),
registerEmailField.getText());
} else {
PurchaserInfoSetPanel.display(registerUsernameField.getText(),
registerPasswordField.getText(),
registerEmailField.getText());
}
resetFields();
} catch (UsernameExistsException | IOException exception) {
registerMessageText.setText(exception.getMessage());
out.writeUTF("checkUsernameForRegistration:" + registerUsernameField.getText());
out.flush();
} catch (IOException exception) {
exception.printStackTrace();
}
}

Expand All @@ -135,35 +114,131 @@ private void processLogin() {
loginMessageText.setText("Invalid username");
return;
}
// check password only consist words, numbers and longer than 8
// check password only consist words, numbers and longer than 8
if (!loginPasswordField.getText().matches("\\w{8,}")) {
loginMessageText.setText("Invalid password");
return;
}
try {
LoginPageController.processLogin(loginUsernameField.getText(),
loginPasswordField.getText());
resetFields();
LoginPanel.window.close();
// TODO: goto next scene
} catch (UsernameNotExistsException | WrongPasswordException exception) {
loginMessageText.setText(exception.getMessage());
out.writeUTF("processLogin:" + loginUsernameField.getText() + "," + loginPasswordField.getText());
out.flush();
// TODO: goto next scene
} catch (IOException e) {
e.printStackTrace();
}
}

private void playButtonSound() {
MediaPlayer mediaPlayer = new MediaPlayer(new Media(new File("src/server.main/resources/media/sound/Mouse-Click-00-c-FesliyanStudios.com.mp3").toURI().toString()));
MediaPlayer mediaPlayer = new MediaPlayer(new Media(new File("src/main/resources/media/sound/Mouse-Click-00-c-FesliyanStudios.com.mp3").toURI().toString()));
mediaPlayer.play();
}


public void input() {
while (true) {
String input;
try {
input = in.readUTF();
if (input.startsWith("MainManagerIsRegistered")) {
accountTypesList.remove(0);
accountTypes.setItems(accountTypesList);

} else if (input.startsWith("MainManagerIsNotRegistered")) {
accountTypes.setItems(accountTypesList);

} else if (input.startsWith("checkUsernameForRegistrationFalse")) {
int colonIndex = input.indexOf(":");
String exceptionMessage = input.substring(colonIndex + 1);
Platform.runLater(new Runnable() {
@Override
public void run() {
registerMessageText.setText(exceptionMessage);
}
});

} else if (input.startsWith("checkUsernameForRegistrationTrue")) {
Platform.runLater(new Runnable() {
@Override
public void run() {
processGoToInfoSetPanel();
}
});

} else if (input.startsWith("loginSuccessful:")) {
int colonIndex = input.indexOf(":");
LoginPanelController.token = input.substring(colonIndex + 1);
Platform.runLater(new Runnable() {
@Override
public void run() {
resetFields();
LoginPanel.window.close();
}
});

} else if (input.startsWith("loginUnsuccessful:")) {
int colonIndex = input.indexOf(":");
String exceptionMessage = input.substring(colonIndex + 1);
Platform.runLater(new Runnable() {
@Override
public void run() {
loginMessageText.setText(exceptionMessage);
}
});

}
} catch (IOException e) {
e.printStackTrace();
}

}
}

private void processGoToInfoSetPanel() {
LoginPanel.window.close();
try {
if (accountTypes.getValue().equals("Head Manager")) {
ManagerInfoSetPanel.display(registerUsernameField.getText(),
registerPasswordField.getText(),
registerEmailField.getText());

} else if (accountTypes.getValue().equals("Seller")) {
SellerInfoSetPanel.display(registerUsernameField.getText(),
registerPasswordField.getText(),
registerEmailField.getText());

} else {
PurchaserInfoSetPanel.display(registerUsernameField.getText(),
registerPasswordField.getText(),
registerEmailField.getText());

}
} catch (IOException e) {
e.printStackTrace();
}
resetFields();
}

public void processInitialize() {
try {
Socket socket = new Socket(IP, PORT);
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
new Thread(this::input).start();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {

processInitialize();
loggedInAccount = null;
// manager can register only once
if (LoginPageController.isIsMainManagerRegistered()) {
accountTypesList.remove(0);
try {
out.writeUTF("IsMainManagerRegistered");
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
accountTypes.setItems(accountTypesList);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class ManagerAccountPageController implements Initializable {

@FXML
private void logout() throws IOException {
out.writeUTF("logout " + LoginPanelController.token);
out.writeUTF("logout " + LoginPanelController.getToken());
out.flush();
LoginPanelController.setLoggedInAccount(null);
ManagerAccountPage.primaryStage.close();
Expand All @@ -96,7 +96,7 @@ public void processWriteInformation(String firstName, String lastName, String us

public void writeInformationForManager() {
try {
out.writeUTF("get_information " + LoginPanelController.token);
out.writeUTF("get_information " + LoginPanelController.getToken());
out.flush();
} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void applyChanges() {
@FXML
public void logout() throws IOException {
ManagerEditInfoPage.primaryStage.close();
out.writeUTF("logout " + LoginPanelController.token);
out.writeUTF("logout " + LoginPanelController.getToken());
out.flush();
LoginPanelController.setLoggedInAccount(null);
MainMenu.display(Main.window);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void goPaymentAction() throws IOException {
String response = in.readUTF();
receiveMessageId.setText(response);
if (response.equals("done successfully")) {
out.writeUTF("set_balance " + amountId.getText() + " " + LoginPanelController.token);
out.writeUTF("set_balance " + amountId.getText() + " " + LoginPanelController.getToken());
out.flush();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void back() throws IOException {

@FXML
public void logout() throws IOException {
out.writeUTF("logout " + LoginPanelController.token);
out.writeUTF("logout " + LoginPanelController.getToken());
out.flush();
LoginPanelController.setLoggedInAccount(null);
AllProductsForAuctionPage.primaryStage.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
process();
String balance = null;
try {
out.writeUTF("get_balance " + LoginPanelController.token);
out.writeUTF("get_balance " + LoginPanelController.getToken());
out.flush();
balance = in.readUTF();
} catch (IOException e) {
Expand All @@ -70,7 +70,7 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
public void receiveAction() throws IOException {
String balance = null;
try {
out.writeUTF("get_balance " + LoginPanelController.token);
out.writeUTF("get_balance " + LoginPanelController.getToken());
out.flush();
balance = in.readUTF();
} catch (IOException e) {
Expand All @@ -89,7 +89,7 @@ public void receiveAction() throws IOException {
String respond = in.readUTF();
receiveMessageId.setText(respond);
if (respond.equals("done successfully")) {
out.writeUTF("set_balance " + amountId.getText() + " " + LoginPanelController.token);
out.writeUTF("set_balance " + amountId.getText() + " " + LoginPanelController.getToken());
out.flush();
String amount = in.readUTF();
creditId.setText("Credit " + amount + " $");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
opens client.graphicView.userRegion.userAccount.managerAccount to javafx.fxml;
opens client.graphicView.userRegion.userAccount.sellerAccount to javafx.fxml;
opens client.graphicView.userRegion.userAccount.purchaserAccount to javafx.fxml;
opens server to javafx.graphics;
// opens server to javafx.graphics;
}
Loading

0 comments on commit 33e5b11

Please sign in to comment.