Creating a "Data" folder in my C: Drive using my Java program

Reply

Join Date: Nov 2007
Posts: 6
Reputation: ITAutobot25 is an unknown quantity at this point 
Solved Threads: 0
ITAutobot25 ITAutobot25 is offline Offline
Newbie Poster

Creating a "Data" folder in my C: Drive using my Java program

 
0
  #1
Nov 18th, 2007
My assignment requires that I use exception handling to create a directory and file if necessary.

I was able to create a C:data\inventory.dat file, provided that there's an existing Data folder (directory) in my C: drive; however, I have a feeling that the assignment wants me to create an actual Data folder if one is not available. When I remove the Data folder from my C: drive, I get "C:\data\inventory.dat (The system cannot find the path specified)".

How am I to create able a folder that currently does not exist? I'm reading the java.sun website on the File class, the mkdirs methods, and the FileSystemView class; however, I'm having trouble implementing the requirements in my code (I wish API documentation was more understandable to me.). Can an example be provided? Thank you everyone; below is my code.

  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.HeadlessException;
  5. import java.awt.Image;
  6.  
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.ObjectOutputStream;
  15. import java.io.Serializable;
  16.  
  17. import javax.swing.ImageIcon;
  18. import javax.swing.JButton;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JPanel;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.JTextArea;
  25.  
  26.  
  27. public class InventoryGUI extends JFrame implements ActionListener,Serializable
  28. {
  29. private JTextArea textArea;
  30.  
  31. private JButton first,
  32. next,
  33. previous,
  34. last,
  35. add,
  36. modify,
  37. delete,
  38. save,
  39. search,
  40. exit;
  41.  
  42. JLabel imageLabel;
  43.  
  44. private static Inventory inv = new Inventory();
  45.  
  46. /**
  47. * @param arg0
  48. * @throws HeadlessException
  49. */
  50. public InventoryGUI( String arg0 ) throws HeadlessException
  51. {
  52. super( "Inventory GUI" );
  53.  
  54. textArea = new JTextArea( 250,30 );
  55.  
  56. JScrollPane scrollPane = new JScrollPane( textArea );
  57.  
  58. textArea.setEditable( false );
  59.  
  60. scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
  61.  
  62. scrollPane.setPreferredSize( new Dimension( 250, 250 ) );
  63.  
  64. JPanel cp = new JPanel();
  65.  
  66. cp.setSize( 250, 40 );
  67.  
  68. cp.setLayout( new BorderLayout() );
  69.  
  70. cp.add( scrollPane,BorderLayout.CENTER );
  71.  
  72. JPanel buttonPaenl = new JPanel();
  73.  
  74. JPanel buttonPaenl1 = new JPanel();
  75.  
  76. first = new JButton( "First" );
  77. first.addActionListener( this );
  78.  
  79. search = new JButton( "Search" );
  80. search.addActionListener( this );
  81.  
  82. next = new JButton( "Next" );
  83. next.addActionListener( this );
  84.  
  85. previous = new JButton( "Previous" );
  86. previous.addActionListener( this );
  87.  
  88. last = new JButton( "Last" );
  89. last.addActionListener( this );
  90.  
  91. add = new JButton( "Add" );
  92. add.addActionListener( this );
  93.  
  94. modify = new JButton( "Modify" );
  95. modify.addActionListener( this );
  96.  
  97. delete = new JButton( "Delete" );
  98. delete.addActionListener( this );
  99.  
  100. save = new JButton( "Save" );
  101. save.addActionListener( this );
  102.  
  103. exit = new JButton( "Exit" );
  104. exit.addActionListener( this );
  105.  
  106. buttonPaenl.setLayout( new FlowLayout() );
  107. buttonPaenl1.setLayout( new FlowLayout() );
  108. buttonPaenl.add( first );
  109. buttonPaenl.add( previous );
  110. buttonPaenl.add( next );
  111. buttonPaenl.add( last );
  112. buttonPaenl1.add( add );
  113. buttonPaenl1.add( modify );
  114. buttonPaenl1.add( delete );
  115. buttonPaenl1.add( save );
  116. buttonPaenl1.add( search );
  117. buttonPaenl1.add( exit );
  118.  
  119. cp.add( buttonPaenl,BorderLayout.SOUTH );
  120. cp.add( buttonPaenl1,BorderLayout.NORTH );
  121.  
  122. ImageIcon icon = new ImageIcon( "logo.gif" );
  123. imageLabel = new JLabel( icon, JLabel.CENTER );
  124. cp.add( imageLabel, BorderLayout.EAST );
  125. this.setContentPane( cp );
  126.  
  127. this.setTitle( " Week Nine Final Project: Inventory Program Part Six - Transformers Autobot Inventory ( More Than Meets the Eye o_0?! ) " );
  128.  
  129. this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  130. this.textArea.setText(inv.getFirst());
  131. this.setSize(400, 400);
  132. this.pack();
  133. this.setVisible( true );
  134. }
  135.  
  136. @Override
  137. public void actionPerformed( ActionEvent e )
  138. {
  139. // TODO Auto-generated method stub
  140. if( e.getActionCommand().equals( "First" ) )
  141. {
  142. textArea.setText( inv.getFirst() );
  143. }
  144.  
  145. if( e.getActionCommand().equals( "Next" ) )
  146. {
  147. textArea.setText(inv.getNext());
  148. }
  149.  
  150. if( e.getActionCommand().equals( "Previous" ) )
  151. {
  152. textArea.setText( inv.getPrevious() );
  153. }
  154.  
  155. if( e.getActionCommand().equals( "Last" ) )
  156. {
  157. textArea.setText(inv.getLast());
  158. }
  159.  
  160. if( e.getActionCommand().equals( "Save" ) )
  161. {
  162. save();
  163. }
  164.  
  165. if( e.getActionCommand().equals( "Exit" ) )
  166. {
  167. System.exit(0);
  168. }
  169.  
  170. if( e.getActionCommand().equals( "Add" ) )
  171. {
  172. InventoryDialogue id = new InventoryDialogue( true," Add a new Inventory " );
  173. }
  174.  
  175. if( e.getActionCommand().equals( "Modify" ) )
  176. {
  177. InventoryDialogue id = new InventoryDialogue( false," Modify Inventory " );
  178. }
  179.  
  180. if( e.getActionCommand().equals( "Search" ) )
  181. {
  182. String s = JOptionPane.showInputDialog( null, " Enter Product Name ",null, 1 );
  183.  
  184. s = inv.Search(s);
  185.  
  186. if( s == null )
  187. {
  188. JOptionPane.showMessageDialog( this, " No Results Found " ) ;
  189. }
  190.  
  191. else textArea.setText( s );
  192. }
  193.  
  194. if( e.getActionCommand().equals( "Delete" ) )
  195. {
  196. inv.getproducts().remove( inv.index );
  197. }
  198.  
  199. }
  200.  
  201. public void save()
  202. {
  203. File f = new File( "C:\\data\\inventory.dat" );
  204.  
  205. try
  206. {
  207. FileOutputStream out = new FileOutputStream( f );
  208.  
  209. try
  210. {
  211. ObjectOutputStream objectOut = new ObjectOutputStream( out );
  212. objectOut.writeObject( inv );
  213. }
  214.  
  215. catch (IOException e)
  216. {
  217. // TODO Auto-generated catch block
  218. JOptionPane.showMessageDialog( null, e.getMessage() );
  219. e.printStackTrace();
  220. }
  221. }
  222.  
  223. catch ( FileNotFoundException e )
  224. {
  225. // TODO Auto-generated catch block
  226. JOptionPane.showMessageDialog( null, e.getMessage() );
  227. e.printStackTrace();
  228. }
  229.  
  230. }
  231.  
  232. public static Inventory getInv()
  233. {
  234. return inv;
  235. }
  236.  
  237. public static void setInv( Inventory inv )
  238. {
  239. InventoryGUI.inv = inv;
  240. }
  241.  
  242. /**
  243. * @param args
  244. */
  245. public static void main( String[] args )
  246. {
  247. InventoryGUI inventory = new InventoryGUI( "Inventory" );
  248. }
  249. }
  1. import java.awt.BorderLayout;
  2. import java.awt.FlowLayout;
  3. import java.awt.LayoutManager;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.BoxLayout;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14.  
  15.  
  16. public class InventoryDialogue extends JFrame implements ActionListener
  17. {
  18. private boolean status = false; //false is for modify
  19.  
  20. JTextField nameTextField,
  21. unitTextField,
  22. priceTextField;
  23.  
  24. JLabel nameLabel,
  25. unitLabel,
  26. priceLabel;
  27.  
  28. JPanel TextFields,
  29. buttons;
  30.  
  31. JButton modify,
  32. add;
  33.  
  34. public InventoryDialogue( Boolean stat, String title )
  35. {
  36. super( title );
  37. status = stat;
  38. JPanel but = new JPanel();
  39. modify = new JButton( "Modify" );
  40. modify.addActionListener( this );
  41. add = new JButton( "Add" );
  42. add.addActionListener( this );
  43. but.add( add );
  44. but.add( modify );
  45.  
  46. if( status == false )
  47. {
  48. add.setVisible( false );
  49. }
  50.  
  51. else
  52. {
  53. modify.setVisible( false );
  54. }
  55.  
  56. nameTextField = new JTextField( "", 20 );
  57. unitTextField = new JTextField( "", 5 );
  58. priceTextField = new JTextField( "" , 5 );
  59. nameLabel = new JLabel( "Name" );
  60. unitLabel = new JLabel( "Units" );
  61. priceLabel = new JLabel( "Price per Unit" );
  62. JPanel p = new JPanel();
  63.  
  64. p.setLayout( new FlowLayout() );
  65. p.add( nameLabel );
  66. p.add( nameTextField );
  67. p.add( unitLabel );
  68. p.add( unitTextField );
  69. p.add( priceLabel );
  70. p.add( priceTextField );
  71. JPanel cp = new JPanel();
  72. cp.setLayout( new BorderLayout() );
  73. cp.add( p, BorderLayout.CENTER );
  74. cp.add( but, BorderLayout.EAST );
  75. this.setContentPane( cp );
  76. this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
  77. this.pack();
  78. this.setVisible( true );
  79. }
  80.  
  81. @Override
  82. public void actionPerformed( ActionEvent arg0 )
  83. {
  84. if( arg0.getActionCommand().equals( "Add" ) )
  85. {
  86. String name;
  87. int units;
  88. Double price;
  89. name = nameTextField.getText();
  90.  
  91. try
  92. {
  93. units = Integer.parseInt( unitTextField.getText() );
  94. }
  95.  
  96. catch ( NumberFormatException e )
  97. {
  98. JOptionPane.showMessageDialog( null, e.getMessage() );
  99.  
  100. e.printStackTrace();
  101.  
  102. return;
  103. }
  104.  
  105. try
  106. {
  107. price = Double.parseDouble( priceTextField.getText() );
  108. }
  109.  
  110. catch ( NumberFormatException e )
  111. {
  112. // TODO Auto-generated catch block
  113. JOptionPane.showMessageDialog( null, e.getMessage() );
  114.  
  115. e.printStackTrace();
  116.  
  117. return;
  118. }
  119.  
  120. InventoryGUI.getInv().getproducts().add( new ProductModified( name, InventoryGUI.getInv().products.size(),
  121. units,price ) );
  122.  
  123. this.dispose();
  124. }
  125.  
  126. if( arg0.getActionCommand().equals( "Modify" ) )
  127. {
  128. String name;
  129. int units;
  130. Double price;
  131. name = nameTextField.getText();
  132.  
  133. try
  134. {
  135. units = Integer.parseInt( unitTextField.getText() );
  136. }
  137.  
  138. catch ( NumberFormatException e )
  139. {
  140. JOptionPane.showMessageDialog( null,"Please Enter Valid Integers" );
  141.  
  142. e.printStackTrace();
  143.  
  144. return;
  145. }
  146.  
  147. try
  148. {
  149. price = Double.parseDouble( priceTextField.getText() );
  150. }
  151.  
  152. catch ( NumberFormatException e )
  153. {
  154. // TODO Auto-generated catch block
  155. JOptionPane.showMessageDialog( null,"Please Enter Valid Price" );
  156.  
  157. //e.printStackTrace();
  158. return;
  159. }
  160.  
  161. InventoryGUI.getInv().getproducts().set( InventoryGUI.getInv().index,
  162. new ProductModified( name, InventoryGUI.getInv().index, units, price ) );
  163.  
  164. this.dispose();
  165. }
  166.  
  167. }
  168.  
  169. public static void main( String args[] )
  170. {
  171. InventoryDialogue idf = new InventoryDialogue( true,"cc" );
  172. }
  173. }
  1. import java.text.Collator;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.Locale;
  5. import java.io.Serializable;
  6.  
  7. public class Inventory implements Serializable
  8. {
  9. private String inventoryName; // name of inventory
  10.  
  11. public String restockRate; // restock rate percentage
  12.  
  13. public double totalRestock;
  14.  
  15. public static int index;
  16.  
  17. public ArrayList < ProductModified > products = new ArrayList < ProductModified > ();
  18.  
  19. public Inventory()
  20. {
  21. products.add( new ProductModified( "No Sale Bot", 0, 1, 0.00 ) );
  22. products.add( new ProductModified( "Optimus Prime", 7, 52, 160.00 ) );
  23. products.add( new ProductModified( "BumbleeBee", 16, 61, 97.00 ) );
  24. products.add( new ProductModified( "Ironhide", 25, 106, 88.00 ) );
  25. products.add( new ProductModified( "Ratchet", 34, 125, 79.00 ) );
  26. products.add( new ProductModified( "Jazz", 0, 124, 70.00 ) );
  27.  
  28. index = 0;
  29. }
  30.  
  31. public void sort()
  32. {
  33. Locale loc = Locale.ENGLISH;
  34.  
  35. ProductModified Temp;
  36.  
  37. Collator col = Collator.getInstance( loc );
  38.  
  39. for ( int i = 0; i < products.size(); i++ )
  40. {
  41. for ( int j = i + 1; j < products.size(); j++ )
  42. {
  43. if( col.compare( products.get( i ).getAutobotName(), products.get( j ).getAutobotName() ) > 0 )
  44. {
  45. Temp = products.get( i );
  46.  
  47. products.set( i, products.get( j ) );
  48. products.set( j, Temp );
  49. }
  50. }
  51. }
  52. }
  53.  
  54. public String getInventoryName()
  55. {
  56. return inventoryName;
  57. }
  58.  
  59. public void setInventoryName( String inventoryName )
  60. {
  61. this.inventoryName = inventoryName;
  62. }
  63.  
  64. public String getRestockRate()
  65. {
  66. return restockRate;
  67. }
  68.  
  69. public void setRestockRate( String restockRate )
  70. {
  71. this.restockRate = restockRate;
  72. }
  73.  
  74. public double getTotalRestock()
  75. {
  76. return totalRestock;
  77. }
  78.  
  79. public void setTotalRestock( double totalRestock )
  80. {
  81. this.totalRestock = totalRestock;
  82. }
  83.  
  84. public ArrayList < ProductModified > getproducts()
  85. {
  86. return products;
  87. }
  88.  
  89. public void seproducts( ArrayList < ProductModified > products)
  90. {
  91. this.products = products;
  92. }
  93.  
  94. public String toString()
  95. {
  96. String transformersSub = new String(" ");
  97.  
  98. transformersSub = "Welcome to the\n" + getInventoryName() + "!\n\n";
  99.  
  100. transformersSub = transformersSub + "Below is the available inventory:\n\n";
  101.  
  102. transformersSub = transformersSub + "The restock interest rate is at " + getRestockRate() + ".\n\n";
  103.  
  104. transformersSub = transformersSub + "Index\tProductModified #\t\t\tName\t\tUnits\t\tPrice\t\t";
  105.  
  106. transformersSub = transformersSub + "Unit Restock Value\t";
  107.  
  108. transformersSub = transformersSub + "Stock Value\t\t";
  109.  
  110. transformersSub = transformersSub + "Total ProductModified Restock\n";
  111.  
  112. Iterator < ProductModified > i = this.getproducts().iterator();
  113.  
  114. while(i.hasNext())
  115. {
  116. transformersSub = transformersSub + i.next();
  117. }
  118.  
  119. return transformersSub;
  120. }
  121.  
  122. public Double getTotal()
  123. {
  124. Double Total = 0.0;
  125.  
  126. ProductModified p;
  127.  
  128. Iterator < ProductModified > i = products.iterator();
  129.  
  130. while( i.hasNext() )
  131. {
  132. p = i.next();
  133.  
  134. Total = Total + p.getPrice() * p.getUnit();
  135. }
  136.  
  137. return Total;
  138. }
  139.  
  140. public String processOutPut( ProductModified p )
  141. {
  142. String out = "Welcome to the Transformers Autobot Toy Inventory\n\nTotal Inventory = "
  143. + this.getTotal() + "\nRestocking Fee = 5%"
  144. + "\n\nItem #\tName\tUnit\tPrice\tUnit Restock Value"
  145. + p;
  146.  
  147. return out;
  148. }
  149.  
  150. public String getFirst()
  151. {
  152. index = 0;
  153.  
  154. return processOutPut( products.get(0) );
  155.  
  156. }
  157.  
  158. public String getLast()
  159. {
  160. index = products.size() - 1;
  161.  
  162. return processOutPut( products.get( products.size() - 1 ) );
  163. }
  164.  
  165. public String getNext()
  166. {
  167. if( index == products.size() - 1 )
  168. {
  169. getFirst();
  170.  
  171. return getFirst();
  172. }
  173.  
  174. else
  175. {
  176. index++;
  177.  
  178. return processOutPut( products.get( index ) );
  179. }
  180. }
  181.  
  182. public String getPrevious()
  183. {
  184. if( index == 0 )
  185. {
  186. return getLast();
  187. }
  188.  
  189. else
  190.  
  191. return processOutPut(products.get(--index));
  192. }
  193.  
  194. public String Search( String Search )
  195. {
  196. Iterator < ProductModified > i = products.iterator();
  197.  
  198. String te = null;
  199.  
  200. ProductModified p;
  201.  
  202. while( i.hasNext() )
  203. {
  204. p = i.next();
  205.  
  206. if( p.getAutobotName().equals( Search ) )
  207. {
  208. te = processOutPut( p );
  209. }
  210. }
  211.  
  212. return te;
  213. }
  214. }
  1. import static java.lang.System.out;
  2. import java.io.Serializable;
  3.  
  4. //class declaration for TransformersProduct
  5. public class Product implements Serializable
  6. {
  7. public String autobotName; // array of autobot toy names
  8. public int productNumber; // product # array ID for product
  9. public int unit; // array of autobot toy units
  10. public double price; // array of autobot prices
  11. public double inventoryValue;
  12.  
  13. /**
  14. * @param autobotName
  15. * @param productNumber
  16. * @param unit
  17. * @param price
  18. */
  19.  
  20. public Product( String autobotName, int productNumber, int unit,
  21. double price )
  22. {
  23. super();
  24. this.autobotName = autobotName;
  25. this.productNumber = productNumber;
  26. this.unit = unit;
  27. this.price = price;
  28. }
  29.  
  30. public String getAutobotName()
  31. {
  32. return autobotName;
  33. }
  34.  
  35. public void setAutobotName( String autobotName )
  36. {
  37. this.autobotName = autobotName;
  38. }
  39.  
  40. public int getProductNumber()
  41. {
  42. return productNumber;
  43. }
  44.  
  45. public void setProductNumber( int productNumber )
  46. {
  47. this.productNumber = productNumber;
  48. }
  49.  
  50. public int getUnit()
  51. {
  52. return unit;
  53. }
  54.  
  55. public void setUnit( int unit )
  56. {
  57. this.unit = unit;
  58. }
  59.  
  60. public double getPrice()
  61. {
  62. return price;
  63. }
  64.  
  65. public void setPrice( double price )
  66. {
  67. this.price = price;
  68. }
  69.  
  70. public double getInventoryValue()
  71. {
  72. return inventoryValue;
  73. }
  74.  
  75. public Double inventoryValue()
  76. {
  77. return this.getPrice() * this.getUnit();
  78. }
  79.  
  80. public String toString()
  81. {
  82. return "\n"+this.productNumber+"\t"+this.autobotName+"\t"+this.unit +"\t"+this.price+ "\t";
  83. }
  84. }
  1. import java.io.Serializable;
  2.  
  3. public class ProductModified extends Product implements Serializable
  4. {
  5. public ProductModified( String autobotName, int productNumber, int unit,
  6. double price )
  7. {
  8. super( autobotName, productNumber, unit, price );
  9. this.inventoryValue = this.inventoryValue();
  10. // TODO Auto-generated constructor stub
  11. }
  12.  
  13. public Double inventoryValue()
  14. {
  15. return this.getUnit() * this.getPrice() + this.getUnit() * this.getPrice() * 0.05;
  16. }
  17.  
  18. public String toString()
  19. {
  20. return "\n" + this.productNumber + "\t" + this.autobotName
  21. + "\t" + this.unit + "\t" + this.price + "\t"
  22. + this.getInventoryValue();
  23. }
  24. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Creating a "Data" folder in my C: Drive using my Java program

 
0
  #2
Nov 18th, 2007
Maybe something like this:

  1. public class DirTest
  2. {
  3. public static final String PATH = "c:/DELETE_THIS";
  4.  
  5. public static final String NAME = "inventory.dat";
  6.  
  7. public static void main(String[] args)
  8. {
  9. try
  10. {
  11. File base = new File(PATH);
  12. if (!(base.exists() && base.isDirectory()))
  13. if (!base.mkdir())
  14. throw new IOException("Directory creation failed");
  15. File file = new File(base, NAME);
  16. BufferedWriter buf = new BufferedWriter(new FileWriter(file));
  17. try
  18. {
  19. buf.write("Hello world");
  20. }
  21. finally
  22. {
  23. if (buf != null)
  24. buf.close();
  25. }
  26. }
  27. catch (IOException e)
  28. {
  29. System.out.println(e);
  30. System.exit(1);
  31. }
  32. }
  33. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,433
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Creating a "Data" folder in my C: Drive using my Java program

 
0
  #3
Nov 18th, 2007
File.mkdir() or File.mkdirs() is what you need to use.

edit: s.o.s. beat me to the post The only issue there is that it is not using exception handling to create the directory. Personally, I think that is the better way to do it, but his assignment requires it to be done by handling the exception.

Just catch the exception from the FileOutputStream, create the directory, and then try to create the stream again. Outside of that assignment though, I'd say use the way that s.o.s. posted above. It's much cleaner to check for the existence of the directory prior to trying to use it. The catch block approach breaks the flow of your code because its not reentrant from where the exception occurred.
Last edited by Ezzaral; Nov 18th, 2007 at 12:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: ITAutobot25 is an unknown quantity at this point 
Solved Threads: 0
ITAutobot25 ITAutobot25 is offline Offline
Newbie Poster

Sorting Problem

 
0
  #4
Nov 18th, 2007
Thanks for all of your help and examples. I managed to get that working. I do have one last problem. My sorter is not working. I managed to sort by product name in my previous assignment; however, I can't get it to work on this one. I changed things around so much in the program that I'm lost on how to get it going again. I also blocked that part of the code off with delimiters; I didn't want it to interfere with program

  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.HeadlessException;
  5. import java.awt.Image;
  6.  
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.ObjectOutputStream;
  15. import java.io.Serializable;
  16.  
  17. import javax.swing.ImageIcon;
  18. import javax.swing.JButton;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JPanel;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.JTextArea;
  25.  
  26.  
  27. public class TransformersGUIMainExecution extends JFrame implements ActionListener,Serializable
  28. {
  29. private JTextArea textArea;
  30.  
  31. private JButton first,
  32. next,
  33. previous,
  34. last,
  35. add,
  36. modify,
  37. delete,
  38. save,
  39. search,
  40. exit;
  41.  
  42. JLabel imageLabel;
  43.  
  44. private static TransformersInventoryInfo inv = new TransformersInventoryInfo();
  45.  
  46. /**
  47. * @param arg0
  48. * @throws HeadlessException
  49. */
  50. public TransformersGUIMainExecution( String arg0 ) throws HeadlessException
  51. {
  52. super( "Inventory GUI" );
  53.  
  54. textArea = new JTextArea( 250,30 );
  55.  
  56. JScrollPane scrollPane = new JScrollPane( textArea );
  57.  
  58. textArea.setEditable( false );
  59.  
  60. scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
  61.  
  62. scrollPane.setPreferredSize( new Dimension( 250, 250 ) );
  63.  
  64. JPanel cp = new JPanel();
  65.  
  66. cp.setSize( 250, 40 );
  67.  
  68. cp.setLayout( new BorderLayout() );
  69.  
  70. cp.add( scrollPane,BorderLayout.CENTER );
  71.  
  72. JPanel buttonPaenl = new JPanel();
  73.  
  74. JPanel buttonPaenl1 = new JPanel();
  75.  
  76. first = new JButton( "First" );
  77. first.addActionListener( this );
  78.  
  79. search = new JButton( "Search" );
  80. search.addActionListener( this );
  81.  
  82. next = new JButton( "Next" );
  83. next.addActionListener( this );
  84.  
  85. previous = new JButton( "Previous" );
  86. previous.addActionListener( this );
  87.  
  88. last = new JButton( "Last" );
  89. last.addActionListener( this );
  90.  
  91. add = new JButton( "Add" );
  92. add.addActionListener( this );
  93.  
  94. modify = new JButton( "Modify" );
  95. modify.addActionListener( this );
  96.  
  97. delete = new JButton( "Delete" );
  98. delete.addActionListener( this );
  99.  
  100. save = new JButton( "Save" );
  101. save.addActionListener( this );
  102.  
  103. exit = new JButton( "Exit" );
  104. exit.addActionListener( this );
  105.  
  106. buttonPaenl.setLayout( new FlowLayout() );
  107. buttonPaenl1.setLayout( new FlowLayout() );
  108. buttonPaenl.add( first );
  109. buttonPaenl.add( previous );
  110. buttonPaenl.add( next );
  111. buttonPaenl.add( last );
  112. buttonPaenl1.add( add );
  113. buttonPaenl1.add( modify );
  114. buttonPaenl1.add( delete );
  115. buttonPaenl1.add( save );
  116. buttonPaenl1.add( search );
  117. buttonPaenl1.add( exit );
  118.  
  119. cp.add( buttonPaenl,BorderLayout.SOUTH );
  120. cp.add( buttonPaenl1,BorderLayout.NORTH );
  121.  
  122. ImageIcon icon = new ImageIcon( "AutobotCompanyLogo.jpg" );
  123. imageLabel = new JLabel( icon, JLabel.CENTER );
  124. cp.add( imageLabel, BorderLayout.EAST );
  125. this.setContentPane( cp );
  126.  
  127. this.setTitle( " Week Nine Final Project: Inventory Program Part Six - Transformers Autobot Inventory ( More Than Meets the Eye o_0?! ) " );
  128.  
  129. this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  130. this.textArea.setText(inv.getFirst());
  131. this.setSize(400, 400);
  132. this.pack();
  133. this.setVisible( true );
  134. }
  135.  
  136. @Override
  137. public void actionPerformed( ActionEvent e )
  138. {
  139. // TODO Auto-generated method stub
  140. if( e.getActionCommand().equals( "First" ) )
  141. {
  142. textArea.setText( inv.getFirst() );
  143. }
  144.  
  145. if( e.getActionCommand().equals( "Next" ) )
  146. {
  147. textArea.setText(inv.getNext());
  148. }
  149.  
  150. if( e.getActionCommand().equals( "Previous" ) )
  151. {
  152. textArea.setText( inv.getPrevious() );
  153. }
  154.  
  155. if( e.getActionCommand().equals( "Last" ) )
  156. {
  157. textArea.setText(inv.getLast());
  158. }
  159.  
  160. if( e.getActionCommand().equals( "Save" ) )
  161. {
  162. save();
  163. }
  164.  
  165. if( e.getActionCommand().equals( "Exit" ) )
  166. {
  167. System.exit(0);
  168. }
  169.  
  170. if( e.getActionCommand().equals( "Add" ) )
  171. {
  172. TransformersInventoryDialogue id = new TransformersInventoryDialogue( true," Add a new Inventory " );
  173. }
  174.  
  175. if( e.getActionCommand().equals( "Modify" ) )
  176. {
  177. TransformersInventoryDialogue id = new TransformersInventoryDialogue( false," Modify Inventory " );
  178. }
  179.  
  180. if( e.getActionCommand().equals( "Search" ) )
  181. {
  182. String s = JOptionPane.showInputDialog( null, " Enter Product Name ",null, 1 );
  183.  
  184. s = inv.Search(s);
  185.  
  186. if( s == null )
  187. {
  188. JOptionPane.showMessageDialog( this, " No Results Found " ) ;
  189. }
  190.  
  191. else textArea.setText( s );
  192. }
  193.  
  194. if( e.getActionCommand().equals( "Delete" ) )
  195. {
  196. inv.getproducts().remove( inv.index );
  197. }
  198.  
  199. }
  200.  
  201. public void save()
  202. {
  203. // Initialize object
  204. String filepath = "C:/data";
  205.  
  206. File myDir = new File( filepath );
  207.  
  208. // Check if directory exists
  209. if( !myDir.exists( ) )
  210. {
  211. try
  212. {
  213. if( !myDir.mkdirs( ) )
  214. {
  215. // Could not create folder(s), so end execution here
  216. }
  217. }
  218. catch( SecurityException e )
  219. {
  220. // Error handling here
  221. }
  222. }
  223.  
  224.  
  225. File f = new File( "C:\\data\\inventory.dat" );
  226.  
  227. try
  228. {
  229. FileOutputStream out = new FileOutputStream( f );
  230.  
  231. try
  232. {
  233. ObjectOutputStream objectOut = new ObjectOutputStream( out );
  234. objectOut.writeObject( inv );
  235. }
  236.  
  237. catch (IOException e)
  238. {
  239. // TODO Auto-generated catch block
  240. JOptionPane.showMessageDialog( null, e.getMessage() );
  241. e.printStackTrace();
  242. }
  243. }
  244.  
  245. catch ( FileNotFoundException e )
  246. {
  247. // TODO Auto-generated catch block
  248. JOptionPane.showMessageDialog( null, e.getMessage() );
  249. e.printStackTrace();
  250. }
  251.  
  252. }
  253.  
  254. public static TransformersInventoryInfo getInv()
  255. {
  256. return inv;
  257. }
  258.  
  259. public static void setInv( TransformersInventoryInfo inv )
  260. {
  261. TransformersGUIMainExecution.inv = inv;
  262. }
  263.  
  264. /**
  265. * @param args
  266. */
  267. public static void main( String[] args )
  268. {
  269. TransformersGUIMainExecution TransformersInventoryInfo = new TransformersGUIMainExecution( "Inventory" );
  270. }
  271. }
  1. import java.awt.BorderLayout;
  2. import java.awt.FlowLayout;
  3. import java.awt.LayoutManager;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.BoxLayout;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14.  
  15.  
  16. public class TransformersInventoryDialogue extends JFrame implements ActionListener
  17. {
  18. private boolean status = false; //false is for modify
  19.  
  20. JTextField nameTextField,
  21. unitTextField,
  22. priceTextField;
  23.  
  24. JLabel nameLabel,
  25. unitLabel,
  26. priceLabel;
  27.  
  28. JPanel TextFields,
  29. buttons;
  30.  
  31. JButton modify,
  32. add;
  33.  
  34. public TransformersInventoryDialogue( Boolean stat, String title )
  35. {
  36. super( title );
  37. status = stat;
  38. JPanel but = new JPanel();
  39. modify = new JButton( "Modify" );
  40. modify.addActionListener( this );
  41. add = new JButton( "Add" );
  42. add.addActionListener( this );
  43. but.add( add );
  44. but.add( modify );
  45.  
  46. if( status == false )
  47. {
  48. add.setVisible( false );
  49. }
  50.  
  51. else
  52. {
  53. modify.setVisible( false );
  54. }
  55.  
  56. nameTextField = new JTextField( "", 20 );
  57. unitTextField = new JTextField( "", 5 );
  58. priceTextField = new JTextField( "" , 5 );
  59. nameLabel = new JLabel( "Name" );
  60. unitLabel = new JLabel( "Units" );
  61. priceLabel = new JLabel( "Price per Unit" );
  62. JPanel p = new JPanel();
  63.  
  64. p.setLayout( new FlowLayout() );
  65. p.add( nameLabel );
  66. p.add( nameTextField );
  67. p.add( unitLabel );
  68. p.add( unitTextField );
  69. p.add( priceLabel );
  70. p.add( priceTextField );
  71. JPanel cp = new JPanel();
  72. cp.setLayout( new BorderLayout() );
  73. cp.add( p, BorderLayout.CENTER );
  74. cp.add( but, BorderLayout.EAST );
  75. this.setContentPane( cp );
  76. this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
  77. this.pack();
  78. this.setVisible( true );
  79. }
  80.  
  81. @Override
  82. public void actionPerformed( ActionEvent arg0 )
  83. {
  84. if( arg0.getActionCommand().equals( "Add" ) )
  85. {
  86. String name;
  87. int units;
  88. Double price;
  89. name = nameTextField.getText();
  90.  
  91. try
  92. {
  93. units = Integer.parseInt( unitTextField.getText() );
  94. }
  95.  
  96. catch ( NumberFormatException e )
  97. {
  98. JOptionPane.showMessageDialog( null, "Please Enter Valid Integers" );
  99.  
  100. e.printStackTrace();
  101.  
  102. return;
  103. }
  104.  
  105. try
  106. {
  107. price = Double.parseDouble( priceTextField.getText() );
  108. }
  109.  
  110. catch ( NumberFormatException e )
  111. {
  112. // TODO Auto-generated catch block
  113. JOptionPane.showMessageDialog( null, "Please Enter Valid Price" );
  114.  
  115. e.printStackTrace();
  116.  
  117. return;
  118. }
  119.  
  120. TransformersGUIMainExecution.getInv().getproducts().add( new TransformersSubProduct( name, TransformersGUIMainExecution.getInv().products.size(),
  121. units,price ) );
  122.  
  123. this.dispose();
  124. }
  125.  
  126. if( arg0.getActionCommand().equals( "Modify" ) )
  127. {
  128. String name;
  129. int units;
  130. Double price;
  131. name = nameTextField.getText();
  132.  
  133. try
  134. {
  135. units = Integer.parseInt( unitTextField.getText() );
  136. }
  137.  
  138. catch ( NumberFormatException e )
  139. {
  140. JOptionPane.showMessageDialog( null,"Please Enter Valid Integers" );
  141.  
  142. e.printStackTrace();
  143.  
  144. return;
  145. }
  146.  
  147. try
  148. {
  149. price = Double.parseDouble( priceTextField.getText() );
  150. }
  151.  
  152. catch ( NumberFormatException e )
  153. {
  154. // TODO Auto-generated catch block
  155. JOptionPane.showMessageDialog( null,"Please Enter Valid Price" );
  156.  
  157. //e.printStackTrace();
  158. return;
  159. }
  160.  
  161. TransformersGUIMainExecution.getInv().getproducts().set( TransformersGUIMainExecution.getInv().index,
  162. new TransformersSubProduct( name, TransformersGUIMainExecution.getInv().index, units, price ) );
  163.  
  164. this.dispose();
  165. }
  166.  
  167. }
  168.  
  169. public static void main( String args[] )
  170. {
  171. TransformersInventoryDialogue idf = new TransformersInventoryDialogue( true,"cc" );
  172. }
  173. }
  1. import java.text.Collator;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.Locale;
  5. import java.io.Serializable;
  6.  
  7. public class TransformersInventoryInfo implements Serializable
  8. {
  9. //private String inventoryName; // name of inventory
  10.  
  11. //public String restockRate; // restock rate percentage
  12.  
  13. //public double totalRestock;
  14.  
  15. public static int index;
  16.  
  17. public ArrayList < TransformersSubProduct > products = new ArrayList < TransformersSubProduct > ();
  18.  
  19. public TransformersInventoryInfo()
  20. {
  21. products.add( new TransformersSubProduct( "No Sale Bot", 0, 1, 0.00 ) );
  22. products.add( new TransformersSubProduct( "Optimus Prime", 7, 52, 160.00 ) );
  23. products.add( new TransformersSubProduct( "BumbleeBee", 16, 61, 97.00 ) );
  24. products.add( new TransformersSubProduct( "Ironhide", 25, 106, 88.00 ) );
  25. products.add( new TransformersSubProduct( "Ratchet", 34, 115, 79.00 ) );
  26. products.add( new TransformersSubProduct( "Jazz", 43, 124, 70.00 ) );
  27.  
  28. index = 0;
  29. }
  30.  
  31. /*public void sort()
  32.   {
  33. Locale loc = Locale.ENGLISH;
  34.  
  35. ProductModified Temp;
  36.  
  37. Collator col = Collator.getInstance( loc );
  38.  
  39. for ( int i = 0; i < products.size(); i++ )
  40. {
  41. for ( int j = i + 1; j < products.size(); j++ )
  42. {
  43. if( col.compare( products.get( i ).getAutobotName(), products.get( j ).getAutobotName() ) > 0 )
  44. {
  45. Temp = products.get( i );
  46.  
  47. products.set( i, products.get( j ) );
  48. products.set( j, Temp );
  49. }
  50.   }
  51.   }
  52.   }
  53.  
  54.   /*public String getInventoryName()
  55.   {
  56.   return inventoryName;
  57.   }
  58.  
  59. public void setInventoryName( String inventoryName )
  60. {
  61. this.inventoryName = inventoryName;
  62. }
  63.  
  64. public String getRestockRate()
  65. {
  66. return restockRate;
  67. }
  68.  
  69. public void setRestockRate( String restockRate )
  70. {
  71. this.restockRate = restockRate;
  72. }
  73.  
  74. public double getTotalRestock()
  75. {
  76. return totalRestock;
  77. }
  78.  
  79. public void setTotalRestock( double totalRestock )
  80. {
  81. this.totalRestock = totalRestock;
  82. }*/
  83.  
  84. public ArrayList < TransformersSubProduct > getproducts()
  85. {
  86. return products;
  87. }
  88.  
  89. public void seproducts( ArrayList < TransformersSubProduct > products)
  90. {
  91. this.products = products;
  92. }
  93.  
  94. /*public String toString()
  95.   {
  96. String transformersSub = new String(" ");
  97.  
  98. transformersSub = "Welcome to the\n" + getInventoryName() + "!\n\n";
  99.  
  100. transformersSub = transformersSub + "Below is the available inventory:\n\n";
  101.  
  102. transformersSub = transformersSub + "The restock interest rate is at " + getRestockRate() + ".\n\n";
  103.  
  104. transformersSub = transformersSub + "Index\tProductModified #\t\t\tName\t\tUnits\t\tPrice\t\t";
  105.  
  106. transformersSub = transformersSub + "Unit Restock Value\t";
  107.  
  108. transformersSub = transformersSub + "Stock Value\t\t";
  109.  
  110. transformersSub = transformersSub + "Total ProductModified Restock\n";
  111.  
  112. Iterator < ProductModified > i = this.getproducts().iterator();
  113.  
  114. while(i.hasNext())
  115. {
  116. transformersSub = transformersSub + i.next();
  117. }
  118.  
  119.   return transformersSub;
  120.   }*/
  121.  
  122. public Double getTotal()
  123. {
  124. Double Total = 0.00;
  125.  
  126. TransformersSubProduct p;
  127.  
  128. Iterator < TransformersSubProduct > i = products.iterator();
  129.  
  130. while( i.hasNext() )
  131. {
  132. p = i.next();
  133.  
  134. Total = Total + p.getPrice() * p.getUnit();
  135. }
  136.  
  137. return Total;
  138. }
  139.  
  140. public Double getTotalRestock()
  141. {
  142. Double TotalRestock = 0.00;
  143.  
  144. TransformersSubProduct p;
  145.  
  146. Iterator < TransformersSubProduct > i = products.iterator();
  147.  
  148. while( i.hasNext() )
  149. {
  150. p = i.next();
  151.  
  152. TotalRestock = TotalRestock + ( 0.05 * p.getPrice() * p.getUnit() )
  153. + ( p.getPrice() * p.getUnit() );
  154. }
  155.  
  156. return TotalRestock;
  157. }
  158.  
  159. public String processOutPut( TransformersSubProduct p )
  160. {
  161. String out = "Welcome to the Transformers Autobot Toy Inventory!" + "\n\nBelow is the available Inventory:"
  162. + "\n\nThe restock interest rate is at 5%"
  163. + "\n\nItem #\tName\t\tUnits\tPrice\tUnit Restock\tStock Value\tTotal Product Restock"
  164. + p + "\n\nThe value of the entire inventory is $" + this.getTotal()
  165. + "\n\nThe 5% restocking cost for the entire inventory is $" + this.getTotalRestock();
  166.  
  167. return out;
  168. }
  169.  
  170. public String getFirst()
  171. {
  172. index = 0;
  173.  
  174. return processOutPut( products.get(0) );
  175.  
  176. }
  177.  
  178. public String getLast()
  179. {
  180. index = products.size() - 1;
  181.  
  182. return processOutPut( products.get( products.size() - 1 ) );
  183. }
  184.  
  185. public String getNext()
  186. {
  187. if( index == products.size() - 1 )
  188. {
  189. getFirst();
  190.  
  191. return getFirst();
  192. }
  193.  
  194. else
  195. {
  196. index++;
  197.  
  198. return processOutPut( products.get( index ) );
  199. }
  200. }
  201.  
  202. public String getPrevious()
  203. {
  204. if( index == 0 )
  205. {
  206. return getLast();
  207. }
  208.  
  209. else
  210.  
  211. return processOutPut(products.get(--index));
  212. }
  213.  
  214. public String Search( String Search )
  215. {
  216. Iterator < TransformersSubProduct > i = products.iterator();
  217.  
  218. String te = null;
  219.  
  220. TransformersSubProduct p;
  221.  
  222. while( i.hasNext() )
  223. {
  224. p = i.next();
  225.  
  226. if( p.getAutobotName().equals( Search ) )
  227. {
  228. te = processOutPut( p );
  229. }
  230. }
  231.  
  232. return te;
  233. }
  234. }
  1. import static java.lang.System.out;
  2. import java.io.Serializable;
  3.  
  4. //class declaration for TransformersProduct
  5. public class TransformersProduct implements Serializable
  6. {
  7. public String autobotName; // array of autobot toy names
  8.  
  9. public int productNumber; // product # array ID for product
  10. public int unit; // array of autobot toy units
  11.  
  12. public double price; // array of autobot prices
  13.  
  14. public double restock;
  15. public double stock;
  16. public double totalRestock;
  17.  
  18. /**
  19. * @param autobotName
  20. * @param productNumber
  21. * @param unit
  22. * @param price
  23. */
  24.  
  25. public TransformersProduct( String autobotName, int productNumber, int unit,
  26. double price )
  27. {
  28. super();
  29. this.autobotName = autobotName;
  30. this.productNumber = productNumber;
  31. this.unit = unit;
  32. this.price = price;
  33. }
  34.  
  35. public String getAutobotName()
  36. {
  37. return autobotName;
  38. }
  39.  
  40. public void setAutobotName( String autobotName )
  41. {
  42. this.autobotName = autobotName;
  43. }
  44.  
  45. public int getProductNumber()
  46. {
  47. return productNumber;
  48. }
  49.  
  50. public void setProductNumber( int productNumber )
  51. {
  52. this.productNumber = productNumber;
  53. }
  54.  
  55. public int getUnit()
  56. {
  57. return unit;
  58. }
  59.  
  60. public void setUnit( int unit )
  61. {
  62. this.unit = unit;
  63. }
  64.  
  65. public double getPrice()
  66. {
  67. return price;
  68. }
  69.  
  70. public void setPrice( double price )
  71. {
  72. this.price = price;
  73. }
  74.  
  75. public double getRestock()
  76. {
  77. return restock;
  78. }
  79.  
  80. public Double restock()
  81. {
  82. return 0.05 * this.getPrice() + this.getPrice();
  83. }
  84.  
  85. public double getStock()
  86. {
  87. return stock;
  88. }
  89.  
  90. public Double stock()
  91. {
  92. return this.getPrice() * this.getUnit();
  93. }
  94.  
  95. public double getTotalRestock()
  96. {
  97. return totalRestock;
  98. }
  99.  
  100. public Double totalRestock()
  101. {
  102. return this.getPrice() + this.getUnit() * this.getPrice() * 0.05;
  103. }
  104.  
  105. public String toString()
  106. {
  107. return "\n"+this.productNumber+"\t"+this.autobotName+"\t"+this.unit +"\t"+this.price+ "\t";
  108. }
  109. }
  1. import java.io.Serializable;
  2.  
  3. public class TransformersSubProduct extends TransformersProduct implements Serializable
  4. {
  5. public TransformersSubProduct( String autobotName, int productNumber, int unit,
  6. double price )
  7. {
  8. super( autobotName, productNumber, unit, price );
  9.  
  10. this.restock = this.restock();
  11.  
  12. this.stock = this.stock();
  13.  
  14. this.totalRestock = this.totalRestock();
  15. // TODO Auto-generated constructor stub
  16. }
  17.  
  18. public Double restock()
  19. {
  20. return this.getPrice() + this.getPrice() * 0.05;
  21. }
  22.  
  23. public Double stock()
  24. {
  25. return this.getUnit() * this.getPrice();
  26. }
  27.  
  28. public Double totalRestock()
  29. {
  30. return this.getUnit() * this.getPrice() + this.getUnit() * this.getPrice() * 0.05;
  31. }
  32.  
  33. public String toString()
  34. {
  35. return "\n\n" + this.productNumber + "\t" + this.autobotName
  36. + "\t\t" + this.unit + "\t$ " + this.price
  37. + "\t$ " + this.getRestock()
  38. + "\t$ " + this.getStock()
  39. + "\t$ " + this.getTotalRestock()
  40. + "\n";
  41. }
  42. }



Here's my previous assignment, with the sorter working.

  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.TextArea;
  5. import java.awt.Container;
  6.  
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. import java.text.Collator;
  11.  
  12. import java.util.Locale;
  13.  
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JPanel;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTextArea;
  19. import javax.swing.ImageIcon;
  20. import javax.swing.JLabel;
  21.  
  22.  
  23. import javax.swing.border.Border;
  24.  
  25. // class declaration for TransformersInventory5
  26. public class TransformersInventory5 extends JFrame implements ActionListener
  27. {
  28. private static TransformersProductSubClass myTransformers;
  29. public static int currentRecordShown = 0;
  30. private JTextArea textArea;
  31. private JButton first,next,previous,last;
  32.  
  33. // declare array name autobotNameArray
  34. static String autobotNameArray[] = { "No Sale bot", "Optimus Prime", "BumbleBee",
  35. "Ironhide", "Ratchet", "Jazz" };
  36.  
  37. static int autobotProductArray[] = { 0, 7, 16, 25, 34, 43 }; // product # array
  38. static int autobotUnitArray[] = { 1, 52, 61, 106, 115, 124 }; // array of autobot toy units
  39.  
  40. static double autobotPriceArray[] = { 0.00, 160.00, 97.00, 88.00, 79.00,70.00 }; // array of autobot toy prices
  41.  
  42. static double autobotRestockArray[] = { 0.05 * autobotPriceArray[ 0 ] + autobotPriceArray[ 0 ],
  43. 0.05 * autobotPriceArray[ 1 ] + autobotPriceArray[ 1 ],
  44. 0.05 * autobotPriceArray[ 2 ] + autobotPriceArray[ 2 ],
  45. 0.05 * autobotPriceArray[ 3 ] + autobotPriceArray[ 3 ],
  46. 0.05 * autobotPriceArray[ 4 ] + autobotPriceArray[ 4 ],
  47. 0.05 * autobotPriceArray[ 5 ] + autobotPriceArray[ 5 ] }; // array of autobot stock values
  48.  
  49. static double autobotStockArray[] = { autobotUnitArray[ 0 ] * autobotPriceArray[ 0 ],
  50. autobotUnitArray[ 1 ] * autobotPriceArray[ 1 ],
  51. autobotUnitArray[ 2 ] * autobotPriceArray[ 2 ],
  52. autobotUnitArray[ 3 ] * autobotPriceArray[ 3 ],
  53. autobotUnitArray[ 4 ] * autobotPriceArray[ 4 ],
  54. autobotUnitArray[ 5 ] * autobotPriceArray[ 5 ] }; // array of autobot stock values
  55.  
  56. static double autobotTotalRestockArray[] = { 0.05 * autobotStockArray[ 0 ] + autobotStockArray[ 0 ],
  57. 0.05 * autobotStockArray[ 1 ] + autobotStockArray[ 1 ],
  58. 0.05 * autobotStockArray[ 2 ] + autobotStockArray[ 2 ],
  59. 0.05 * autobotStockArray[ 3 ] + autobotStockArray[ 3 ],
  60. 0.05 * autobotStockArray[ 4 ] + autobotStockArray[ 4 ],
  61. 0.05 * autobotStockArray[ 5 ] + autobotStockArray[ 5 ] }; // array of autobot stock values
  62.  
  63. public TransformersInventory5()
  64. {
  65. textArea = new JTextArea( 250,30 );
  66.  
  67. JScrollPane scrollPane = new JScrollPane( textArea );
  68.  
  69. textArea.setEditable( false );
  70.  
  71. scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
  72.  
  73. scrollPane.setPreferredSize( new Dimension( 250, 250 ) );
  74.  
  75. JPanel cp = new JPanel();
  76.  
  77. cp.setSize( 250, 40 );
  78.  
  79. cp.setLayout( new BorderLayout() );
  80.  
  81. cp.add( scrollPane,BorderLayout.CENTER );
  82.  
  83. JPanel buttonPaenl = new JPanel();
  84.  
  85. first = new JButton( "First" );
  86. first.addActionListener(this);
  87.  
  88. next = new JButton( "Next" );
  89. next.addActionListener( this );
  90.  
  91. previous = new JButton( "Previous" );
  92. previous.addActionListener( this );
  93.  
  94. last = new JButton( "Last" );
  95. last.addActionListener( this );
  96.  
  97. buttonPaenl.setLayout( new FlowLayout() );
  98. buttonPaenl.add( first );
  99. buttonPaenl.add( previous );
  100. buttonPaenl.add( next );
  101. buttonPaenl.add( last );
  102. cp.add( buttonPaenl,
  103. BorderLayout.SOUTH );
  104.  
  105. this.setContentPane( cp );
  106.  
  107. this.setTitle( "Week Eight CheckPoint: Inventory Program Part 5 ( Oooey GUI Phooey -- o_0?! )" );
  108.  
  109. this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  110.  
  111. this.pack();
  112.  
  113. inventorySorter();
  114.  
  115. myTransformers = new TransformersProductSubClass( "Transformers Autobot Toy Inventory",
  116. "5%",
  117. autobotNameArray,
  118. autobotProductArray,
  119. autobotUnitArray,
  120. autobotPriceArray,
  121. autobotRestockArray,
  122. autobotStockArray,
  123. autobotTotalRestockArray );
  124. textArea.setText( myTransformers.outputInventory() );
  125.  
  126. this.setVisible( true );
  127. } // end
  128.  
  129. // main method begins program execution
  130. public static void main( String args[] )
  131. {
  132.  
  133. JFrame frame = new JFrame();
  134. ImageIcon icon = new ImageIcon("AutobotCompanyLogo.jpg");
  135. JLabel label = new JLabel(icon);
  136. Container contentPane = frame.getContentPane();
  137.  
  138. contentPane.add(label);
  139. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  140. frame.pack();
  141. frame.setVisible(true);
  142.  
  143. TransformersInventory5 tfI2 = new TransformersInventory5();
  144. } // end main
  145.  
  146. // inventorySorter method
  147. public static void inventorySorter()
  148. {
  149. String sorter;
  150.  
  151. int sorterAutobotProductArray,
  152. sorterAutobotUnitArray;
  153.  
  154. double sorterAutobotPriceArray,
  155. sorterAutobotRestockArray,
  156. sorterAutobotStockArray,
  157. sorterAutobotTotalRestockArray;
  158.  
  159. Locale loc = Locale.ENGLISH;
  160.  
  161. Collator col = Collator.getInstance( loc );
  162.  
  163. for ( int i = 0; i < autobotNameArray.length; i++ )
  164. {
  165. for ( int j = i + 1; j < autobotNameArray.length; j++ )
  166. {
  167. if( col.compare(autobotNameArray[i], autobotNameArray[j] ) > 0 )
  168. {
  169. sorter = autobotNameArray[ i ];
  170. sorterAutobotProductArray = autobotProductArray[ i ];
  171. sorterAutobotUnitArray = autobotUnitArray[ i ];
  172. sorterAutobotPriceArray = autobotPriceArray[ i ];
  173. sorterAutobotRestockArray = autobotRestockArray[ i ];
  174. sorterAutobotStockArray = autobotStockArray[ i ];
  175. sorterAutobotTotalRestockArray = autobotTotalRestockArray[ i ];
  176.  
  177. autobotNameArray[ i ] = autobotNameArray[ j ];
  178. autobotProductArray[ i ] = autobotProductArray[ j ];
  179. autobotUnitArray[ i ] = autobotUnitArray[ j ];
  180. autobotPriceArray[ i ] = autobotPriceArray[ j ];
  181. autobotRestockArray[ i ] = autobotRestockArray[ j ];
  182. autobotStockArray[ i ] = autobotStockArray[ j ];
  183. autobotTotalRestockArray[ i ] = autobotTotalRestockArray[ j ];
  184.  
  185. autobotNameArray[ j ] = sorter;
  186. autobotProductArray[ j ] = sorterAutobotProductArray;
  187. autobotUnitArray[ j ] = sorterAutobotUnitArray;
  188. autobotPriceArray[ j ] = sorterAutobotPriceArray;
  189. autobotRestockArray[ j ] = sorterAutobotRestockArray;
  190. autobotStockArray[ j ] = sorterAutobotStockArray;
  191. autobotTotalRestockArray[ j ] = sorterAutobotTotalRestockArray;
  192. } // end 2nd inner if
  193. } // end 1st inner if
  194. } // end for
  195. } // end inventorySorter
  196.  
  197. public void actionPerformed( ActionEvent e )
  198. {
  199. if( e.getActionCommand().equals( "First" ) )
  200. {
  201. textArea.setText( getFirst() );
  202. }
  203.  
  204. if( e.getActionCommand().equals( "Next" ) )
  205. {
  206. textArea.setText( getNext() );
  207. }
  208.  
  209. if( e.getActionCommand().equals( "Previous" ) )
  210. {
  211. textArea.setText( getPrevious() );
  212. }
  213.  
  214. if( e.getActionCommand().equals( "Last" ) )
  215. {
  216. textArea.setText( getLast() );
  217. }
  218.  
  219. }
  220. public String getFirst()
  221. {
  222. String firstRecord = getTitleForPrinting();
  223.  
  224. firstRecord = firstRecord + "\n" + 1 + "\t" + myTransformers.product[ 1 ] + "\t\t\t"
  225. + myTransformers.autobotName[ 1 ] + "\t\t" + myTransformers.unit[ 1 ]
  226. + "\t\t$ " + myTransformers.price[ 1 ] + "\t\t$ " + myTransformers.restock[ 1 ]
  227. + "\t\t$ " + myTransformers.stock[ 1 ] + "\t\t$ " + myTransformers.totalRestock[ 1 ]
  228. + "\n";
  229.  
  230. currentRecordShown=1;
  231.  
  232. return firstRecord;
  233. }
  234.  
  235. public String getLast()
  236. {
  237. int lastindex;
  238.  
  239. lastindex = myTransformers.product.length-1;
  240.  
  241. String lastRecord = getTitleForPrinting();
  242.  
  243. lastRecord = lastRecord + "\n" + lastindex + "\t" + myTransformers.product[ lastindex ] + "\t\t\t"
  244. + myTransformers.autobotName[ lastindex ] + "\t\t" + myTransformers.unit[ lastindex ] + "\t\t$ "
  245. + myTransformers.price[ lastindex ] + "\t\t$ " + myTransformers.restock[ lastindex ] + "\t\t$ "
  246. + myTransformers.stock[ lastindex] + "\t\t$ "+ myTransformers.totalRestock[ lastindex ] + "\n";
  247.  
  248. currentRecordShown = lastindex + 1;
  249.  
  250. return lastRecord;
  251. }
  252.  
  253. public String getNext()
  254. {
  255. if( currentRecordShown == myTransformers.product.length )
  256. return getFirst();
  257.  
  258. else
  259. {
  260. currentRecordShown++;
  261.  
  262. String nextRecord = getTitleForPrinting();
  263.  
  264. int index = currentRecordShown - 1;
  265.  
  266. nextRecord = nextRecord + "\n" + index + "\t" + myTransformers.product[ index ] + "\t\t\t"
  267. + myTransformers.autobotName[ index ] + "\t\t" + myTransformers.unit[ index ] + "\t\t$ "
  268. + myTransformers.price[ index ] + "\t\t$ " + myTransformers.restock[ index ] + "\t\t$ "
  269. + myTransformers.stock[ index ] + "\t\t$ " + myTransformers.totalRestock[ index ] + "\n";
  270.  
  271. return nextRecord;
  272. }
  273. }
  274. public String getPrevious()
  275. {
  276. if( currentRecordShown == 1 )
  277. return getLast();
  278.  
  279. else
  280. {
  281. currentRecordShown--;
  282.  
  283. String previousRecord=getTitleForPrinting();
  284.  
  285. int index = currentRecordShown - 1;
  286.  
  287. previousRecord = previousRecord + "\n" + index + "\t" + myTransformers.product[ index ] + "\t\t\t"
  288. + myTransformers.autobotName[ index ] + "\t\t" + myTransformers.unit[ index ] + "\t\t$ "
  289. + myTransformers.price[ index ] + "\t\t$ " + myTransformers.restock[ index ] + "\t\t$ "
  290. + myTransformers.stock[ index ] + "\t\t$ " + myTransformers.totalRestock[ index ]
  291. + "\n";
  292.  
  293. return previousRecord;
  294. }
  295. }
  296.  
  297. public String getTitleForPrinting()
  298. {
  299. String transformersSub = new String(" ");
  300.  
  301. transformersSub = "Welcome to the\n" + myTransformers.getInventoryName() + "!\n\n";
  302.  
  303. transformersSub = transformersSub + "Below is the available inventory:\n\n";
  304.  
  305. transformersSub = transformersSub + "The restock interest rate is at " + myTransformers.getRestockRate()
  306. + ".\n\n";
  307.  
  308. transformersSub = transformersSub + "Index\tProduct #\t\t\tName\t\tUnits\t\tPrice\t\t";
  309.  
  310. transformersSub = transformersSub + "Unit Restock Value\t";
  311.  
  312. transformersSub = transformersSub + "Stock Value\t\t";
  313.  
  314. transformersSub = transformersSub + "Total Product Restock\n";
  315.  
  316. return transformersSub;
  317. }
  318. }
  1. import static java.lang.System.out;
  2.  
  3.  
  4. // class declaration for TransformersProduct
  5. public class TransformersProduct
  6. {
  7. private String inventoryName; // name of inventory
  8.  
  9. public String restockRate; // restock rate percentage
  10.  
  11. public String autobotName[]; // array of autobot toy names
  12.  
  13. public int product[]; // product # array
  14. public int unit[]; // array of autobot toy units
  15.  
  16. public double price[]; // array of autobot prices
  17. public double restock[];
  18. public double stock[]; // array of autobot stock values
  19. public double totalRestock[];
  20.  
  21. /* nine-argument constructor initializes inventoryName, restockRate,
  22.   *autobotName, product, unit, price, restock, stock, and totalRestock arrays. */
  23. public TransformersProduct( String name, String rate, String autobotNameArray[],
  24. int autobotProductArray[], int autobotUnitArray[],
  25. double autobotPriceArray[], double autobotRestockArray[],
  26. double autobotStockArray[], double autobotTotalRestockArray[] )
  27. {
  28. inventoryName = name; // initialize inventoryName
  29. restockRate = rate; // initialize restock rate
  30. autobotName = autobotNameArray; // initialize autobotName
  31. product = autobotProductArray; // initialize product
  32. unit = autobotUnitArray; // initialize unit
  33. price = autobotPriceArray; // initialize price
  34. restock = autobotRestockArray;
  35. stock = autobotStockArray;
  36. totalRestock = autobotTotalRestockArray; // initialize stock
  37. } // end eight-argument TransformersProduct constructor
  38.  
  39. // method to set the inventory name
  40. public void setInventoryName ( String name )
  41. {
  42. inventoryName = name; // store inventory name
  43. } // end method setInventoryName
  44.  
  45. // method to retrieve the inventory name
  46. public String getInventoryName()
  47. {
  48. return inventoryName; //
  49. } // end method getInventoryName
  50.  
  51. // method to set the inventory name
  52. public void setRestockRate ( String rate )
  53. {
  54. restockRate = rate; // store restock rate
  55. } // end method setInventoryName
  56.  
  57. // method to retrieve the restock rate
  58. public String getRestockRate()
  59. {
  60. return restockRate; //
  61. } // end method getInventoryName
  62.  
  63. // display a welcome message to the TransformersInventory5 user
  64. public void displayMessage()
  65. {
  66. // getInventoryName gets the name of the inventory
  67. out.printf( "Welcome to the\n%s!\n\n",
  68. getInventoryName() );
  69. } // end method displayMessage
  70.  
  71. // display a welcome message to the TransformersInventory5 user
  72. public void displayMessage2()
  73. {
  74. // getInventoryName gets the name of the inventory
  75. out.printf( "The restock interest rate is at %s.\n\n",
  76. getRestockRate() );
  77. } // end method displayMessage2
  78.  
  79. //method to display the invertory arrays output
  80. public void processInventory()
  81. {
  82. // output inventory arrays
  83. outputInventory();
  84. } // end method processInventory
  85.  
  86. // output inventory arrays
  87. public String outputInventory()
  88. {
  89. // displays available inventory message
  90. out.println( "Below is the available inventory:\n" );
  91.  
  92. // create column headings
  93. out.print( "Index\tProduct #\t\tName\t\tUnits\t\t\tPrice\t\t" );
  94.  
  95. out.print( " Unit Restock Value\t" ); // create Stock Value column
  96.  
  97. out.print( "Stock Value\t" ); // create Stock Value column
  98.  
  99. out.print( "Total Product Restock\n" ); // create Stock Value column
  100.  
  101. // output each arrays' elements' value
  102. for ( int toyCounter = 0; toyCounter < autobotName.length; toyCounter++)
  103. out.printf( "\n%5d%12d%19s%17d $%10.2f $%10.2f $%10.2f $%10.2f\n",
  104. toyCounter, product[ toyCounter ], autobotName[ toyCounter ],
  105. unit[ toyCounter ], price[ toyCounter ], restock[ toyCounter ],
  106. stock[ toyCounter ], totalRestock[ toyCounter ] );
  107.  
  108. // calculate entire inventory value
  109. double totalStockValue = stock[ 0 ] + stock[ 1 ] + stock[ 2 ] + stock[ 3 ] + stock[ 4 ] + stock[ 5 ];
  110.  
  111. // calculate the restocking cose
  112. double totalRestockValue = 0.05 * totalStockValue + totalStockValue;
  113.  
  114. out.printf( "\nThe value of the entire inventory is $ %.2f\n", totalStockValue );
  115. out.printf( "\nThe restocking cost for the entire inventory is $ %.2f\n", totalRestockValue );
  116.  
  117. return "Please Call the subClass function";
  118.  
  119. } // end outputInventory
  120. }
  1. import static java.lang.System.out;
  2.  
  3. public class TransformersProductSubClass extends TransformersProduct
  4. {
  5. public TransformersProductSubClass( String name, String rate, String autobotNameArray[],
  6. int autobotProductArray[], int autobotUnitArray[],
  7. double autobotPriceArray[], double autobotRestockArray[],
  8. double autobotStockArray[], double autobotTotalRestockArray[] )
  9. {
  10. super( name, rate, autobotNameArray, autobotProductArray, autobotUnitArray,
  11. autobotPriceArray, autobotRestockArray, autobotStockArray, autobotTotalRestockArray );
  12. }
  13.  
  14. public String outputInventory()
  15. {
  16. String transformersSub = new String(" ");
  17.  
  18. transformersSub = "Welcome to the\n" + getInventoryName() + "!\n\n";
  19.  
  20. transformersSub = transformersSub + "Below is the available inventory:\n\n";
  21.  
  22. transformersSub = transformersSub + "The restock interest rate is at " + getRestockRate() + ".\n\n";
  23.  
  24. transformersSub = transformersSub + "Index\tProduct #\t\t\tName\t\tUnits\t\tPrice\t\t";
  25.  
  26. transformersSub = transformersSub + "Unit Restock Value\t";
  27.  
  28. transformersSub = transformersSub + "Stock Value\t\t";
  29.  
  30. transformersSub = transformersSub + "Total Product Restock\n";
  31.  
  32. for ( int toyCounter = 0; toyCounter < autobotName.length; toyCounter++)
  33. {
  34. transformersSub = transformersSub + "\n" + toyCounter + "\t" + product[ toyCounter ]
  35. + "\t\t\t" + autobotName[ toyCounter ] + "\t\t" + unit[ toyCounter ] + "\t\t$ "+ price[ toyCounter ]
  36. + "\t\t$ "+ restock[ toyCounter ] + "\t\t$ "+ stock[ toyCounter ] + "\t\t$ "+ totalRestock[ toyCounter ]
  37. + "\n";
  38. }
  39.  
  40. double totalStockValue = stock[ 0 ] + stock[ 1 ] + stock[ 2 ] + stock[ 3 ] + stock[ 4 ] + stock[ 5 ];
  41. double totalRestockValue = 0.05 * totalStockValue + totalStockValue;
  42.  
  43. transformersSub = transformersSub + "\nThe value of the entire inventory with additional 5% is $ "
  44. + totalStockValue + "\n";
  45.  
  46. transformersSub = transformersSub + "\nThe restocking cost for the entire inventory is $ "
  47. + totalRestockValue;
  48.  
  49. return transformersSub;
  50.  
  51. }
  52. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Creating a "Data" folder in my C: Drive using my Java program

 
0
  #5
Nov 19th, 2007
1) keep backups...
2) keep organised...
3) don't hope we're going to read thousands of lines of code to find the one that's wrong.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: ITAutobot25 is an unknown quantity at this point 
Solved Threads: 0
ITAutobot25 ITAutobot25 is offline Offline
Newbie Poster

Re: Sorting Problem

 
0
  #6
Nov 19th, 2007
Sorry, I was off in a rush to work, I forgot to explain where is the error. It's in the third part of the code (TransformersInventoryInfo class). The sort is located near the beginning of the class at /* public void sort().
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,341
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 250
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Creating a "Data" folder in my C: Drive using my Java program

 
0
  #7
Nov 19th, 2007
Well, if it starts with "/*" then either that is the problem, or that code is not the problem, as it is commented out.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC