Trouble displaying subclass arguments in GUI

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 356 Views - Last Post: 18 March 2012 - 01:50 PM Rate Topic: -----

#1 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Trouble displaying subclass arguments in GUI

Posted 17 March 2012 - 07:29 PM

OK Here is what have to do I have to get the serial number and restocking fee to only show in the GUI for the Monitor3D items and I am lost as to how to do that any help would be appreciated.

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble displaying subclass arguments in GUI

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 05:55 AM

Lots of code with an unclear question, but here's what I think you're asking. Let me see if I've gotten it wrong:

MonitorGUI displays monitor inventory. Specifics about each monitor in the inventory are shown on the left, including labels and values for serial number and restocking fee.

Buttons to the right allow the user to select the first, previous, next, and last items in the inventory.

There are 'regular' monitors and 3D monitors in the inventory.

When a 3D monitor is shown, all monitor inventory data labels and values should be shown.

When a regular monitor is shown, all monitor inventory data labels and values should be shown EXCEPT the labels and values for serial number and restocking fee.

If I've gotten it right, from a quick glance I suggest you could (1) modify your displayMonitor() method to show different data for regular and 3D monitors, OR (2) have a displayMonitor() method and a display3DMonitor() method and use the appropriate method depending on which type of monitor is being displayed. I'm sure there are other options, maybe better ones, but those occur to me first.
Was This Post Helpful? 1
  • +
  • -

#3 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 07:29 AM

View PostGregBrannon, on 18 March 2012 - 05:55 AM, said:

Lots of code with an unclear question, but here's what I think you're asking. Let me see if I've gotten it wrong:

MonitorGUI displays monitor inventory. Specifics about each monitor in the inventory are shown on the left, including labels and values for serial number and restocking fee.

Buttons to the right allow the user to select the first, previous, next, and last items in the inventory.

There are 'regular' monitors and 3D monitors in the inventory.

When a 3D monitor is shown, all monitor inventory data labels and values should be shown.

When a regular monitor is shown, all monitor inventory data labels and values should be shown EXCEPT the labels and values for serial number and restocking fee.

If I've gotten it right, from a quick glance I suggest you could (1) modify your displayMonitor() method to show different data for regular and 3D monitors, OR (2) have a displayMonitor() method and a display3DMonitor() method and use the appropriate method depending on which type of monitor is being displayed. I'm sure there are other options, maybe better ones, but those occur to me first.


Sorry if I was unclear and you got exactly what I was trying to say. I will try what you have suggested and let you know. Thank you!
Was This Post Helpful? 0
  • +
  • -

#4 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 11:28 AM

View PostGregBrannon, on 18 March 2012 - 05:55 AM, said:

Lots of code with an unclear question, but here's what I think you're asking. Let me see if I've gotten it wrong:

MonitorGUI displays monitor inventory. Specifics about each monitor in the inventory are shown on the left, including labels and values for serial number and restocking fee.

Buttons to the right allow the user to select the first, previous, next, and last items in the inventory.

There are 'regular' monitors and 3D monitors in the inventory.

When a 3D monitor is shown, all monitor inventory data labels and values should be shown.

When a regular monitor is shown, all monitor inventory data labels and values should be shown EXCEPT the labels and values for serial number and restocking fee.

If I've gotten it right, from a quick glance I suggest you could (1) modify your displayMonitor() method to show different data for regular and 3D monitors, OR (2) have a displayMonitor() method and a display3DMonitor() method and use the appropriate method depending on which type of monitor is being displayed. I'm sure there are other options, maybe better ones, but those occur to me first.


Ok I tried to modify by display method by invoking the 3D class but I am getting error messages.

Here is the code

	public void displayMonitor() {

		txt.setText("\nItem Number: " + inv.get(currentDisplay).getItem() );
		txt.append("\nMonitor Name: " + inv.get(currentDisplay).getName() );

		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("\nSerial Number: " + inv.get(currentDisplay)D.getSerial());
		txt.append("\nRestocking Fee: $" + String.format("%.2f",inv.get(currentDisplay)D.Fee()) );
		 txt.append("\nTotal value With Restocking Fees: $" + String.format("%.2f",inv.get(currentDisplay)D.restockValue()) + "\n\n" );



		txt.append("Total value Of Full Monitor Inventory: $" + String.format("%.2f",inv.value()));
	}


And here is the errors.
C:\Users\Tony's\Documents\School Docs\IT 215\Text Editor\Inventoryprogram\MonitorGUI.java:111: ')' expected
		txt.append("\nSerial Number: " + inv.get(currentDisplay)D.getSerial());
		                                                        ^
C:\Users\Tony's\Documents\School Docs\IT 215\Text Editor\Inventoryprogram\MonitorGUI.java:111: illegal start of expression
		txt.append("\nSerial Number: " + inv.get(currentDisplay)D.getSerial());
		                                                         ^
C:\Users\Tony's\Documents\School Docs\IT 215\Text Editor\Inventoryprogram\MonitorGUI.java:111: ';' expected
		txt.append("\nSerial Number: " + inv.get(currentDisplay)D.getSerial());
		                                                                   ^
C:\Users\Tony's\Documents\School Docs\IT 215\Text Editor\Inventoryprogram\MonitorGUI.java:112: ')' expected
		txt.append("\nRestocking Fee: $" + String.format("%.2f",inv.get(currentDisplay)D.Fee()) );
		                                                                               ^
C:\Users\Tony's\Documents\School Docs\IT 215\Text Editor\Inventoryprogram\MonitorGUI.java:112: ';' expected
		txt.append("\nRestocking Fee: $" + String.format("%.2f",inv.get(currentDisplay)D.Fee()) );
		                                                                                      ^
C:\Users\Tony's\Documents\School Docs\IT 215\Text Editor\Inventoryprogram\MonitorGUI.java:113: ')' expected
		 txt.append("\nTotal value With Restocking Fees: $" + String.format("%.2f",inv.get(currentDisplay)D.restockValue()) + "\n\n" );
		                                                                                                  ^
C:\Users\Tony's\Documents\School Docs\IT 215\Text Editor\Inventoryprogram\MonitorGUI.java:113: ';' expected
		 txt.append("\nTotal value With Restocking Fees: $" + String.format("%.2f",inv.get(currentDisplay)D.restockValue()) + "\n\n" );
		                                                                                                                  ^
7 errors

Tool completed with exit code 1


Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 11:34 AM

The statements
        txt.append("\nSerial Number: " + inv.get(currentDisplay)D.getSerial());
        txt.append("\nRestocking Fee: $" + String.format("%.2f",inv.get(currentDisplay)D.Fee()) );
         txt.append("\nTotal value With Restocking Fees: $" + String.format("%.2f",inv.get(currentDisplay)D.restockValue()) + "\n\n" );


don't make sense. Adding a D.method() to the end of those statements is not correct.
Was This Post Helpful? 0
  • +
  • -

#6 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 11:38 AM

View PostGregBrannon, on 18 March 2012 - 11:34 AM, said:

The statements
        txt.append("\nSerial Number: " + inv.get(currentDisplay)D.getSerial());
        txt.append("\nRestocking Fee: $" + String.format("%.2f",inv.get(currentDisplay)D.Fee()) );
         txt.append("\nTotal value With Restocking Fees: $" + String.format("%.2f",inv.get(currentDisplay)D.restockValue()) + "\n\n" );


don't make sense. Adding a D.method() to the end of those statements is not correct.



Ok I thought that I was invoking the D monitor from the Monitor3D class since I have it added to the arraylist like this
inv.add(D);

I also tried this
Monitor3D.getSerial()

but that did not work either.
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 11:42 AM

I think you want to do something more like:

