Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
� Conflicts:
�	src/main/java/Controller/SQL/SQL.java
�	src/main/java/Controller/Server.java
  • Loading branch information
MartinBrave committed Jul 24, 2020
2 parents 6e797c6 + 01b6e1c commit d5c8a98
Show file tree
Hide file tree
Showing 136 changed files with 2,166 additions and 5,445 deletions.
1 change: 0 additions & 1 deletion AP.iml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/Bank/Connector.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,69 @@
package Bank;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Connector {
private static final int PORT_NUMBER = 1989;
private Controller controller;

public Connector() {
controller = new Controller();
}

public static void main(String[] args) throws IOException {
(new Connector()).run();
}

public void run() throws IOException {
ServerSocket serverSocket = new ServerSocket(PORT_NUMBER);
System.out.println("BANK server started successfully...");
while (true) {
System.out.println("listening for clients ....");
Socket clientSocket = serverSocket.accept();
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));

(new ClientHandler(controller, clientSocket, dataInputStream, dataOutputStream)).start();
}
}

static class ClientHandler extends Thread {
final Controller controller;
Socket socket;
DataInputStream dataInputStream;
DataOutputStream dataOutputStream;

public ClientHandler(Controller controller, Socket socket, DataInputStream dataInputStream, DataOutputStream dataOutputStream) {
this.controller = controller;
this.socket = socket;
this.dataInputStream = dataInputStream;
this.dataOutputStream = dataOutputStream;
}

@Override
public void run() {
while (!Thread.interrupted()) {
try {
String command = dataInputStream.readUTF();
System.out.println("FROM CLIENT: " + command);
String respond = "";
synchronized (controller) {
controller.takeAction(command);
respond = controller.getServerAnswer();
System.out.println("FROM SERVER: " + respond);
}
dataOutputStream.writeUTF(respond);
dataOutputStream.flush();
} catch (IOException e) {
System.out.println("client closed its socket ):");
Thread.currentThread().interrupt();
}
}
}
}

private void requestDecoder(){

}
Expand Down
31 changes: 22 additions & 9 deletions src/main/java/Bank/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,50 @@ public void takeAction(String command) {
getAllReceiptsInvolvingMe(command);
} else if (command.startsWith("get amount of transaction+")) {
getTransactionAmount(command);
} else if (command.startsWith("get money from+")) {
getMoneyFrom(command);
} else {
serverAnswer = "invalid input";
}

}

private void getMoneyFrom(String command) {
try {
String username = command.split("\\+")[2];
long amount = Long.parseLong(command.split("\\+")[3]);
Account account = Account.getAccountWithUsername(username);
assert account != null;
if (account.getBalance() > amount) {
account.setBalance(account.getBalance() - amount);
serverAnswer = "successful";
return;
}
serverAnswer = "not enough credit";
} catch (Exception e) {
serverAnswer = "something went wrong";
e.printStackTrace();
}
}

private void getTransactionAmount(String command) {
try {
String token = command.split("\\+")[1];
int ID = Integer.parseInt(command.split("\\+")[2]);
Transaction transaction = Transaction.getTransactionBasedOnID(ID);

if (tokenIsWrong(token)) {
serverAnswer = "token isn't authentic";
return;
}

if (tokenIsExpired(token)) {
serverAnswer = "token has expired";
return;
}

if (transaction == null) {
serverAnswer = "wrong transaction id";
return;
}

serverAnswer = Long.toString(transaction.getAmount());

} catch (Exception e) {
serverAnswer = "something went wrong";
}
Expand Down Expand Up @@ -121,21 +137,17 @@ private void getAllReceiptsByMe(String command) {
try {
StringBuilder result = new StringBuilder("");
String token = command.split("\\+")[1];

if (tokenIsWrong(token)) {
serverAnswer = "token isn't authentic";
return;
}

if (tokenIsExpired(token)) {
serverAnswer = "token has expired";
return;
}

for (String string : Transaction.getAllTransactionsWithSource(command.split("\\+")[2])) {
result.append(string).append("\n");
}

serverAnswer = result.toString();
} catch (Exception e) {
serverAnswer = "something went wrong";
Expand Down Expand Up @@ -333,3 +345,4 @@ public String getServerAnswer() {
// pay transaction with id+token+id+username
// get all receipts by me+token+username
// get all receipts involving me+token+username
// get money from+token+username+amount
12 changes: 6 additions & 6 deletions src/main/java/Bank/SQL/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public SQL() {
connection.createStatement();
Statement stmt;
stmt = connection.createStatement();
String sql = "CREATE DATABASE bankdatabase";
String sql = "CREATE DATABASE bank";
stmt.executeUpdate(sql);
} catch (Exception s) {
System.out.println(s.getMessage());
}
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/bankdatabase?" + "user=root&password=");
connection = DriverManager.getConnection("jdbc:mysql://localhost/bank?" + "user=root&password=");
Statement stmt = connection.createStatement();
String sql;
sql = "CREATE TABLE Neuer " + "(id INTEGER not NULL, " + "name blob, " + " PRIMARY KEY ( id ))";
sql = "CREATE TABLE taylor " + "(id INTEGER not NULL, " + "name blob, " + " PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
} catch (Exception s) {
System.out.println(s.getMessage());
Expand All @@ -45,8 +45,8 @@ private void close() {
public void insert(String data) {
try {
data = encode(data);
PreparedStatement statement = connection.prepareStatement("INSERT INTO Neuer (id,name) values (?,?)");
statement.executeUpdate("DELETE FROM Neuer");
PreparedStatement statement = connection.prepareStatement("INSERT INTO taylor (id,name) values (?,?)");
statement.executeUpdate("DELETE FROM taylor");
statement.setInt(1, 1);
statement.setString(2, data);
statement.executeUpdate();
Expand All @@ -58,7 +58,7 @@ public void insert(String data) {

public byte[] show() throws SQLException {
String string = "";
String sql = "SELECT id, name FROM Neuer";
String sql = "SELECT id, name FROM taylor";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/Controller/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import Model.Product.Product;
import Model.Request.Request;
import Model.Storage;
import Model.Token.Token;

import java.net.ServerSocket;

Expand All @@ -34,8 +35,9 @@ public void login(String username, String password) {
return;
}
}
Server.setAnswer("login successful as " + account.getRole() + " " + username);
account.setOnline(true);
Server.setAnswer("login successful as " + account.getRole() + " " + username);
Token.addOnlineUsers(username, System.currentTimeMillis());
} else {
Server.setAnswer("Your Password Is Incorrect");
}
Expand All @@ -48,19 +50,10 @@ public void logout(String username) {
Account account = Storage.getAccountWithUsername(username);
assert account != null;
account.setOnline(false);
Token.addOnlineUsers(username, (long) -1);
Server.setAnswer("logout successful");
}

/*public void forgotPassword(String username) {
if (Storage.isThereAccountWithUsername(username)) {
Account account = Storage.getAccountWithUsername(username);
assert account != null;
Server.setAnswer("here is your password: " + account.getPassword());
} else {
Server.setAnswer("the username doesn't exists");
}
}*/

public void viewAccountInformation(String username) {
Account account = Storage.getAccountWithUsername(username);
assert account != null;
Expand Down Expand Up @@ -181,7 +174,7 @@ public void editCompany(String username, String company) {
}

public void deleteAccount(String bossUsername, String username) {
if(bossUsername.equals(username)){
if (bossUsername.equals(username)) {
Server.setAnswer("you're not allowed to delete yourself :)");
return;
}
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/Controller/BankConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@
import Bank.Controller.*;
import Model.RandomString;

import java.io.*;
import java.net.Socket;
import java.security.SecureRandom;
import java.util.Base64;

public class BankConnector {
private static final int bankPort = 1989;
private static final int BANK_PORT = 1989;
private static final SecureRandom secureRandom = new SecureRandom();
private static final Base64.Encoder base64Encoder = Base64.getUrlEncoder();

public static String sendToBank(String command) {
Controller controller = new Controller();
String result = "";
try {
Socket socket = new Socket("localhost", BANK_PORT);
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

dataOutputStream.writeUTF(command);
dataOutputStream.flush();

result = dataInputStream.readUTF();
socket.close();
} catch (IOException e) {
System.out.println("something went wrong with bank server");
result = "something went wrong";
}
return result;
/*Controller controller = new Controller();
controller.takeAction(command);
System.out.println("message: " + command);
System.out.println("result: " + controller.getServerAnswer());
return controller.getServerAnswer();
return controller.getServerAnswer();*/
}

private static String requestEncoder(String command) {
Expand Down
32 changes: 17 additions & 15 deletions src/main/java/Controller/CreditController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,41 @@
import Model.Storage;

import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;

public class CreditController {
private int wagePercentage = 5;
private int minimumCredit = 1000;
private static CreditController creditController = new CreditController();
public class CreditController implements Serializable {
private int wagePercentage;
private int minimumCredit;
public static CreditController creditController = new CreditController(5, 1000);

public void setWagePercentage(int wagePercentage) {
public CreditController(int wagePercentage, int minimumCredit) {
this.wagePercentage = wagePercentage;
this.minimumCredit = minimumCredit;
}

public void setWagePercentage(int wagePercentage) {
creditController.wagePercentage = wagePercentage;
}

public int getMinimumCredit() {
return minimumCredit;
return creditController.minimumCredit;
}

public int getWagePercentage() {
return wagePercentage;
return creditController.wagePercentage;
}

public void setMinimumCredit(int minimumCredit) {
this.minimumCredit = minimumCredit;
}

public void addCredit(String username, int amount) {
Account account = Storage.getAccountWithUsername(username);
if (account == null || account instanceof Salesman || account instanceof Boss) {
return;
}
creditController.minimumCredit = minimumCredit;
}

public static CreditController getCreditController() {
return creditController;
}

public static void setCreditController(CreditController creditController) {
CreditController.creditController = creditController;
}

}
Loading

0 comments on commit d5c8a98

Please sign in to comment.