Hey check out my prog. It has an error can u find it?

Reply

Join Date: Mar 2005
Posts: 13
Reputation: buggytoast is an unknown quantity at this point 
Solved Threads: 0
buggytoast buggytoast is offline Offline
Newbie Poster

Hey check out my prog. It has an error can u find it?

 
0
  #1
Mar 22nd, 2006
Hi everyone! (it's been awhile XD)
I've gone and done my homework. And here I am with a program that works...pretty well I think.

There appears to be a problem with the saving/loading. Originally it worked without a problem, but when I attempted to make it more graphically friendly (when it displays in the list), the file saves, however I can't load it in panel 2.

You can get a 'Load Model Set', by just copy pasting the data below. Put it into a text file. (notepad)

48-209-351 1.3 300 5
60-344-351 1.45 250 12
72-554-351 1.32 200 9
84-754-351 1.35 350 3
96-1042-351 1.4 280 7

As I said before, originally it works, but now it doesn't. I'm nearing a deadline and I have to do uber documentation, can anyone help? I hope this is enough effort lol. Peace guys

PLEASE DO NOT COPY!!!!!!!

Here is Class TreeClassics
  1. // TreeClassics Program
  2. import java.io.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import java.util.*;
  7.  
  8. public class TreeClassics extends JFrame
  9. {
  10. //include aspects of GUI
  11. private JTextField quantityField, thicknessField, widthField,
  12. plysField, modelField, heightField, tipField, groupField;
  13. // modelField, heightField, tipField, groupField have not been
  14. // implemented yet; for panel 3
  15. //
  16. private JEditorPane scrollListContent;
  17. private JTextArea enteredContent;
  18. private JList enteredItemsList, loadedList, buggerList;
  19.  
  20. // Labels for field input identifier
  21. private JLabel quantityLabel, thicknessLabel,
  22. widthLabel, colorLabel, plysLabel,
  23. productionLabel, informLabel, specifyLabel, helpLabel,
  24. help2Label, help3Label, help4Label, help5Label,
  25. model1Label, quantity1Label, width1Label, plys1Label,
  26. color1Label, thickness1Label;
  27.  
  28. // The three tab panels
  29. private JPanel panel1, panel2, panel3;
  30.  
  31. // Combobox selection system
  32. private JComboBox modelComboBox, colorComboBox;
  33.  
  34. private JScrollPane orderScrollPane, enteredScrollPane, orderScrollPane2;
  35.  
  36. private JButton
  37. loadModelsButton, enterButton, removeTempItemsButton,
  38. saveListButton, deleteModelsButton, modelInfoButton,
  39. modelButton, thicknessButton, widthButton, colorButton,
  40. loadListButton, clearListButton, loadListButton2, selectButton;
  41.  
  42. private Vector<Model> modelList;
  43. private String[] colors = {"Green", "Teal", "Dark Blue", "White", "Gray"};
  44. private OrderList prodList;
  45.  
  46. private TreeClassics()
  47. {
  48. // GUI Setup for program
  49. super("Tree Classics Production Line");
  50.  
  51. this.prodList = new OrderList();
  52. this.modelList = new Vector<Model>();
  53.  
  54. // Creating JTabbed Pane
  55. JTabbedPane tabbedPane = new JTabbedPane();
  56.  
  57. // -------------------------PANEL 1 INTERFACE---------------------------
  58. // Set up Panel 1
  59. JLabel label1 = new JLabel();
  60. panel1 = new JPanel( null );
  61. panel1.add( label1 );
  62. tabbedPane.addTab( " Production Order ",
  63. null, panel1, "Production orders are created here");
  64.  
  65. // Model number selection label
  66. productionLabel = new JLabel( "Model Number");
  67. productionLabel.setBounds( 20, 20, 100, 20 );
  68. panel1.add( productionLabel );
  69.  
  70. // Add Combo Box into Panel 1: PRODUCTION ORDER
  71. modelComboBox = new JComboBox();
  72. modelComboBox.setBounds(20, 40, 150, 20);
  73. panel1.add(modelComboBox);
  74.  
  75. // Add Help Button into Panel1
  76. modelInfoButton = new JButton( "Model Information" );
  77. modelInfoButton.setBounds( 20, 70, 150, 20);
  78. panel1.add(modelInfoButton);
  79.  
  80. // Titles for Selection and treeData types
  81. // Quantity
  82. quantityLabel = new JLabel( "Quantity");
  83. quantityLabel.setBounds( 250, 20, 100, 20 );
  84. panel1.add(quantityLabel);
  85.  
  86. // Thickness
  87. thicknessLabel = new JLabel( "Thickness");
  88. thicknessLabel.setBounds( 250, 75, 100, 20 );
  89. panel1.add(thicknessLabel);
  90.  
  91. // Width
  92. widthLabel = new JLabel( "Width");
  93. widthLabel.setBounds( 370, 20, 100, 20 );
  94. panel1.add(widthLabel);
  95.  
  96. // Color
  97. colorLabel = new JLabel( "Color");
  98. colorLabel.setBounds( 490, 20, 100, 20 );
  99. panel1.add(colorLabel);
  100.  
  101. // Plys
  102. plysLabel = new JLabel( "Plys");
  103. plysLabel.setBounds( 370, 75, 100, 20 );
  104. panel1.add( plysLabel );
  105.  
  106. // Color 1 Label
  107. color1Label = new JLabel( "Color");
  108. color1Label.setBounds( 320, 130, 100, 20 );
  109. panel1.add(color1Label);
  110.  
  111. // Models 1 Label
  112. model1Label = new JLabel( "Model Number");
  113. model1Label.setBounds( 20, 130, 100, 20 );
  114. panel1.add(model1Label);
  115.  
  116. // quantity 1 Label
  117. quantity1Label = new JLabel( "Quantity");
  118. quantity1Label.setBounds( 120, 130, 100, 20 );
  119. panel1.add(quantity1Label);
  120.  
  121. // thickness 1 Label
  122. thickness1Label = new JLabel( "Thickness");
  123. thickness1Label.setBounds( 185, 130, 100, 20 );
  124. panel1.add(thickness1Label);
  125.  
  126. // width 1 Label
  127. width1Label = new JLabel( "Width");
  128. width1Label.setBounds( 260, 130, 100, 20 );
  129. panel1.add(width1Label);
  130.  
  131. // plys 1 Label
  132. plys1Label = new JLabel( "Plys");
  133. plys1Label.setBounds( 370, 130, 100, 20 );
  134. panel1.add(plys1Label);
  135.  
  136. // Quantity field input
  137. quantityField = new JTextField();
  138. quantityField = new JTextField("0", 15 );
  139. quantityField.setBounds( 250, 40 , 100, 20);
  140. panel1.add( quantityField );
  141.  
  142. // Width field input
  143. widthField = new JTextField();
  144. widthField = new JTextField("0", 15 );
  145. widthField.setBounds( 370, 40 , 100, 20);
  146. panel1.add( widthField );
  147.  
  148. // Thickness field input
  149. thicknessField = new JTextField();
  150. thicknessField = new JTextField("0", 15 );
  151. thicknessField.setBounds( 250, 95 , 100, 20);
  152. panel1.add(thicknessField);
  153.  
  154. // Plys field input
  155. plysField = new JTextField();
  156. plysField = new JTextField("0", 15 );
  157. plysField.setBounds( 370, 95 , 100, 20);
  158. panel1.add(plysField);
  159.  
  160. // Color scroll pane input
  161. colorComboBox = new JComboBox( colors );
  162. colorComboBox.setMaximumRowCount( 5 );
  163. colorComboBox.setBounds( 490, 40, 100, 20 );
  164. colorComboBox.setSelectedIndex( 0 );
  165. panel1.add(colorComboBox);
  166.  
  167. // Panel 1 scroll pane for entered information
  168. //enteredContent = new JTextArea();
  169. //enteredContent.setEditable(false);
  170. enteredItemsList = new JList(prodList);
  171. enteredScrollPane = new JScrollPane(enteredItemsList);
  172. enteredScrollPane.setBounds(20,150,400,150);
  173. enteredScrollPane.createVerticalScrollBar();
  174. panel1.add(enteredScrollPane);
  175.  
  176. // Add Enter Button into Panel1
  177. enterButton = new JButton("Enter");
  178. enterButton.setBounds( 450, 150 , 100, 20);
  179. panel1.add(enterButton);
  180.  
  181. removeTempItemsButton = new JButton("Remove Selected");
  182. removeTempItemsButton.setBounds( 450, 180 , 150, 20);
  183. panel1.add(removeTempItemsButton);
  184.  
  185. // Add Save Button into Panel1
  186. saveListButton = new JButton( "Save List" );
  187. saveListButton.setBounds( 450, 210 , 150, 20);
  188. panel1.add(saveListButton);
  189.  
  190. // Add load model button into Panel1
  191. loadModelsButton = new JButton("Load Model Set");
  192. loadModelsButton.setBounds( 450, 240, 150, 20);
  193. panel1.add( loadModelsButton);
  194.  
  195. // Add delete model button into Panel1
  196. deleteModelsButton = new JButton("Delete Model Set");
  197. deleteModelsButton.setBounds( 450, 270, 150, 20);
  198. panel1.add(deleteModelsButton);
  199.  
  200. // ------------------PANEL 2 INTERFACE----------------------------------
  201. // Set up Panel 2
  202. JLabel label2 = new JLabel( "Panel Two", SwingConstants.CENTER );
  203. panel2 = new JPanel( null );
  204. panel2.add( label2 );
  205. tabbedPane.addTab( " List Orders ",
  206. null, panel2, "Orders are listed and organized here");
  207.  
  208. // Add Infomer Label into Panel 2
  209. informLabel = new JLabel( "Information of this list");
  210. informLabel.setBounds( 40, 20, 300, 20 );
  211. panel2.add( informLabel );
  212.  
  213. // Add scroll pane label into Panel 2
  214. informLabel = new JLabel( "Model Quantity Thickness Width Color Plys");
  215. informLabel.setBounds( 20, 60, 400, 20 );
  216. panel2.add( informLabel );
  217.  
  218. // Panel 2 scroll pane
  219. scrollListContent = new JEditorPane();
  220. scrollListContent.setEditable(false);
  221. orderScrollPane = new JScrollPane(scrollListContent);
  222. orderScrollPane.setBounds(20,80,400,220);
  223. orderScrollPane.createVerticalScrollBar();
  224. panel2.add(orderScrollPane);
  225.  
  226. // Add Sort by Model Button into Panel2
  227. modelButton = new JButton( "Sort by Model #" );
  228. modelButton.setBounds( 450, 80 , 150, 20);
  229. panel2.add( modelButton );
  230.  
  231. // Add Sort by Thickness Button into Panel2
  232. thicknessButton = new JButton( "Sort by Thickness" );
  233. thicknessButton.setBounds( 450, 170 , 150, 20);
  234. panel2.add( thicknessButton );
  235.  
  236. // Add Sort by Width Button into Panel2
  237. widthButton = new JButton( "Sort by Width" );
  238. widthButton.setBounds( 450, 110 , 150, 20);
  239. panel2.add( widthButton );
  240.  
  241. // Add Sort by Color Button into Panel2
  242. colorButton = new JButton( "Sort by Color" );
  243. colorButton.setBounds( 450, 140 , 150, 20);
  244. panel2.add( colorButton );
  245.  
  246. // Add Load Button into Panel2
  247. loadListButton = new JButton( "Load List" );
  248. loadListButton.setBounds( 450, 200 , 150, 20);
  249. panel2.add(loadListButton);
  250.  
  251. // Add Clear Button into Panel2
  252. clearListButton = new JButton( "Clear List" );
  253. clearListButton.setBounds( 450, 230 , 150, 20);
  254. panel2.add(clearListButton);
  255.  
  256. // ------------------PANEL 3 INTERFACE----------------------------------
  257. // Set up Panel 3: For Printing
  258. JLabel label3 = new JLabel( "Panel Three", SwingConstants.CENTER );
  259. panel3 = new JPanel( null );
  260. panel3.add( label3 );
  261. tabbedPane.addTab( " HELP PANEL ",
  262. null, panel3, "Select the set of data of models to use");
  263.  
  264.  
  265. // Add Specify Label into Panel 3
  266. specifyLabel = new JLabel( "WELCOME TO THE HELP PANEL");
  267. specifyLabel.setBounds( 40, 20, 200, 20 );
  268. panel3.add( specifyLabel );
  269.  
  270. // Add Help Label into Panel 3
  271. helpLabel = new JLabel( "Step 1. LOAD MODEL SET");
  272. helpLabel.setBounds( 40, 60, 200, 20 );
  273. panel3.add( helpLabel );
  274.  
  275. // Add Help2 Label into Panel 3
  276. help2Label = new JLabel( "Step 2. INSERT ALL NECCESARY DATA INTO FIELDS");
  277. help2Label.setBounds( 40, 80, 300, 20 );
  278. panel3.add( help2Label );
  279.  
  280. // Add Help3 Label into Panel 3
  281. help3Label = new JLabel( "Step 3. SAVE / EDIT LIST AND PROCEED TO PANEL 2 ");
  282. help3Label.setBounds( 40, 100, 300, 20 );
  283. panel3.add( help3Label );
  284.  
  285. // Add Help4 Label into Panel 3
  286. help4Label = new JLabel( "Step 4. INSERT ALL NECCESARY DATA ");
  287. help4Label.setBounds( 40, 120, 300, 20 );
  288. panel3.add( help4Label );
  289.  
  290. // Add Help5 Label into Panel 3
  291. help5Label = new JLabel( "Step 5. INSERT ALL NECCESARY DATA ");
  292. help5Label.setBounds( 40, 140, 300, 20 );
  293. panel3.add( help5Label );
  294.  
  295. getContentPane().add( tabbedPane );
  296. setSize( 640, 400 );
  297. setVisible(true);
  298.  
  299. //Panel 1 handlers------------------------------------------------------
  300. // Help button to check fields entry
  301. modelInfoButton.addActionListener (
  302. new ActionListener()
  303. { public void actionPerformed( ActionEvent e) {showModelInfo();} }
  304. );
  305.  
  306. // Enter button to check fields entry
  307. enterButton.addActionListener (
  308. new ActionListener()
  309. { public void actionPerformed(ActionEvent e) {readInput();} }
  310. );
  311.  
  312. // button to remove selected items from temp list
  313. removeTempItemsButton.addActionListener (
  314. new ActionListener()
  315. { public void actionPerformed(ActionEvent e) {removeTempItems();} }
  316. );
  317.  
  318. // Save button call
  319. saveListButton.addActionListener (
  320. new ActionListener()
  321. { public void actionPerformed(ActionEvent e) {saveList();} }
  322. );
  323.  
  324. // Load button call
  325. loadModelsButton.addActionListener(
  326. new ActionListener()
  327. { public void actionPerformed(ActionEvent e) {loadModels();} }
  328. );
  329.  
  330. // Clear button
  331. deleteModelsButton.addActionListener (
  332. new ActionListener()
  333. { public void actionPerformed( ActionEvent e) {deleteModels();} }
  334. );
  335.  
  336. //Panel 2 handlers------------------------------------------------------
  337. // Load button call
  338. loadListButton.addActionListener(
  339. new ActionListener()
  340. { public void actionPerformed(ActionEvent e) {openList();} }
  341. );
  342.  
  343. // Clear button
  344. clearListButton.addActionListener (
  345. new ActionListener()
  346. { public void actionPerformed(ActionEvent e) {clearList();} }
  347. );
  348.  
  349. // Sort by model button
  350. modelButton.addActionListener (
  351. new ActionListener()
  352. {
  353. public void actionPerformed(ActionEvent e) {prodList.sort(0);
  354. scrollListContent.setText(prodList.toString());
  355. } }
  356. );
  357.  
  358. // Sort by thickness button
  359. modelButton.addActionListener (
  360. new ActionListener()
  361. {
  362. public void actionPerformed(ActionEvent e) {prodList.sort(1);
  363. scrollListContent.setText(prodList.toString());
  364. } }
  365. );
  366.  
  367. // Width sort button
  368. widthButton.addActionListener (
  369. new ActionListener()
  370. {
  371. public void actionPerformed(ActionEvent e) {prodList.sort(2);
  372. scrollListContent.setText(prodList.toString());
  373. } }
  374. );
  375.  
  376. // Color sort button
  377. colorButton.addActionListener (
  378. new ActionListener()
  379. {
  380. public void actionPerformed(ActionEvent e) {prodList.sort(3);
  381. scrollListContent.setText(prodList.toString());
  382. } }
  383. );
  384.  
  385. //Panel 3 handlers------------------------------------------------------
  386. // Load button call
  387. }
  388.  
  389. //read from input fields, then add item to list
  390. private void readInput()
  391. {
  392. String modelNo = (String)(modelComboBox.getSelectedItem());
  393. if (modelNo == null) return;
  394. try {
  395. int quantity = Integer.parseInt(quantityField.getText());
  396. double thickness = Double.parseDouble(thicknessField.getText());
  397. double width = Double.parseDouble(widthField.getText());
  398. String color = (String)(colorComboBox.getSelectedItem());
  399. double plys = Double.parseDouble(plysField.getText());
  400.  
  401. if (quantity < 0 || thickness < 0 || width < 0 || plys < 0)
  402. throw new NumberFormatException("Negative attributes");
  403. prodList.add(
  404. new TreeData( modelNo, quantity, thickness, width, color, plys));
  405. // Repaints the scroll pane to display newly entered data
  406. enteredItemsList.setListData(prodList);
  407. }
  408. catch (NumberFormatException e)
  409. { JOptionPane.showMessageDialog(this, "At least 1 field is illegal",
  410. "Error", JOptionPane.ERROR_MESSAGE ); }
  411. }
  412.  
  413. private void removeTempItems()
  414. {
  415. int[] select = enteredItemsList.getSelectedIndices();
  416. for (int i = select.length-1; i >= 0; i--)
  417. {
  418. int selectIndex = select[i];
  419. if (selectIndex < 0) return;
  420. prodList.removeElementAt(selectIndex);
  421. enteredItemsList.removeSelectionInterval(selectIndex, selectIndex);
  422. enteredItemsList.repaint();
  423. }
  424. }
  425.  
  426. //pop up a window showing model info
  427. private void showModelInfo()
  428. {
  429. //(String)(modelComboBox.getSelectedItem())
  430. }
  431.  
  432. /**
  433.   * CLEAR FILE
  434.   * Clears the scroll list pane in the 2nd panel
  435.   */
  436. private void clearList()
  437. {
  438. this.prodList = null; //Cleanup by garbage collector
  439. scrollListContent.setText("No list loaded.");
  440. panel2.repaint();
  441. }
  442.  
  443. /**
  444.   * OPEN FILE
  445.   * Opens
  446.   */
  447. private void openList()
  448. {
  449. File inFile = chooseFile(false);
  450. if (inFile == null) return;
  451.  
  452. prodList = new OrderList(inFile, this);
  453. scrollListContent.setText(prodList.toString());
  454. //panel2.repaint();
  455. }
  456.  
  457. //save list to file
  458. private void saveList()
  459. {
  460. if (prodList.size() == 0) {
  461. JOptionPane.showMessageDialog(this, "There is nothing to save",
  462. "ERROR", JOptionPane.ERROR_MESSAGE );
  463. return;
  464. }
  465.  
  466. File outFile = chooseFile(true);
  467. if (outFile == null) return;
  468. if (outFile.exists())
  469. if (JOptionPane.showConfirmDialog(this,
  470. "File " + outFile.getName() + " already exists. Overwrite?",
  471. "WARNING", JOptionPane.YES_NO_OPTION,
  472. JOptionPane.WARNING_MESSAGE) == JOptionPane.NO_OPTION)
  473. return;
  474.  
  475. try {
  476. FileWriter output = new FileWriter(outFile);
  477. String toWrite = prodList.toString();
  478. output.write(toWrite, 0, toWrite.length());
  479. output.flush();
  480. output.close();
  481. }
  482. catch (IOException e)
  483. { JOptionPane.showMessageDialog(this, "File cannot be saved",
  484. "ERROR", JOptionPane.ERROR_MESSAGE ); }
  485. }
  486.  
  487. //load list of available models to combo box
  488. private void loadModels()
  489. {
  490. File inFile = chooseFile(false);
  491. if (inFile == null) return;
  492. try {
  493. if (!(inFile.exists()))
  494. throw new IOException("File does not exist");
  495. modelComboBox.removeAllItems(); //clean up before adding
  496. Scanner in = new Scanner(inFile);
  497. while (in.hasNextLine()) {
  498. Scanner lineScanner = new Scanner(in.nextLine());
  499. try {
  500. String modelID = lineScanner.next();
  501. double height = lineScanner.nextDouble();
  502. int tipNum = lineScanner.nextInt();
  503. int group = lineScanner.nextInt();
  504. modelList.add(new Model(modelID, height, tipNum, group));
  505. modelComboBox.addItem(modelID);
  506. }
  507. catch (NoSuchElementException e) { continue; }
  508. }
  509. }
  510. catch (IOException e)
  511. { JOptionPane.showMessageDialog(this, "Error loading file",
  512. "Error", JOptionPane.ERROR_MESSAGE); }
  513. }
  514.  
  515. private File chooseFile(boolean saveDialog)
  516. {
  517. JFileChooser fileChooser = new JFileChooser();
  518. fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
  519.  
  520. // open file chooser; if user clicked Cancel then return null
  521. int chooserDialogReturn;
  522. if (saveDialog)
  523. chooserDialogReturn = fileChooser.showSaveDialog(this);
  524. else
  525. chooserDialogReturn = fileChooser.showOpenDialog(this);
  526. if (chooserDialogReturn == JFileChooser.CANCEL_OPTION)
  527. return null;
  528.  
  529. return fileChooser.getSelectedFile(); // get selected file
  530. }
  531.  
  532. //clear model dropdown list
  533. private void deleteModels()
  534. {
  535. modelList.removeAllElements();
  536. modelComboBox.removeAllItems();
  537. }
  538.  
  539. //================================MAIN======================================
  540.  
  541. public static void main(String args[])
  542. {
  543. TreeClassics tabbedPane = new TreeClassics();
  544. tabbedPane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  545. }
  546. }

Here is the TreeData
  1. /**
  2.  * The TreeData class
  3.  */
  4. public class TreeData
  5. {
  6. private String modelNo; // Tree model number (Format: XX-XXX-XXX)
  7. private int quantity; // Quantity of desired tree model
  8. private double thickness; // Thickness of plys needed for each tree (mm)
  9. private double width;
  10. private String color;
  11. private double plys;
  12.  
  13. // Class constructor (blank data)
  14. public TreeData()
  15. {}
  16.  
  17. // Class constructor (new data)
  18. public TreeData(String modelNo, int quantity, double thickness,
  19. double width, String color, double plys)
  20. {
  21. setModelNo(modelNo);
  22. setQuantity(quantity);
  23. setThickness(thickness);
  24. setWidth(width);
  25. setColor(color);
  26. setPlys(plys);
  27. }
  28.  
  29. public void setQuantity(int quantity)
  30. {this.quantity = quantity; }
  31.  
  32. public int getQuantity()
  33. {return quantity; }
  34.  
  35. public void setModelNo(String modelNo)
  36. {this.modelNo = modelNo;}
  37.  
  38. public String getModelNo()
  39. {return modelNo;}
  40.  
  41. public void setThickness(double thickness)
  42. {this.thickness = thickness;}
  43.  
  44. public double getThickness()
  45. {return thickness;}
  46.  
  47. public void setWidth(double width)
  48. {this.width = width;}
  49.  
  50. public double getWidth()
  51. {return width;}
  52.  
  53. public void setColor(String color)
  54. {this.color = color;}
  55.  
  56. public String getColor()
  57. {return color;}
  58.  
  59. public void setPlys(double plys)
  60. {this.plys = plys;}
  61.  
  62. public double getPlys()
  63. {return plys;}
  64.  
  65. public String toString()
  66. {
  67. return modelNo + " " + quantity + " " + thickness + " " + width +
  68. " " + color + " " + plys;
  69. }
  70.  
  71. public boolean equals(Object o)
  72. {
  73. if (!(o instanceof TreeData))
  74. throw new IllegalArgumentException("Wrong type: expects TreeData");
  75.  
  76. TreeData item = (TreeData)o;
  77. if (item.modelNo == modelNo &&
  78. item.thickness == thickness &&
  79. item.width == width &&
  80. item.color.equals(color) &&
  81. item.plys == plys)
  82. return true;
  83. else
  84. return false;
  85. }
  86. }

Here is OrderList
  1. /**
  2.  
  3.  
  4. import java.io.*;
  5. import java.util.*;
  6. import javax.swing.*;
  7. import java.awt.*;
  8.  
  9. public class OrderList extends Vector<TreeData>
  10. {
  11.   public static final int SORT_BY_MODEL_NO = 0;
  12.   public static final int SORT_BY_THICKNESS = 1;
  13.   public static final int SORT_BY_WIDTH = 2;
  14.   public static final int SORT_BY_COLOR = 3;
  15.  
  16.   // Constructor
  17.   public OrderList()
  18.   {
  19.   super(); // accomodate 5 items, extend by 5 when overflow
  20.   }
  21.  
  22.   /**
  23.   * Constructor with stuff
  24.   * @param file
  25.   */
  26. public OrderList(File inFile, Component c)
  27. {
  28. try
  29. {
  30. Scanner in = new Scanner(inFile);
  31. while (in.hasNextLine())
  32. {
  33. Scanner lineScanner = new Scanner(in.nextLine());
  34. lineScanner.useDelimiter("\t");
  35. try {
  36. String model = lineScanner.next();
  37. int quantity = lineScanner.nextInt();
  38. double thickness = lineScanner.nextDouble();
  39. double width = lineScanner.nextDouble();
  40. String color = lineScanner.next();
  41. double plys = lineScanner.nextDouble();
  42.  
  43. add(new TreeData(model, quantity, thickness, width, color, plys));
  44. }
  45. catch (NoSuchElementException e) { continue; }
  46. }
  47. }
  48. catch (IOException e)
  49. { JOptionPane.showMessageDialog(c, "Error Opening File",
  50. "Error", JOptionPane.ERROR_MESSAGE ); }
  51. }
  52.  
  53. //add item to order list; if item of same specs exists, add to quantity
  54. public boolean add(TreeData item)
  55. {
  56. for (int i = 0; i < size(); i++) {
  57. TreeData temp = get(i);
  58. if (temp.equals(item)) {
  59. temp.setQuantity(temp.getQuantity() + item.getQuantity());
  60. return true;
  61. }
  62. }
  63. return super.add(item);
  64. }
  65.  
  66. /**
  67.   *
  68.   * @param sortType
  69.   */
  70. public void sort(int sortType)
  71. {
  72. quicksort(0, size()-1, sortType);
  73. }
  74.  
  75. /**
  76.   * Sort an array using Quicksort algorithm. Adapted from method in C at
  77.   * http://www.tcnj.edu/~mmmartin/CMSC410/qs-complexity.doc
  78.   * @param a An array of Comparable items.
  79.   * @param left Start point of range.
  80.   * @param right End point of range.
  81.   */
  82. private void quicksort(int left, int right, int sortType)
  83. {
  84. if (left >= right) return;
  85. else
  86. {
  87. int l = left;
  88. int r = right;
  89. TreeData pivot = get((l+r)/2); // choose pivot as middle item
  90. // partition
  91. do
  92. {
  93. while (compare(get(l),pivot,sortType) < 0) l++;
  94. while (compare(get(r),pivot,sortType) > 0) r--;
  95. if (l <= r)
  96. {
  97. // swap a[l] and a[r]
  98. TreeData tmp = get(l);
  99. set(l, get(r));
  100. set(r, tmp);
  101. // move on
  102. l++;
  103. r--;
  104. }
  105. }
  106. while (l <= r);
  107.  
  108. // recursively sort left and right partitions
  109. quicksort(left, r, sortType);
  110. quicksort(l, right, sortType);
  111. }
  112. }
  113.  
  114. /**
  115.   * @param a First object of field type to compare
  116.   * @param b Second object of field type to compare
  117.   * @param sortType Identifier for field type
  118.   */
  119. private int compare(TreeData a, TreeData b, int sortType)
  120. {
  121. String s1, s2;
  122. double d1, d2;
  123.  
  124. // Sorts by model number or color (string type)
  125. switch (sortType)
  126. {
  127. case SORT_BY_MODEL_NO:
  128. s1 = a.getModelNo();
  129. s2 = b.getModelNo();
  130. // If s1 is <= s2, returns true
  131. return s1.compareTo(s2);
  132. case SORT_BY_COLOR:
  133. s1 = a.getColor();
  134. s2 = b.getColor();
  135. // If s1 is <= s2, returns true
  136. return s1.compareTo(s2);
  137. case SORT_BY_THICKNESS:
  138. // Class compare
  139. d1 = a.getThickness();
  140. d2 = b.getThickness();
  141. return Double.compare(d1,d2);
  142. case SORT_BY_WIDTH:
  143. // Class compare
  144. d1 = a.getWidth();
  145. d2 = b.getWidth();
  146. return Double.compare(d1,d2);
  147. }
  148. return 0;
  149. }
  150.  
  151. /**
  152.   *
  153.   */
  154. public String toString()
  155. {
  156. String listOutput = "";
  157. for (int i = 0; i < size(); i++)
  158. {
  159. TreeData item = get(i);
  160. if (item == null) break;
  161. listOutput += item.toString() + "\r\n";
  162. }
  163. return listOutput;
  164. }
  165.  
  166. /**
  167.   *
  168.   */
  169. public static void main(String[] args)
  170. {
  171. OrderList list = new OrderList();
  172. list.sort(SORT_BY_WIDTH);
  173. for (int i = 0; i<5; i++)
  174. System.out.println(list.get(i).toString());
  175. }
  176. }

