package badgegui; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.LineNumberReader; import java.text.DecimalFormat; import javax.swing.BoxLayout; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class MeritBadge extends JFrame { private Container cp = getContentPane(); private JButton load = new JButton("Load"); private JButton add = new JButton("Add"); private JButton modify = new JButton("Modify"); private JButton delete = new JButton("Delete"); private JButton search = new JButton("Search"); private JButton save = new JButton("Save"); private JButton cancel = new JButton("Cancel"); private JLabel item = new JLabel(" Item #"); private JTextField itemValue = new JTextField(); private JLabel title = new JLabel(" Merit Badge Name"); private JTextField titleValue = new JTextField(); private JLabel unit = new JLabel(" Units in Stock"); private JTextField unitValue = new JTextField(); private JLabel unitbadgePrice = new JLabel(" Book Cost Price"); private JTextField badgePriceValue = new JTextField(); private JLabel season = new JLabel(" Best Time for Badge"); private JTextField seasonValue = new JTextField(); private JLabel valueOfMeritBadge = new JLabel(" Value of MeritBadge"); private JLabel restockingFee = new JLabel(" Restocking Fee Added"); private JTextField rfValue = new JTextField(); private JTextField totalMBValue = new JTextField(); private JLabel totalValue = new JLabel(); private JButton first = new JButton("First"); private JButton previous = new JButton("Previous"); private JButton next = new JButton("Next"); private JButton last = new JButton("Last"); private static int badgeItem[] = new int[10]; private static String badgeName[] = new String[10]; private static int badgeUnits[] = new int[10]; private static double badgePrice[] = new double[10]; private static boolean restockFee[] = new boolean[10]; static String seasonCode[] = new String[10]; private int current = 0; private static int total = 0; public static int getCount() { return total; } public MeritBadge(){}// public MeritBadge(int mbitem, String mbbadgeName, int mbbadgeUnits, double mbbadgePrice)// create constructor to pass information to arrays { badgeItem[total] = mbitem; badgeName[total] = mbbadgeName; badgeUnits[total] = mbbadgeUnits; badgePrice[total] = mbbadgePrice; total = total + 1; } private void display() { DecimalFormat df=new DecimalFormat("$#0.00"); itemValue.setText(badgeItem[current]+""); itemValue.setEnabled(false); titleValue.setText(badgeName[current]); titleValue.setEnabled(false); unitValue.setText(badgeUnits[current]+""); unitValue.setEnabled(false); badgePriceValue.setText(df.format(badgePrice[current])); badgePriceValue.setEnabled(false); seasonValue.setText(seasonCode[current]); seasonValue.setEnabled(false); rfValue.setText(df.format(restockFee(badgePrice[current], badgeUnits[current]))); rfValue.setEnabled(false); totalMBValue.setText(df.format(valueOfInventory(badgePrice[current], badgeUnits[current]))); totalMBValue.setEnabled(false); totalValue.setText(" The total MeritBadge value is "+df.format(showValueofInventory())); } public void showInventory() { ButtonListener buttonListener = new ButtonListener(); first.addActionListener(buttonListener); previous.addActionListener(buttonListener); next.addActionListener(buttonListener); last.addActionListener(buttonListener); load.addActionListener(buttonListener); add.addActionListener(buttonListener); modify.addActionListener(buttonListener); delete.addActionListener(buttonListener); search.addActionListener(buttonListener); save.addActionListener(buttonListener); cancel.addActionListener(buttonListener); JPanel top = new JPanel(); top.setLayout(new FlowLayout()); top.add(load); top.add(add); top.add(modify); top.add(delete); top.add(search); top.add(save); top.add(cancel); JPanel up = new JPanel(); up.setLayout(new GridLayout(7,2)); up.add(item); up.add(itemValue); up.add(title); up.add(titleValue); up.add(unit); up.add(unitValue); up.add(unitbadgePrice); up.add(badgePriceValue); up.add(season); up.add(seasonValue); up.add(valueOfMeritBadge); up.add(totalMBValue); up.add(restockingFee); up.add(rfValue); display(); JPanel middle = new JPanel(); middle.setLayout(new FlowLayout()); middle.add(first); middle.add(previous); middle.add(next); middle.add(last); JPanel down = new JPanel(); down.setLayout(new BorderLayout()); down.add(BorderLayout.CENTER, totalValue); JPanel all = new JPanel(); all.setLayout(new BoxLayout(all, BoxLayout.Y_AXIS)); all.add(top); all.add(up); all.add(down); all.add(middle); cp.add(BorderLayout.NORTH, all); } public double showValueofInventory()//calculates the total value of the inventory { double totalVal = 0; for (int j = 0; j < total; j++) { totalVal = totalVal + valueOfInventory(badgePrice[j], badgeUnits[j]); } return totalVal; } public void displayImage() //Displays the total of the inventory { Icon icon = new ImageIcon("mb.jpg"); JLabel image = new JLabel(icon); image.setPreferredSize(new Dimension(0, 0)); add( image ); } public MeritBadge(int mbitem, String mbbadgeName, String mbSeason, int mbbadgeUnits, double mbbadgePrice, boolean fee)// create constructor capture the new items and to pass information original constructor to pass info to arrays { seasonCode[total] = mbSeason; restockFee[total] = fee; MeritBadge I = new MeritBadge( mbitem, mbbadgeName, mbbadgeUnits, mbbadgePrice); } public static double restockFee(double price,int units) //calculates restocking fee { return 1.05*MeritBadge.valueOfInventory(price, units); } public static double valueOfInventory(double p,int u)// calculates value of single inventory item. { return p*u; } class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); if (button == first) current = (0) % getCount(); if (button == previous) current = (current + getCount() - 1) % getCount(); if (button == next) current = (current + 1) % getCount(); if (button == last) current = (4) % getCount(); if (button == load) ; if (button == add) ; if (button == modify); if (button == delete) ; if (button == search) ; if (button == save); if (button == cancel) ; } } }