0

You are probably right. The assigment says show the values in the GUI, but not specifically in fields in the GUI, so maybe we could do it that way.
I am willing to give it a shot if you are willing to show me.

I put my other code up that I have been working on today also. I am taking tomorrow off work to concentrate and try and get both versions working. I will either be alot better by tomorrow, or alot more confused. :o)

0

Instead, consider this: Why not simply display the value of calculated fields in the toString() info that shows in the list entry and drop the text fields for them altogether? There is little need to provide text entry fields for data that the user doesn't need to enter or edit - such as values calculated from other inputs.

Genious in its simplicity, and it shortens my code considerably since I no longer need all those labels and fields.

I do have one more question though, concerning the placement in the tostring:

public float getTotal()
{
return(price * nstock)+((price * nstock)* restock);
}


//new toString


public String toString()
{
return "Artist:" + artist +
" CD Name: " + name +
" Item Number: " + itemno +
" Price: $" +price +
" In Stock: " + nstock +
" Stock Value: " + value +
" Restocking Fee: " + restock +
" Total Inventory Value: $"  + total;

How could I put a calculated field like the one above in here? What would it look like?

Edited by happygeek: fixed formatting

0

I am a little confused as to when these calculations to be output are even performed. In my previous applications, I just output Value and by its very definition(stock * price) the total was what I received. I am still putting those figures in, stock and price show up in my output, but value is $0.
Is something different doing it this way?
I have removed all those fields, and am just inputting numbers.
Should I then have, in my addactionevent the calculations to be performed?
Why would value not be calculated as before as soon as price and stock are input?

1

Do your methods look something like this in terms of functionality such that the value/total is being updated each time either the price, number in stock, and restock number changes?:

public void setNstock(int cdStock)
{
nstock = cdStock;
value=updateValue();
}

public void setPrice(float cdPrice)
{
 price = cdPrice;
 value=updateValue();
}

public void setRestock(int restocknum)
{
restock=restocknum;
total=getTotal();
}

public float updateValue()
{return nstock * price;}

public float getValue()
{return value;}

public float getTotal()
{return value + (value * restock);}

If so, I can't see why you shouldn't be able to use your toString:

public String toString()
{
return "Artist:" + artist +
" CD Name: " + name +
" Item Number: " + itemno +
" Price: $" +price +
" In Stock: " + nstock +
" Stock Value: " + value +
" Restocking Fee: " + restock +
" Total Inventory Value: $" + total;
}
Votes + Comments
thanks for the help
0

Consider also that methods may be called in toString() as well, so this is just as valid

public String toString()
{
return "Artist:" + artist +
" CD Name: " + name +
" Item Number: " + itemno +
" Price: $" +price +
" In Stock: " + nstock +
" Stock Value: " + getValue()+
" Restocking Fee: " + restock +
" Total Inventory Value: $" + getTotal();
}

You can code getValue() either way you choose: by maintaining a "value" variable and updating it as other properties are changed, or by simply returning the calculation itself return nstock * price; (this would not be so great for performance if the calculation was called often or was expensive to calc. That really isn't a consideration in your particular case though.).

0

That is what I thought. I stayed up till midnight last night trying every different combination under the sun, and they still show up as 0.
Here is what I have now, along with the two classes. This is really getting to me.

import java.util.*;

public class CdwArtist extends Compactdisk
{
 
 private String artist; // artist performing on cd
 private float restock; // restocking percentage
 private float total; // total of all inventory stock
  
 // Artist constructor
  public CdwArtist()
 {
 artist = "";
 restock = .05f;
 total = 0;
 }
 
 public CdwArtist(String in)
 {
 artist=in;
 }
 
 // get values
 public void setArtist(String in)
 {
 artist=in;
 }
 public void setRestock(float cdRestock)
 {
 restock = cdRestock;
 }
 public void setTotal(float cdtotal)
 {
 total = cdtotal;
 }
  
 // return value
 public String getArtist()
 {
 return (artist);
 }
	
 // returns indivudual inventory value for a disk
 public float getTotal()
 {
 return(price * nstock)+((price * nstock)* restock);
 }	
	
	//new toString
	
	public String toString()
	{
	return ("Artist: " + artist + 
	" CD Name: " + name + 
	" Item Number: " + itemno + 
	" Price: $" +price + 
	" In Stock: " + nstock + 
	" Stock Value: $" + getValue() + 
	" Restocking Fee: $" + restock + 
	" Total Inventory Value: $"  + getTotal());
	}
} //End Class
import java.lang.Comparable;

public class Compactdisk implements Comparable
{// begin class

	//InventoryCD class has 5 fields
	public String name; //  Name of cd
	public float price; // price of cd
	public int itemno; // item number of cd
	public int nstock; // how many units in stock	
	private int i; // cd counter for array
	public float value; // value for single cd inventory

	
	//Compact disk class constructor
	public Compactdisk()
	
		// 4 fields need to be set up
		{ 
		name = "";
		price = 0;
		itemno = 0;
		nstock = 0;
		i = 0;
		value = 0;
		}
		
		// set values
	   public void setName(String diskName)
	   {
	   name = diskName;
	   }
		public void setPrice(float cdPrice)
	   {
	   price = cdPrice;
	   }
		public void setItemno(int cdItemno)
	   {
	   itemno = cdItemno;
	   }
		 public void setNstock(int cdStock)
	   {
	   nstock = cdStock;
	   }
		public void setValue(float cdValue)
		{
		value = cdValue;
		}
		public void seti(int Count)
		{
		i = Count;
		}
	
		
	   // return values
		public String getName()
		{	
		return (name);
		}
		public float getPrice()
		{	
		return (price);
		}
		public int getItemno()
		{	
		return (itemno);
		}
		public int getNstock()
		{	
		return (nstock);
		}
		public int compareTo(Object in)
	   {
	   return ((Comparable)name.toLowerCase()).compareTo((Comparable)((Compactdisk)in).getName().toLowerCase());
	   }
					
		// returns indivudual inventory value for a disk
	   public float getValue()
	   {
	   return (price * nstock);
	   }
		
			
	   
		
			
}// end class

I am at a loss

0

I am sure this is a symptom of the problem, but the only thing that IS showing up is Name, Artist and .05 for restocking fee, everything else, even price that I enter shows up as 0. Something has to be pointed to the wrong place I think.

0

I am so stupid. I left price out of the add statement. How was anything supposed to work without the price in there.

        // Create cd to add
        CdwArtist newCD = new CdwArtist();
        newCD.setArtist(artistField.getText());
        newCD.setName(cdNameField.getText());   
        newCD.setItemno(Integer.parseInt(itemField.getText()));
        newCD.setNstock(Integer.parseInt(nstockField.getText()));
        newCD.setPrice(Integer.parseInt(priceField.getText()));

Quick question though, how do I make my output carry two zeros after the decimal. Right now it has $2.0, or $4.0 and that is not right for monetary considerartion.

Edited by pritaeas: fixed formatting

0

I am sure this is a symptom of the problem, but the only thing that IS showing up is Name, Artist and .05 for restocking fee, everything else, even price that I enter shows up as 0. Something has to be pointed to the wrong place I think.

Yes, it sounds like your setters are not being called properly.

0

I am sure this is a symptom of the problem, but the only thing that IS showing up is Name, Artist and .05 for restocking fee, everything else, even price that I enter shows up as 0. Something has to be pointed to the wrong place I think.

I just tested the code you posted up above, commented in the restocking line (which was commented out in your Inventory2), replaced your methods with the methods that I modified above, and your program worked fine (I think).

Artist: f CD Name: f Item Number: 1 Price: $20.0 In Stock: 22 Stock Value: $0.0 Restocking Fee: $5.0 Total Inventory Value: $2640.0

Is an example output...

Granted, the math is wrong I think (or I don't understand what you're trying to do with it), but it is displaying values for all your toString fields. The value field I entered as 0 (just for a test).

Is that what you wanted it to do?

0

That is not exactly what I get, but I have my math and all correct now (see above post), my problem is the zero after the decimal point. That is a monetary field and should carry two zeros.
I try entering 20.00 in the price field and the program explodes also, so I need some way to input and export these values as dollar type amounts.

Does that make sense?

0

In an effort to correct the problem I have gone through and changed all participating variables to read as such, hoping it was simple formatting issue.

public Compactdisk()

    // 4 fields need to be set up
    { 
    name = "";
    price = .00f;
    itemno = 0;
    nstock = 0;
    i = 0;
    value = .00f;
    }

Nothing changes. The math is still right, but $20.00 shows as $20.0

If I put in an off price, such as 10.51 everything except the total is then right. The total will carry out how ever many spots it needs. 50.2345555 for example.
I just need these fields to round to a two digit decimal place.

Edited by mike_2000_17: Fixed formatting

0

One issue may be that you are creating FormattedTextFields with a null format. You pass an uninitialized NumberFormat variable to them. Correcting those though will not affect what you are getting in the toString output. To make those conform to a format you will need to use a DecimalFormat such as new DecimalFormat("0.00") which has a format() function that you can use to present the string representation that you need.
http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html

0

I looked at the documentation, and I know it should help, but it does not. I just do not yet understand how to read those parameters on that site and mine down to what I need.

I do however understand new DecimalFormat("0.00") ... I just do not know how or where to impliment.

In the runnable? Maybe in CdwArtist (as that is where my toString is? Maybe even in Inventory as that is my primary class? I want to put it right under my // new JLIst model
format = new DecimalFormat("0.00") ??

Even if that is right, how would I show an individual parameter to utilize the new format?

0

I looked at the documentation, and I know it should help, but it does not. I just do not yet understand how to read those parameters on that site and mine down to what I need.

I understand it's a lot to swallow, but keep in mind, if you intend to do much with programming at all, in any language, you'll eventually need to get comfortable with using the api docs and Sun has a lot of links in there to tutorials that present basic usage.

You may also find the following tutorial in debugging helpful in squashing some of the errors you come across:
http://www.seas.upenn.edu/~cse1xx/projects/Debug/GuideToDebugging/beginners.html

0

You are right, and I know it. If I am ever to be self sufficient I must learn the APIs. This is not something that was covered before class, not something covered during class, we are just kind of told they are there if we need them during the first week, and left to discover them on our own. I do not really have a problem with that, but when thrown neck deep in coding assignments trying to figure them out just confuses me more. I graduate in two more weeks, and at that time I plan to actually begin learning all these things that will actaully make me a better programmer.
Immediate problem for me is my grade, so long term has to be set aside for now, as frustrating as it may be.
I did go through the tutorial on formatting, and it was pretty helpful in some respects.
I am trying this:

import java.util.*;

public class CdwArtist extends Compactdisk
{
 
 private String artist; // artist performing on cd
 private float restock; // restocking percentage
 private float total; // total of all inventory stock
 

  
 // Artist constructor
  public CdwArtist()
 {
 artist = "";
 restock = .05f;
 total = .00f;
 }
 
 public CdwArtist(String in)
 {
 artist=in;
 }
 
 // get values
 public void setArtist(String in)
 {
 artist=in;
 }
 public void setRestock(float cdRestock)
 {
 restock = cdRestock;
 }
 public void setTotal(float cdtotal)
 {
 total = cdtotal;
 }
  
 // return value
 public String getArtist()
 {
 return (artist);
 }
	
 // returns indivudual inventory value for a disk
 public float getTotal()
 {
 return(price * nstock)+((price * nstock)* restock);
 }	
	
	static public void customFormat(String pattern, float value, float price, float total ) 
	{ 
	DecimalFormat myFormatter = new DecimalFormat(pattern); 
	String output = myFormatter.format(value); 
	System.out.println(value + " " + pattern + " " + output); 
	} 

	
	//new toString
	public String toString()
	{
	return "Artist: " + artist +
	" CD Name: " + name + 
	" Item Number: " + itemno + 
	" Price: $" +price + 
	" In Stock: " + nstock + 
	" Stock Value: $" + getValue() + 
	" Restocking Fee: $" + restock + 
	" Total Inventory Value: $"  + getTotal();
	}
} //End Class

and it compiles, but I do not think "pattern" and "value" are literal are they? Should I not have ###.## for my pattern, and 0.00 for my value?
They error out when I do that of course, so I know it is wrong, and I think I understand what I have written, should take a parameter and put it in a certain pattern and value, but now the tutorial starts losing me as to exactly what pattern and value are, and how to apply them to what I have here. I would not think I would have an output line in there at all, but like I said, I start getting really confused at this point.

0

For your purpose, all you really need is one formatter in the cd class

DecimalFormat formatter = new DecimalFormat("$0.00");

and used in toString() like

" Stock Value: " + formatter.format(getValue()) +
0

Wow. For once I was actually thinking too much. That was way too simple. I tend to over analize when I start getting tired.
One more question before I close this out and get some sleep, maybe take a day or two off before I start working on new buttons to put in here, but I am going to try and format the output to be more aestetic than one continuous line, which will mean I only want to display one cd at a time in the output box.
I have been doing stupid crap to it for two days trying to impliment this, I know it has to be simple, but I am just not thinking about it right.
Here is my latest attempt ... I know I need to call newCD right?

public String toString()
	{
	return listModel.newCD("Artist: " + artist +
	" CD Name: " + name + 
	" Item Number: " + itemno + 
	" Price: $" + formatter.format(getPrice()) + 
	" In Stock: " + nstock + 
	" Stock Value: $" + formatter.format(getValue())  + 
	" Restocking Fee: $" + restock + 
	" Total Inventory Value: $"  + formatter.format(getTotal()));

I think this is the closest I have to being right, because it only gives me the symbol error, every other thing I have tried really blows up.

0

listModel does not have a newCD method. It has a get(int index) method, which you have used before. Just retrieve the object like you have in the past and call toString() on it.

0

OK, you have to be talking about currCD that I kept trying to stick everywhere.
Now that I think I know the right place to put it I cannot get it to work.

return ("Artist: " + currCD.getArtist() +

I have tried it three or four different ways thinking I just had the syntax wrong, but I am now beginning to thing that maybe once again I am just using the wrong one.

0

of course not, if you could I would be a programming genious, because I try it 10 times a day it seams.
I also tried putting
CdwArtist currentCD = (CdwArtist) listModel.get( currCD );
back into my inventory class, and calling
return ("Artist: " + (listModel.get(currCD)artist)
but it errors also.
I think I am just grabbing at straws now that I am in the 11th hour and this project is due any minute now.

0

toString() is a method of the CdwArtist class. You do not need to access the list at all - it's purpose is to return information about the object instance itself. Methods that need that information call aCdObject.toString() to get it's string value. You seem to be running yourself in circles there but I cannot see why. You had a working toString() on the class so I'm not sure what you are trying to change.

0

Right now, each time I add a cd it lists it in the panel. When I add the next one, it keeps the first one in the panel and lists the second one underneath it.
I only want one listed at a time. When I hit add, I want the last one out of the panel and ONLY the new one in there.
I thought that would be accomplished by fiddling with the toString, but now that you say that it seems like more of a panel issue.

0

Well, that is because it is a JList, which is suppose to display multiple items. toString() merely determines what the list displays for each item you add to it.

If you just want to show a single description then all you need is a text area or a label to show the description for a particular object.

0

Text area?
I have never heard of this.
Can I make it display the full contents of the currCD, whichever one that may be?

Care to quickly show? Or is this a long drawn out process that will only confuse me further.

This is the final step of this project, "The GUI should display the information one product at a time"

1
JTextArea jta = new JTextArea();
jta.append("CD Description");
//Or you can do
jta.setText("CD Description");

Preferences up to you just look up API

Votes + Comments
Thanks for trying.
0

Thanks Peter, but I think that is going to be too little too late. I need to get my project submitted, do not really have the time to try and learn and impliment that.
I do not want to sit here and just beg for code so I am going to close this thread.
Hopefully next week will be a better week.
I appreciate everyones help.

0

Even after I turned it in I kept working on it. I did discover one interesting piece of information, you can use html tags to format toString output. <br> being a newline command in html.

public String toString()
	{
	return "<html>Artist: " + artist +
	" CD Name: " + name + 
	"<br> Item Number: " + itemno + 
	" Price: " + formatter.format(getPrice()) + 
	"<br> In Stock: " + nstock + 
	" Stock Value: " + formatter.format(getValue())  + 
	"<br> Restocking Fee: $" + restock + 
	"<br> Total Inventory Value: "  + formatter.format(getTotal())+ "</html>";
	}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.