And Finally Models here
  1. public class Model
  2. {
  3. private String modelNo;
  4. private double height;
  5. private int tipNum;
  6. private int group;
  7.  
  8. // Blank constructor
  9. public Model()
  10. {}
  11.  
  12. // Enter values for model information
  13. public Model(String modelNo, double height, int tipNum, int group)
  14. {
  15. setModelNo(modelNo);
  16. setHeight(height);
  17. setTipNum(tipNum);
  18. setGroup(group);
  19. }
  20.  
  21. // Model number methods
  22. public void setModelNo(String modelNo)
  23. {this.modelNo = modelNo;}
  24.  
  25. public String getModelNo()
  26. {return modelNo;}
  27.  
  28. // Height methods
  29. public void setHeight(double height)
  30. {this.height = height;}
  31.  
  32. public double getHeight()
  33. {return height;}
  34.  
  35. // tipNum methods
  36. public void setTipNum(int tipNum)
  37. {this.tipNum = tipNum;}
  38.  
  39. public int getTipNum()
  40. {return tipNum;}
  41.  
  42. // group methods
  43. public void setGroup(int group)
  44. {this.group = group;}
  45.  
  46. public int getGroup()
  47. {return group;}
  48. }

It's probably a really basic problem but i can't seem to pinpoint it. Thanks in advance guys.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 762
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is online now Online
Master Poster

Re: Hey check out my prog. It has an error can u find it?

 
0
  #2
Mar 22nd, 2006
It'd help if you described to us whats not working. Whats the intended affect? whats it doing instead? Can you just not read the file back in, or is it that you cant get it to display?
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 13
Reputation: buggytoast is an unknown quantity at this point 
Solved Threads: 0
buggytoast buggytoast is offline Offline
Newbie Poster

Re: Hey check out my prog. It has an error can u find it?

 
0
  #3
Mar 22nd, 2006
Sincerest apologies. What happens is that I save the file no problem however when I move to panel 2 to load the file, it fails to display. Hope that covers everything. PEace.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 9
Reputation: Nilesh Pagar is an unknown quantity at this point 
Solved Threads: 1
Nilesh Pagar Nilesh Pagar is offline Offline
Newbie Poster

Re: Hey check out my prog. It has an error can u find it?

 
0
  #4
Mar 23rd, 2006
Not bigger mistake..... yaar ....

For the Scanner object,
inside OrderList(File inFile, Component c) constructor ... you are setting Delimiter to "\t" just remove that line or comment that line.... and you will be aware from your nightmare..... thats it... do it fast.........


just check out the definition of :

public Scanner useDelimiter(Pattern pattern)
Sets this scanner's delimiting pattern to the specified pattern.

Parameters :
pattern - A delimiting pattern
Returns:
this scanner


since that pattern is not matching so its not reading properly but actually it can read first value then goes to nextline wherin it does the same..... so by commenting that line your program will definitely work.

ALTERNATIVE:

check out toString() implementation of TreeData there u are not specifying the space value equals to "\t" and same no. of spaces are being written to file ... therefore while reading from a file its not doing well......


Nilesh.................
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 13
Reputation: buggytoast is an unknown quantity at this point 
Solved Threads: 0
buggytoast buggytoast is offline Offline
Newbie Poster

Re: Hey check out my prog. It has an error can u find it?

 
0
  #5
Mar 31st, 2006
Hey thanks everyone...It works now .

BTW as a side note, my teacher doesn't want me to use "vectors" cuz it shows implementation and not "mastery". I just go get the source code and change the variables right?
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC