4 Replies - 7408 Views - Last Post: 06 March 2009 - 07:39 PM Rate Topic: -----

#1 MarkyMarc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-March 09

How to get GUI to display output in decimal/currency format?

Posted 06 March 2009 - 07:06 PM

My code is working perfectly, all except that after I removed the system.out.println to replace with the display GUI, it removed my formatting so it is not displaying the output in currency format. Can someone help me format it so it displays correctly?

Thanks so much. Here is the code for the 3 files:
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
// Sort array alphabetically and get total value of inventory
public class Inventory
{ //begin class inventory

//formats output to currency format
		public static DecimalFormat formatter = new DecimalFormat("$##,###.00");

	   public static void main(String[] args)
	{ //begin main method


		

		DVD productarray[] = new DVD [9]; //array declaration
		  productarray[0] = new DVD (1001, "Rocky", 5, 21.99, "1976");
		  //set first array
		  productarray[1] = new DVD (1002, "Rocky II", 7, 18.95, "1978");
		  //set second array
		  productarray[2] = new DVD (1003, "Rocky III", 12, 11.95, "1983");
		  //set third array
		  productarray[3] = new DVD (1004, "Rocky IV", 25, 12.95, "1985");
		  //set fourth array
		  productarray[4] = new DVD (1005, "Rocky V", 30, 9.95,
				  "1990");
		   productarray[5] = new DVD (1006, "Rocky Balboa", 19, 17.95,
				  "2006");
		   productarray[6] = new DVD (1007, "Arnold", 23, 5.25,
				  "1974");
		   productarray[7] = new DVD (1008, "Honey I Shrunk the Kids", 5, 17.95, "1989");
productarray[8] = new DVD (1009, "Gone With the Wind", 10, 25.75, "1939");

		  //set fifth array



		productarray[productarray.length -1].arraysort(productarray);
		 //call sort method

JTextArea textArea1 = new JTextArea();

textArea1.append("Welcome to Inventory Program Part 5\n" +
				"Melody's DVD Collection\n"); 
				//greeting

for(int i =0; i < productarray.length; i++)
				{ //begin for loop


				textArea1.append("\nItem Number: " +
						productarray[i].getItemNumber()
				+"\n" +
		  "DVD Title: " + productarray[i].getProductName()+ "\n" +
		  "Number in Stock: " + productarray[i].getInStock()+ "\n"
		  + "Price per DVD $: " + productarray[i].getItemPrice()
		  + "\n" + "Value of Inventory for this DVD: " +
		  productarray[i].CalculateValuePrice() + "\n" +
		 "Year of movie: " + productarray[i].getYear() +
		 "\n" + "Restocking fee: " +
		 productarray[i].getRestockFee() + "\n");
				//set display parameters
				} //end for loop

textArea1.append("\nTotal Inventory Value: " +
 productarray[productarray.length -1].CalculateTotalValue(productarray));
		 //display inventory total value

JFrame frame = new JFrame();
		frame.getContentPane().add(new JScrollPane(textArea1));
		frame.setSize(600,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);


	} //end main method


		
		
	 
	  

	   
		
} //end class inventory



import java.text.DecimalFormat;
/**
 *
 * @author M
 */
public abstract class Product
{ //begin product class

	//formats output to currency format
	   static DecimalFormat formatter = new DecimalFormat("$##,###.00");

	private long ItemNumber; //declare item number
	private String ProductName; //declare product name
	private int InStock; //declare number in stock
	private double ItemPrice; //declare price of each
	private double ValuePrice; //declare value price
	private static double TotalValue; //declare total value


public Product (long ItemNumber, String ProductName,
		int InStock, double ItemPrice)//constructor
{ //begin constructor and set constructor values
	this.ItemNumber = ItemNumber;
	this.ProductName = ProductName;
	this.InStock = InStock;
	this.ItemPrice = ItemPrice;
 } //end constructor


	public double CalculateTotalValue (Product productarray[])
	{//begin method
		for (int i = 0; i < productarray.length; i++)
		{//begin for loop
			TotalValue = TotalValue + productarray[i].getValuePrice();
			//statment to add value price through array
		}//end for loop
		return TotalValue;
	}//end method


	public abstract double CalculateValuePrice ();

	public double getValuePrice ()
	{//begin method
		return ValuePrice;
	}//end method


