Join 150,124 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,021 people online right now. Registration is fast and FREE... Join Now!
// LabelFrame constructor adds JLabels to JFrame public GUI() { super( "Inventory of DVD Movies" ); // JLabel constructor with a string argument //this.inventory1 = inventory1;
JLabel jl; JPanel jp;
setLayout( new FlowLayout() ); // set frame layout
label2 = new JLabel("Item#: " ); label2.setHorizontalTextPosition( SwingConstants.LEFT ); label2.setVerticalTextPosition( SwingConstants.TOP ); jtfItem = new JTextField (10); jtfItem.setEditable(false); add( label2 ); // add label2 to JFrame add(jtfItem);
label3 = new JLabel( "Title of Movie: "); label3.setHorizontalTextPosition( SwingConstants.RIGHT ); label3.setVerticalTextPosition( SwingConstants.CENTER ); jtfVideo = new JTextField (30); jtfVideo.setEditable(true); add( label3 ); // add label2 to JFrame add(jtfVideo);
label4 = new JLabel("Movie Rating:"); label4.setHorizontalTextPosition( SwingConstants.LEFT ); label4.setVerticalTextPosition( SwingConstants.TOP ); jtfRating = new JTextField (5); jtfRating.setEditable(true); add( label4 ); // add label2 to JFrame add(jtfRating);
label5 = new JLabel("Item Number:"); label5.setHorizontalTextPosition( SwingConstants.LEFT ); label5.setVerticalTextPosition( SwingConstants.TOP ); jtfNumber = new JTextField (20); jtfNumber.setEditable(true); add( label5 ); // add label2 to JFrame add(jtfNumber);
label6 = new JLabel( "# of Items in Stock:"); label6.setHorizontalTextPosition( SwingConstants.LEFT ); label6.setVerticalTextPosition( SwingConstants.TOP ); jtfStock = new JTextField (10); jtfStock.setEditable(true); add( label6 ); // add label2 to JFrame add(jtfStock);
label7 = new JLabel( "Restocking Fee:" ); label7.setHorizontalTextPosition( SwingConstants.LEFT ); label7.setVerticalTextPosition( SwingConstants.TOP ); jtfReStock = new JTextField (10); jtfReStock.setEditable(false); add( label7 ); // add label2 to JFrame add(jtfReStock);
label8 = new JLabel( "Purchase Price:"); label8.setHorizontalTextPosition( SwingConstants.LEFT ); label8.setVerticalTextPosition( SwingConstants.TOP ); jtfPrice = new JTextField (10); jtfPrice.setEditable(true); add( label8 ); // add label2 to JFrame add(jtfPrice);
label9 = new JLabel( "Inventory Value:"); label9.setHorizontalTextPosition( SwingConstants.LEFT ); label9.setVerticalTextPosition( SwingConstants.TOP ); jtfValue = new JTextField (10); jtfValue.setEditable(false); add( label9 ); // add label2 to JFrame add(jtfValue);
label10 = new JLabel( "Total Inventory Value:\n"); label10.setHorizontalTextPosition( SwingConstants.LEFT ); label10.setVerticalTextPosition( SwingConstants.TOP ); jtftotalInv = new JTextField (10); jtftotalInv.setEditable(false); add( label10 ); // add label2 to JFrame add(jtftotalInv);
Icon dvd = new ImageIcon(getClass().getResource( "dvd.jpg" )); label11 = new JLabel ( dvd, SwingConstants.LEFT); label11.setVerticalTextPosition( SwingConstants.TOP); add( label11 );
btnFirst = new JButton( "First" ); btnFirst.addActionListener(this); add( btnFirst );
btnPrevious = new JButton( "Previous" ); btnPrevious.addActionListener(this); add( btnPrevious );
btnNext = new JButton( "Next" ); btnNext.addActionListener(this); add( btnNext );
btnLast = new JButton( "Last" ); btnLast.addActionListener(this); add( btnLast );
btnAdd = new JButton( "Add" ); btnAdd.addActionListener(this); add( btnAdd );
btnDelete = new JButton( "Delete" ); btnDelete.addActionListener(this); add( btnDelete );
btnModify = new JButton( "Modify" ); btnModify.addActionListener(this); add( btnModify );
btnSave = new JButton( "Save" ); btnSave.addActionListener(this); add( btnSave );
btnSearch = new JButton( "Search" ); btnSearch.addActionListener(this); add( btnSearch );
public void actionPerformed(ActionEvent e) { FeeQtyProduct temp = (FeeQtyProduct) dvd.getFeeQtyProduct(index); int item = dvd.getitem() +1;
index = (item - 2) % item; repaintGUI();
if(video.getText().equals("Add Video" )) // catches for assigning more then one blank at a time { JOptionPane.showMessageDialog(null, "Please fill out the blank entry you already have before adding more\nRemember: Push Modify when you finish with your changes","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); }
if(rating.getText().equals("Add Rating" ) !=true) // allows the adding of an entry { FeeQtyProduct product = new FeeQtyProduct( "Add Video", Integer.parseInt(number.getText()) + 1, 0, 0.0, "Add Rating"); index = (item - 1) % item; dvd.addFeeQtyProduct(product ); repaintGUI(); } if (item == 29) // catches for going over static inventory size { dvd.removeFeeQtyProduct(temp); JOptionPane.showMessageDialog(null, "Please No More Entries\n You can increase my capacity in the java file CdInvApp.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); }
}// end action
}
btnSave.addActionListener(new ActionListener() // saves the dat file in English { public void actionPerformed(ActionEvent e) { int number = dvd.getNumber(); InventoryStorage record = new InventoryStorage(); //calls inventroy storage class record.openFile(); int currentRecord = 0; // keeps track of number of cycles
do // cycles through the list adding them one at a time { record.addRecords(); currentRecord = currentRecord + 1; index = (++index) % number;
} while (currentRecord < number); //ends while
record.closeFile(); //closes file }//end action
} // end class
btnModify.addActionListener(new ActionListener() // saves changes to the GUI {
public void actionPerformed(ActionEvent e) {
if (videoText.getText().equals("")) //traps for blank entry { JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); repaintGUI(); }
if (ratingText.getText().equals("")) //traps for blank entry { JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); repaintGUI(); }
} catch (Exception d) { JOptionPane.showMessageDialog(null, "Recheck Entry use numbers for price and quantity.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); repaintGUI(); }
String name; int number; int stock; double price; String rating; // declares variables
video = videoText.getText(); number = Integer.parseInt(numberText.getText()); stock = Integer.parseInt(stockText.getText()); price = Double.parseDouble(priceText.getText()); rating = ratingText.getText();
btndelete.addActionListener(new ActionListener() //allows user to delete entry and sorts product numbers to be in sequence { public void actionPerformed(ActionEvent e) {
int number = dvd.getNumber(); FeeQtyProduct temp = (FeeQtyProduct) dvd.getFeeQtyProduct(index);
dvd.remove(index); repaintGUI(); int i = Integer.parseInt(numberText.getText()); index = (++index) % item; repaintGUI(); int j = Integer.parseInt(numberText.getText());
if (i > j)// my own little sort mechanism to keep Product numbers in sequence index = (-- index) % number; repaintGUI(); temp = (FeeQtyProduct) dvd.getFeeQtyProduct(index); temp.setnumber(j-1); index = (++index) % number; repaintGUI();
}// end if
if(Integer.parseInt(numberText.getText()) == number) // uses a different method to remove entry if it is the last one prevents blank entry from being in the mix { dvd.removeFeeQtyProduct(temp); index = (++index) % number; repaintGUI(); }//end if
}// end if
if (number == 1) // catches for 0 items error {
FeeQtyProduct product = new FeeQtyProduct( "Add Video",number, 0, 0.0, "Add Rating"); dvd.addFeeQtyProduct(product); JOptionPane.showMessageDialog(null, "Delete Process Complete.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); repaintGUI(); } //end if
}// end action
} // end class
btnSearch.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //declare variables and gets text from GUI String search = searchText.getText(); String name = videoText.getText(); int number = Integer.parseInt(numberText.getText()); int number = dvd.getNumber(); int currentRecord = 0;
do // gets text and checks for a match { index = (++index) % number; repaintGUI(); name = artistText.getText(); number = Integer.parseInt(numberText.getText()); search = searchText.getText(); currentRecord = (++currentRecord);
if(name.equalsIgnoreCase( search )) // stops on item repaintGUI();
if(searchText.getText().equals("" )) //catches for no entry in the search box { JOptionPane.showMessageDialog(null, "You wanna search for nothing?","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); index = (--index) % number; break; }
if(currentRecord == number +1) // displays message if no results match search { JOptionPane.showMessageDialog(null, "No Results Found","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); break; }
}while(video.equalsIgnoreCase(search ) != true); //ends do while
}
}
class InventoryStorage // creates the output file { private Formatter output; // object used to output text to file
public void openFile() { try { String strDirectoy ="C://data/";
// Create one directory boolean success = (new File(strDirectoy)).mkdir();
if (success) {
JOptionPane.showMessageDialog(null, "we had to create a directory named data in your C drive.","That's affirmative", JOptionPane.PLAIN_MESSAGE);
} //end if
}//end try
catch (Exception e) {
JOptionPane.showMessageDialog(null, "You do not have write access to the C: drive.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
}//end catch
try {
output = new Formatter( "C:/data/inventory.dat");
} // end try
catch ( SecurityException securityException ) //catches for write access and exits program { JOptionPane.showMessageDialog(null, "You do not have write access to this file.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); System.exit( 1 );
} // end catch
catch ( FileNotFoundException filesNotFoundException ) //catches for write access and exits program { JOptionPane.showMessageDialog(null, "Please create a file named data in your c drive","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); System.exit( 1 );
public void addRecords() // adds the records to the file { FeeQtyProduct record = (FeeQtyProduct) dvd.getFeeQtyProduct(index);
// rounds the output numbers to 2 decimals
BigDecimal roundPrice = new BigDecimal(Double.toString(record.restock() )); roundPrice = roundPrice.setScale(2, RoundingMode.HALF_UP);
BigDecimal roundValue= new BigDecimal(Double.toString(record.total())); roundValue= roundValue.setScale(2, RoundingMode.HALF_UP);
BigDecimal roundTotal= new BigDecimal(Double.toString(dvd.value())); roundTotal = roundTotal.setScale(2, RoundingMode.HALF_UP);
if (record != null) //catches for blank record - overkill because we can never have a blank record { output.format( "Video:%s Item Number#: %s Items in Stock#:%. Price: $%.Rating: %s" , record.getVideo(), record.getNumber(), record.getStock(), record.getPrice() , " Rating", record.getRating() + " with restock Fee $" + (roundPrice) + " value: $" + (roundValue) + " Inventory Total $" + (roundTotal) + "\t\n END OF LINE\t\t");
} // end if
} // end addRecords
public void closeFile() // close the file { if ( output != null ) output.close();
I am still checking my {} but what about this other error?
C:\Users\Michael\Desktop\java6\GUI.java:547: illegal start of expression public void addRecords() // adds the records to the file ^ C:\Users\Michael\Desktop\java6\GUI.java:579: ';' expected } // end class GUI ^ C:\Users\Michael\Desktop\java6\GUI.java:579: '}' expected } // end class GUI
CODE
import javax.swing.JOptionPane; import java.awt.FlowLayout; // specifies how components are arranged import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; // provides basic window features import javax.swing.JLabel; // displays text and images import javax.swing.SwingConstants; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JPanel; import javax.swing.Box;
public class GUI extends JFrame implements ActionListener {
// LabelFrame constructor adds JLabels to JFrame public GUI() { super( "Inventory of DVD Movies" ); // JLabel constructor with a string argument //this.inventory1 = inventory1;
JLabel jl; JPanel jp;
setLayout( new FlowLayout() ); // set frame layout
label2 = new JLabel("Item#: " ); label2.setHorizontalTextPosition( SwingConstants.LEFT ); label2.setVerticalTextPosition( SwingConstants.TOP ); jtfItem = new JTextField (10); jtfItem.setEditable(false); add( label2 ); // add label2 to JFrame add(jtfItem);
label3 = new JLabel( "Title of Movie: "); label3.setHorizontalTextPosition( SwingConstants.RIGHT ); label3.setVerticalTextPosition( SwingConstants.CENTER ); jtfVideo = new JTextField (30); jtfVideo.setEditable(true); add( label3 ); // add label2 to JFrame add(jtfVideo);
label4 = new JLabel("Movie Rating:"); label4.setHorizontalTextPosition( SwingConstants.LEFT ); label4.setVerticalTextPosition( SwingConstants.TOP ); jtfRating = new JTextField (5); jtfRating.setEditable(true); add( label4 ); // add label2 to JFrame add(jtfRating);
label5 = new JLabel("Item Number:"); label5.setHorizontalTextPosition( SwingConstants.LEFT ); label5.setVerticalTextPosition( SwingConstants.TOP ); jtfNumber = new JTextField (20); jtfNumber.setEditable(true); add( label5 ); // add label2 to JFrame add(jtfNumber);
label6 = new JLabel( "# of Items in Stock:"); label6.setHorizontalTextPosition( SwingConstants.LEFT ); label6.setVerticalTextPosition( SwingConstants.TOP ); jtfStock = new JTextField (10); jtfStock.setEditable(true); add( label6 ); // add label2 to JFrame add(jtfStock);
label7 = new JLabel( "Restocking Fee:" ); label7.setHorizontalTextPosition( SwingConstants.LEFT ); label7.setVerticalTextPosition( SwingConstants.TOP ); jtfReStock = new JTextField (10); jtfReStock.setEditable(false); add( label7 ); // add label2 to JFrame add(jtfReStock);
label8 = new JLabel( "Purchase Price:"); label8.setHorizontalTextPosition( SwingConstants.LEFT ); label8.setVerticalTextPosition( SwingConstants.TOP ); jtfPrice = new JTextField (10); jtfPrice.setEditable(true); add( label8 ); // add label2 to JFrame add(jtfPrice);
label9 = new JLabel( "Inventory Value:"); label9.setHorizontalTextPosition( SwingConstants.LEFT ); label9.setVerticalTextPosition( SwingConstants.TOP ); jtfValue = new JTextField (10); jtfValue.setEditable(false); add( label9 ); // add label2 to JFrame add(jtfValue);
label10 = new JLabel( "Total Inventory Value:\n"); label10.setHorizontalTextPosition( SwingConstants.LEFT ); label10.setVerticalTextPosition( SwingConstants.TOP ); jtftotalInv = new JTextField (10); jtftotalInv.setEditable(false); add( label10 ); // add label2 to JFrame add(jtftotalInv);
Icon dvd = new ImageIcon(getClass().getResource( "dvd.jpg" )); label11 = new JLabel ( dvd, SwingConstants.LEFT); label11.setVerticalTextPosition( SwingConstants.TOP); add( label11 );
btnFirst = new JButton( "First" ); btnFirst.addActionListener(this); add( btnFirst );
btnPrevious = new JButton( "Previous" ); btnPrevious.addActionListener(this); add( btnPrevious );
btnNext = new JButton( "Next" ); btnNext.addActionListener(this); add( btnNext );
btnLast = new JButton( "Last" ); btnLast.addActionListener(this); add( btnLast );
btnAdd = new JButton( "Add" ); btnAdd.addActionListener(this); add( btnAdd );
btnDelete = new JButton( "Delete" ); btnDelete.addActionListener(this); add( btnDelete );
btnModify = new JButton( "Modify" ); btnModify.addActionListener(this); add( btnModify );
btnSave = new JButton( "Save" ); btnSave.addActionListener(this); add( btnSave );
btnSearch = new JButton( "Search" ); btnSearch.addActionListener(this); add( btnSearch );
if(video.getText().equals("Add Video" )) // catches for assigning more then one blank at a time { JOptionPane.showMessageDialog(null, "Please fill out the blank entry you already have before adding more\nRemember: Push Modify when you finish with your changes","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); }
if(rating.getText().equals("Add Rating" ) !=true) // allows the adding of an entry { FeeQtyProduct product = new FeeQtyProduct( "Add Video", Integer.parseInt(number.getText()) + 1, 0, 0.0, "Add Rating"); index = (item - 1) % item; dvd.addFeeQtyProduct(product ); repaintGUI(); } if (item == 29) // catches for going over static inventory size { dvd.removeFeeQtyProduct(temp); JOptionPane.showMessageDialog(null, "Please No More Entries\n You can increase my capacity in the java file CdInvApp.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); }
}
if(buttonPressed == btnSave) { int number = dvd.getNumber(); InventoryStorage record = new InventoryStorage(); //calls inventroy storage class record.openFile(); int currentRecord = 0; // keeps track of number of cycles
do // cycles through the list adding them one at a time { record.addRecords(); currentRecord = currentRecord + 1; index = (++index) % number;
} while (currentRecord < number); //ends while
record.closeFile(); //closes file
}
if(buttonPressed == btnModify) {
if (videoText.getText().equals("")) //traps for blank entry { JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); repaintGUI(); }
if (ratingText.getText().equals("")) //traps for blank entry { JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); repaintGUI(); }
} catch (Exception d) { JOptionPane.showMessageDialog(null, "Recheck Entry use numbers for price and quantity.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); repaintGUI(); }
String name; int number; int stock; double price; String rating; // declares variables
video = videoText.getText(); number = Integer.parseInt(numberText.getText()); stock = Integer.parseInt(stockText.getText()); price = Double.parseDouble(priceText.getText()); rating = ratingText.getText();
dvd.remove(index); repaintGUI(); int i = Integer.parseInt(numberText.getText()); index = (++index) % item; repaintGUI(); int j = Integer.parseInt(numberText.getText());
if (i > j)// my own little sort mechanism to keep Product numbers in sequence index = (-- index) % number; repaintGUI(); temp = (FeeQtyProduct) dvd.getFeeQtyProduct(index); temp.setnumber(j-1); index = (++index) % number; repaintGUI();
}// end if
if(Integer.parseInt(numberText.getText()) == number) // uses a different method to remove entry if it is the last one prevents blank entry from being in the mix { dvd.removeFeeQtyProduct(temp); index = (++index) % number; repaintGUI(); }//end if
}// end if
if (number == 1) // catches for 0 items error {
FeeQtyProduct product = new FeeQtyProduct( "Add Video",number, 0, 0.0, "Add Rating"); dvd.addFeeQtyProduct(product); JOptionPane.showMessageDialog(null, "Delete Process Complete.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); repaintGUI(); } //end if
}// end action
if(buttonPressed == btnSearch) { //declare variables and gets text from GUI String search = searchText.getText(); String name = videoText.getText(); int number = Integer.parseInt(numberText.getText()); int number = dvd.getNumber(); int currentRecord = 0;
do // gets text and checks for a match { index = (++index) % number; repaintGUI(); name = artistText.getText(); number = Integer.parseInt(numberText.getText()); search = searchText.getText(); currentRecord = (++currentRecord);
if(name.equalsIgnoreCase( search )) // stops on item repaintGUI();
if(searchText.getText().equals("" )) //catches for no entry in the search box { JOptionPane.showMessageDialog(null, "You wanna search for nothing?","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); index = (--index) % number; break; }
if(currentRecord == number +1) // displays message if no results match search { JOptionPane.showMessageDialog(null, "No Results Found","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); break; }
}while(video.equalsIgnoreCase(search ) != true); //ends do while
}
class InventoryStorage // creates the output file { private Formatter output; // object used to output text to file
public void openFile() { try { String strDirectoy ="C://data/";
// Create one directory boolean success = (new File(strDirectoy)).mkdir();
if (success) {
JOptionPane.showMessageDialog(null, "we had to create a directory named data in your C drive.","That's affirmative", JOptionPane.PLAIN_MESSAGE);
} //end if
}//end try
catch (Exception e) {
JOptionPane.showMessageDialog(null, "You do not have write access to the C: drive.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
}//end catch
try {
output = new Formatter( "C:/data/inventory.dat");
} // end try
catch ( SecurityException securityException ) //catches for write access and exits program { JOptionPane.showMessageDialog(null, "You do not have write access to this file.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); System.exit( 1 );
} // end catch
catch ( FileNotFoundException filesNotFoundException ) //catches for write access and exits program { JOptionPane.showMessageDialog(null, "Please create a file named data in your c drive","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE); System.exit( 1 );
public void addRecords() // adds the records to the file { FeeQtyProduct record = (FeeQtyProduct) dvd.getFeeQtyProduct(index);
// rounds the output numbers to 2 decimals
BigDecimal roundPrice = new BigDecimal(Double.toString(record.restock() )); roundPrice = roundPrice.setScale(2, RoundingMode.HALF_UP);
BigDecimal roundValue= new BigDecimal(Double.toString(record.total())); roundValue= roundValue.setScale(2, RoundingMode.HALF_UP);
BigDecimal roundTotal= new BigDecimal(Double.toString(dvd.value())); roundTotal = roundTotal.setScale(2, RoundingMode.HALF_UP);
if (record != null) //catches for blank record - overkill because we can never have a blank record { output.format( "Video:%s Item Number#: %s Items in Stock#:%. Price: $%.Rating: %s" , record.getVideo(), record.getNumber(), record.getStock(), record.getPrice() , " Rating", record.getRating() + " with restock Fee $" + (roundPrice) + " value: $" + (roundValue) + " Inventory Total $" + (roundTotal) + "\t\n END OF LINE\t\t");