Monitor class
public class Monitor {
private int item;
private String name;
private int units;
private double price;
// constructor
public Monitor(int item, String name, int units, double price) {
this.item = item;
this.name = name;
this.units = units;
this.price = price;
}
// total value
public double value() {
return units*price;
}
// getters and setters
public int getItem() {
return item;
}
public void setItem(int item) {
this.item = item;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUnits() {
return units;
}
public void setUnits(int units) {
this.units = units;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Monitor3D class
public class Monitor3D extends Monitor {
private String serial = "";
public Monitor3D(int item, String name, int units, double price, String serial) {
super(item, name, units, price);
this.serial = serial;
}
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
// total value
public double restockvalue() {
return 1.05*getUnits()*getPrice();
}
// fee
public double fee() {
return 0.05*getUnits()*getPrice();
}
}
Monitors class
import java.util.*;
public class Monitors {
private ArrayList<Monitor> monitorlist;
public Monitors() {
monitorlist = new ArrayList<Monitor>();
}
// adding and getting items
public void add(Monitor p) {
monitorlist.add(p);
}
public Monitor get(int i) {
return monitorlist.get(i);
}
public int size() {
return monitorlist.size();
}
public void sort() {
// bubble sort
int n = monitorlist.size();
for (int search = 1; search < n; search++) {
for (int i = 0; i < n-search; i++) {
if (monitorlist.get(i).getName().compareToIgnoreCase(monitorlist.get(i+1).getName()) > 0) {
// swap
Monitor temp = monitorlist.get(i);
monitorlist.set(i,monitorlist.get(i+1));
monitorlist.set(i+1,temp);
}
}
}
}
// value
public double value() {
double total = 0.0;
for (int i = 0; i < monitorlist.size(); i++) {
total += get(i).value();
}
return total;
}
}
MonitorGUI class
import javax.swing.*;
import java.awt.event.*;
import javax.swing.Icon;
import java.awt.BorderLayout;
//Stores and then gets info on a DVD
public class MonitorGUI extends JFrame {
private JTextArea txt;
private Monitors inv;
private int currentDisplay = 0;
public MonitorGUI() {
super("Monitor Inventory");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // quit if the window is closed
init();
}
public void init() {
Monitor B = new Monitor(2479,"Monitor B", 20, 450.00);
Monitor A = new Monitor(2480, "Monitor A", 10, 350.00);
Monitor C = new Monitor(2481, "Monitor C", 15, 250.00);
Monitor3D E = new Monitor3D(2482, "Monitor3D E", 10, 150.55, "25947");
Monitor3D D = new Monitor3D(2483, "Monitor3D D", 12, 250.25, "11234");
// make an inventory and put in the objects
inv = new Monitors();
inv.add(B)/>;
inv.add(A);
inv.add(C);
inv.add(E);
inv.add(D);
inv.sort();
// output the info
for (int i = 0; i < inv.size(); i++) {
}
// setup the interface
JPanel panel = new JPanel();
txt = new JTextArea(15,40);
txt.setEditable(false);//user shouldn't change it
panel.add(txt);
JButton first = new JButton("First");
first.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentDisplay = 0;// go to the beginning
displayMonitor();
}
});
panel.add(first);
JButton previous = new JButton("Previous");
previous.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (currentDisplay > 0) currentDisplay--;
else currentDisplay = inv.size()-1;
displayMonitor();
}
});
panel.add(previous);
JButton next = new JButton("Next");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (currentDisplay < inv.size()-1) currentDisplay++; //advance to the end
else currentDisplay = 0;
displayMonitor();
}
});
panel.add(next);
JButton last = new JButton("Last");
last.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentDisplay = inv.size()-1;
displayMonitor();
}
});
panel.add(last);
String path = "mylogo.jpg";
JLabel label = new JLabel(new ImageIcon("mylogo.jpg"));
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label); // default center section
getContentPane().add(panel);
displayMonitor();
}
// view software
public void displayMonitor() {
txt.setText("\nItem Number: " + inv.get(currentDisplay).getItem() );
txt.append("\nMonitor Name: " + inv.get(currentDisplay).getName() );
txt.append("\nSerial Number");
txt.append("\nNumber In Stock: " + inv.get(currentDisplay).getUnits() );
txt.append("\nUnit Price: $" + String.format("%.2f",inv.get(currentDisplay).getPrice()) );
txt.append("\nTotal value in stock: $" + String.format("%.2f",inv.get(currentDisplay).value()) + "\n\n" );
txt.append("\nRestocking Fee: $");
txt.append("\nTotal value With Restocking Fees: $" + "\n\n");
txt.append("Total value Of Full Monitor Inventory: $" + String.format("%.2f",inv.value()));
}
public static void main(String args[]) {
MonitorGUI gui = new MonitorGUI();
gui.pack();
gui.setVisible(true);
}
//
}
The way it is currently the GUI just displays the titles for serial number, restocking fee, and value including restocking fee but it displays them for all the items. I need help figuring out how to get it to only show for the 3D monitors.
It displays the rest of the inventory correctly!

New Topic/Question
Reply



MultiQuote




|