diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 53f67df..692abff 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -35,12 +35,12 @@ public void inventoryFromFile() { String token = null; String item = null; - while(fileScanner.hasNextLine()) { // loops as long as there is another line to read + while(fileScanner.hasNextLine()) { // loops as long as there is another line to read token = fileScanner.next(); if (token.equals("merchant")) currentMerchant = fileScanner.nextInt(); else { - item = token + " " + fileScanner.nextInt(); // a string containing the item name and price + item = token + " " + fileScanner.nextInt(); // a string containing the item name and price if (currentMerchant == 1) merchantInventoryList1.add(item); else if (currentMerchant == 2) @@ -49,11 +49,11 @@ else if (currentMerchant == 3) merchantInventoryList3.add(item); playerInventoryList.add(item); } - if (fileScanner.hasNextLine()) // only advances to the next line if there is one to read + if (fileScanner.hasNextLine()) // only advances to the next line if there is one to read fileScanner.nextLine(); } - } catch (FileNotFoundException e) { // if inventory.txt is deleted or missing + } catch (FileNotFoundException e) { // if inventory.txt is deleted or missing System.out.println("Inventory file not found"); e.printStackTrace(); } @@ -112,8 +112,7 @@ public ArrayList getPlayerInventoryList() { } /** - * This method will create a new instance of Transaction - * incomplete method + * This method will create a new instance of Transaction which runs independently * * @param player * @param targetMerchant The merchant that the player is trading with @@ -122,6 +121,7 @@ public void createTransaction(Player player, Merchant targetMerchant) { toggleMovement("OFF"); Transaction newTransaction = new Transaction(player, targetMerchant); + toggleMovement("ON"); } /** diff --git a/MerchantRPGCSE2102/src/view/Test.java b/MerchantRPGCSE2102/src/view/Test.java index 072e852..470a1a6 100644 --- a/MerchantRPGCSE2102/src/view/Test.java +++ b/MerchantRPGCSE2102/src/view/Test.java @@ -13,6 +13,9 @@ import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; +import javax.swing.JTextPane; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; public class Test extends JFrame { @@ -55,7 +58,7 @@ public Test() { contentPane.add(lblSelectTheItem); JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); - lblSelectTheAmount.setBounds(61, 197, 168, 14); + lblSelectTheAmount.setBounds(61, 197, 275, 30); contentPane.add(lblSelectTheAmount); JButton btnAccept = new JButton("Accept"); @@ -66,5 +69,18 @@ public Test() { textField.setBounds(61, 238, 86, 20); contentPane.add(textField); textField.setColumns(10); + + JLabel lblYouHave = new JLabel("You have:"); + lblYouHave.setBounds(61, 27, 86, 14); + contentPane.add(lblYouHave); + + JButton btnCancel = new JButton("Cancel"); + btnCancel.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent arg0) { + } + }); + btnCancel.setBounds(61, 306, 89, 23); + contentPane.add(btnCancel); } } diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 0db30eb..f953fc8 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -28,7 +28,8 @@ public class TransactionUI extends JFrame { private static final ComboBoxModel[] Item = null; private JPanel contentPane; - private static Transaction MASTER; //TransactionUI class will hold the instance of the Transaction that created it + private static Transaction MASTER; //TransactionUI class will hold the instance of the Transaction that created it + private static boolean _inTransaction; //Prevents the user from making multiple transaction windows /** * Launch the application. @@ -51,6 +52,7 @@ public void run() { */ public TransactionUI(Transaction master) { MASTER = master; + _inTransaction = false; setTitle("Transaction Window"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 430); @@ -58,7 +60,7 @@ public TransactionUI(Transaction master) { contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); - + JButton btnBuy = new JButton("BUY"); btnBuy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { @@ -67,47 +69,52 @@ public void actionPerformed(ActionEvent arg0) { btnBuy.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { - System.out.println("BUY"); //temporary test code - createBuyWindow(); + System.out.println("BUY"); //temporary test code + if(!_inTransaction) + createBuyWindow(); } }); btnBuy.setBounds(58, 155, 169, 105); contentPane.add(btnBuy); - + JButton btnSell = new JButton("SELL"); btnSell.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { - System.out.println("SELL"); //temporary test code - createSellWindow(); + System.out.println("SELL"); //temporary test code + if(!_inTransaction) + createSellWindow(); } }); btnSell.setBounds(351, 155, 169, 105); contentPane.add(btnSell); - + JButton btnCancel = new JButton("Cancel"); btnCancel.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { - System.out.println("Cancel"); //temporary test code - MASTER.actionCancel(); - exitWindow(); + System.out.println("Cancel"); //temporary test code + if(!_inTransaction) + { + MASTER.actionCancel(); + exitWindow(); + } } }); btnCancel.setBounds(246, 344, 89, 23); contentPane.add(btnCancel); - + JLabel lblWouldYouLike = new JLabel("Would you like to:"); lblWouldYouLike.setFont(new Font("Tahoma", Font.PLAIN, 15)); lblWouldYouLike.setBounds(233, 76, 193, 32); contentPane.add(lblWouldYouLike); - + JLabel lblOr = new JLabel("OR"); lblOr.setFont(new Font("Tahoma", Font.PLAIN, 15)); lblOr.setBounds(277, 189, 35, 32); contentPane.add(lblOr); } - + /** * Will exit the transaction window * incomplete method @@ -116,84 +123,162 @@ public void exitWindow() { this.dispose(); } - + /** * Will create a window for the BUY option - * incomplete method + * INCOMPLETE METHOD */ public static void createBuyWindow() { - EventQueue.invokeLater(new Runnable() + _inTransaction = true; //does not allow new transaction windows to be opened while buy window is + final JFrame frame = new JFrame("Buy"); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frame.setBounds(100, 100, 400, 400); + JPanel contentPane = new JPanel(); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + frame.setContentPane(contentPane); + contentPane.setLayout(null); + + String[] itemList = new String[MASTER.getTargetMerchant().getInventory().length]; //create a new array of strings with the length of the player's inventory //item that the player chooses from the combobox + for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list { + itemList[i] = MASTER.getTargetMerchant().getInventory()[i].getItemName(); + itemList[i] = itemList[i] + " ($" + MASTER.getTargetMerchant().getInventory()[i].getAdjustedPrice() + ")"; + } + + JLabel lblYouHave = new JLabel("You have: $" + MASTER.getPlayer().getPlayerCash()); + lblYouHave.setBounds(61, 27, 86, 14); + contentPane.add(lblYouHave); + + @SuppressWarnings({ "rawtypes", "unchecked" }) + final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from + comboBox.setBounds(61, 141, 131, 20); + contentPane.add(comboBox); + + final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into + textField.setBounds(61, 238, 86, 20); + contentPane.add(textField); + textField.setColumns(10); + + JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); //label containing instructions + lblSelectTheItem.setBounds(61, 105, 168, 14); + contentPane.add(lblSelectTheItem); + + JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to buy:"); //label containing instructions + lblSelectTheAmount.setBounds(61, 197, 275, 30); + contentPane.add(lblSelectTheAmount); + + JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows + btnCancel.addMouseListener(new MouseAdapter() { @Override - public void run() - { - JFrame frame = new JFrame("Buy"); - frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frame.setBounds(100, 100, 600, 600); - JPanel contentPane = new JPanel(); - contentPane = new JPanel(); - contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); - frame.setContentPane(contentPane); - contentPane.setLayout(null); - - frame.setVisible(true); + public void mouseClicked(MouseEvent arg0) { + _inTransaction = false; + frame.dispose(); + } + }); + btnCancel.setBounds(61, 306, 89, 23); + contentPane.add(btnCancel); + + JButton btnAccept = new JButton("Accept"); //create an accept button to consume the information the player entered into the menu and text field + btnAccept.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { //information is consumed when player clicks the accept button + String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu + System.out.println(chosenItem); + + try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot + int chosenAmount = Integer.parseInt(textField.getText()); + if(MASTER.actionBuy(chosenItem, chosenAmount)) //will attempt to buy the specified amount of the chosen item + { + System.out.println(chosenAmount); + _inTransaction = false; + frame.dispose(); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionBuy returns a false + } + catch(NumberFormatException exception){ + System.out.println("Must input an amount that you can afford"); + } } }); + btnAccept.setBounds(247, 306, 89, 23); + contentPane.add(btnAccept); + frame.setVisible(true); } - + /** * Will create a window for the SELL option - * incomplete method + * INCOMPLETE METHOD: Still needs to integrate with the transaction limit in the daily cycle */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void createSellWindow() { + _inTransaction = true; //does not allow new transaction windows to be opened if sell window is final JFrame frame = new JFrame("Sell"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frame.setBounds(100, 100, 600, 600); + frame.setBounds(100, 100, 400, 400); JPanel contentPane = new JPanel(); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); frame.setContentPane(contentPane); contentPane.setLayout(null); - + String[] itemList = new String[MASTER.getPlayer().getInventory().length]; //create a new array of strings with the length of the player's inventory //item that the player chooses from the combobox for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list { itemList[i] = MASTER.getPlayer().getInventory()[i].getItemName(); + itemList[i] = itemList[i] + " (x" + MASTER.getPlayer().getInventory()[i].getQuantity() + ")"; } - - final JComboBox comboBox = new JComboBox(itemList); + + final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from comboBox.setBounds(61, 141, 131, 20); contentPane.add(comboBox); - - final JTextField textField = new JTextField(); + + final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into textField.setBounds(61, 238, 86, 20); contentPane.add(textField); textField.setColumns(10); - - JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); + + JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); //label containing instructions lblSelectTheItem.setBounds(61, 105, 168, 14); contentPane.add(lblSelectTheItem); - - JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); - lblSelectTheAmount.setBounds(61, 197, 168, 14); + + JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); //label containing instructions + lblSelectTheAmount.setBounds(61, 197, 275, 30); contentPane.add(lblSelectTheAmount); - JButton btnAccept = new JButton("Accept"); + JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows + btnCancel.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent arg0) { + _inTransaction = false; + frame.dispose(); + } + }); + btnCancel.setBounds(61, 306, 89, 23); + contentPane.add(btnCancel); + + JButton btnAccept = new JButton("Accept"); //create an accept button to consume the information the player entered into the menu and text field btnAccept.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent e) { - String chosenItem = comboBox.getSelectedItem().toString(); + public void mouseClicked(MouseEvent e) { //information is consumed when player clicks the accept button + String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu System.out.println(chosenItem); - - try{ + + try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot int chosenAmount = Integer.parseInt(textField.getText()); - System.out.println(chosenAmount); - frame.dispose(); + if(MASTER.actionSell(chosenItem, chosenAmount)) //will attempt to sell the specified amount of the chosen item + { + System.out.println(chosenAmount); + _inTransaction = false; + frame.dispose(); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false } - catch(NumberFormatException ex){ + catch(NumberFormatException exception){ System.out.println("Must input a number less than or equal to the total amount of the item that you have"); } }