displayMonitor()
{
   // show the data common to both regular and 3D monitors

   if ( 3DMonitor )
   {
      // show 3DMonitor-only data
   }

}

Was This Post Helpful? 1
  • +
  • -

#8 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 11:49 AM

View PostGregBrannon, on 18 March 2012 - 11:42 AM, said:

I think you want to do something more like:

displayMonitor()
{
   // show the data common to both regular and 3D monitors

   if ( 3DMonitor )
   {
      // show 3DMonitor-only data
   }

}

I was thinking I needed an if/else statement but was unsure. I should have trusted my gut!

Or just an if statement! Thanks!
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 12:01 PM


	// 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" );
        if(inv.get(currentDisplay) instanceof Monitor3D) {
            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()));
	}




And not a good idea to call inv.get(currentDisplay) more than once. Do:

Monitor selected = inv.get(currentDisplay);
selected.getName();
selected.getValue();
....
Was This Post Helpful? 1
  • +
  • -

#10 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 12:03 PM

View Postpbl, on 18 March 2012 - 12:01 PM, said:


	// 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" );
        if(inv.get(currentDisplay) instanceof Monitor3D) {
            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()));
	}




And not a good idea to call inv.get(currentDisplay) more than once. Do:

Monitor selected = inv.get(currentDisplay);
selected.getName();
selected.getValue();
....

Ok Thanks for the advice!
Was This Post Helpful? 0
  • +
  • -

#11 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 12:21 PM

View Postpbl, on 18 March 2012 - 12:01 PM, said:


	// 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" );
        if(inv.get(currentDisplay) instanceof Monitor3D) {
            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()));
	}




And not a good idea to call inv.get(currentDisplay) more than once. Do:

Monitor selected = inv.get(currentDisplay);
selected.getName();
selected.getValue();
....

OK the insatnceof worked but I cant figure out the syntax to call the serial, restock fee, and restock value methods to display it in the GUI.

That should be instanceof! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 12:45 PM

Try:
           if(inv.get(currentDisplay) instanceof Monitor3D) {
               Monitor3D monitor3D = (Monitor3D)( inv.get( currentDisplay ) );
               txt.append("\nRestocking Fee: $" + ( monitor3D.getSerial() ) );


You don't have a getter for the restocking fee, so you'll have to write one of those in your Monitor3D class and then do as above to display that.
Was This Post Helpful? 1
  • +
  • -

#13 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 12:50 PM

View PostGregBrannon, on 18 March 2012 - 12:45 PM, said:

Try:
           if(inv.get(currentDisplay) instanceof Monitor3D) {
               Monitor3D monitor3D = (Monitor3D)( inv.get( currentDisplay ) );
               txt.append("\nRestocking Fee: $" + ( monitor3D.getSerial() ) );


You don't have a getter for the restocking fee, so you'll have to write one of those in your Monitor3D class and then do as above to display that.

I see! That makes sense!
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 12:51 PM

Have you noticed ?

   if(selected instanceof Monitor3D) {
         Monitor3d d3 = (Monitor3D) selected;  
         txt.append("\nRestocking Fee: $ " + d3.getSelectedFee());  // or whatever the function name is  
         txt.append("\nRestocking Fee: $");  
         txt.append("\nTotal value With Restocking Fees: $" + "\n\n");  
   }  


Was This Post Helpful? 1
  • +
  • -

#15 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble displaying subclass arguments in GUI

Posted 18 March 2012 - 12:53 PM

View Postpbl, on 18 March 2012 - 12:51 PM, said:

Have you noticed ?

   if(selected instanceof Monitor3D) {
         Monitor3d d3 = (Monitor3D) selected;  
         txt.append("\nRestocking Fee: $ " + d3.getSelectedFee());  // or whatever the function name is  
         txt.append("\nRestocking Fee: $");  
         txt.append("\nTotal value With Restocking Fees: $" + "\n\n");  
   }  


Yes Thank you! I have been starring at it for three days now and just didn't see it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2