Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package HomePrac;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.*;
- import javax.swing.JOptionPane;
- public class FilePrinter {
- private static Scanner x;
- public static void main(String[] args) throws IOException {
- PrintWriter outfile = new PrintWriter ("total.txt");
- JOptionPane.showMessageDialog(null, "Welcome to the tech store!", "Tech Store", 1);
- String fitems = "", str = "" , gitem="", totalsaver = "", newitem = "", newitems = "" , newstr = "";
- double total = 0, fprice = 0, gprice = 0, newprice = 0;
- double totalall = 0, finaltotal = 0;
- int fqty = 0, gqty = 0, newqty = 0, howmany = 0;
- do
- {
- String items [] = {"Monitors", "Processors", "Keyboards", "Mice", "Laptops"};
- String monimodel, procemodel, kbmodel, micemodel, lapmodel;
- // Models:
- String[] monitorsm = {"BenQ EX3501R", "Alienware AW3418DW", "Asus ROG Swift PG27UQ"};
- String[] processorsm = {"AMD Ryzen Threadripper 1950X" , "AMD Ryzen 5 1600X" , "Intel Core i5-7600K"};
- String[] keyboardsm = {"Corsair K70 RGB Mk.2", "Das Keyboard 4 Professional", "Razer BlackWidow Elite"};
- String[] micem = {"Logitech G502 Proteus Spectrum", "Logitech G300s", "Razer Atheris"};
- String[] laptopsm = {"Dell XPS 13", "Lenovo Yoga C930", "MSI GS65 Stealth"};
- 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");
- // Monitors
- switch (item) {
- case "Monitor":
- case "Monitors":
- case "1":
- JOptionPane.showMessageDialog(null, "You have chosen " +items[0]);
- 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]);
- if (monimodel.equalsIgnoreCase("1"))
- {
- double m1price = 45000;
- JOptionPane.showMessageDialog(null, "You have chosen " + monitorsm[0] + ". The price for this is " + m1price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m1price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +monitorsm[0]+ ". The total price is " + total);
- outfile.println(monitorsm[0]+":"+total+":"+qty+":");
- }
- else if (monimodel.equalsIgnoreCase("2"))
- {
- double m2price = 50000;
- JOptionPane.showMessageDialog(null, "You have chosen " + monitorsm[1] + ". The price for this is " + m2price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m2price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +monitorsm[1]+ ". The total price is " + total);
- outfile.println(monitorsm[1]+":"+total+":"+qty+":");
- }
- else if (monimodel.equalsIgnoreCase("3"))
- {
- double m3price = 50000;
- JOptionPane.showMessageDialog(null, "You have chosen " + monitorsm[2] + ". The price for this is " + m3price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m3price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +monitorsm[2]+ ". The total price is " + total);
- outfile.println(monitorsm[2]+":"+total+":"+qty+":");
- }
- break;
- case "processors":
- case "Processors":
- case "2":
- JOptionPane.showMessageDialog(null, "You have chosen " +items[1]);
- 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]);
- if (procemodel.equalsIgnoreCase("1"))
- {
- double m1price = 30000;
- JOptionPane.showMessageDialog(null, "You have chosen " + processorsm[0] + ". The price for this is " + m1price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m1price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +processorsm[0]+ ". The total price is " + total);
- outfile.println(processorsm[0]+":"+total+":"+qty+":");
- }
- else if (procemodel.equalsIgnoreCase("2"))
- {
- double m2price = 25000;
- JOptionPane.showMessageDialog(null, "You have chosen " + processorsm[1] + ". The price for this is " + m2price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m2price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +processorsm[1]+ ". The total price is " + total);
- outfile.println(processorsm[1]+":"+total+":"+qty+":");
- }
- else if (procemodel.equalsIgnoreCase("3"))
- {
- double m3price = 18000;
- JOptionPane.showMessageDialog(null, "You have chosen " + processorsm[2] + ". The price for this is " + m3price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m3price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +processorsm[2]+ ". The total price is " + total);
- outfile.println(processorsm[2]+":"+total+":"+qty+":");
- }
- break;
- case "Keyboard":
- case "keyboards":
- case "3":
- JOptionPane.showMessageDialog(null, "You have chosen " +items[2]);
- 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]);
- if (kbmodel.equalsIgnoreCase("1"))
- {
- double m1price = 8000;
- JOptionPane.showMessageDialog(null, "You have chosen " + keyboardsm[0] + ". The price for this is " + m1price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m1price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +keyboardsm[0]+ ". The total price is " + total);
- outfile.println(keyboardsm[0]+":"+total+":"+qty+":");
- }
- else if (kbmodel.equalsIgnoreCase("2"))
- {
- double m2price = 9500;
- JOptionPane.showMessageDialog(null, "You have chosen " + keyboardsm[1] + ". The price for this is " + m2price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m2price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +keyboardsm[1]+ ". The total price is " + total);
- outfile.println(keyboardsm[1]+":"+total+":"+qty+":");
- }
- else if (kbmodel.equalsIgnoreCase("3"))
- {
- double m3price = 12000;
- JOptionPane.showMessageDialog(null, "You have chosen " + keyboardsm[2] + ". The price for this is " + m3price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m3price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +keyboardsm[2]+ ". The total price is " + total);
- outfile.println(keyboardsm[2]+":"+total+":"+qty+":");
- }
- break;
- case "Mice":
- case "Mouse":
- case "4":
- JOptionPane.showMessageDialog(null, "You have chosen " +items[3]);
- 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]);
- if (procemodel.equalsIgnoreCase("1"))
- {
- double m1price = 7000;
- JOptionPane.showMessageDialog(null, "You have chosen " + micem[0] + ". The price for this is " + m1price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m1price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +micem[0]+ ". The total price is " + total);
- outfile.println(micem[0]+":"+total+":"+qty+":");
- }
- else if (procemodel.equalsIgnoreCase("2"))
- {
- double m2price = 3500;
- JOptionPane.showMessageDialog(null, "You have chosen " + micem[1] + ". The price for this is " + m2price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m2price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +micem[1]+ ". The total price is " + total);
- outfile.println(micem[1]+":"+total+":"+qty+":");
- }
- else if (procemodel.equalsIgnoreCase("3"))
- {
- double m3price = 6500;
- JOptionPane.showMessageDialog(null, "You have chosen " + micem[2] + ". The price for this is " + m3price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m3price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +micem[2]+ ". The total price is " + total);
- outfile.println(micem[2]+":"+total+":"+qty+":");
- }
- break;
- case "Laptops":
- case "Laptop":
- case "5":
- JOptionPane.showMessageDialog(null, "You have chosen " +items[4]);
- 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]);
- if (procemodel.equalsIgnoreCase("1"))
- {
- double m1price = 80000;
- JOptionPane.showMessageDialog(null, "You have chosen " + laptopsm[0] + ". The price for this is " + m1price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m1price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +laptopsm[0]+ ". The total price is " + total);
- outfile.println(laptopsm[0]+":"+total+":"+qty+":");
- }
- else if (procemodel.equalsIgnoreCase("2"))
- {
- double m2price = 75000;
- JOptionPane.showMessageDialog(null, "You have chosen " + laptopsm[1] + ". The price for this is " + m2price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m2price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +laptopsm[1]+ ". The total price is " + total);
- outfile.println(laptopsm[1]+":"+total+":"+qty+":");
- }
- else if (procemodel.equalsIgnoreCase("3"))
- {
- double m3price = 90000;
- JOptionPane.showMessageDialog(null, "You have chosen " + laptopsm[2] + ". The price for this is " + m3price );
- int qty = Integer.parseInt(JOptionPane.showInputDialog("Please enter quantity"));
- total = totalprice (m3price,qty);
- JOptionPane.showMessageDialog(null, "You have purchased " + qty + " of the " +laptopsm[2]+ ". The total price is " + total);
- outfile.println(laptopsm[2]+":"+total+":"+qty+":");
- }
- }
- }
- while (JOptionPane.showConfirmDialog(null, "Do you want to shop more?")==0);
- outfile.close();
- // Store items purchased:
- Scanner infile = new Scanner (new FileReader("total.txt"));
- infile.useDelimiter(":");
- while (infile.hasNext()) {
- fitems = infile.next();
- fprice = infile.nextDouble();
- fqty = infile.nextInt();
- infile.nextLine();
- finaltotal = finaltotal + fprice;
- str = str + "Item: " + fitems + "\nQuantity: " + fqty + "\nTotal Price: "+ String.format("%.2f",fprice) + "\n\n";
- totalsaver = totalsaver + fitems +":" + fprice +":"+ fqty +":"+"\n";
- }
- infile.close();
- JOptionPane.showMessageDialog(null, "The items you purchased are:\n\n" + str);
- // UPDATE ITEM INCOMPLETE
- int update = JOptionPane.showConfirmDialog(null, "Do you want to update items from your purchase?");
- if (update==0)
- {
- String sitem = JOptionPane.showInputDialog(null, "Search for item name");
- Scanner ifile = new Scanner (new FileReader("total.txt"));
- ifile.useDelimiter(":");
- boolean found = false;
- while (ifile.hasNext() && !found) {
- gitem = ifile.next();
- gprice = ifile.nextDouble();
- gqty = ifile.nextInt();
- ifile.nextLine();
- if (gitem.equalsIgnoreCase(sitem)) {
- JOptionPane.showMessageDialog (null, "Item: " + gitem + "\nPrice: " + gprice + "\nQuantity: " + gqty, "Item Found", 1);
- found = true;
- 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));
- switch (confirm) { // ADD ITEM
- case 1:
- PrintWriter addfile = new PrintWriter ("total.txt");
- howmany = Integer.parseInt(JOptionPane.showInputDialog(null, "How many more of the " + gitem + " would you like to add?"));
- JOptionPane.showMessageDialog(null, "Item has been successfully updated! Added " + howmany + " more of the " + gitem );
- gprice = gprice/gqty;
- gprice = gprice*howmany;
- totalall = gprice;
- newitem = gitem +":" + gprice +":" + howmany +":";
- addfile.println(totalsaver + newitem);
- addfile.close();
- Scanner newlist = new Scanner (new FileReader("total.txt"));
- newlist.useDelimiter(":");
- while (newlist.hasNext()) {
- newitems = newlist.next();
- newprice = newlist.nextDouble();
- newqty = newlist.nextInt();
- newlist.nextLine();
- newstr = newstr + "Item: " + newitems + "\nQuantity: " + newqty + "\nTotal Price: "+ String.format("%.2f",newprice) + "\n\n";
- }
- newlist.close();
- JOptionPane.showMessageDialog(null, "Your updated purchase list:\n\n" + newstr);
- break;
- case 2: // DELETE ITEM
- String filepath = "total.txt";
- String removeItem = gitem +":" + gprice +":" + howmany +":";;
- deleteItem (filepath,removeItem);
- JOptionPane.showMessageDialog(null, "Your updated purchase list:\n\n" + newstr);
- }
- }
- }
- ifile.close();
- if (!found){
- JOptionPane.showMessageDialog(null,"Item not found", "Error", 1);
- }
- }
- JOptionPane.showMessageDialog(null, "\nThank you for shopping with us! The total of your purchase is " + String.format("%.2f",finaltotal+totalall), "Checkout",1);
- }
- public static double totalprice(double x, double y) {
- double z = x*y;
- return z;
- }
- public static void deleteItem (String filepath, String removeItem) throws IOException {
- String tempFile = "temp.txt";
- File oldFile = new File (filepath);
- File newFile = new File (tempFile);
- String item = "";
- double price = 0;
- int qty = 0;
- try {
- FileWriter fw = new FileWriter(tempFile, true);
- BufferedWriter bw = new BufferedWriter(fw);
- PrintWriter pw = new PrintWriter(bw);
- x = new Scanner(new FileReader(filepath));
- x.useDelimiter ("[:]");
- while (x.hasNext()){
- {
- item = x.next();
- price = x.nextDouble();
- qty = x.nextInt();
- x.nextLine();
- if (!item.equalsIgnoreCase(removeItem))
- {
- pw.println(item);
- }
- }
- x.close();
- pw.flush();
- pw.close();
- oldFile.delete();
- File dump = new File (filepath);
- newFile.renameTo(dump);
- }
- }
- catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Error");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement