Advertisement
cynthiarez

FINISHED (FILEREADER/PRINTWRITER) student, class prog.

Sep 23rd, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.72 KB | None | 0 0
  1. //PARTIALLY DONE CODE FOR FINAL PROJECT. JUST NEED TO MAKE IT LOOK GOOD, COMPLETE ALL ITEMS, ADD TRY CATCH, ETC
  2. package HomePrac;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.PrintWriter;
  6. import java.util.*;
  7. import javax.swing.JOptionPane;
  8. public class FilePrinter {
  9.  
  10. public static void main(String[] args) throws FileNotFoundException {
  11.  
  12. PrintWriter outfile = new PrintWriter ("total.txt");
  13.  
  14. JOptionPane.showMessageDialog(null, "Welcome to the tech store!", "Tech Store", 1);
  15. String fitems = "", str = "" , fitem="";
  16. int exit = 0;
  17. double total = 0;
  18. double totalall = 0;
  19.  
  20. do
  21. {
  22.  
  23.  
  24. String items [] = {"Monitors", "Processors", "Keyboards", "Mice", "Laptops"};
  25. String monimodel, procemodel, kbmodel, micemodel, lapmodel;
  26.  
  27. // Models:
  28. String[] monitorsm = {"BenQ EX3501R", "Alienware AW3418DW", "Asus ROG Swift PG27UQ"};
  29. String[] processorsm = {"AMD Ryzen Threadripper 1950X" , "AMD Ryzen 5 1600X" , "Intel Core i5-7600K"};
  30. String[] keyboardsm = {"Corsair K70 RGB Mk.2", "Das Keyboard 4 Professional", "Razer BlackWidow Elite"};
  31. String[] micem = {"Logitech G502 Proteus Spectrum", "Logitech G300s", "Razer Atheris"};
  32. String[] laptopsm = {"Dell XPS 13", "Lenovo Yoga C930", "MSI GS65 Stealth"};
  33.  
  34. String item = JOptionPane.showInputDialog("Please select the number of the item you are looking for. Here are our available ones:" +"\n" + "\n (1)Monitors" + "\n (2)Processors" + "\n (3)Keyboards" + "\n (4)Mice" + "\n (5)Laptops");
  35. // Monitors
  36. switch (item) {
  37.  
  38. case "Monitor":
  39. case "Monitors":
  40. case "1":
  41. JOptionPane.showMessageDialog(null, "You have chosen " +items[0]);
  42. monimodel = JOptionPane.showInputDialog("Please select the number of the model you want. These are our current available ones:" + "\n" +"(1)"+monitorsm[0] + "\n" + "(2)"+monitorsm[1] + "\n" + "(3)"+monitorsm[2]);
  43.  
  44. if (monimodel.equalsIgnoreCase("1"))
  45. {
  46. double m1price = 45000;
  47.  
  48. JOptionPane.showMessageDialog(null, "You have chosen " + monitorsm[0] + ". the price for this is " + m1price );
  49.  
  50. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  51. total = totalprice (m1price,qty);
  52.  
  53. totalall = totalall + total;
  54.  
  55. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +monitorsm[0]+ ". The price is " + total);
  56.  
  57. outfile.println(monitorsm[0]+":");
  58.  
  59. }
  60.  
  61. else if (monimodel.equalsIgnoreCase("2"))
  62. {
  63. double m2price = 50000;
  64.  
  65. JOptionPane.showMessageDialog(null, "You have chosen " + monitorsm[1] + ". the price for this is " + m2price );
  66.  
  67. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  68. total = totalprice (m2price,qty);
  69.  
  70. totalall = totalall + total;
  71.  
  72. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +monitorsm[1]+ ". The price is " + total);
  73. outfile.println(monitorsm[1]+":");
  74.  
  75. }
  76.  
  77. else if (monimodel.equalsIgnoreCase("3"))
  78. {
  79.  
  80. double m3price = 50000;
  81.  
  82. JOptionPane.showMessageDialog(null, "You have chosen " + monitorsm[2] + ". the price for this is " + m3price );
  83.  
  84. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  85. total = totalprice (m3price,qty);
  86. totalall = totalall + total;
  87.  
  88.  
  89. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +monitorsm[2]+ ". The price is " + total);
  90. outfile.println(monitorsm[2]+":");
  91.  
  92. }
  93. break;
  94.  
  95. case "processors":
  96. case "Processors":
  97. case "2":
  98.  
  99. JOptionPane.showMessageDialog(null, "You have chosen " +items[1]);
  100. procemodel = JOptionPane.showInputDialog("Please select the number of the model you want. These are our current available ones:" + "\n" +"(1)"+processorsm[0] + "\n" + "(2)"+processorsm[1] + "\n" + "(3)"+processorsm[2]);
  101.  
  102. if (procemodel.equalsIgnoreCase("1"))
  103. {
  104. double m1price = 30000;
  105.  
  106. JOptionPane.showMessageDialog(null, "You have chosen " + processorsm[0] + ". the price for this is " + m1price );
  107.  
  108. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  109. total = totalprice (m1price,qty);
  110.  
  111. totalall = totalall + total;
  112.  
  113. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +processorsm[0]+ ". The price is " + total);
  114.  
  115. outfile.println(processorsm[0]+":");
  116.  
  117. }
  118.  
  119. else if (procemodel.equalsIgnoreCase("2"))
  120. {
  121. double m2price = 25000;
  122.  
  123. JOptionPane.showMessageDialog(null, "You have chosen " + processorsm[1] + ". the price for this is " + m2price );
  124.  
  125. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  126. total = totalprice (m2price,qty);
  127.  
  128. totalall = totalall + total;
  129.  
  130. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +processorsm[1]+ ". The price is " + total);
  131. outfile.println(processorsm[1]+":");
  132.  
  133. }
  134.  
  135. else if (procemodel.equalsIgnoreCase("3"))
  136. {
  137.  
  138. double m3price = 18000;
  139.  
  140. JOptionPane.showMessageDialog(null, "You have chosen " + processorsm[2] + ". the price for this is " + m3price );
  141.  
  142. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  143. total = totalprice (m3price,qty);
  144. totalall = totalall + total;
  145.  
  146.  
  147. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +processorsm[2]+ ". The price is " + total);
  148. outfile.println(processorsm[2]+":");
  149.  
  150. }
  151. break;
  152.  
  153. case "Keyboard":
  154. case "keyboards":
  155. case "3":
  156.  
  157. JOptionPane.showMessageDialog(null, "You have chosen " +items[2]);
  158. kbmodel = JOptionPane.showInputDialog("Please select the number of the model you want. These are our current available ones:" + "\n" +"(1)"+keyboardsm[0] + "\n" + "(2)"+keyboardsm[1] + "\n" + "(3)"+keyboardsm[2]);
  159.  
  160. if (kbmodel.equalsIgnoreCase("1"))
  161. {
  162. double m1price = 8000;
  163.  
  164. JOptionPane.showMessageDialog(null, "You have chosen " + keyboardsm[0] + ". the price for this is " + m1price );
  165.  
  166. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  167. total = totalprice (m1price,qty);
  168.  
  169. totalall = totalall + total;
  170.  
  171. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +keyboardsm[0]+ ". The price is " + total);
  172.  
  173. outfile.println(keyboardsm[0]+":");
  174.  
  175. }
  176.  
  177. else if (kbmodel.equalsIgnoreCase("2"))
  178. {
  179. double m2price = 9500;
  180.  
  181. JOptionPane.showMessageDialog(null, "You have chosen " + keyboardsm[1] + ". the price for this is " + m2price );
  182.  
  183. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  184. total = totalprice (m2price,qty);
  185.  
  186. totalall = totalall + total;
  187.  
  188. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +keyboardsm[1]+ ". The price is " + total);
  189. outfile.println(keyboardsm[1]+":");
  190.  
  191. }
  192.  
  193. else if (kbmodel.equalsIgnoreCase("3"))
  194. {
  195.  
  196. double m3price = 12000;
  197.  
  198. JOptionPane.showMessageDialog(null, "You have chosen " + keyboardsm[2] + ". the price for this is " + m3price );
  199.  
  200. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  201. total = totalprice (m3price,qty);
  202. totalall = totalall + total;
  203.  
  204.  
  205. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +keyboardsm[2]+ ". The price is " + total);
  206. outfile.println(keyboardsm[2]+":");
  207.  
  208. }
  209. break;
  210.  
  211. case "Mice":
  212. case "Mouse":
  213. case "4":
  214.  
  215. JOptionPane.showMessageDialog(null, "You have chosen " +items[3]);
  216. procemodel = JOptionPane.showInputDialog("Please select the number of the model you want. These are our current available ones:" + "\n" +"(1)"+micem[0] + "\n" + "(2)"+micem[1] + "\n" + "(3)"+micem[2]);
  217.  
  218. if (procemodel.equalsIgnoreCase("1"))
  219. {
  220. double m1price = 7000;
  221.  
  222. JOptionPane.showMessageDialog(null, "You have chosen " + micem[0] + ". the price for this is " + m1price );
  223.  
  224. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  225. total = totalprice (m1price,qty);
  226.  
  227. totalall = totalall + total;
  228.  
  229. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +micem[0]+ ". The price is " + total);
  230.  
  231. outfile.println(micem[0]+":");
  232.  
  233. }
  234.  
  235. else if (procemodel.equalsIgnoreCase("2"))
  236. {
  237. double m2price = 3500;
  238.  
  239. JOptionPane.showMessageDialog(null, "You have chosen " + micem[1] + ". the price for this is " + m2price );
  240.  
  241. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  242. total = totalprice (m2price,qty);
  243.  
  244. totalall = totalall + total;
  245.  
  246. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +micem[1]+ ". The price is " + total);
  247. outfile.println(micem[1]+":");
  248.  
  249. }
  250.  
  251. else if (procemodel.equalsIgnoreCase("3"))
  252. {
  253.  
  254. double m3price = 6500;
  255.  
  256. JOptionPane.showMessageDialog(null, "You have chosen " + micem[2] + ". the price for this is " + m3price );
  257.  
  258. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  259. total = totalprice (m3price,qty);
  260. totalall = totalall + total;
  261.  
  262.  
  263. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +micem[2]+ ". The price is " + total);
  264. outfile.println(micem[2]+":");
  265.  
  266. }
  267. break;
  268.  
  269. case "Laptops":
  270. case "Laptop":
  271. case "5":
  272.  
  273. JOptionPane.showMessageDialog(null, "You have chosen " +items[4]);
  274. procemodel = JOptionPane.showInputDialog("Please select the number of the model you want. These are our current available ones:" + "\n" +"(1)"+laptopsm[0] + "\n" + "(2)"+laptopsm[1] + "\n" + "(3)"+laptopsm[2]);
  275.  
  276. if (procemodel.equalsIgnoreCase("1"))
  277. {
  278. double m1price = 80000;
  279.  
  280. JOptionPane.showMessageDialog(null, "You have chosen " + laptopsm[0] + ". the price for this is " + m1price );
  281.  
  282. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  283. total = totalprice (m1price,qty);
  284.  
  285. totalall = totalall + total;
  286.  
  287. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +laptopsm[0]+ ". The price is " + total);
  288.  
  289. outfile.println(laptopsm[0]+":");
  290.  
  291. }
  292.  
  293. else if (procemodel.equalsIgnoreCase("2"))
  294. {
  295. double m2price = 75000;
  296.  
  297. JOptionPane.showMessageDialog(null, "You have chosen " + laptopsm[1] + ". the price for this is " + m2price );
  298.  
  299. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  300. total = totalprice (m2price,qty);
  301.  
  302. totalall = totalall + total;
  303.  
  304. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +laptopsm[1]+ ". The price is " + total);
  305. outfile.println(laptopsm[1]+":");
  306.  
  307. }
  308.  
  309. else if (procemodel.equalsIgnoreCase("3"))
  310. {
  311.  
  312. double m3price = 90000;
  313.  
  314. JOptionPane.showMessageDialog(null, "You have chosen " + laptopsm[2] + ". the price for this is " + m3price );
  315.  
  316. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  317. total = totalprice (m3price,qty);
  318. totalall = totalall + total;
  319.  
  320.  
  321. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +laptopsm[2]+ ". The price is " + total);
  322. outfile.println(laptopsm[2]+":");
  323.  
  324. }
  325.  
  326. }
  327. }
  328.  
  329. while (JOptionPane.showConfirmDialog(null, "Do you want to shop more?")==0);
  330. outfile.close();
  331.  
  332. // Store items purchased:
  333. Scanner infile = new Scanner (new FileReader("total.txt"));
  334. infile.useDelimiter(":");
  335.  
  336. while (infile.hasNext()) {
  337.  
  338. fitems = infile.next();
  339. infile.nextLine();
  340. str = str + fitems + "\n";
  341.  
  342. }
  343.  
  344. JOptionPane.showMessageDialog(null, "The items you purchased are:\n" + str);
  345.  
  346. // UPDATE ITEM INCOMPLETE
  347.  
  348. int update = JOptionPane.showConfirmDialog(null, "Do you want to update your purchase?");
  349.  
  350. if (update==0)
  351. {
  352. String sitem = JOptionPane.showInputDialog(null, "Search for item name");
  353.  
  354.  
  355. Scanner ifile = new Scanner (new FileReader("total.txt"));
  356. ifile.useDelimiter(":");
  357. boolean found = false;
  358.  
  359. while (ifile.hasNext()) {
  360.  
  361. fitem = ifile.next();
  362.  
  363. ifile.nextLine();
  364.  
  365.  
  366. if (fitem.equalsIgnoreCase(sitem)){
  367. JOptionPane.showMessageDialog (null, "Item: " + fitem , "Item Found", 1);
  368. found = true;
  369. }
  370. ifile.close();
  371. if (!found){
  372. JOptionPane.showMessageDialog(null,"Item not found", "Error", 1);
  373. }
  374.  
  375. JOptionPane.showMessageDialog(null, "\nThank you for shopping with us! The total of your purchase is " + String.format("%.2f",totalall), "Checkout",1);
  376. }}
  377. }
  378. public static double totalprice(double x, double y) {
  379.  
  380. double z = x*y;
  381. return z;
  382. }
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement