kspider811 0 Newbie Poster

I can not figure out how to add to my array. The invntory class and dvdrate subclass is not to change. So within my InventoryProgram2 I am told to copy a temp array with the addition of an element to the actual array using the add button. This is so I can browse through the array using the first, last,next,and previous buttons.

// Inventory subclass
// rating feature

public class dvdrate extends Inventory
{
	private String dvdRating;
	private double fee;


		public dvdrate(String ProductName, String Item, int Units, double Price, String dvdRating)
		{
			super(ProductName, Item, Units, Price);
			this.dvdRating = dvdRating;
		}

		public void setdvdRating (String dvdRating)
		{
			this.dvdRating = dvdRating;
		}

		public String getdvdRating()
		{
			return dvdRating;
		}

		public void setfee (double fee)
		{
			this.fee = fee;
		}

		public double getfee()
		{
			return  getPrice()*0.05;
		}

		public static double TotalInventoryValue(dvdrate[] dvds)
		{

			double TotalInventoryValue=0;
			for (int a=0;a < dvds.length;a++)
			{
				TotalInventoryValue += dvds[a].getInventoryValue();

			}

			return TotalInventoryValue;
		}
		public double getInventoryValue()
		{
			double InventoryValue = 0;
			InventoryValue = getUnits()*getPrice();
			return InventoryValue + (InventoryValue*0.05);
		}
}//end class
//week 5 inventory.java
//inventory class that contains item number, name of product, number of units, and price per unit.

public class Inventory
{
	private String ProductName;
	private String Item;
	private	int Units;
	private double Price;
	private double InventoryValue;
	private double TotalInventoryValue;
    private int a;
	// Inventory constructor
	public Inventory()
	{
		this(null,null,0,0);
	}// end Inventroy constructor



	public Inventory(String ProductName, String Item, int Units, double Price)
	{
		setInventory1(ProductName, Item, Units, Price);
	}


	public String toString()
	{
		return String.format("%s,%s,%s,%s,",this.ProductName, this.Item, this.Units, this.Price);
	}



	public void setInventory1( String ProductName, String Item, int Units, double Price)
	{
		setProductName(ProductName);
		setItem(Item);
		setUnits(Units);
		setPrice(Price);
	}

		//set productName
		public void setProductName(String ProductName)
		{
			this.ProductName = ProductName;
		}//end set ProductName

		//retrieve productName
		public String getProductName()
		{

			return this.ProductName;
		}//end getProductName

		//set Item
		public void setItem(String Item)
		{
			this.Item = Item;
		}// end setItem

		//retrieve Item
		public String getItem()
		{
			return this.Item;
		}// end getItem

		//set Units
		public void setUnits( int Units)
		{
			this.Units = Units;
		}//set Units

		//retrieve Units
		public int getUnits()
		{
			return this.Units;
		}//end get Units

		//set Price
		public void setPrice(double Price)
		{
			this.Price = Price;
		}// end setPrice

		//retrieve Price
		public double getPrice()
		{
			return this.Price;
		}//end getPrice

		public double getInventoryValue()
		{
			InventoryValue = Units*Price;
			return this.InventoryValue;
		}

		public static double TotalInventoryValue(Inventory[] dvds)
		{
			double TotalInventoryValue=0;
			for (int a=0;a < dvds.length;a++)
			{
				TotalInventoryValue += dvds[a].InventoryValue;

			}

			return TotalInventoryValue;
		}





		public static Inventory[]SortInventory(Inventory[] dvds)

			{
			Inventory tmp;
			for (int a=0;a<dvds.length;a++)
			{
				for(int b=a+1;b<dvds.length;b++)
					{
						String s1 =dvds[a].getProductName();
						String s2 =dvds[b].getProductName();
							if(s1.compareTo(s2)>0)
							{
								tmp = dvds[a];
								dvds[a] = dvds[b];
								dvds[b] = tmp;
							}
					}
			}

			return dvds;
			}


}//end class
// inventory test program
//Invetory object and call
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.io.RandomAccessFile;
import java.io.IOException;

