Advertisement
cynthiarez

Final Project (Delete part has bugs)

Oct 9th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.07 KB | None | 0 0
  1. package HomePrac;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.util.*;
  10. import javax.swing.JOptionPane;
  11. public class FilePrinter {
  12.  
  13. private static Scanner x;
  14.  
  15. public static void main(String[] args) throws IOException {
  16.  
  17. PrintWriter outfile = new PrintWriter ("total.txt");
  18.  
  19. JOptionPane.showMessageDialog(null, "Welcome to the tech store!", "Tech Store", 1);
  20. String fitems = "", str = "" , gitem="", totalsaver = "", newitem = "", newitems = "" , newstr = "";
  21. double total = 0, fprice = 0, gprice = 0, newprice = 0;
  22. double totalall = 0, finaltotal = 0;
  23. int fqty = 0, gqty = 0, newqty = 0, howmany = 0;
  24.  
  25. do
  26. {
  27.  
  28.  
  29. String items [] = {"Monitors", "Processors", "Keyboards", "Mice", "Laptops"};
  30. String monimodel, procemodel, kbmodel, micemodel, lapmodel;
  31.  
  32. // Models:
  33. String[] monitorsm = {"BenQ EX3501R", "Alienware AW3418DW", "Asus ROG Swift PG27UQ"};
  34. String[] processorsm = {"AMD Ryzen Threadripper 1950X" , "AMD Ryzen 5 1600X" , "Intel Core i5-7600K"};
  35. String[] keyboardsm = {"Corsair K70 RGB Mk.2", "Das Keyboard 4 Professional", "Razer BlackWidow Elite"};
  36. String[] micem = {"Logitech G502 Proteus Spectrum", "Logitech G300s", "Razer Atheris"};
  37. String[] laptopsm = {"Dell XPS 13", "Lenovo Yoga C930", "MSI GS65 Stealth"};
  38.  
  39. 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");
  40. // Monitors
  41. switch (item) {
  42.  
  43. case "Monitor":
  44. case "Monitors":
  45. case "1":
  46. JOptionPane.showMessageDialog(null, "You have chosen " +items[0]);
  47. 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]);
  48.  
  49. if (monimodel.equalsIgnoreCase("1"))
  50. {
  51. double m1price = 45000;
  52.  
  53. JOptionPane.showMessageDialog(null, "You have chosen " + monitorsm[0] + ". The price for this is " + m1price );
  54.  
  55. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  56. total = totalprice (m1price,qty);
  57.  
  58.  
  59. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +monitorsm[0]+ ". The total price is " + total);
  60.  
  61. outfile.println(monitorsm[0]+":"+total+":"+qty+":");
  62.  
  63.  
  64. }
  65.  
  66. else if (monimodel.equalsIgnoreCase("2"))
  67. {
  68. double m2price = 50000;
  69.  
  70. JOptionPane.showMessageDialog(null, "You have chosen " + monitorsm[1] + ". The price for this is " + m2price );
  71.  
  72. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  73. total = totalprice (m2price,qty);
  74.  
  75.  
  76. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +monitorsm[1]+ ". The total price is " + total);
  77. outfile.println(monitorsm[1]+":"+total+":"+qty+":");
  78.  
  79.  
  80. }
  81.  
  82. else if (monimodel.equalsIgnoreCase("3"))
  83. {
  84.  
  85. double m3price = 50000;
  86.  
  87. JOptionPane.showMessageDialog(null, "You have chosen " + monitorsm[2] + ". The price for this is " + m3price );
  88.  
  89. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  90. total = totalprice (m3price,qty);
  91.  
  92.  
  93. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +monitorsm[2]+ ". The total price is " + total);
  94. outfile.println(monitorsm[2]+":"+total+":"+qty+":");
  95.  
  96. }
  97. break;
  98.  
  99. case "processors":
  100. case "Processors":
  101. case "2":
  102.  
  103. JOptionPane.showMessageDialog(null, "You have chosen " +items[1]);
  104. 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]);
  105.  
  106. if (procemodel.equalsIgnoreCase("1"))
  107. {
  108. double m1price = 30000;
  109.  
  110. JOptionPane.showMessageDialog(null, "You have chosen " + processorsm[0] + ". The price for this is " + m1price );
  111.  
  112. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  113. total = totalprice (m1price,qty);
  114.  
  115. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +processorsm[0]+ ". The total price is " + total);
  116.  
  117. outfile.println(processorsm[0]+":"+total+":"+qty+":");
  118.  
  119. }
  120.  
  121. else if (procemodel.equalsIgnoreCase("2"))
  122. {
  123. double m2price = 25000;
  124.  
  125. JOptionPane.showMessageDialog(null, "You have chosen " + processorsm[1] + ". The price for this is " + m2price );
  126.  
  127. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  128. total = totalprice (m2price,qty);
  129.  
  130. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +processorsm[1]+ ". The total price is " + total);
  131. outfile.println(processorsm[1]+":"+total+":"+qty+":");
  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.  
  145.  
  146. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +processorsm[2]+ ". The total price is " + total);
  147. outfile.println(processorsm[2]+":"+total+":"+qty+":");
  148.  
  149. }
  150. break;
  151.  
  152. case "Keyboard":
  153. case "keyboards":
  154. case "3":
  155.  
  156. JOptionPane.showMessageDialog(null, "You have chosen " +items[2]);
  157. 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]);
  158.  
  159. if (kbmodel.equalsIgnoreCase("1"))
  160. {
  161. double m1price = 8000;
  162.  
  163. JOptionPane.showMessageDialog(null, "You have chosen " + keyboardsm[0] + ". The price for this is " + m1price );
  164.  
  165. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  166. total = totalprice (m1price,qty);
  167.  
  168. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +keyboardsm[0]+ ". The total price is " + total);
  169.  
  170. outfile.println(keyboardsm[0]+":"+total+":"+qty+":");
  171.  
  172. }
  173.  
  174. else if (kbmodel.equalsIgnoreCase("2"))
  175. {
  176. double m2price = 9500;
  177.  
  178. JOptionPane.showMessageDialog(null, "You have chosen " + keyboardsm[1] + ". The price for this is " + m2price );
  179.  
  180. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  181. total = totalprice (m2price,qty);
  182.  
  183.  
  184. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +keyboardsm[1]+ ". The total price is " + total);
  185. outfile.println(keyboardsm[1]+":"+total+":"+qty+":");
  186.  
  187. }
  188.  
  189. else if (kbmodel.equalsIgnoreCase("3"))
  190. {
  191.  
  192. double m3price = 12000;
  193.  
  194. JOptionPane.showMessageDialog(null, "You have chosen " + keyboardsm[2] + ". The price for this is " + m3price );
  195.  
  196. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  197. total = totalprice (m3price,qty);
  198.  
  199. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +keyboardsm[2]+ ". The total price is " + total);
  200. outfile.println(keyboardsm[2]+":"+total+":"+qty+":");
  201.  
  202. }
  203. break;
  204.  
  205. case "Mice":
  206. case "Mouse":
  207. case "4":
  208.  
  209. JOptionPane.showMessageDialog(null, "You have chosen " +items[3]);
  210. 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]);
  211.  
  212. if (procemodel.equalsIgnoreCase("1"))
  213. {
  214. double m1price = 7000;
  215.  
  216. JOptionPane.showMessageDialog(null, "You have chosen " + micem[0] + ". The price for this is " + m1price );
  217.  
  218. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  219. total = totalprice (m1price,qty);
  220.  
  221.  
  222. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +micem[0]+ ". The total price is " + total);
  223.  
  224. outfile.println(micem[0]+":"+total+":"+qty+":");
  225.  
  226. }
  227.  
  228. else if (procemodel.equalsIgnoreCase("2"))
  229. {
  230. double m2price = 3500;
  231.  
  232. JOptionPane.showMessageDialog(null, "You have chosen " + micem[1] + ". The price for this is " + m2price );
  233.  
  234. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  235. total = totalprice (m2price,qty);
  236.  
  237.  
  238. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +micem[1]+ ". The total price is " + total);
  239. outfile.println(micem[1]+":"+total+":"+qty+":");
  240.  
  241. }
  242.  
  243. else if (procemodel.equalsIgnoreCase("3"))
  244. {
  245.  
  246. double m3price = 6500;
  247.  
  248. JOptionPane.showMessageDialog(null, "You have chosen " + micem[2] + ". The price for this is " + m3price );
  249.  
  250. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  251. total = totalprice (m3price,qty);
  252.  
  253.  
  254.  
  255. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +micem[2]+ ". The total price is " + total);
  256. outfile.println(micem[2]+":"+total+":"+qty+":");
  257.  
  258. }
  259. break;
  260.  
  261. case "Laptops":
  262. case "Laptop":
  263. case "5":
  264.  
  265. JOptionPane.showMessageDialog(null, "You have chosen " +items[4]);
  266. 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]);
  267.  
  268. if (procemodel.equalsIgnoreCase("1"))
  269. {
  270. double m1price = 80000;
  271.  
  272. JOptionPane.showMessageDialog(null, "You have chosen " + laptopsm[0] + ". The price for this is " + m1price );
  273.  
  274. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  275. total = totalprice (m1price,qty);
  276.  
  277. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +laptopsm[0]+ ". The total price is " + total);
  278.  
  279. outfile.println(laptopsm[0]+":"+total+":"+qty+":");
  280.  
  281. }
  282.  
  283. else if (procemodel.equalsIgnoreCase("2"))
  284. {
  285. double m2price = 75000;
  286.  
  287. JOptionPane.showMessageDialog(null, "You have chosen " + laptopsm[1] + ". The price for this is " + m2price );
  288.  
  289. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  290. total = totalprice (m2price,qty);
  291.  
  292.  
  293.  
  294. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +laptopsm[1]+ ". The total price is " + total);
  295. outfile.println(laptopsm[1]+":"+total+":"+qty+":");
  296.  
  297. }
  298.  
  299. else if (procemodel.equalsIgnoreCase("3"))
  300. {
  301.  
  302. double m3price = 90000;
  303.  
  304. JOptionPane.showMessageDialog(null, "You have chosen " + laptopsm[2] + ". The price for this is " + m3price );
  305.  
  306. int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
  307. total = totalprice (m3price,qty);
  308.  
  309.  
  310.  
  311. JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +laptopsm[2]+ ". The total price is " + total);
  312. outfile.println(laptopsm[2]+":"+total+":"+qty+":");
  313.  
  314. }
  315.  
  316. }
  317. }
  318.  
  319.  
  320.  
  321.  
  322. while (JOptionPane.showConfirmDialog(null, "Do you want to shop more?")==0);
  323. outfile.close();
  324.  
  325. // Store items purchased:
  326. Scanner infile = new Scanner (new FileReader("total.txt"));
  327. infile.useDelimiter(":");
  328.  
  329. while (infile.hasNext()) {
  330.  
  331. fitems = infile.next();
  332. fprice = infile.nextDouble();
  333. fqty = infile.nextInt();
  334.  
  335. infile.nextLine();
  336.  
  337. finaltotal = finaltotal + fprice;
  338. str = str + "Item: " + fitems + "\nQuantity: " + fqty + "\nTotal Price: "+ String.format("%.2f",fprice) + "\n\n";
  339.  
  340. totalsaver = totalsaver + fitems +":" + fprice +":"+ fqty +":"+"\n";
  341.  
  342. }
  343.  
  344. infile.close();
  345.  
  346. JOptionPane.showMessageDialog(null, "The items you purchased are:\n\n" + str);
  347.  
  348. // UPDATE ITEM INCOMPLETE
  349.  
  350. int update = JOptionPane.showConfirmDialog(null, "Do you want to update items from your purchase?");
  351.  
  352. if (update==0)
  353. {
  354.  
  355. String sitem = JOptionPane.showInputDialog(null, "Search for item name");
  356.  
  357.  
  358. Scanner ifile = new Scanner (new FileReader("total.txt"));
  359. ifile.useDelimiter(":");
  360. boolean found = false;
  361.  
  362. while (ifile.hasNext() && !found) {
  363.  
  364. gitem = ifile.next();
  365. gprice = ifile.nextDouble();
  366. gqty = ifile.nextInt();
  367.  
  368. ifile.nextLine();
  369.  
  370.  
  371.  
  372. if (gitem.equalsIgnoreCase(sitem)) {
  373.  
  374. JOptionPane.showMessageDialog (null, "Item: " + gitem + "\nPrice: " + gprice + "\nQuantity: " + gqty, "Item Found", 1);
  375. found = true;
  376.  
  377. int confirm = Integer.parseInt(JOptionPane.showInputDialog(null,"What would you like to do with this item? \n\nPress 1 to add more of this item \nPress 2 to delete this item", "Update Item", 1));
  378.  
  379. switch (confirm) { // ADD ITEM
  380.  
  381. case 1:
  382.  
  383. PrintWriter addfile = new PrintWriter ("total.txt");
  384.  
  385. howmany = Integer.parseInt(JOptionPane.showInputDialog(null, "How many more of the " + gitem + " would you like to add?"));
  386. JOptionPane.showMessageDialog(null, "Item has been successfully updated! Added " + howmany + " more of the " + gitem );
  387.  
  388. gprice = gprice/gqty;
  389. gprice = gprice*howmany;
  390.  
  391. totalall = gprice;
  392.  
  393. newitem = gitem +":" + gprice +":" + howmany +":";
  394.  
  395. addfile.println(totalsaver + newitem);
  396. addfile.close();
  397.  
  398.  
  399.  
  400. Scanner newlist = new Scanner (new FileReader("total.txt"));
  401. newlist.useDelimiter(":");
  402.  
  403. while (newlist.hasNext()) {
  404.  
  405. newitems = newlist.next();
  406. newprice = newlist.nextDouble();
  407. newqty = newlist.nextInt();
  408.  
  409. newlist.nextLine();
  410.  
  411.  
  412. newstr = newstr + "Item: " + newitems + "\nQuantity: " + newqty + "\nTotal Price: "+ String.format("%.2f",newprice) + "\n\n";
  413.  
  414. }
  415. newlist.close();
  416.  
  417. JOptionPane.showMessageDialog(null, "Your updated purchase list:\n\n" + newstr);
  418.  
  419.  
  420. break;
  421.  
  422. case 2: // DELETE ITEM
  423.  
  424. String filepath = "total.txt";
  425. String removeItem = gitem +":" + gprice +":" + howmany +":";;
  426. deleteItem (filepath,removeItem);
  427.  
  428. JOptionPane.showMessageDialog(null, "Your updated purchase list:\n\n" + newstr);
  429.  
  430.  
  431.  
  432. }
  433.  
  434.  
  435. }
  436. }
  437. ifile.close();
  438. if (!found){
  439. JOptionPane.showMessageDialog(null,"Item not found", "Error", 1);
  440.  
  441. }
  442.  
  443.  
  444.  
  445. }
  446.  
  447. JOptionPane.showMessageDialog(null, "\nThank you for shopping with us! The total of your purchase is " + String.format("%.2f",finaltotal+totalall), "Checkout",1);
  448.  
  449. }
  450. public static double totalprice(double x, double y) {
  451.  
  452. double z = x*y;
  453. return z;
  454. }
  455.  
  456. public static void deleteItem (String filepath, String removeItem) throws IOException {
  457.  
  458. String tempFile = "temp.txt";
  459. File oldFile = new File (filepath);
  460. File newFile = new File (tempFile);
  461. String item = "";
  462. double price = 0;
  463. int qty = 0;
  464.  
  465. try {
  466. FileWriter fw = new FileWriter(tempFile, true);
  467. BufferedWriter bw = new BufferedWriter(fw);
  468. PrintWriter pw = new PrintWriter(bw);
  469.  
  470. x = new Scanner(new FileReader(filepath));
  471. x.useDelimiter ("[:]");
  472.  
  473. while (x.hasNext()){
  474. {
  475. item = x.next();
  476. price = x.nextDouble();
  477. qty = x.nextInt();
  478.  
  479. x.nextLine();
  480.  
  481. if (!item.equalsIgnoreCase(removeItem))
  482. {
  483. pw.println(item);
  484. }
  485.  
  486. }
  487. x.close();
  488. pw.flush();
  489. pw.close();
  490. oldFile.delete();
  491. File dump = new File (filepath);
  492. newFile.renameTo(dump);
  493.  
  494. }
  495. }
  496. catch (Exception e) {
  497. JOptionPane.showMessageDialog(null, "Error");
  498. }
  499.  
  500. }
  501.  
  502. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement