I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone help? my script is below. thank you
[code=java]
import java.awt.*; //import all java.awt
import java.awt.event. *; //import all java.awt.event
import java.util.*; //import all java.util
import javax.swing.*; //import all javax.swing
class Product //start Product superclass
{
public String[] ItemName; //item's name
public int[] ItemNumber; //items's unique product number
public int[] ItemQuantity; //item's quantity in stock
public double[] ItemPrice; //item's price per
public Product(String[] name, int[] number, int[] quantity, double[] price) //product constructor
{
ItemName = name; //set ItemName to name
ItemNumber = number; //set ItemNumber to number
ItemQuantity = quantity; //set ItemQuantity to quantity
ItemPrice = price; //set ItemPrice to price
} //end Product constructor
public Product SortedArray(Pro duct InvPart2) //start Product method SortedArray
{
String[] name=new String[InvPart2.ItemNa me.length]; //new name for sorting array
int [] number = new int[InvPart2.ItemNu mber.length]; //new number for sorting array
int[] quantity = new int[InvPart2.ItemQu antity.length]; //new quantity for sorting array
double [] price = new double [InvPart2.ItemPr ice.length]; //new price for sorting array
name = (String[])InvPart2.ItemN ame.clone(); // place name in sorting array
Arrays.sort(nam e); //sort by name
for (int counter = 0; counter < name.length; counter++) //loop and counter for sorting array
{
for(int counter2=0;coun ter2<name.lengt h;counter2++)
//loop and counter to match unsorted array and sorted one
{
if(name[counter].equals(InvPart 2.ItemName[counter2]))
//if statement for when a match occurs
{
quantity[counter]=InvPart2.ItemQ uantity[counter2];
//set quantity equal to sorted array quantity
price[counter]=InvPart2.ItemP rice[counter2];
//set price equal to sorted array price
number[counter]=InvPart2.ItemN umber[counter2];
//set number equal to sorted array number
break; //break for if statement
} //end if statement
} //end for loop counter2
} //end for loop counter
Product SortedProductAr ray = new Product (name, number, quantity, price);
//new sorted product array replace old product array
return SortedProductAr ray; //return new product array sorted
} //end Product method SortedArray
public double TotalInvWorth(i nt[] quantity, double[] price) //start Product method TotalInvWorth
{
double total=0.00F; //set double total = 0
for (int counter = 0; counter < quantity.length ; counter++)
//loop and counter to multiply each quantity x price in array
{
double perprodworth=qu antity[counter]*price[counter];
// multiply quantity x price per counter in array = perprodworth
total=total+per prodworth; //add perprodworth to total
}
return total; //return total in TotalInvWorth
} //end Product method TotalInvWorth
} //end Product superclass
class DVD extends Product //start DVD subclass of Product
{
public int[] NumberOfDisc; //new feature number of disc in movie
public DVD(String[] name, int[] number, int[] quantity, double[] price, int[] numdisc)
//dvd constructor
{
super(name, number, quantity, price); //variables from superclass Product
NumberOfDisc=nu mdisc; //new variable number of disc per dvd
} //end dvd constructor
public DVD SortedArray(DVD Inventory5) //start DVD method SortedArray
{
int [] numdisc = new int[Inventory5.Numb erOfDisc.length]; //new number for sorting array
Product DVDProduct=new Product(Invento ry5.ItemName,In ventory5.ItemNu mber,
Inventory5.Item Quantity,Invent ory5.ItemPrice) ; //set DVDProduct equal to superclass array
Product SortedDVDProduc t=super.SortedA rray(DVDProduct );
for (int counter = 0; counter < SortedDVDProduc t.ItemName.leng th; counter++) //loop and counter for sorting array
{
for(int counter2=0;coun ter2<Inventory5 .ItemName.lengt h;counter2++)
//loop and counter to match unsorted array and sorted one
{
if(SortedDVDPro duct.ItemName[counter].equals(Invento ry5.ItemName[counter2]))
//if statement for when a match occurs
{
numdisc[counter]=Inventory5.Num berOfDisc[counter2];
break; //break for if statement
} //end if statement
} //end counter2 loop
} //end counter loop
DVD SortedProductAr ray = new DVD(SortedDVDPr oduct.ItemName, SortedDVDProduc t.ItemNumber,
SortedDVDProduc t.ItemQuantity, SortedDVDProduc t.ItemPrice,num disc);
//set SortedProductAr ray equal to superclass array plus new feature
return SortedProductAr ray; //return sortedproductar ray
} //end DVD method SortedArray
public double DVDRestock () //start DVD method DVDRestock
{
double totalInvworthbe forerestockingf ee=super.TotalI nvWorth(ItemQua ntity, ItemPrice);
//set totalInvWorthBe foreRestockingF ee to TotalInvWorth in superclass
double restockfee=0.00 F; //set restock fee to 0
double totalinvworthwi threstockfee=0. 00F; //set toatal worth after fee to 0
restockfee = 0.05F*totalInvw orthbeforeresto ckingfee; //restock fee = 5% to invworth before fee
totalinvworthwi threstockfee=re stockfee+totalI nvworthbeforere stockingfee; //add 5% to original inv worth
return totalinvworthwi threstockfee; //return inv total with fee
} //end DVD method DVDRestock
} //end DVD subclass
public class Inventory5 extends JFrame implements ActionListener
{ // start class Inventory5
public Inventory5()
{ //start Inventory5 constructor
this.initialize (); //set initialize
} //end Inventory5 constructor
private JTextArea text = new JTextArea(); //initializing jtextarea
private JButton firstBtn; //initializing first Jbutton
private JButton prevBtn; //initializing previous Jbutton
private JButton nextBtn; //initializing next Jbutton
private JButton lastBtn; //initializing last Jbutton
private JButton logoBtn; //initializing logo Jbutton
private ImageIcon logo = new ImageIcon("logo .gif"); //initializing picture for logo Jbutton
private int buttonCounter; //initializing buttonCounter for place in array
public void initialize() //start initialize method
{
firstBtn=new JButton("First" ); //set firstBtn to JButton First
prevBtn=new JButton("Previo us"); //set prevBtn to JButton Previous
nextBtn=new JButton("Next") ; //set nextBtn to JButton Next
lastBtn=new JButton("Last") ; //set lastBtn to JButton Last
logoBtn = new JButton(logo); //set logoBtn to JButton logo
this.getContent Pane().add(this .logoBtn); //add logoBtn to content pane
this.logoBtn.se tBounds(0, 0, 75, 75); //set logoBtn's size
this.logoBtn.se tBorderPainted( false); //turn border off for logoBtn
this.getContent Pane().add(this .firstBtn); //add firstBtn to content pane
this.firstBtn.s etBounds(40, 300, 100, 30); //set firstBtn's size
this.firstBtn.s etActionCommand ("FIRST"); //set action command for firstBtn to FIRST
this.firstBtn.a ddActionListene r(this); //set action lisnter for firstBtn
this.getContent Pane().add(this .prevBtn); //add prevBtn to content pane
this.prevBtn.se tBounds(150, 300, 100, 30); //set prevBtn's size
this.prevBtn.se tActionCommand( "PREV"); //set action command for prevBtn to PREV
this.prevBtn.ad dActionListener (this); //set action lisnter for prevBtn
this.getContent Pane().add(this .nextBtn); //add nextBtn to content pane
this.nextBtn.se tBounds(260, 300, 100, 30); //set nextBtn's size
this.nextBtn.se tActionCommand( "NEXT"); //set action command for nextBtn to NEXT
this.nextBtn.ad dActionListener (this); //set action lisnter for nextBtn
this.getContent Pane().add(this .lastBtn); //add lastBtn to content pane
this.lastBtn.se tBounds(370, 300, 100, 30); //set lastBtn's size
this.lastBtn.se tActionCommand( "LAST"); //set action command for lastBtn to LAST
this.lastBtn.ad dActionListener (this); //set action lisnter for lastBtn
this.getContent Pane().add(this .text,BorderLay out.CENTER); //add text to window
this.setSize(52 0, 400); //set frame size
this.setResizab le(false); //set frame resizable
this.setVisible (true); //set frame visiable
this.setDefault CloseOperation( JFrame.EXIT_ON_ CLOSE); //add to properly close app
} //end initialize method
public static void main(String[] args) //start main method
{
Inventory5 part4=new Inventory5();
part4.processin g(part4.buttonC ounter);
//needed to call processing method
} //end main method
private void processing(int counter) //start processing method
{
//setting static data for variables in each array
String[] name = {"Star Trek", "Star Gate", "Fifth Element", "Armageddon ", "Star Wars "};
int[] number = {1, 2, 4, 5, 7};
int[] quantity = {2, 5, 6, 3, 9};
double[] price = {(double) 15.99, (double) 16.99, (double) 11.99,
(double) 13.95, (double) 14.95};
int[] numDisks= {1,2,1,2,1};
StringBuffer output=new StringBuffer("" ); //new stringbuffer for output
Product Inventory5 = new Product (name, number, quantity, price);
//needed to reslove Product constructor
output.append(S tring.format("\ n\n\n\n\n\nBefo re Restocking Fee, Inventory Is Worth A Total Of: $%.2f\n",
Inventory5.Tota lInvWorth(Inven tory5.ItemQuant ity,Inventory5. ItemPrice)));
//output to string total worth of inventory before fee
DVD DVDSub = new DVD (name, number, quantity, price, numDisks);
//needed to reslove DVD constructor
DVD SortedDVD = DVDSub.SortedAr ray(DVDSub);
//needed to replace original arrays with sorted one with new feature
output.append(S tring.format("A fter Restocking Fee of 5 Percent," +
" Inventory Is Worth A Total Of: $%.2f\n\n",DVDS ub.DVDRestock() ));
//output to string total worth of inventory after fee
output.append(S tring.format("I nventory\n")); //output to string inventory list
output.append(S tring.format("D VD Name:\tProdID:\ tStock:\tPrice: \tDVDDisc:\n")) ; //output to string headers
output.append(S tring.format("% s\t%d\t%d\t%.2f \t%d\t\n",Sorte dDVD.ItemName[counter], SortedDVD.ItemN umber[counter],
SortedDVD.ItemQ uantity[counter],SortedDVD.Item Price[counter],SortedDVD.Numb erOfDisc[counter]));
//out for array buttonCounter determines where in which line of array is outputed
this.text.setTe xt(output.toStr ing()); //output all text to String to place in GUI
} //end processing method
public void actionPerformed (ActionEvent event) //started actionperformed method
{
this.initialize (); //initialize window
if(((JButton)ev ent.getSource() ).getActionComm and().equals("F IRST")) //if statement for first button
{
this.buttonCoun ter=0; //set buttonCounter to 0
}
else if(((JButton)ev ent.getSource() ).getActionComm and().equals("P REV")) //if statement for prev button
{
if (this.buttonCou nter > 0) //if statement for only if buttonCounter is greater than 0
this.buttonCoun ter=--this.buttonCoun ter; //-1 from buttonCounter
}
else if(((JButton)ev ent.getSource() ).getActionComm and().equals("N EXT")) //if statement for next button
{
if (this.buttonCou nter < 4) //if statement for only if buttonCounter is less than 4
this.buttonCoun ter=++this.butt onCounter; //+1 to buttonCounter
}
else if(((JButton)ev ent.getSource() ).getActionComm and().equals("L AST"))
{
this.buttonCoun ter=4; //set buttonCounter to 4
}
this.processing (this.buttonCou nter); //return buttonCounter to processing method
} //end actionperformed method
} // end class Inventory5[/code]
[code=java]
import java.awt.*; //import all java.awt
import java.awt.event. *; //import all java.awt.event
import java.util.*; //import all java.util
import javax.swing.*; //import all javax.swing
class Product //start Product superclass
{
public String[] ItemName; //item's name
public int[] ItemNumber; //items's unique product number
public int[] ItemQuantity; //item's quantity in stock
public double[] ItemPrice; //item's price per
public Product(String[] name, int[] number, int[] quantity, double[] price) //product constructor
{
ItemName = name; //set ItemName to name
ItemNumber = number; //set ItemNumber to number
ItemQuantity = quantity; //set ItemQuantity to quantity
ItemPrice = price; //set ItemPrice to price
} //end Product constructor
public Product SortedArray(Pro duct InvPart2) //start Product method SortedArray
{
String[] name=new String[InvPart2.ItemNa me.length]; //new name for sorting array
int [] number = new int[InvPart2.ItemNu mber.length]; //new number for sorting array
int[] quantity = new int[InvPart2.ItemQu antity.length]; //new quantity for sorting array
double [] price = new double [InvPart2.ItemPr ice.length]; //new price for sorting array
name = (String[])InvPart2.ItemN ame.clone(); // place name in sorting array
Arrays.sort(nam e); //sort by name
for (int counter = 0; counter < name.length; counter++) //loop and counter for sorting array
{
for(int counter2=0;coun ter2<name.lengt h;counter2++)
//loop and counter to match unsorted array and sorted one
{
if(name[counter].equals(InvPart 2.ItemName[counter2]))
//if statement for when a match occurs
{
quantity[counter]=InvPart2.ItemQ uantity[counter2];
//set quantity equal to sorted array quantity
price[counter]=InvPart2.ItemP rice[counter2];
//set price equal to sorted array price
number[counter]=InvPart2.ItemN umber[counter2];
//set number equal to sorted array number
break; //break for if statement
} //end if statement
} //end for loop counter2
} //end for loop counter
Product SortedProductAr ray = new Product (name, number, quantity, price);
//new sorted product array replace old product array
return SortedProductAr ray; //return new product array sorted
} //end Product method SortedArray
public double TotalInvWorth(i nt[] quantity, double[] price) //start Product method TotalInvWorth
{
double total=0.00F; //set double total = 0
for (int counter = 0; counter < quantity.length ; counter++)
//loop and counter to multiply each quantity x price in array
{
double perprodworth=qu antity[counter]*price[counter];
// multiply quantity x price per counter in array = perprodworth
total=total+per prodworth; //add perprodworth to total
}
return total; //return total in TotalInvWorth
} //end Product method TotalInvWorth
} //end Product superclass
class DVD extends Product //start DVD subclass of Product
{
public int[] NumberOfDisc; //new feature number of disc in movie
public DVD(String[] name, int[] number, int[] quantity, double[] price, int[] numdisc)
//dvd constructor
{
super(name, number, quantity, price); //variables from superclass Product
NumberOfDisc=nu mdisc; //new variable number of disc per dvd
} //end dvd constructor
public DVD SortedArray(DVD Inventory5) //start DVD method SortedArray
{
int [] numdisc = new int[Inventory5.Numb erOfDisc.length]; //new number for sorting array
Product DVDProduct=new Product(Invento ry5.ItemName,In ventory5.ItemNu mber,
Inventory5.Item Quantity,Invent ory5.ItemPrice) ; //set DVDProduct equal to superclass array
Product SortedDVDProduc t=super.SortedA rray(DVDProduct );
for (int counter = 0; counter < SortedDVDProduc t.ItemName.leng th; counter++) //loop and counter for sorting array
{
for(int counter2=0;coun ter2<Inventory5 .ItemName.lengt h;counter2++)
//loop and counter to match unsorted array and sorted one
{
if(SortedDVDPro duct.ItemName[counter].equals(Invento ry5.ItemName[counter2]))
//if statement for when a match occurs
{
numdisc[counter]=Inventory5.Num berOfDisc[counter2];
break; //break for if statement
} //end if statement
} //end counter2 loop
} //end counter loop
DVD SortedProductAr ray = new DVD(SortedDVDPr oduct.ItemName, SortedDVDProduc t.ItemNumber,
SortedDVDProduc t.ItemQuantity, SortedDVDProduc t.ItemPrice,num disc);
//set SortedProductAr ray equal to superclass array plus new feature
return SortedProductAr ray; //return sortedproductar ray
} //end DVD method SortedArray
public double DVDRestock () //start DVD method DVDRestock
{
double totalInvworthbe forerestockingf ee=super.TotalI nvWorth(ItemQua ntity, ItemPrice);
//set totalInvWorthBe foreRestockingF ee to TotalInvWorth in superclass
double restockfee=0.00 F; //set restock fee to 0
double totalinvworthwi threstockfee=0. 00F; //set toatal worth after fee to 0
restockfee = 0.05F*totalInvw orthbeforeresto ckingfee; //restock fee = 5% to invworth before fee
totalinvworthwi threstockfee=re stockfee+totalI nvworthbeforere stockingfee; //add 5% to original inv worth
return totalinvworthwi threstockfee; //return inv total with fee
} //end DVD method DVDRestock
} //end DVD subclass
public class Inventory5 extends JFrame implements ActionListener
{ // start class Inventory5
public Inventory5()
{ //start Inventory5 constructor
this.initialize (); //set initialize
} //end Inventory5 constructor
private JTextArea text = new JTextArea(); //initializing jtextarea
private JButton firstBtn; //initializing first Jbutton
private JButton prevBtn; //initializing previous Jbutton
private JButton nextBtn; //initializing next Jbutton
private JButton lastBtn; //initializing last Jbutton
private JButton logoBtn; //initializing logo Jbutton
private ImageIcon logo = new ImageIcon("logo .gif"); //initializing picture for logo Jbutton
private int buttonCounter; //initializing buttonCounter for place in array
public void initialize() //start initialize method
{
firstBtn=new JButton("First" ); //set firstBtn to JButton First
prevBtn=new JButton("Previo us"); //set prevBtn to JButton Previous
nextBtn=new JButton("Next") ; //set nextBtn to JButton Next
lastBtn=new JButton("Last") ; //set lastBtn to JButton Last
logoBtn = new JButton(logo); //set logoBtn to JButton logo
this.getContent Pane().add(this .logoBtn); //add logoBtn to content pane
this.logoBtn.se tBounds(0, 0, 75, 75); //set logoBtn's size
this.logoBtn.se tBorderPainted( false); //turn border off for logoBtn
this.getContent Pane().add(this .firstBtn); //add firstBtn to content pane
this.firstBtn.s etBounds(40, 300, 100, 30); //set firstBtn's size
this.firstBtn.s etActionCommand ("FIRST"); //set action command for firstBtn to FIRST
this.firstBtn.a ddActionListene r(this); //set action lisnter for firstBtn
this.getContent Pane().add(this .prevBtn); //add prevBtn to content pane
this.prevBtn.se tBounds(150, 300, 100, 30); //set prevBtn's size
this.prevBtn.se tActionCommand( "PREV"); //set action command for prevBtn to PREV
this.prevBtn.ad dActionListener (this); //set action lisnter for prevBtn
this.getContent Pane().add(this .nextBtn); //add nextBtn to content pane
this.nextBtn.se tBounds(260, 300, 100, 30); //set nextBtn's size
this.nextBtn.se tActionCommand( "NEXT"); //set action command for nextBtn to NEXT
this.nextBtn.ad dActionListener (this); //set action lisnter for nextBtn
this.getContent Pane().add(this .lastBtn); //add lastBtn to content pane
this.lastBtn.se tBounds(370, 300, 100, 30); //set lastBtn's size
this.lastBtn.se tActionCommand( "LAST"); //set action command for lastBtn to LAST
this.lastBtn.ad dActionListener (this); //set action lisnter for lastBtn
this.getContent Pane().add(this .text,BorderLay out.CENTER); //add text to window
this.setSize(52 0, 400); //set frame size
this.setResizab le(false); //set frame resizable
this.setVisible (true); //set frame visiable
this.setDefault CloseOperation( JFrame.EXIT_ON_ CLOSE); //add to properly close app
} //end initialize method
public static void main(String[] args) //start main method
{
Inventory5 part4=new Inventory5();
part4.processin g(part4.buttonC ounter);
//needed to call processing method
} //end main method
private void processing(int counter) //start processing method
{
//setting static data for variables in each array
String[] name = {"Star Trek", "Star Gate", "Fifth Element", "Armageddon ", "Star Wars "};
int[] number = {1, 2, 4, 5, 7};
int[] quantity = {2, 5, 6, 3, 9};
double[] price = {(double) 15.99, (double) 16.99, (double) 11.99,
(double) 13.95, (double) 14.95};
int[] numDisks= {1,2,1,2,1};
StringBuffer output=new StringBuffer("" ); //new stringbuffer for output
Product Inventory5 = new Product (name, number, quantity, price);
//needed to reslove Product constructor
output.append(S tring.format("\ n\n\n\n\n\nBefo re Restocking Fee, Inventory Is Worth A Total Of: $%.2f\n",
Inventory5.Tota lInvWorth(Inven tory5.ItemQuant ity,Inventory5. ItemPrice)));
//output to string total worth of inventory before fee
DVD DVDSub = new DVD (name, number, quantity, price, numDisks);
//needed to reslove DVD constructor
DVD SortedDVD = DVDSub.SortedAr ray(DVDSub);
//needed to replace original arrays with sorted one with new feature
output.append(S tring.format("A fter Restocking Fee of 5 Percent," +
" Inventory Is Worth A Total Of: $%.2f\n\n",DVDS ub.DVDRestock() ));
//output to string total worth of inventory after fee
output.append(S tring.format("I nventory\n")); //output to string inventory list
output.append(S tring.format("D VD Name:\tProdID:\ tStock:\tPrice: \tDVDDisc:\n")) ; //output to string headers
output.append(S tring.format("% s\t%d\t%d\t%.2f \t%d\t\n",Sorte dDVD.ItemName[counter], SortedDVD.ItemN umber[counter],
SortedDVD.ItemQ uantity[counter],SortedDVD.Item Price[counter],SortedDVD.Numb erOfDisc[counter]));
//out for array buttonCounter determines where in which line of array is outputed
this.text.setTe xt(output.toStr ing()); //output all text to String to place in GUI
} //end processing method
public void actionPerformed (ActionEvent event) //started actionperformed method
{
this.initialize (); //initialize window
if(((JButton)ev ent.getSource() ).getActionComm and().equals("F IRST")) //if statement for first button
{
this.buttonCoun ter=0; //set buttonCounter to 0
}
else if(((JButton)ev ent.getSource() ).getActionComm and().equals("P REV")) //if statement for prev button
{
if (this.buttonCou nter > 0) //if statement for only if buttonCounter is greater than 0
this.buttonCoun ter=--this.buttonCoun ter; //-1 from buttonCounter
}
else if(((JButton)ev ent.getSource() ).getActionComm and().equals("N EXT")) //if statement for next button
{
if (this.buttonCou nter < 4) //if statement for only if buttonCounter is less than 4
this.buttonCoun ter=++this.butt onCounter; //+1 to buttonCounter
}
else if(((JButton)ev ent.getSource() ).getActionComm and().equals("L AST"))
{
this.buttonCoun ter=4; //set buttonCounter to 4
}
this.processing (this.buttonCou nter); //return buttonCounter to processing method
} //end actionperformed method
} // end class Inventory5[/code]
Comment