Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.idea/workspace.xml
#	Data/Accounts/Buyers.json
#	src/main/java/View/Menu/AdminMenus/AdminMenu.java
  • Loading branch information
parhampch committed Jul 23, 2020
2 parents d2f569a + be3b450 commit abeab9a
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/main/java/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Client extends Application {

public static void main(String[] args){
try {
Socket socket = new Socket("127.0.0.1", 8080);
Socket socket = new Socket("127.0.0.1", 9595);
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
window.setOnCloseRequest(e -> {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/Controller/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,23 @@ else if (loggedInAccount instanceof SellerAccount)
return "nothing to show.";
}

public String changeCredit(Account account , int much , String bankUsername , String bankPassword , String bankId){
String res = "not available";
if (much > 0){
res = WorkWithBank.increaseCredit(much , bankUsername, bankPassword, bankId);
if (res.equalsIgnoreCase("done successfully")){
account.setCredit(account.getCredit() + much);
}
}
else if (account instanceof SellerAccount){
if (account.getCredit() + much < AdminManager.getMinimumValue())
return "Not enough money";
res = WorkWithBank.decreaseCredit(-much , bankUsername , bankPassword, bankId);
if (res.equalsIgnoreCase("done successfully")){
account.setCredit(account.getCredit() + much);
}
}
return res;
}

}
33 changes: 33 additions & 0 deletions src/main/java/Controller/AdminManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,39 @@
import java.util.ArrayList;

public class AdminManager {
private static int commission = 5 ;
private static int minimumValue = 100;

public static int getCommission() {
return commission;
}

public static void setCommission(int commission) {
AdminManager.commission = commission;
}

public static int getMinimumValue() {
return minimumValue;
}

public static void setMinimumValue(int minimumValue) {
AdminManager.minimumValue = minimumValue;
}

public String changeFinancialSetting(String field, String changTo){
if (field.equalsIgnoreCase("minimumValue")){
setMinimumValue(Integer.parseInt(changTo));
return "Done" ;
}
else if (field.equalsIgnoreCase("commission")){
if (Integer.parseInt(changTo) > 100)
return "Should be less than 100" ;
setCommission(Integer.parseInt(changTo));
return "Done";
}
else
return "Invalid field";
}

public String showAccountWithUsername(String username) {
Account account = Database.getAccountByUsername(username);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/Controller/ClientThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ else if (input.startsWith("PrOfCart"))
}
output = new Gson().toJson(allProducts);
}
else if (input.startsWith("changeCredit")){
details = input.split(" ");
output = accountManager.changeCredit(account , Integer.parseInt(details[1]), details[2] , details[3], details[4]);
}
else if (input.startsWith("changeFinancialSetting")){
details = input.split(" ");
output = adminManager.changeFinancialSetting(details[1] , details[2]);
}
else if (input.startsWith("Exit"))
{
clientSocket.close();
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/Controller/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ public class Server {
public static void main(String[] args) {
Server server = new Server();
Database.initialize();
try {
WorkWithBank.ConnectToBankServer();
} catch (IOException e) {
e.printStackTrace();
}
server.waitForClient();
}

public Server() {
try {
this.serverSocket = new ServerSocket(8080);
this.serverSocket = new ServerSocket(9595);
allOnlineAdmins = new ArrayList<>();
allOnlineBuyers = new ArrayList<>();
allOnlineSellers = new ArrayList<>();
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/Controller/WorkWithBank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package Controller;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class WorkWithBank {
public static final int PORT = 2222;
public static final String IP = "127.0.0.1";

private static DataOutputStream outputStream;
private static DataInputStream inputStream;

private static final String AdminBankUsername = "Admin";
private static final String AdminBankPassword = "Admin";
private static final String AdminBankId = "47039";

public static void ConnectToBankServer() throws IOException {
try {
Socket socket = new Socket(IP, PORT);
outputStream = new DataOutputStream(socket.getOutputStream());
inputStream = new DataInputStream(socket.getInputStream());
inputStream.readUTF();
} catch (IOException e) {
throw new IOException("Exception while initiating connection:");
}
}



private static String sendMessage(String msg){
try {
outputStream.writeUTF(msg);
return inputStream.readUTF();
} catch (IOException e) {
return "Exception while sending message:";
}

}

public static String increaseCredit(int much, String bankUsername, String bankPassword, String bankId) {
String token = get_token(bankUsername , bankPassword);
String res = sendMessage("create_receipt " + token + " move " + much + " " + bankId + " " + AdminBankId + " increaseCredit");
if (res.matches("[0-9]+")){
res = sendMessage("pay " + res) ;
}
return res ;
}

private static String get_token(String bankUsername, String bankPassword) {
return sendMessage("get_token " + bankUsername + " " + bankPassword);
}

public static String decreaseCredit(int much, String bankUsername, String bankPassword, String bankId) {
String token = get_token(AdminBankUsername , AdminBankPassword);
String res = sendMessage("create_receipt " + token + " move " + much + " " + AdminBankId + " " + bankId + " decreaseCredit");
if (res.matches("[0-9]+")){
res = sendMessage("pay " + res) ;
}
return res ;
}
}
97 changes: 3 additions & 94 deletions src/main/java/View/Menu/AdminMenus/AdminMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ public void show() {
manageDiscounts.getStyleClass().add("dark-blue");
manageDiscounts.setMaxWidth(Double.MAX_VALUE);

Button manageLogs = new Button("Manage Logs");
manageLogs.setOnAction(e -> handleAllLogs());
manageLogs.getStyleClass().add("dark-blue");
manageLogs.setMaxWidth(Double.MAX_VALUE);

Button manageAddAdmin = new Button("Add Manager");
manageAddAdmin.setOnAction(e -> handleAddAdmin());
manageAddAdmin.getStyleClass().add("dark-blue");
Expand All @@ -113,9 +108,8 @@ public void show() {
GridPane.setConstraints(manageProduct, 0, 4);
GridPane.setConstraints(manageCategories, 0, 5);
GridPane.setConstraints(manageDiscounts, 0, 6);
GridPane.setConstraints(manageLogs, 0, 7);
GridPane.setConstraints(manageAddAdmin, 0, 8);
GridPane.setConstraints(logout, 0, 9);
GridPane.setConstraints(manageAddAdmin, 0, 7);
GridPane.setConstraints(logout, 0, 8);
GridPane.setHalignment(editInfoButton, HPos.CENTER);
GridPane.setHalignment(manageUser, HPos.CENTER);
GridPane.setHalignment(onlineUser, HPos.CENTER);
Expand All @@ -124,10 +118,9 @@ public void show() {
GridPane.setHalignment(manageCategories, HPos.CENTER);
GridPane.setHalignment(manageDiscounts, HPos.CENTER);
GridPane.setHalignment(manageAddAdmin, HPos.CENTER);
GridPane.setHalignment(manageLogs, HPos.CENTER);
GridPane.setHalignment(logout, HPos.CENTER);

allButtons.getChildren().addAll(editInfoButton, manageUser, manageRequest, manageProduct, manageCategories, manageDiscounts, manageAddAdmin, logout, onlineUser, manageLogs);
allButtons.getChildren().addAll(editInfoButton, manageUser, manageRequest, manageProduct, manageCategories, manageDiscounts, manageAddAdmin, logout, onlineUser);

try {
dataOutputStream.writeUTF("GetLoggedAccount");
Expand Down Expand Up @@ -414,88 +407,6 @@ public void handleManageProduct()
}
}

private void handleAllLogs()
{
try {
super.setPane();
dataOutputStream.writeUTF("GetAllBuyerAccounts");
dataOutputStream.flush();
ArrayList<BuyerAccount> allBuyers = new Gson().fromJson(dataInputStream.readUTF(), new TypeToken<ArrayList<BuyerAccount>>(){}.getType());
ArrayList<BuyLog> allLogs = new ArrayList<>();
for (BuyerAccount buyer : allBuyers) {
allLogs.addAll(buyer.getBuyLogs());
}
Scene scene = new Scene(super.mainPane, 1000, 600);
scene.getStylesheets().add(new File("Data/Styles/Buttons.css").toURI().toString());
scene.getStylesheets().add(new File("Data/Styles/textfield.css").toURI().toString());
scene.getStylesheets().add(new File("Data/Styles/backgrounds.css").toURI().toString());
scene.getStylesheets().add(new File("Data/Styles/choicebox.css").toURI().toString());
super.mainPane.getStyleClass().add("admin-page");
ScrollPane scrollPane = new ScrollPane();
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
scrollPane.getStyleClass().add("scroll-pane");
GridPane gridPane = new GridPane();
gridPane.setHgap(20);
gridPane.setVgap(10);
gridPane.setAlignment(Pos.CENTER);
Label info = new Label("All Buy Logs");
info.setFont(Font.font(25));
GridPane.setHalignment(info, HPos.CENTER);
info.setAlignment(Pos.CENTER);
GridPane.setConstraints(info, 1, 0);
gridPane.getChildren().add(info);
int i = 1;
for (BuyLog buyLog : allLogs) {
Label label = new Label(String.valueOf(buyLog.getLogId()));
label.setFont(Font.font(15));
Button button = new Button("show");
button.setMaxWidth(Double.MAX_VALUE);
button.getStyleClass().add("dark-blue");
button.setAlignment(Pos.CENTER);
button.setOnAction(e -> {
handleShowLog(buyLog);
});
GridPane.setConstraints(label, 0, i);
GridPane.setConstraints(button, 2, i);
gridPane.getChildren().addAll(label, button);
i++;
}
Button back = new Button("back");
back.setAlignment(Pos.CENTER);
back.getStyleClass().add("dark-blue");
back.setMaxWidth(Double.MAX_VALUE);
GridPane.setHalignment(back, HPos.CENTER);
back.setOnAction(e -> show());
GridPane.setConstraints(back,1, i);
gridPane.getChildren().add(back);

scrollPane.setContent(gridPane);
super.mainPane.setCenter(scrollPane);

Menu.window.setScene(scene);
}
catch (Exception e)
{
e.printStackTrace();
}
}

private void handleShowLog(BuyLog buyLog) {
Stage newWindow = new Stage();
Pane pane = buyLog.showLogWithGraphic();
((GridPane)pane).setAlignment(Pos.CENTER);
Scene scene = new Scene(pane, 600, 400);
scene.getStylesheets().add(new File("Data/Styles/backgrounds.css").toURI().toString());
pane.getStyleClass().add("admin-popup");

newWindow.setScene(scene);
newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setOnCloseRequest(e -> handleAllLogs());
newWindow.showAndWait();
}

public void handleManageUsers()
{
try {
Expand Down Expand Up @@ -1413,6 +1324,4 @@ public void handleAddAdmin()

Menu.window.setScene(scene);
}


}
Loading

0 comments on commit abeab9a

Please sign in to comment.