	public long getItemNumber () //method to get item number
	{ //begin method
		return ItemNumber;
	} //end method


	public String getProductName () //method to get product name
	{ //begin method
		return ProductName;
	} //end method


	public int getInStock () //method to get products in stock
	{ //begin method
		return InStock;
	} //end method

	public double ItemValue()  // Method to calculate the value of the inventory
				{
					return (double) ItemPrice * InStock;
				}


	public double getItemPrice () //method to get item price
	{ //begin method
		return ItemPrice;
	} //end method
	public void setValuePrice(double ValuePrice)
	{ //begin method
		this.ValuePrice = ValuePrice;
	} //end method


	 

	public void arraysort(Product productarray[])
	{ //begin method
		for(int i = 0; i < productarray.length - 1; i++)
		{//begin for loop
			for(int c = i + 1; c < productarray.length; c++)
			{ //begin for loop
				String name1 = productarray[i].ProductName;
				String name2 = productarray[c].ProductName;
				if(name1.compareToIgnoreCase(name2) > 0)
				{ //begin if statement
					Product temp = productarray[i];
					productarray[i] = productarray[c];
					productarray[c] = temp;
				}//end if statement
			}//end for loop

		}//end for loop

	} //end method

} //end product class




import java.text.DecimalFormat;
/**
 *
 * @author M
 */
public class DVD extends Product
{//begin subclass
	DecimalFormat format = new DecimalFormat("$##,###.00");

	private String Year; //set variable for type of movie
	private double RestockFee; //set variable for restocking fee

	public DVD (long ItemNumber, String ProductName,
		int InStock, double ItemPrice,String Year)
	{ //begin constructor
	  super(ItemNumber, ProductName, InStock, ItemPrice);
	  this.Year = Year;
	} //end constructor

	public String getYear()
	{//begin method
		return Year;
	}//end method

	public double getRestockFee()
	{//begin method
		return RestockFee;
	}//end method

	@ Override

	public double CalculateValuePrice()
	{//begin method
		double tempValue = getInStock() * getItemPrice();
		RestockFee = tempValue *.05; //multiplication of 5% to value
		setValuePrice (tempValue + RestockFee);//additon
		return getValuePrice();
	}//end method



  @Override


  public String toString()
				{
					return "\nThe year of the movie is: " + Year;
				}

}//end subclass



Is This A Good Question/Topic? 0
  • +

Replies To: How to get GUI to display output in decimal/currency format?

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: How to get GUI to display output in decimal/currency format?

Posted 06 March 2009 - 07:18 PM

If im understanding you correctly, You just need it to print with a dollar sign and 2 decimal points correct? If that's so, us a printf statement.
System.out.printf("$%5.2f", theNumberYouWantFormatted);



Hope that helps, if im misunderstanding please clarify B)
Was This Post Helpful? 0
  • +
  • -

#3 MarkyMarc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-March 09

Re: How to get GUI to display output in decimal/currency format?

Posted 06 March 2009 - 07:28 PM

View PostFuzzyness, on 6 Mar, 2009 - 06:18 PM, said:

If im understanding you correctly, You just need it to print with a dollar sign and 2 decimal points correct? If that's so, us a printf statement.
System.out.printf("$%5.2f", theNumberYouWantFormatted);



Hope that helps, if im misunderstanding please clarify B)


I can't do it that way because I replaced the system.out.printf with the textArea1.append to get it to appear in the GUI frame, so I can't used system.out.printf at all.
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: How to get GUI to display output in decimal/currency format?

Posted 06 March 2009 - 07:32 PM

I see I see, well i'm sorry but I wont be able to help you. I haven't hit that segment of Java in my ap class at school yet. Haven't learned GUI yet or JFrame yet . Goodluck with the program!

This post has been edited by Fuzzyness: 06 March 2009 - 07:33 PM

Was This Post Helpful? 0
  • +
  • -

#5 MarkyMarc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-March 09

Re: How to get GUI to display output in decimal/currency format?

Posted 06 March 2009 - 07:39 PM

View PostFuzzyness, on 6 Mar, 2009 - 06:32 PM, said:

I see I see, well i'm sorry but I wont be able to help you. I haven't hit that segment of Java in my ap class at school yet. Haven't learned GUI yet or JFrame yet . Goodluck with the program!

Thanks anyway for attempting to help. I appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1