Thread: File I/0
View Single Post
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

File I/0

 
0
  #1
Dec 2nd, 2008
This program is all working except when a user chooses the same file as before it needs to show both orders in the file but it is only showing the latest one.

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5.  
  6. /**
  7.  * Program that allows user to select a meal.
  8.  * Includes sides, drink, and main entree
  9.  * Writes users order to user specified file.
  10.  * @author Kimberlie
  11.  * @version 12/4/08
  12.  */
  13. public class MealBuilder {
  14.  
  15. /*
  16.   * Main
  17.   * Sets up frame
  18.   * @param args not used
  19.   */
  20. public static void main(String[] args)
  21. {
  22. EventQueue.invokeLater(
  23. new Runnable()
  24. {
  25. public void run()
  26. {
  27. MealBuilderFrame frame = new MealBuilderFrame();
  28. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29. frame.setVisible(true);
  30. }
  31. });
  32. }
  33.  
  34. }
  35.  
  36. /*
  37.  * MealBuilderFrame class
  38.  * extends JFrame
  39.  * Sets up frame, sets size and title
  40.  * Declares and adds panel
  41.  */
  42. class MealBuilderFrame extends JFrame
  43. {
  44. /*
  45.   * MealBuilderFrame constructor
  46.   * @param none
  47.   */
  48. public MealBuilderFrame()
  49. {
  50. this.setTitle("Meal Builder");
  51. this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  52.  
  53. MealBuilderPanel panel = new MealBuilderPanel();
  54. this.add(panel);
  55. }
  56.  
  57. final static int DEFAULT_WIDTH = 550;
  58. final static int DEFAULT_HEIGHT = 400;
  59. }
  60.  
  61. /*
  62.  * MealBuilderPanel class
  63.  * extends JPanel
  64.  * Sets up panel with components
  65.  */
  66. class MealBuilderPanel extends JPanel
  67. {
  68. /*
  69.   * MealBuilderPanel constructor
  70.   * @param none
  71.   */
  72. public MealBuilderPanel()
  73. {
  74. this.setLayout(new BorderLayout());
  75.  
  76. //ButtonGroup for radio buttons
  77. ButtonGroup entree = new ButtonGroup();
  78. ButtonGroup drinkGroup = new ButtonGroup();
  79.  
  80. //North
  81. this.add(new JLabel("Server: Kimberlie Davis"), BorderLayout.NORTH);
  82.  
  83. //West
  84. this.add(new JLabel(" "), BorderLayout.WEST);
  85.  
  86. //Center
  87. JPanel centerPanel = new JPanel(new GridLayout(1,4));
  88. this.add(centerPanel, BorderLayout.CENTER);
  89.  
  90. //south
  91. JPanel southPanel = new JPanel();
  92. this.add(southPanel, BorderLayout.SOUTH);
  93. JButton submit = new JButton("Order");
  94. southPanel.add(submit);
  95.  
  96. //Box
  97. Box centerBox = Box.createVerticalBox();
  98. Box centerBox2 = Box.createVerticalBox();
  99. Box centerBox3 = Box.createVerticalBox();
  100.  
  101. //Adding everything to centerPanel
  102. centerPanel.add(centerBox, 1, 0);
  103. centerPanel.add(centerBox2);
  104. centerPanel.add(centerBox3);
  105.  
  106. mainEntreeBox = new JRadioButton[5];
  107. drinkBox = new JRadioButton[10];
  108. sideBox = new JCheckBox[6];
  109.  
  110. //Setting up choices
  111. String[] drink = {"Root beer", "Sprite", "Coffee", "Hot Chocolate", "Whie Chocolate", "Jarritos", "Dr. Pepper", "Shake", "Tea", "Coke"};
  112. String[] side = {"Salad", "Soup", "French Fries", "Potato salad", "Apple sauce", "Fruit"};
  113. String[] mainEntree = {"Hamburger", "Cheeseburger", "Hotdog", "Ham sandwhich", "Turkey sandwhich"};
  114.  
  115. JLabel chooseEntree = new JLabel("Choose your main entree: ");
  116. JLabel chooseSide = new JLabel("Choose your sides:");
  117. JLabel chooseDrink = new JLabel("Choose your drink:");
  118.  
  119. //Main Entree
  120. centerBox.add(chooseEntree);
  121. for(int i = 0; i < 5; i++)
  122. {
  123. mainEntreeBox[i] = new JRadioButton(mainEntree[i]);
  124. entree.add(mainEntreeBox[i]);
  125. centerBox.add(mainEntreeBox[i]);
  126. }
  127.  
  128. //Sides
  129. centerBox2.add(chooseSide);
  130. for(int i = 0; i < 6; i++)
  131. {
  132. sideBox[i] = new JCheckBox(side[i]);
  133. centerBox2.add(sideBox[i]);
  134. }
  135.  
  136. //Drink
  137. centerBox3.add(chooseDrink);
  138. for(int i = 0; i < 10; i++)
  139. {
  140. drinkBox[i]= new JRadioButton(drink[i]);
  141. centerBox3.add(drinkBox[i]);
  142. drinkGroup.add(drinkBox[i]);
  143. }
  144.  
  145. //Declaring and adding ActionListener
  146. SubmitAction action = new SubmitAction();
  147. submit.addActionListener(action);
  148. }
  149.  
  150. private class SubmitAction implements ActionListener
  151. {
  152. public void actionPerformed(ActionEvent event)
  153. {
  154. String fileName = new String();
  155. file = JOptionPane.showInputDialog(null, "Enter the file name to store your order to:");
  156.  
  157. try
  158. {
  159. //writeTo = new File(file + ".txt");
  160. if(fileChosen == false)
  161. {
  162. writer = new BufferedWriter(new FileWriter(file + ".txt"));
  163. fileName = file;
  164. fileChosen = true;
  165. }
  166.  
  167. else
  168. {
  169. if(!file.equals(fileName))
  170. {
  171. writer = new BufferedWriter(new FileWriter(file + ".txt"));
  172. fileName = file;
  173. }
  174.  
  175. }
  176.  
  177. int i, a, b;
  178.  
  179. writer.write("Entree chosen:");
  180. for(i = 0; i < 5; i++)
  181. {
  182. //Write entree chosen to file
  183. if(mainEntreeBox[i].isSelected())
  184. {
  185. writer.write(mainEntreeBox[i].getText());
  186. writer.newLine();
  187. }
  188. }
  189.  
  190. //write drink chosen to file
  191. writer.write("Drink chosen:");
  192. for(a = 0; a < 10; a++)
  193. {
  194. if(drinkBox[a].isSelected())
  195. {
  196. writer.write(drinkBox[a].getText());
  197. writer.newLine();
  198. }
  199. }
  200.  
  201. //write sides chosen to file
  202. writer.write("Side(s) chosen:");
  203. writer.newLine();
  204. for(b = 0; b < 6; b++)
  205. {
  206. if(sideBox[b].isSelected())
  207. {
  208. writer.write(sideBox[b].getText());
  209. writer.newLine();
  210. }
  211. }
  212. writer.close();
  213. }
  214.  
  215. catch(IOException e)
  216. {
  217. JOptionPane.showMessageDialog(null, "Error: unable to process order.\nPlease try again");
  218. }
  219. }
  220. }
  221. //datafields
  222. JRadioButton[] mainEntreeBox;
  223. JRadioButton[] drinkBox;
  224. JCheckBox[] sideBox;
  225.  
  226. String file;
  227. File writeTo;
  228. BufferedWriter writer;
  229.  
  230. boolean fileChosen = false;
  231. }
Reply With Quote