private void showHelp(Graphics g) // PRE: g must be initialized. // POST: Displays the help image to the screen. { JOptionPane.showMessageDialog(this, "Once you are satisfied, click anywhere to continue!"); Drawing.drawImage(g, 0, 0, getWidth(), getHeight(), ".\\images\\help.jpg"); }
public void buyItemFromUser(User buyer, User seller, Item currentItem) // PRE: buyer, seller, currentItem must be initialized // POST: Purchases item from the store and has stores it into inventory. { int buyer_balance; // The new balance of the buyer int seller_balance; // The new balance of the seller String str; // First query String str2; // Second query String str3; // Third query buyer_balance = buyer.getBalance() - currentItem.getPrice(); seller_balance = seller.getBalance() + currentItem.getPrice(); if (buyer_balance > 0) // If the buyer wont go negative { str = String.format( "Update users set balance = (%d) where user_name = '%s'", buyer_balance, buyer.getUserName()); str2 = String.format( "Update users set balance = (%d) where user_name = '%s'", seller_balance, seller.getUserName()); str3 = String.format( "Update items set owner_id = (%d) where item_name = '%s'", buyer.getUserId(), currentItem.getItemName()); updateTables(str, str2, str3); } else { // Prompt the user with an error JOptionPane.showMessageDialog( null, "You will go bankrupt if you try buying that, try selling some items."); } }
@Override public void keyPressed(KeyEvent e) { int option; // The option the user selected. User buyer; // User object for buyer User seller; // User object for seller Item selected_item; // Item object for selected item if (help) // if in help mode { return; } switch (e.getKeyCode()) // handle event associated with key { case KeyEvent.VK_F1: // if f1 was pressed switchStore(0); break; case KeyEvent.VK_F2: // if f2 was pressed switchStore(1); break; case KeyEvent.VK_F3: // if f3 was pressed switchStore(2); break; case KeyEvent.VK_F4: // if f4 was pressed switchStore(3); break; case KeyEvent.VK_D: // if d or right was pressed case KeyEvent.VK_RIGHT: if (currentPage != totalPages - 1) // if we are not on the last page { currentPage++; itemSelected = -1; } break; case KeyEvent.VK_A: // if a or left was pressed case KeyEvent.VK_LEFT: if (currentPage != 0) // if we are not on the first page { currentPage--; itemSelected = -1; } break; case KeyEvent.VK_W: // if w or up was pressed case KeyEvent.VK_UP: if (itemSelected == -1) // if no items currently selected { itemSelected = 0; } else // if an item was selected { itemSelected--; if (itemSelected < 0) // loop if at beginning of list { itemSelected = ITEMSPERPAGE - 1; } } break; case KeyEvent.VK_S: // if s or down was pressed case KeyEvent.VK_DOWN: itemSelected++; if (itemSelected >= ITEMSPERPAGE) // if at end of list, loop around { itemSelected = 0; } break; case KeyEvent.VK_ENTER: // if enter was pressed if (itemSelected == -1) // if no item was selected { break; } selected_item = itemsArray[itemSelected]; option = JOptionPane.showConfirmDialog( this, ((!mode) ? "Buy " : "Sell ") + "for " + "$" + selected_item.getPrice() + "?"); if (option == 0 && mode) // if they choose to buy { buyer = usersArray[0]; switch (store) // change message based on value of store. { case 0: seller = usersArray[2]; break; case 1: seller = usersArray[1]; break; case 2: seller = usersArray[3]; break; case 3: seller = usersArray[4]; break; default: seller = usersArray[1]; } buyItemFromUser(buyer, seller, selected_item); } else if (option == 0 && mode) // if they choose to sell { seller = usersArray[0]; switch (store) // change message based on value of store. { case 0: buyer = usersArray[2]; break; case 1: buyer = usersArray[1]; break; case 2: buyer = usersArray[3]; break; case 3: buyer = usersArray[4]; break; default: buyer = usersArray[1]; } buyItemFromUser(buyer, seller, selected_item); } itemSelected = -1; break; default: return; } repaint(); }
@Override public void mousePressed(MouseEvent e) { CustomButton buttonPressed; // Button that was pressed. String buttonName; // The name of the button. int option; // Option chosen by user. User buyer; // User object for buyer User seller; // User object for seller Item selected_item; // Item object for selected item if (help) // if in help mode { help = false; repaint(); return; } wasItemSelected(e.getX(), e.getY()); buttonPressed = CustomButton.wasPressed(e.getX(), e.getY()); if (buttonPressed == null) // if no button was pressed { return; } buttonName = buttonPressed.getName(); playAudio(-1); switch (buttonName) // handle event associated with button name { case "rightTab": currentPage++; itemSelected = -1; break; case "leftTab": currentPage--; itemSelected = -1; break; case "buyTab": mode = false; break; case "sellTab": mode = true; break; case "button": selected_item = itemsArray[itemSelected]; option = JOptionPane.showConfirmDialog( this, ((!mode) ? "Buy " : "Sell ") + "for " + "$" + selected_item.getPrice() + "?"); if (option == 0 && (!mode)) // if they choose to buy { buyer = usersArray[0]; switch (store) // change message based on value of store. { case 0: seller = usersArray[2]; break; case 1: seller = usersArray[1]; break; case 2: seller = usersArray[3]; break; case 3: seller = usersArray[4]; break; default: seller = usersArray[1]; } buyItemFromUser(buyer, seller, selected_item); } else if (option == 0 && mode) // if they choose to sell { seller = usersArray[0]; switch (store) // change message based on value of store. { case 0: buyer = usersArray[2]; break; case 1: buyer = usersArray[1]; break; case 2: buyer = usersArray[3]; break; case 3: buyer = usersArray[4]; break; default: buyer = usersArray[1]; } buyItemFromUser(buyer, seller, selected_item); } itemSelected = -1; break; case "nextStore": if (++store == NUMSTORES) // if we're on the last store, loop around { store = 0; } switchStore(store); break; case "sort": // something orderToSort = JOptionPane.showOptionDialog( this, "Sort by:", "Sort", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, SORTOPTIONS, null); break; case "help": help = true; break; default: } repaint(); }