From a2e3879cae0fa694dfc51abab43f90d60a54c9f0 Mon Sep 17 00:00:00 2001 From: Solargale Date: Wed, 10 Apr 2019 17:58:05 -0600 Subject: [PATCH] Created logic class for Warehouse --- src/gui/TravelGUI.java | 1 - src/gui/WarehouseGUI.java | 132 ++++++++-------------- src/logic/MainLogic.java | 4 - src/logic/TaipanShopLogic.java | 5 +- src/logic/WarehouseLogic.java | 90 +++++++++++++++ src/text/WarehouseText.java | 195 ++++++++------------------------- 6 files changed, 183 insertions(+), 244 deletions(-) delete mode 100644 src/logic/MainLogic.java diff --git a/src/gui/TravelGUI.java b/src/gui/TravelGUI.java index 567b96e..1298dc2 100644 --- a/src/gui/TravelGUI.java +++ b/src/gui/TravelGUI.java @@ -18,7 +18,6 @@ import logic.Player; import java.util.Random; public class TravelGUI extends Player { - private TaipanShopGUI shop; private Label firm = new Label(); private Label wItemsText = new Label(); private Label wItemSpaceText = new Label(); diff --git a/src/gui/WarehouseGUI.java b/src/gui/WarehouseGUI.java index 7b576c5..a5c5585 100644 --- a/src/gui/WarehouseGUI.java +++ b/src/gui/WarehouseGUI.java @@ -1,6 +1,7 @@ package gui; import javafx.geometry.Insets; +import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.BorderPane; @@ -9,10 +10,11 @@ import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.stage.Stage; import logic.Player; +import logic.WarehouseLogic; /** * 2019-03-19 - * Authors:Siddhant, Vikram, Harkamal, Haris, Nathan + * Authors:Siddhant and Harkamal * WarehouseGUI allows the user to store goods in a warehouse in order to have more hold on the ship */ @@ -90,13 +92,7 @@ public class WarehouseGUI extends Player { textField = new TextField(); //Creating the box for the warehouse GUI - borderPane.setAlignment(hBox, javafx.geometry.Pos.CENTER); - hBox.setAlignment(javafx.geometry.Pos.CENTER); - hBox.setPrefHeight(100.0); - hBox.setPrefWidth(200.0); - hBox.setSpacing(10.0); - - vBox.setAlignment(javafx.geometry.Pos.CENTER_LEFT); + settingBorderFormat(hBox, vBox); vBox.setPrefHeight(200.0); vBox.setPrefWidth(100.0); @@ -137,20 +133,14 @@ public class WarehouseGUI extends Player { //Takes you to the HK warehouse - borderPane.setAlignment(title, javafx.geometry.Pos.CENTER); + borderPane.setAlignment(title, Pos.CENTER); title.setText("Hong Kong Warehouse"); title.setFont(new Font(24.0)); title.setPadding(new Insets(10.0, 0.0, 0.0, 0.0)); borderPane.setTop(title); // Making the BOX again - borderPane.setAlignment(hBox0, javafx.geometry.Pos.CENTER); - hBox0.setAlignment(javafx.geometry.Pos.CENTER); - hBox0.setPrefHeight(100.0); - hBox0.setPrefWidth(200.0); - hBox0.setSpacing(10.0); - - vBox0.setAlignment(javafx.geometry.Pos.CENTER_LEFT); + settingBorderFormat(hBox0, vBox0); vBox0.setSpacing(10.0); playerLabel.setText("Player:"); @@ -171,10 +161,10 @@ public class WarehouseGUI extends Player { playerOpium.setText("Opium:"); - vBox1.setAlignment(javafx.geometry.Pos.CENTER_LEFT); + vBox1.setAlignment(Pos.CENTER_LEFT); vBox1.setSpacing(10.0); - houseLabel.setAlignment(javafx.geometry.Pos.TOP_CENTER); + houseLabel.setAlignment(Pos.TOP_CENTER); houseLabel.setText("Warehouse:"); houseLabel.setFont(new Font(18.0)); @@ -231,85 +221,46 @@ public class WarehouseGUI extends Player { taipanShopGUI.initializeShop(stage); stage.show(); }); + //Runs when the withdraw button is selected. and removes the amount of the selected good withdrawButton.setOnAction(event -> { - try { - int withdraw = Integer.parseInt(textField.getText()); - - if(withdraw <= 0){ - title.setText("Please enter a valid value"); - } - //Transfers general amount - else if(Goods.getSelectedToggle() == generalRadio && withdraw <= getwGeneral()){ - setGeneralHeld(getPlayer().getGeneralHeld()+withdraw); - setwGeneral(getPlayer().getwGeneral()-withdraw); - } - //Transfers Arms amount - else if(Goods.getSelectedToggle() == armsRadio && withdraw <= getwArms()){ - setArmsHeld(getPlayer().getArmsHeld()+withdraw); - setwArms(getPlayer().getwArms()-withdraw); - } - //Transfers Silk Amount - else if(Goods.getSelectedToggle() == silkRadio && withdraw <= getwSilk()){ - setSilkHeld(getPlayer().getSilkHeld()+withdraw); - setwSilk(getPlayer().getwSilk()-withdraw); - } - //Transfers Opium amount - else if(Goods.getSelectedToggle() == opiumRadio && withdraw <= getwOpium()) { - setOpiumHeld(getPlayer().getOpiumHeld() + withdraw); - setwOpium(getPlayer().getwOpium() - withdraw); - } - // Ensures a valid value is entered - else{ - title.setText("Please enter a valid value"); - } - updateLabels(); + WarehouseLogic warehouseLogic = new WarehouseLogic(getPlayer()); + if(Goods.getSelectedToggle() == generalRadio){ + title.setText(warehouseLogic.withdraw(textField.getText(),1)); } - catch (Exception e) { - title.setText("Please enter a valid value"); + else if(Goods.getSelectedToggle() == armsRadio){ + title.setText(warehouseLogic.withdraw(textField.getText(),2)); } + else if(Goods.getSelectedToggle() == silkRadio){ + title.setText(warehouseLogic.withdraw(textField.getText(),3)); + } + else if(Goods.getSelectedToggle() == opiumRadio){ + title.setText(warehouseLogic.withdraw(textField.getText(),4)); + } + setPlayer(warehouseLogic.getPlayer()); + updateLabels(); }); + //Button to add a user entered amount to the warehouse depositButton.setOnAction(event -> { - try { - int deposit = Integer.parseInt(textField.getText()); - - if(deposit <= 0){ - title.setText("Please enter a valid value"); - } - //Transfers general amount - else if(Goods.getSelectedToggle() == generalRadio && deposit <= getGeneralHeld()){ - setGeneralHeld(getPlayer().getGeneralHeld()-deposit); - setwGeneral(getPlayer().getwGeneral()+deposit); - } - //Transfers Arms amount - else if(Goods.getSelectedToggle() == armsRadio && deposit <= getArmsHeld()){ - setArmsHeld(getPlayer().getArmsHeld()-deposit); - setwArms(getPlayer().getwArms()+deposit); - } - //Transfers Silk amount - else if(Goods.getSelectedToggle() == silkRadio && deposit <= getSilkHeld()){ - setSilkHeld(getPlayer().getSilkHeld()-deposit); - setwSilk(getPlayer().getwSilk()+deposit); - } - //Transfers Opium amount - else if(Goods.getSelectedToggle() == opiumRadio && deposit <= getOpiumHeld()){ - setOpiumHeld(getPlayer().getOpiumHeld()-deposit); - setwOpium(getPlayer().getwOpium()+deposit); - } - //Checks if the correct value is entered - else{ - title.setText("Please enter a valid value"); - } - updateLabels(); + WarehouseLogic warehouseLogic = new WarehouseLogic(getPlayer()); + if(Goods.getSelectedToggle() == generalRadio){ + title.setText(warehouseLogic.deposit(textField.getText(),1)); } - catch (Exception e) { - title.setText("Please enter a valid value"); + else if(Goods.getSelectedToggle() == armsRadio){ + title.setText(warehouseLogic.deposit(textField.getText(),2)); } + else if(Goods.getSelectedToggle() == silkRadio){ + title.setText(warehouseLogic.deposit(textField.getText(),3)); + } + else if(Goods.getSelectedToggle() == opiumRadio){ + title.setText(warehouseLogic.deposit(textField.getText(),4)); + } + setPlayer(warehouseLogic.getPlayer()); + updateLabels(); }); - - //Create the sceene and box of the desired size + //Create the scene and box of the desired size Scene root = new Scene(borderPane, 600, 480); root.getStylesheets().add("styleguide.css"); @@ -319,6 +270,15 @@ public class WarehouseGUI extends Player { return stage; } + public void settingBorderFormat(HBox hBox, VBox vBox) { + borderPane.setAlignment(hBox, javafx.geometry.Pos.CENTER); + hBox.setAlignment(javafx.geometry.Pos.CENTER); + hBox.setPrefHeight(100.0); + hBox.setPrefWidth(200.0); + hBox.setSpacing(10.0); + vBox.setAlignment(javafx.geometry.Pos.CENTER_LEFT); + } + /** * As soon as the transfer is made the table of the amount of goods is updated using this method */ diff --git a/src/logic/MainLogic.java b/src/logic/MainLogic.java deleted file mode 100644 index cc625e3..0000000 --- a/src/logic/MainLogic.java +++ /dev/null @@ -1,4 +0,0 @@ -package logic; - -public class MainLogic extends Player{ -} diff --git a/src/logic/TaipanShopLogic.java b/src/logic/TaipanShopLogic.java index 6bf7d90..2dcf213 100644 --- a/src/logic/TaipanShopLogic.java +++ b/src/logic/TaipanShopLogic.java @@ -1,6 +1,7 @@ -package logic; /** +package logic; + +/** * TaipanShopLogic deals with the computations necessary for the shop such as randomizing prices. - * * Author: Vikram Bawa */ import java.util.Random; diff --git a/src/logic/WarehouseLogic.java b/src/logic/WarehouseLogic.java index 36ae6fd..25c882c 100644 --- a/src/logic/WarehouseLogic.java +++ b/src/logic/WarehouseLogic.java @@ -1,4 +1,94 @@ package logic; public class WarehouseLogic extends Player { + + /** + * constructor; only runs when a Player object is provided. The constructor is fully encapsulated. + * + * @param player is a Player object that will be copied and the player instance variable is set to the copy. + */ + public WarehouseLogic(Player player) { + Player playerDummy = new Player(player); + setPlayer(playerDummy); + } + + public String deposit(String str, int goodsNum) { + try { + int deposit = Integer.parseInt(str); + if(deposit <= 0){ + return "Please enter a valid value"; + } + //Transfers General amount + else if(goodsNum == 1 && deposit <= getGeneralHeld()){ + setGeneralHeld(getPlayer().getGeneralHeld()-deposit); + setwGeneral(getPlayer().getwGeneral()+deposit); + return "Successful deposit"; + } + //Transfers Arms amount + else if(goodsNum == 2 && deposit <= getArmsHeld()){ + setArmsHeld(getPlayer().getArmsHeld()-deposit); + setwArms(getPlayer().getwArms()+deposit); + return "Successful deposit"; + } + //Transfers Silk amount + else if(goodsNum == 3 && deposit <= getSilkHeld()){ + setSilkHeld(getPlayer().getSilkHeld()-deposit); + setwSilk(getPlayer().getwSilk()+deposit); + return "Successful deposit"; + } + //Transfers Opium amount + else if(goodsNum == 4 && deposit <= getOpiumHeld()){ + setOpiumHeld(getPlayer().getOpiumHeld()-deposit); + setwOpium(getPlayer().getwOpium()+deposit); + return "Successful deposit"; + } + //Checks if the correct value is entered + else{ + return "Please enter a valid value"; + } + } + catch (Exception e) { + return "Please enter a valid value"; + } + } + + public String withdraw(String str, int goodsNum) { + try { + int withdraw = Integer.parseInt(str); + if(withdraw <= 0){ + return "Please enter a valid value"; + } + //Transfers general amount + else if(goodsNum == 1 && withdraw <= getwGeneral()){ + setGeneralHeld(getPlayer().getGeneralHeld()+withdraw); + setwGeneral(getPlayer().getwGeneral()-withdraw); + return "Successful withdraw"; + } + //Transfers Arms amount + else if(goodsNum == 2 && withdraw <= getwArms()){ + setArmsHeld(getPlayer().getArmsHeld()+withdraw); + setwArms(getPlayer().getwArms()-withdraw); + return "Successful withdraw"; + } + //Transfers Silk Amount + else if(goodsNum == 3 && withdraw <= getwSilk()){ + setSilkHeld(getPlayer().getSilkHeld()+withdraw); + setwSilk(getPlayer().getwSilk()-withdraw); + return "Successful withdraw"; + } + //Transfers Opium amount + else if(goodsNum == 4 && withdraw <= getwOpium()) { + setOpiumHeld(getPlayer().getOpiumHeld() + withdraw); + setwOpium(getPlayer().getwOpium() - withdraw); + return "Successful withdraw"; + } + // Ensures a valid value is entered + else{ + return "Please enter a valid value"; + } + } + catch (Exception e) { + return "Please enter a valid value"; + } + } } diff --git a/src/text/WarehouseText.java b/src/text/WarehouseText.java index a33a300..4f57fd9 100644 --- a/src/text/WarehouseText.java +++ b/src/text/WarehouseText.java @@ -1,6 +1,7 @@ package text; import logic.Player; +import logic.WarehouseLogic; import java.util.Scanner; @@ -20,80 +21,29 @@ public class WarehouseText extends Player { * */ public void addAmount() { - boolean askGood = false; - String amount; - int finalAmount = 0; - System.out.println("Please enter the amount of the good you would like to ADD."); Scanner keyboard = new Scanner(System.in); - amount = keyboard.nextLine();//Asks the user for the amount of the good they would like to add + + System.out.println("Please enter the amount of the good you would like to deposit."); + String amount = keyboard.nextLine(); + //Asks the user for the amount of the good they would like to add /*The try function ensures that the program does not crash due to any errors while giving the program an incorrect input*/ - try { - //The if statement checks that you have enough resources to make the transfer - if (Integer.parseInt(amount) <= getOpiumHeld() || Integer.parseInt(amount) <= getSilkHeld() || Integer.parseInt(amount) <= getGeneralHeld() || Integer.parseInt(amount) <= getArmsHeld()) { - finalAmount = Integer.parseInt(amount); - askGood = true; - } - //Else statement lets the user know that they do not hav enough goods to make the requested transfer - else { - System.out.println("Nice try but you don't have any items of that quantity!"); - askGood = false; - } - //Ensures that goods are only transferred if they have the specified amount - //The user is prompted to enter which good they want to transfer - if (askGood == true) { - String good; - System.out.println("Please enter a good to transfer O, S, G, A :"); - good = keyboard.nextLine(); - int held = 0; - //The following set of loops check to see which good the user has selected and makes the transfer - if (Integer.parseInt(amount) > 0) { - if (good.equalsIgnoreCase("O")) { - if (getOpiumHeld() >= Integer.parseInt(amount)) { - setwOpium(getwOpium() + finalAmount); - held = getOpiumHeld(); - setOpiumHeld(held - finalAmount); - System.out.println(getOpiumHeld()); - } else { - System.out.println("You don't even have that much opium!"); - } - } else if (good.equalsIgnoreCase("S")) { - if (getSilkHeld() >= Integer.parseInt(amount)) { - setwSilk(getwSilk() + finalAmount); - held = getSilkHeld(); - setSilkHeld(held - finalAmount); - } else { - System.out.println("You don't even have that much silk!"); - - } - } else if (good.equalsIgnoreCase("G")) { - if (getGeneralHeld() >= Integer.parseInt(amount)) { - setwGeneral(getwGeneral() + finalAmount); - held = getGeneralHeld(); - setGeneralHeld(held - finalAmount); - } else { - System.out.println("You don't even have that much general cargo!"); - - } - } else if (good.equalsIgnoreCase("A")) { - if (getArmsHeld() >= Integer.parseInt(amount)) { - setwArms(getwArms() + finalAmount); - held = getArmsHeld(); - setArmsHeld(held - finalAmount); - } else { - System.out.println("You don't even have that much Arms!"); - } - } - } - //Ensures no negative amounts are entered - else { - System.out.println("Sorry this transfer cannot be made"); - } - } - //If the program errors out this is the message displayed and the method is re-run, so that the game does not end. - } catch (Exception e) { - System.out.println("Wait, that's not a valid input please try again"); + System.out.println("Please enter a good to transfer O, S, A, G :"); + String good = keyboard.nextLine(); + WarehouseLogic warehouseLogic = new WarehouseLogic(getPlayer()); + if(good.equalsIgnoreCase("g")){ + System.out.println(warehouseLogic.deposit(amount,1)); } + else if(good.equalsIgnoreCase("a")){ + System.out.println(warehouseLogic.deposit(amount,2)); + } + else if(good.equalsIgnoreCase("s")){ + System.out.println(warehouseLogic.deposit(amount,3)); + } + else if(good.equalsIgnoreCase("o")){ + System.out.println(warehouseLogic.deposit(amount,4)); + } + setPlayer(warehouseLogic.getPlayer()); } /** @@ -106,82 +56,28 @@ public class WarehouseText extends Player { */ public void removeAmount() { - String amount; - boolean askGood = false; - int finalAmount = 0; - System.out.println("Please enter the amount of the good you would like to REMOVE"); Scanner keyboard = new Scanner(System.in); - //Prompts the user for the amount they would like to remove - amount = keyboard.nextLine(); - //The if statement checks that you have enough resources to make the transfer - try { - //The if statement checks that you have enough resources to make the transfer - if (Integer.parseInt(amount) <= getwOpium() || Integer.parseInt(amount) <= getwSilk() || Integer.parseInt(amount) <= getwGeneral() || Integer.parseInt(amount) <= getwArms()) { - finalAmount = Integer.parseInt(amount); - askGood = true; - } - //Else statement lets the user know that they do not hav enough goods to make the requested transfer - else { - System.out.println("Nice try but you don't have any items of that quantity in the warehouse!"); - askGood = false; - } - - //Ensures that goods are only transferred if they have the specified amount - //The user is prompted to enter which good they want to transfer - - if (askGood == true) { - String good; - System.out.println("Please enter a good to transfer O, S, G, A :"); - good = keyboard.nextLine(); - int held = 0; - //The following set of loops check to see which good the user has selected and makes the transfer and amount > 0 - if (Integer.parseInt(amount) > 0) { - if (good.equalsIgnoreCase("O")) { - if (getwOpium() >= Integer.parseInt(amount)) { - setwOpium(getwOpium() - Integer.parseInt(amount)); - held = getOpiumHeld(); - setOpiumHeld(held + finalAmount); - } else { - System.out.println("You don't have that much opium stored in the warehouse!"); - } - } else if (good.equalsIgnoreCase("S")) { - if (getwSilk() >= Integer.parseInt(amount)) { - setwSilk(getwSilk() - Integer.parseInt(amount)); - held = getSilkHeld(); - setSilkHeld(held + finalAmount); - } else { - System.out.println("You don't have that much silk stored in the warehouse!"); - } - } else if (good.equalsIgnoreCase("G")) { - if (getwGeneral() >= Integer.parseInt(amount)) { - setwGeneral(getwGeneral() - Integer.parseInt(amount)); - held = getGeneralHeld(); - setGeneralHeld(held + finalAmount); - } else { - System.out.println("You don't have that much general cargo stored in the warehouse!"); - - } - } else if (good.equalsIgnoreCase("A")) { - if (getwArms() >= Integer.parseInt(amount)) { - setwArms(getwArms() - Integer.parseInt(amount)); - held = getArmsHeld(); - setArmsHeld(held + finalAmount); - } else { - System.out.println("You don't have that much arms stored in the warehouse!"); - - } - } - } - //Ensures the value entered is positive - else { - System.out.println("Sorry this transfer cannot be made"); - } - } + System.out.println("Please enter the amount of the good you would like to withdraw."); + String amount = keyboard.nextLine(); + //Asks the user for the amount of the good they would like to withdraw + /*The try function ensures that the program does not crash + due to any errors while giving the program an incorrect input*/ + System.out.println("Please enter a good to withdraw (O)pium, (S)ilk, (A)rms, (G)eneral:"); + String good = keyboard.nextLine(); + WarehouseLogic warehouseLogic = new WarehouseLogic(getPlayer()); + if(good.equalsIgnoreCase("g")){ + System.out.println(warehouseLogic.withdraw(amount,1)); } - //If the program errors out this is the message displayed and the method is re-run, so that the game does not end. - catch (Exception e){ - System.out.println("Wait, that's not a valid input please try again"); + else if(good.equalsIgnoreCase("a")){ + System.out.println(warehouseLogic.withdraw(amount,2)); } + else if(good.equalsIgnoreCase("s")){ + System.out.println(warehouseLogic.withdraw(amount,3)); + } + else if(good.equalsIgnoreCase("o")){ + System.out.println(warehouseLogic.withdraw(amount,4)); + } + setPlayer(warehouseLogic.getPlayer()); } /** @@ -206,26 +102,23 @@ public class WarehouseText extends Player { boolean keepGoing = true; while (keepGoing) { this.showWarehouse(); - String input = " "; - System.out.println("Would you like to add(A) or remove(R) resources? "); + System.out.println("Would you like to (D)eposit or (W)ithdraw resources? "); Scanner keyboard = new Scanner(System.in); - input = keyboard.next(); - if (input.equalsIgnoreCase("R")) { + String input = keyboard.next(); + if (input.equalsIgnoreCase("D")) { this.removeAmount(); this.showWarehouse(); - } else if (input.equalsIgnoreCase("A")) { + } else if (input.equalsIgnoreCase("W")) { this.addAmount(); this.showWarehouse(); - } else{ System.out.println("Don't waste the warehouse's time, try again later with a valid input"); } - String check; //Check to see if the player wants to continue in the warehouse or they are done System.out.println("Would you like to do any other business? Y / N?"); - check = keyboard.nextLine(); + String check = keyboard.nextLine(); check = keyboard.nextLine(); if (check.equalsIgnoreCase("Y")) {