public class InventoryProgram2 extends JFrame
{
	private JPanel buttonJPanel;
	private JPanel buttonJPanel2;
	private JPanel labels;
	private JPanel results;
	private JButton First;
	private JButton Previous;
	private JButton Next;
	private JButton Last;
	private JButton Loadfile;
	private JButton Add;
	private JButton	Modify;
	private JButton	Delete;
	private JButton Search;
	private JButton Save;
	private JLabel logolabel;
	private JLabel	labela[];
	private JTextField Item;
	private JTextField ProductName;
	private JTextField Rating;
	private JTextField Units;
	private JTextField Price;
	private JTextField Fee;
	private JTextField InventoryValue;
	private JTextField TotalValue;
	private JTextField Index;
	private int a;


	public InventoryProgram2()
	{
		super( "Inventory of DVD Movies" );

		labela = new JLabel[9];
		labels = new JPanel();
		labels.setLayout(new GridLayout(9,1,2,2));
		for (int b = 0; b < labela.length;b++)
		{
			labela[0] = new JLabel("<html><font size=+1> Index</html>");
			labela[1] = new JLabel("<html><font size=+1> ItemID</html>");
			labela[2] = new JLabel("<html><font size=+1> Name</html>");
			labela[3] = new JLabel("<html><font size=+1> Rating</html>");
			labela[4] = new JLabel("<html><font size=+1> # in Stock</html>");
			labela[5] = new JLabel("<html><font size=+1> Price</html>");
			labela[6] = new JLabel("<html><font size=+1> Restocking Fee (5%)</html>");
			labela[7] = new JLabel("<html><font size=+1> Inventory Value with fee</html>");
			labela[8] = new JLabel("<html><font size=+1> Total Value with fee(for all the items)</html>");
			labels.add(labela[b]);
		}
		add(labels, BorderLayout.WEST);




		results = new JPanel();
		results.setLayout(new GridLayout(9,1,2,2));

		Index = new JTextField(23);
		Index .setEditable(false);
		results.add(Index );

		Item = new JTextField(23);
		Item.setEditable(false);
		results.add(Item);

		ProductName = new JTextField();
		ProductName.setEditable(false);
		results.add(ProductName);

		Rating = new JTextField();
		Rating.setEditable(false);
		results.add(Rating);

		Units = new JTextField();
		Units.setEditable(false);
		results.add(Units);

		Price = new JTextField();
		Price.setEditable(false);
		results.add(Price);

		Fee = new JTextField();
		Fee.setEditable(false);
		results.add(Fee);

		InventoryValue = new JTextField();
		InventoryValue.setEditable(false);
		results.add(InventoryValue);

		TotalValue = new JTextField();
		TotalValue.setEditable(false);
		results.add(TotalValue);

		buttonJPanel = new JPanel();
		Icon logo = new ImageIcon(getClass().getResource("DSC00228.gif"));
		logolabel = new JLabel(logo);
		buttonJPanel.add( logolabel);

		First = new JButton("First");
		buttonJPanel.add(First);
		Previous = new JButton("Previous");
		buttonJPanel.add(Previous);
		Next = new JButton("Next");
		buttonJPanel.add(Next);
		Last = new JButton("Last");
		buttonJPanel.add(Last);

		ButtonHandler handler = new ButtonHandler();
		First.addActionListener(handler);
		Previous.addActionListener(handler);
		Next.addActionListener(handler);
		Last.addActionListener(handler);



		buttonJPanel2 = new JPanel();
		Loadfile = new JButton("Load file");
		buttonJPanel2.add(Loadfile);
		Add = new JButton("Add");
		buttonJPanel2.add(Add);
		Modify = new JButton("Modify");
		buttonJPanel2.add(Modify);
		Delete = new JButton("Delete");
		buttonJPanel2.add(Delete);
		Search = new JButton("Search");
		buttonJPanel2.add(Search);
		Save = new JButton("Save");
		buttonJPanel2.add(Save);


		Loadfile.addActionListener(handler);
		Add.addActionListener(handler);
		Modify.addActionListener(handler);
		Delete.addActionListener(handler);
		Search.addActionListener(handler);
		Save.addActionListener(handler);

		add(results, BorderLayout.EAST);
		add( buttonJPanel, BorderLayout.SOUTH );
		add(buttonJPanel2, BorderLayout.NORTH);

dvds[0] = new dvdrate("Superman","111",11,12.99,"PG");
dvds[1] = new dvdrate("Batman Returns","222",9,11.99,"PG 13");
dvds[2] = new dvdrate("Madagascar","333",10,10.99,"G");
dvds[3] = new dvdrate("Cars","444",15,13.99,"G");



}//end public constructor
dvdrate[] dvds= new dvdrate[4];

private class ButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{

				Inventory.SortInventory(dvds);



				event.getActionCommand();

				if (event.getActionCommand() == "Next")
				{
					a++;
					if ( a >= dvds.length)
					{
						a = 0;
					}
				}
				if (event.getActionCommand() == "Previous")
				{
					a--;
					if ( a <0)
					{
						a =dvds.length;
						a--;
					}
				}

				if (event.getActionCommand()== "Last")
				{
					a = dvds.length-1;

				}

				if (event.getActionCommand()== "First")
				{
					a = 0;
				}

				if ( event.getActionCommand()=="Add")
				{
					a = dvds.length;
					Index.setText(String.valueOf(a));

					ProductName.setEditable(true);
					ProductName.setText("");
					Item.setEditable(true);
					Item.setText("");
					Rating.setEditable(true);
					Rating.setText("");
					Units.setEditable(true);
					Units.setText(String.valueOf(0));
					Price.setEditable(true);
					Price.setText(String.format("$0.00"));
					Add.setText("Click to Add");
				}
				if ( event.getActionCommand()=="Click to Add")
				{
					ProductName.setEditable(false);
					Item.setEditable(false);
					Rating.setEditable(false);
					Units.setEditable(false);
					Price.setEditable(false);
					Add.setText("Add");
				}
				if (event.getActionCommand()=="Modify")
				{
					Modify.setText("Click to Modify!");
					ProductName.setEditable(true);
					Item.setEditable(true);
					Rating.setEditable(true);
					Units.setEditable(true);
					Price.setEditable(true);
				}
				if (event.getActionCommand()=="Click to Modify!")
				{
					ProductName.setEditable(false);
					Item.setEditable(false);
					Rating.setEditable(false);
					Units.setEditable(false);
					Price.setEditable(false);
					Modify.setText("Modify");
				}
				if  (event.getActionCommand()=="Search")
				{
					ProductName.setEditable(true);
					Search.setText("GO");
				}
				if (event.getActionCommand()=="GO")
				{
					ProductName.setEditable(false);
					Search.setText("Search");

				}


				Index.setText(String.valueOf(a));
				Item.setText( dvds[a].getItem());
				ProductName.setText(dvds[a].getProductName());
				Rating.setText(dvds[a].getdvdRating());
				Units.setText(String.valueOf(dvds[a].getUnits()));
				Price.setText(String.format("$%.2f",dvds[a].getPrice()));
				Fee.setText(String.format("$%.2f",dvds[a].getfee()));
				InventoryValue.setText(String.format("$%.2f",dvds[a].getInventoryValue()));
				TotalValue.setText(String.format("$%.2f",dvdrate.TotalInventoryValue(dvds)));

			}
		}
public static void main( String args[])
	{

//main method begins

InventoryProgram2 b = new InventoryProgram2(); // create LabelFrame
b.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
b.setSize(600,380); // set frame size
b.setVisible( true );



	}//end method main

}//end Class InventoryProgram