Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/GUI/MenuHandler.java
  • Loading branch information
MJavadHzr committed Jul 24, 2020
2 parents c7560ff + eecf413 commit 94f12a3
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 58 deletions.
6 changes: 3 additions & 3 deletions src/main/java/Bank/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class Account implements Serializable {
public static long sumOfCredits = 0;
private static ArrayList<Account> allAccounts = new ArrayList<>();
private static String bossUsername = "BOSS";
private static String bossPassword = "manuel neuer is the best";
private static Account bossAccount = new Account(bossUsername, bossPassword, "first name", "second name");
private static String bossPassword = "manuel";
public static Account bossAccount = new Account(bossUsername, bossPassword, "first name", "second name");
private String firstName;
private String secondName;
private String username;
Expand All @@ -24,7 +24,7 @@ public Account(String username, String password, String firstName, String second
}

public long getBalance() {
return this == bossAccount ? sumOfCredits + balance : this.balance;
return this == bossAccount ? this.balance : this.balance;
}

public String getUsername() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/Bank/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ private void pay(String command) {
String token = command.split("\\+")[1];
int id = Integer.parseInt(command.split("\\+")[2]);
String username = command.split("\\+")[3];
String role = command.split("\\+")[4];
if (tokenIsWrong(token)) {
serverAnswer = "token isn't authentic";
return;
Expand All @@ -323,7 +324,7 @@ private void pay(String command) {
serverAnswer = "it's already done";
return;
}
serverAnswer = transaction.Do();
serverAnswer = transaction.Do(role);
} catch (Exception e) {
serverAnswer = "something went wrong";
}
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/Bank/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,37 @@ public static ArrayList<String> getAllTransactionsInvolvingUsername(String usern
return arrayList;
}

public String withdraw(String username, long amount) {
public String withdraw(String username, long amount, String role) {
Account account = Account.getAccountWithUsername(username);
if (account == null) {
return "invalid username";
}
if (account.getBalance() > amount) {
account.setBalance(account.getBalance() - amount);
Account.bossAccount.setBalance(Account.bossAccount.getBalance() + amount);
this.isDone = true;
Account.sumOfCredits += amount;
return "successful";
}
return "not enough credit";
}

public String deposit(String username, long amount) {
public String deposit(String username, long amount, String role) {
Account account = Account.getAccountWithUsername(username);
if (account == null) {
return "invalid username";
}
account.setBalance(account.getBalance() + amount);
this.isDone = true;
return "successful";

if (role.equalsIgnoreCase("customer")) {
account.setBalance(account.getBalance() + amount);
this.isDone = true;
return "successful";
} else if (role.equalsIgnoreCase("salesman")) {
account.setBalance(account.getBalance() + amount);
Account.bossAccount.setBalance(Account.bossAccount.getBalance() - amount);
this.isDone = true;
return "successful";
}
return null;
}

public String transfer(String fromUsername, String toUsername, long amount) {
Expand All @@ -119,13 +127,13 @@ public boolean isDone() {
return isDone;
}

public String Do() {
public String Do(String role) {
if (transactionType.equals(TransactionType.TRANSFER)) {
return transfer(fromUsername, toUsername, amount);
} else if (transactionType.equals(TransactionType.WITHDRAW)) {
return withdraw(fromUsername, amount);
return withdraw(fromUsername, amount, role);
} else if (transactionType.equals(TransactionType.DEPOSIT)) {
return deposit(fromUsername, amount);
return deposit(fromUsername, amount, role);
}
return "unsuccessful";
}
Expand All @@ -146,4 +154,5 @@ public long getAmount() {
public static ArrayList<Transaction> getAllTransaction() {
return allTransaction;
}

}
39 changes: 25 additions & 14 deletions src/main/java/Controller/Security/Security.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;

public class Security {

Expand All @@ -24,6 +25,7 @@ public static boolean checkStringLength(String command) {
}

private static ArrayList<String> blackListOfIPs = new ArrayList<>();
private static HashSet<String> ips = new HashSet<>();

public static boolean mayContainScript(String command) {
return command.contains("<") || command.contains(">") || command.contains("\\") || command.contains("/");
Expand All @@ -47,9 +49,6 @@ public String decode(String string) {

public static void securityCheck(String command, Socket socket) throws ParseException {

// System.out.println(command);
System.out.println(blackListOfIPs);

if (blackListOfIPs.contains(getIP(socket))) {
return;
}
Expand Down Expand Up @@ -126,19 +125,22 @@ public static void securityCheck(String command, Socket socket) throws ParseExce
}

// making sure it's got one ip

Account account = Storage.getAccountWithUsername(username);
assert account != null;
if (account.getIp() == null) {
account.setIp(getIP(socket));
} else {
if (!account.getIp().equals(getIP(socket))) {
System.out.println("we're under attack by wrong ip");
blackListOfIPs.add(getIP(socket));
return;

try {
Account account = Storage.getAccountWithUsername(username);
assert account != null;
if (account.getIp() == null) {
account.setIp(getIP(socket));
} else {
if (!account.getIp().equals(getIP(socket))) {
System.out.println("we're under attack by wrong ip");
blackListOfIPs.add(getIP(socket));
return;
}
}
} catch (Exception e) {
System.out.println("the error: " + e.getMessage());
}

//checking that it's still authentic

if (!Token.hasTokenExpired(token)) {
Expand Down Expand Up @@ -170,4 +172,13 @@ public static boolean isInBlackList(Socket socket) {
return blackListOfIPs.contains(getIP(socket));
}

public static void addToSetOfIps(String ip) {
ips.add(ip);
}

public static boolean weReachedTheMax() {
ips.removeAll(blackListOfIPs);
return ips.size() > (1989 / 23) / 7;
}

}
5 changes: 5 additions & 0 deletions src/main/java/Controller/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public void run() throws IOException {
continue;
}

if (Security.weReachedTheMax()) {
continue;
}

System.out.println("client accepted");
allClientSockets.add(clientSocket);
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
Expand All @@ -103,6 +107,7 @@ public ClientHandler(Server server, Socket clientSocket, DataInputStream dataInp
this.clientSocket = clientSocket;
this.dataInputStream = dataInputStream;
this.dataOutputStream = dataOutputStream;
Security.addToSetOfIps(Security.getIP(clientSocket));
}

@Override
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/GUI/Bank/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ public class Bank {
private static String password;
public Label creditLabel;
public static Bank bank;
private String username = MenuHandler.getRole().equalsIgnoreCase("boss") ? "BOSS" : MenuHandler.getUsername();

public void back(ActionEvent actionEvent) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/GUI/Bank/LogOrRegister.fxml"));
Stage stage = (Stage) ((Button) actionEvent.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
}

private void checkExpired(ActionEvent actionEvent) throws IOException, ParseException {
private void checkExpired(ActionEvent actionEvent) throws IOException {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "", ButtonType.OK);
MenuHandler.getConnector().clientToServer("bank " + "get all receipts by me+" + Bank.getToken() + "+" + MenuHandler.getUsername());
MenuHandler.getConnector().clientToServer("bank " + "get all receipts by me+" + Bank.getToken() + "+" + username);
String answer = MenuHandler.getConnector().serverToClient();
if (answer.equals("token isn't authentic") || answer.equals("something went wrong") || answer.contains("expired")) {
alert.setContentText("you token is expired, you may wanna login again");
Expand Down Expand Up @@ -85,7 +86,7 @@ public void updateCredit() throws IOException, ParseException {
if (MenuHandler.getRole().equalsIgnoreCase("boss")) {
MenuHandler.getConnector().clientToServer("bank " + "get balance+" + Bank.getToken() + "+" + "BOSS");
} else {
MenuHandler.getConnector().clientToServer("bank " + "get balance+" + Bank.getToken() + "+" + MenuHandler.getUsername());
MenuHandler.getConnector().clientToServer("bank " + "get balance+" + Bank.getToken() + "+" + username);
}
String credit = MenuHandler.getConnector().serverToClient();
if (credit.equals("token has expired")) {
Expand All @@ -98,9 +99,9 @@ public void updateCredit() throws IOException, ParseException {
creditLabel.setText("oops .... ");
}

public static boolean isPossibleToDepositForSalesman(long amount) throws IOException, ParseException {
public static boolean isPossibleToDepositForSalesman(long amount) throws IOException {
if (!MenuHandler.getRole().equalsIgnoreCase("salesman")) {
return false;
return true;
}
long credit = MenuHandler.getCredit();
return credit - amount >= MenuHandler.getMinCredit();
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/GUI/Bank/LogOrRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class LogOrRegister {
public PasswordField createAccountConfirmation;
private String username;

public void login(ActionEvent actionEvent) throws ParseException, IOException {
public void login(ActionEvent actionEvent) throws IOException {
Alert alert = new Alert(Alert.AlertType.WARNING, "", ButtonType.OK);

//there's no user logged
Expand All @@ -35,7 +35,7 @@ public void login(ActionEvent actionEvent) throws ParseException, IOException {
MenuHandler.getConnector().clientToServer("bank " + "get token+" + "BOSS" + "+" + loginPassword.getText());
String token = MenuHandler.getConnector().serverToClient();
System.out.println("this is the token " + token);
if (!token.equals("fuck off, identification was wrong") && !token.equals("something went wrong") ) {
if (!token.equals("fuck off, identification was wrong") && !token.equals("something went wrong")) {
Bank.setToken(token);
alert.setContentText("login successful");
alert.showAndWait();
Expand Down Expand Up @@ -75,7 +75,7 @@ public void login(ActionEvent actionEvent) throws ParseException, IOException {

}

public void createAccount(ActionEvent actionEvent) throws ParseException, IOException {
public void createAccount(ActionEvent actionEvent) throws IOException {
Alert alert = new Alert(Alert.AlertType.WARNING, "", ButtonType.OK);

//there's no user logged
Expand All @@ -86,12 +86,6 @@ public void createAccount(ActionEvent actionEvent) throws ParseException, IOExce
return;
}

if (!createAccountPassword.getText().equals(createAccountConfirmation.getText())) {
alert.setContentText("the password and confirmation aren't the same");
alert.showAndWait();
return;
}

if (MenuHandler.getRole().equalsIgnoreCase("boss")) {
alert.setContentText("as a boss you don't need to register, used the store bank password to login");
alert.showAndWait();
Expand All @@ -109,13 +103,23 @@ public void createAccount(ActionEvent actionEvent) throws ParseException, IOExce
return;
}

if (!createAccountPassword.getText().equals(createAccountConfirmation.getText())) {
alert.setContentText("the password and confirmation aren't the same");
alert.showAndWait();
return;
}


MenuHandler.getConnector().clientToServer("bank " + "create account+" + username + "+" + createAccountPassword.getText() + "+first name+second name");

if (MenuHandler.getConnector().serverToClient().equals("created successfully")) {
alert.setContentText("created successfully");
alert.showAndWait();
createAccountConfirmation.setText("");
createAccountPassword.setText("");
return;
}

alert.setContentText("something went wrong, try again");
alert.showAndWait();
}
Expand Down
44 changes: 27 additions & 17 deletions src/main/java/GUI/Bank/Pane/ManageReceipt.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ public class ManageReceipt {

public void initialize() throws IOException, ParseException {
username = MenuHandler.getRole().equalsIgnoreCase("boss") ? "BOSS" : MenuHandler.getUsername();
System.out.println("here it issssssssssssssssss: " + username);
updateBoxOne();
updateBoxTwo();
}

private void updateBoxOne() throws ParseException, IOException {
private void updateBoxOne() throws IOException {
box1.getChildren().clear();
MenuHandler.getConnector().clientToServer("bank " + "get all receipts by me+" + Bank.getToken() + "+" + MenuHandler.getUsername());
MenuHandler.getConnector().clientToServer("bank " + "get all receipts by me+" + Bank.getToken() + "+" + username);
String answer = MenuHandler.getConnector().serverToClient();
if (answer.equals("token isn't authentic") || answer.equals("something went wrong")) {
//logout
Expand All @@ -51,9 +52,9 @@ private void updateBoxOne() throws ParseException, IOException {
}
}

private void updateBoxTwo() throws ParseException, IOException {
private void updateBoxTwo() throws IOException {
box2.getChildren().clear();
MenuHandler.getConnector().clientToServer("bank " + "get all receipts involving me+" + Bank.getToken() + "+" + MenuHandler.getUsername());
MenuHandler.getConnector().clientToServer("bank " + "get all receipts involving me+" + Bank.getToken() + "+" + username);
String answer = MenuHandler.getConnector().serverToClient();
if (answer.equals("token isn't authentic") || answer.equals("something went wrong")) {
//logout
Expand All @@ -77,8 +78,29 @@ private void updateBoxTwo() throws ParseException, IOException {
}

public void done(ActionEvent actionEvent) throws ParseException, IOException {
MenuHandler.getConnector().clientToServer("bank " + "pay transaction with id+" + Bank.getToken() + "+" + receiptID.getText() + "+" + username);

try {
MenuHandler.getConnector().clientToServer("bank " + "get amount of transaction+" + Bank.getToken() + "+" + receiptID.getText());
long ID = Long.parseLong(receiptID.getText());
long amount = Long.parseLong(MenuHandler.getConnector().serverToClient());
if (200000 > ID && ID >= 100000) {
if (MenuHandler.getRole().equalsIgnoreCase("salesman")) {
long credit = MenuHandler.getCredit();
if (credit - amount <= MenuHandler.getMinCredit()) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "you should keep the min credit", ButtonType.OK);
alert.showAndWait();
return;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

MenuHandler.getConnector().clientToServer("bank " + "pay transaction with id+" + Bank.getToken() + "+" + receiptID.getText() + "+" + username + "+" + MenuHandler.getRole());

String answer = MenuHandler.getConnector().serverToClient();

if (receiptID.getText().equals("") || receiptID.getText() == null) {
Alert alert = new Alert(Alert.AlertType.ERROR, "the receipt ID cannot be empty", ButtonType.OK);
alert.showAndWait();
Expand Down Expand Up @@ -107,18 +129,6 @@ public void done(ActionEvent actionEvent) throws ParseException, IOException {
return;
}

try {
MenuHandler.getConnector().clientToServer("bank " + "get amount of transaction+" + Bank.getToken() + "+" + receiptID.getText());
long ID = Long.parseLong(receiptID.getText());
long amount = Long.parseLong(MenuHandler.getConnector().serverToClient());
if (200000 > ID && ID >= 100000 && !Bank.isPossibleToDepositForSalesman(amount)) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "you should keep the min credit", ButtonType.OK);
alert.showAndWait();
}
} catch (Exception e) {
e.printStackTrace();
}

if (answer.equals("successful")) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, answer, ButtonType.OK);
alert.showAndWait();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/GUI/Bank/Pane/NewReceipt.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public void transferDone(ActionEvent actionEvent) throws ParseException, IOExcep
if (result.equals("successful")) {
alert.setContentText("created successfully");
alert.showAndWait();
transferUsername.setText("");
transferAmount.setText("");
transferDescription.setText("");
return;
}

Expand Down

0 comments on commit 94f12a3

Please sign in to comment.