From 7af00896fdba18c0bb01b53519ecb8df9b04e72d Mon Sep 17 00:00:00 2001 From: Gavin Tran Date: Tue, 24 Jan 2023 11:25:12 -0500 Subject: [PATCH] Conformed to Java conventions and updated SnackBar directory. --- .../java/chapter09/snackbar/SnackBar.java | 74 +++++++-------- .../chapter09/snackbar/VendingMachine.java | 66 +++++++------- src/main/java/chapter09/snackbar/Vendor.java | 90 +++++++------------ 3 files changed, 103 insertions(+), 127 deletions(-) diff --git a/src/main/java/chapter09/snackbar/SnackBar.java b/src/main/java/chapter09/snackbar/SnackBar.java index c644395..a839e86 100644 --- a/src/main/java/chapter09/snackbar/SnackBar.java +++ b/src/main/java/chapter09/snackbar/SnackBar.java @@ -5,21 +5,19 @@ import java.awt.event.*; import javax.swing.*; -public class SnackBar extends JFrame - implements ActionListener -{ - private static final String MY_PASSWORD = "jinx"; - private VendingMachine machine1, machine2, machine3; +public class SnackBar extends JFrame implements ActionListener { + private static final String PASSWORD = "jinx"; + private VendingMachine machine1; + private VendingMachine machine2; + private VendingMachine machine3; - public SnackBar() - { + public SnackBar() { super("Snack Bar"); - Color brandColor1 = new Color(130, 30, 10); // r, g, b + Color brandColor1 = new Color(130, 30, 10); Color brandColor2 = new Color(255, 180, 0); Color brandColor3 = new Color(90, 180, 0); - // Load the coin icon for the vending machine buttons: ImageIcon coin = new ImageIcon("coin.gif"); machine1 = new VendingMachine("Java", brandColor1, 45, coin); @@ -48,40 +46,46 @@ public SnackBar() } /** - * Password field: user strikes + * This method is called whenever a component with this {@link SnackBar} registered on it fires + * an {@link ActionEvent}. + *

+ * Since {@link SnackBar} inherits {@link ActionListener} using the {@code implements} keyword, + * it must have a {@code public void actionPerformed(ActionEvent)} method. Components can + * register it by passing {@code this} as an {@link ActionListener} to an + * {@code addActionListener} method if they have one. + * + * @see chapter11 Chapter 11 - Class Hierarchies and Interfaces */ - public void actionPerformed(ActionEvent e) - { - JPasswordField password = (JPasswordField)e.getSource(); - String word = new String(password.getPassword()); - password.setText(""); - if (MY_PASSWORD.equals(word)) - { - // double amt = Vendor.getTotalSales(); - machine1.reload(); - machine2.reload(); - machine3.reload(); - JOptionPane.showMessageDialog(null, - // String.format("Total sales: $%.2f\n", amt) + - "Machines reloaded", - "Service", JOptionPane.INFORMATION_MESSAGE); - } - else - { - JOptionPane.showMessageDialog(null, + @Override + public void actionPerformed(ActionEvent e) { + JPasswordField passwordField = (JPasswordField) e.getSource(); + String word = new String(passwordField.getPassword()); + passwordField.setText(""); + + if (!PASSWORD.equals(word)) { + JOptionPane.showMessageDialog(this, // SnackBar inherits from a JFrame, so it is the parent "Login failed", "Service", JOptionPane.ERROR_MESSAGE); + return; } - } - // ***************************************************** + // double amt = Vendor.getTotalSales(); + machine1.reload(); + machine2.reload(); + machine3.reload(); + JOptionPane.showMessageDialog( + this, + // String.format("Total sales: $%.2f\n", amt) + + "Machines reloaded", + "Service", + JOptionPane.INFORMATION_MESSAGE + ); + } - public static void main(String[] args) - { + public static void main(String[] args) { SnackBar window = new SnackBar(); window.setBounds(50, 50, 520, 310); window.setDefaultCloseOperation(EXIT_ON_CLOSE); window.setResizable(false); window.setVisible(true); } -} - +} \ No newline at end of file diff --git a/src/main/java/chapter09/snackbar/VendingMachine.java b/src/main/java/chapter09/snackbar/VendingMachine.java index 18a4bed..7f222fa 100644 --- a/src/main/java/chapter09/snackbar/VendingMachine.java +++ b/src/main/java/chapter09/snackbar/VendingMachine.java @@ -5,25 +5,24 @@ import java.awt.event.*; import javax.swing.*; -public class VendingMachine extends JPanel - implements ActionListener -{ +public class VendingMachine extends JPanel implements ActionListener { private static final int FULL_STOCK = 5; - private JButton deposit25c, deposit10c, deposit5c, go; + private JButton deposit25c; + private JButton deposit10c; + private JButton deposit5c; + private JButton go; private JTextField display; private Vendor vendor; boolean trayFull; Color brandColor; String brandName; - public VendingMachine(String brand, Color color, int price, ImageIcon coin) - { + public VendingMachine(String brand, Color color, int price, ImageIcon coin) { setBackground(Color.WHITE); brandColor = color; brandName = brand; - JTextField banner = new JTextField(" " + brandName + - " " + price + "c "); + JTextField banner = new JTextField(" " + brandName + " " + price + "c "); banner.setEditable(false); banner.setFont(new Font("Serif", Font.BOLD, 14)); banner.setHorizontalAlignment(JTextField.CENTER); @@ -68,53 +67,51 @@ public VendingMachine(String brand, Color color, int price, ImageIcon coin) vendor = new Vendor(price, FULL_STOCK); } - public void reload() - { + public void reload() { vendor.setStock(FULL_STOCK); display.setText(" " + vendor.getDeposit() + " "); repaint(); } - public void actionPerformed(ActionEvent e) - { - JButton b = (JButton)e.getSource(); + @Override + public void actionPerformed(ActionEvent e) { + JButton button = (JButton) e.getSource(); - if (b == deposit25c) + if (button == deposit25c) { vendor.addMoney(25); - else if (b == deposit10c) + } else if (button == deposit10c) { vendor.addMoney(10); - else if (b == deposit5c) + } else if (button == deposit5c) { vendor.addMoney(5); - else if (b == go) - { + } else if (button == go) { trayFull = vendor.makeSale(); int change = vendor.getChange(); - if (trayFull) // Successful sale - { + if (trayFull) { + // Successful sale repaint(); JOptionPane.showMessageDialog(null, "Enjoy your " + brandName + "\n" + " Change " + change + "c", "Enjoy " + brandName, JOptionPane.PLAIN_MESSAGE); trayFull = false; - } - else if (change > 0) // Refund - { + } else if (change > 0) { + // Refund JOptionPane.showMessageDialog(null, "Take " + change + "c back", "Money back", JOptionPane.ERROR_MESSAGE); } } - if (vendor.getStock() > 0) + if (vendor.getStock() > 0) { display.setText(" " + vendor.getDeposit() + " "); - else + } else { display.setText("Call service "); + } repaint(); } - public void paintComponent(Graphics g) - { + @Override + public void paintComponent(Graphics g) { super.paintComponent(g); final int x0 = getWidth() / 12; @@ -124,14 +121,15 @@ public void paintComponent(Graphics g) g.setColor(Color.BLACK); g.drawRect(x0, y0, 28, FULL_STOCK * yStep + 4); - int y = y0 + 4, x = x0 + 4; + int y = y0 + 4; + int x = x0 + 4; int stock = vendor.getStock(); int count = FULL_STOCK; - while (count > 0) - { - if (count <= stock) + while (count > 0) { + if (count <= stock) { drawCan(g, x, y); + } y += yStep; count--; } @@ -140,12 +138,12 @@ public void paintComponent(Graphics g) y += yStep; g.drawRect(x0, y - 4, 28, 18); - if (trayFull) + if (trayFull) { drawCan(g, x, y); + } } - private void drawCan(Graphics g, int x, int y) - { + private void drawCan(Graphics g, int x, int y) { g.setColor(brandColor); g.fillRoundRect(x, y, 20, 10, 4, 4); g.setColor(Color.WHITE); diff --git a/src/main/java/chapter09/snackbar/Vendor.java b/src/main/java/chapter09/snackbar/Vendor.java index 2e8bd1f..39d5949 100644 --- a/src/main/java/chapter09/snackbar/Vendor.java +++ b/src/main/java/chapter09/snackbar/Vendor.java @@ -2,104 +2,78 @@ package chapter09.snackbar; /** - * This class implements a vendor that sells one kind - * of items. A vendor carries out sales transactions. + * This class implements a vendor that sells one kind of items. A vendor carries out sales transactions. */ - -public class Vendor -{ - private final int price; //the price of a single item in cents - private int stock; //number of items to place in stock - private int deposit; //number of cents deposited - private int change; //number of cents currently in change +public class Vendor { + private final int price; + private int stock; + private int deposit; + private int change; /** - * Constructs a Vendor - * + * Constructs a Vendor. * @param price the price of a single item in cents (int) * @param stock number of items to place in stock (int) */ - public Vendor(int price, int stock) - { + public Vendor(int price, int stock) { this.price = price; this.stock = stock; } /** - * Returns the number of items currently in stock. - * - * @return number of items currently in stock (int) + * @return The number of items currently in stock. */ - public int getStock() - { + public int getStock() { return stock; } /** * Sets the quantity of items in stock. - * - * @param qty number of items to place in stock (int) + * @param qty The number of items to place in stock. */ - public void setStock(int qty) - { + public void setStock(int qty) { stock = qty; } /** - * Implements a sale. If there are items in stock and - * the deposited amount is greater than or equal to - * the single item price, then adjusts the stock - * and calculates and sets change and returns true; - * otherwise refunds the whole deposit (moves it into - * change) and returns false. - * - * @return true for a successful sale, false otherwise (boolean) + * Makes a sale depending on the current contents of the stock and deposit field. If the deposited + * amount is greater than the price, the change is moved into the change field. + * @return {@code true} if the sale was successful and {@code false} otherwise. */ - public boolean makeSale() - { - if (stock > 0 && deposit > price) - { + public boolean makeSale() { + boolean saleIsSuccess = stock > 0 && deposit > price; + + if (saleIsSuccess) { change = deposit - price; - deposit = 0; stock--; - return true; + } else { + change = deposit; } - change = deposit; deposit = 0; - return false; + return saleIsSuccess; } /** - * Adds a specified amount (in cents) to the - * deposited amount. - * - * @param number of cents to add to the deposit (int) + * @param cents The number of cents to be added to the deposit. */ - public void addMoney(int number) - { - deposit += number; + public void addMoney(int cents) { + deposit += cents; } /** - * Returns and zeroes out the amount of change (from - * the last sale or refund). - * - * @return number of cents in the current change (int) + * Takes and clears the money in the change box. + * @return The number of cents taken from the change box. */ - public int getChange() - { - int tempchange = change; + public int getChange() { + int tempChange = change; change = 0; - return tempchange; + return tempChange; } /** - * Returns the currently deposited amount (in cents). - * - * @return number of cents in the current deposit (int) + * @return The number of cents in the deposit. */ - public int getDeposit() - { + public int getDeposit() { return deposit; } }