Advertisement
Guest User

Java_blyat

a guest
Jun 6th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.42 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.io.*;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6. import java.util.ArrayList;
  7. import java.util.Random;
  8. import java.util.Scanner;
  9. import java.util.stream.Stream;
  10.  
  11. public class freestyler {
  12.     public static void main(String[] args) {
  13.         String[] auth = authorization.authorize();
  14.         if (auth[1].equals("true")) {
  15.             int action = 0;
  16.             if (!auth[0].equals("admin")) {
  17.                 Object[] options = { "Create New", "Show New", "Show confirmed", "Show shipped" };
  18.                 action = JOptionPane.showOptionDialog(null, "What you want to do with orders?", "Select",
  19.                         JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
  20.                         null, options, options[0]);
  21.             }
  22.             switch (auth[0]){  // Control Structure
  23.                 case "user1" :
  24.                     user user1 = new user("user1"); //Object reference
  25.                     if (action == 0) {
  26.                         user1.newOrder();
  27.                     } else {
  28.                      order.getOrders("user1", action);
  29.                     }
  30.                     break;
  31.                 case "user2" :
  32.                     user user2 = new user("user2");
  33.                     if (action == 0) {
  34.                         user2.newOrder();
  35.                     } else {
  36.                         order.getOrders("user2", action);
  37.                     }
  38.                     break;
  39.                 case "user3" :
  40.                     user user3 = new user("user3");
  41.                     if (action == 0) {
  42.                         user3.newOrder();
  43.                     } else {
  44.                         order.getOrders("user3", action);
  45.                     }
  46.                     break;
  47.                 default:
  48.                     admin admin = new admin ();
  49.                     Object[] tasks = {"Show New", "Approve", "Ship", "Last admin" };
  50.                     String last_user = System.getProperty("user.name");
  51.                     int todo = JOptionPane.showOptionDialog(null, "What you want to do with orders?", "Select",
  52.                             JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
  53.                             null, tasks, tasks[0]);
  54.                     switch (todo) {
  55.                         case 1:
  56.                             admin.approveOrder(last_user);
  57.                             break;
  58.                         case 2:
  59.                             admin.shipOrder(last_user);
  60.                             break;
  61.                         case 3:
  62.                             System.out.println("Last accessed by: " + admin.last_admin());
  63.                             break;
  64.                         default:
  65.                             order.getOrders("admin", todo + 1);
  66.                     }
  67.             }
  68.         } else {
  69.             System.exit(0);
  70.         }
  71.     }
  72. }
  73.  
  74. class admin {
  75.     private String last_access = "James";
  76.     static Scanner admin_input = new Scanner(System.in);
  77.     String last_admin() { // Accessor
  78.         return this.last_access;
  79.     }
  80.     void approveOrder(String a) { // Mutator
  81.         this.last_access = a;
  82.         System.out.printf("Enter number of order for approval: ");
  83.         String number = "#" + admin_input.next();
  84.         order.editOrders(number, 0);
  85.     }
  86.     void shipOrder(String a) {
  87.         this.last_access = a;
  88.         System.out.printf("Enter number of order for shipping: ");
  89.         String number = "#" + admin_input.next();
  90.         order.editOrders(number, 1);
  91.     }
  92. }
  93.  
  94. class user {
  95.     private String objName;
  96.     public user(String objN) {
  97.         this.objName = objN;
  98.     } // Constructor
  99.  
  100.     String[] selectItem() {
  101.         double price = 10 + Math.random() * 140; // Math 1
  102.         Object[] categories = { "Men", "Women", "Children" }; // Array
  103.         Object category = JOptionPane.showOptionDialog(null,
  104.                 "Select category", "Category",
  105.                 JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
  106.                 categories, categories[0]);
  107.         Object[] types = { "Top", "Pants", "Skirts", "Dress", "Others"};
  108.         Object type = JOptionPane.showOptionDialog(null,
  109.                 "Select type", "Type",
  110.                 JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
  111.                 types, types[0]);
  112.         Object[] colors = { "White", "Black", "Gray", "Blue", "Red", "Green", "Yellow"};
  113.         Object color = JOptionPane.showOptionDialog(null,
  114.                 "Select color", "Color",
  115.                 JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
  116.                 colors, colors[0]);
  117.         Object[] sizes = {"S", "M", "L", "XL"};
  118.         Object size = JOptionPane.showOptionDialog(null,
  119.                 "Select size", "Size",
  120.                 JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
  121.                 sizes, sizes[0]);
  122.         String[] item = {category.toString(), type.toString(), color.toString(), size.toString(), Double.toString(Math.round(price*100.000)/100.000)}; // String 1 + Math 2
  123.         return item;
  124.     }
  125.     ArrayList<String[]> order() {
  126.         ArrayList<String[]> item = new ArrayList<>();
  127.         item.add(selectItem());
  128.         while (true) {
  129.             if (JOptionPane.showConfirmDialog(null,
  130.                     "Order more items?", "More items", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
  131.                 item.add(selectItem());
  132.             } else {
  133.                 break;
  134.             }
  135.         }
  136.         return item;
  137.     }
  138.     ArrayList<String[]> final_order () {
  139.         System.out.println("Start your order");
  140.         System.out.println("You have ordered: ");
  141.         int i = 0;
  142.         ArrayList<String[]> toReturn = order();
  143.         for (String[] orderList: toReturn) {
  144.             i++;
  145.             System.out.printf("%d. ", i);
  146.             for (String orderItem : orderList) {
  147.                 System.out.printf("%s ", orderItem);
  148.             }
  149.             System.out.printf("\n");
  150.         }
  151.         if (i >= 10) {
  152.             System.out.println("Enjoy your 10% discount for bulk order");
  153.         }
  154.         return toReturn;
  155.     }
  156.     void payment() {
  157.         double total = 0;
  158.         int i = 0;
  159.         ArrayList<String[]> toProceed = final_order();
  160.         for (String[] orderList: toProceed) {
  161.             i++;
  162.             double price = Double.parseDouble(orderList[orderList.length-1]);
  163.             total += price;
  164.         }
  165.         if (i >= 10) {
  166.             total = total / 100.000 * 90.000;
  167.         }
  168.         System.out.println("Total price: " + total);
  169.         if (JOptionPane.showConfirmDialog(null,
  170.                 "Proceed with payment?", "Payment", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
  171.             System.out.println("Order sent to approval");
  172.             order.createNew(objName, toProceed, total);
  173.         } else {
  174.             System.out.println("Order cancelled");
  175.             System.exit(0);
  176.         }
  177.     }
  178.     void newOrder() {
  179.         payment();
  180.     }
  181. }
  182.  
  183.  
  184. class order {
  185.     static Random rand = new Random();
  186.     static StringBuilder strBuilder = new StringBuilder();
  187.     static Boolean createNew (String user, ArrayList<String[]> items, double price) {
  188.         int i = rand.nextInt(999999999) + 100000000;
  189.         float float_price = (float) price; //Typecasting
  190.         try (Writer writer = new BufferedWriter(new OutputStreamWriter(
  191.                 new FileOutputStream("database.txt", true), "utf-8"))) {
  192.             writer.write("#" + i + " " + user + " " + float_price + " 1" + " 0" + " 0" + System.lineSeparator());
  193.             for (String[] order: items) {
  194.                 writer.write("$ ");
  195.                 for (String curItem: order) {
  196.                     writer.write(curItem + " ");
  197.                 }
  198.                 writer.write(System.lineSeparator());
  199.             }
  200.         }catch(Exception ex) {
  201.             System.out.println("Something Wrong");
  202.             System.exit(0);
  203.         }
  204.         return true;
  205.     }
  206.     static Boolean getOrders(String user, Integer k) {
  207.         ArrayList<String> txt = new ArrayList<>();
  208.         Path path = Paths.get("database.txt");
  209.         try (Stream<String> lines = Files.lines(path)) {
  210.             lines.forEach(s -> txt.add(s));
  211.         }catch(IOException ex) {
  212.             System.out.println("Something Wrong");
  213.             System.exit(0);
  214.         }
  215.         switch (k) {
  216.             case 1:
  217.                 System.out.println("Your new orders: ");
  218.                 break;
  219.             case 2:
  220.                 System.out.println("Your confirmed orders: ");
  221.                 break;
  222.             case 3:
  223.                 System.out.println("Your shipped orders: ");
  224.                 break;
  225.         }
  226.         for (String curStr : txt) {
  227.             String firstChar = curStr.substring(0, 1);
  228.             if (firstChar.equals("#")) {
  229.                 String[] splited = curStr.split("\\s+");  // String 2
  230.                 if (splited[1].equals(user) || user.equals("admin")) {
  231.                     if (splited[k+2].equals("1")) {
  232.                         System.out.println(curStr);
  233.                     }
  234.                 }
  235.             }
  236.         }
  237.         return true;
  238.     }
  239.     static Boolean editOrders(String number, Integer k) {
  240.         ArrayList<String> txt = new ArrayList<>();
  241.         ArrayList<String> newtxt = new ArrayList<>();
  242.         String temp;
  243.         Path path = Paths.get("database.txt");
  244.         try (Stream<String> lines = Files.lines(path)) {
  245.             lines.forEach(s -> txt.add(s));
  246.         }catch(IOException ex) {
  247.             System.out.println("Something Wrong");
  248.             System.exit(0);
  249.         }
  250.         for (String curStr : txt) {
  251.             temp = curStr;
  252.             String firstChar = curStr.substring(0, 1);
  253.             if (firstChar.equals("#")) {
  254.                 String[] splited = curStr.split("\\s+");
  255.                 if (splited[0].equals(number)) {
  256.                     if (splited[k + 4].equals("0")) {
  257.                         splited[k + 4] = "1";
  258.                         splited[3] = "0";
  259.                         for (String updatedStr : splited) {
  260.                             strBuilder.append(updatedStr).append(" ");
  261.                         }
  262.                         String newUpdatedStr = strBuilder.toString();
  263.                         temp = newUpdatedStr;
  264.                     } else {
  265.                         System.out.println("Something Wrong");
  266.                         System.exit(0);
  267.                     }
  268.                 }
  269.             }
  270.             newtxt.add(temp);
  271.         }
  272.         try (Writer writer = new BufferedWriter(new OutputStreamWriter(
  273.                 new FileOutputStream("database.txt", false), "utf-8"))) {
  274.             for (String curStr : newtxt) {
  275.                 writer.write(curStr);
  276.                 writer.write(System.lineSeparator());
  277.             }
  278.         }catch(Exception ex) {
  279.             System.out.println("Something Wrong");
  280.             System.exit(0);
  281.         }
  282.         return true;
  283.     }
  284. }
  285.  
  286. class authorization {
  287.     static Scanner user_input = new Scanner(System.in);
  288.     static private String[] login = {"user1", "user2", "user3", "admin"};
  289.     static private String[] password = {"password1", "password2", "password3", "password"};
  290.     static String[] authorize (){
  291.         Boolean authorized = false;
  292.         System.out.printf("Please enter your username: ");
  293.         String uLogin = user_input.next();
  294.         System.out.printf("Please enter your password: ");
  295.         String uPassword = user_input.next();
  296.         for (int i = 0; i <= login.length-1; i++) {
  297.             if (uLogin.equals(login[i]) && uPassword.equals(password[i])) {
  298.                 authorized = true;
  299.             }
  300.         }
  301.         if (authorized) {
  302.             System.out.println("Authorization completed");
  303.         } else {
  304.             System.out.println("Authorization failed");
  305.             System.exit(0);
  306.         }
  307.         String[] toReturn = {uLogin, String.valueOf(authorized)};
  308.         return toReturn;
  309.     }
  310. }
  311.  
  312. /* README
  313.  
  314. Credentials:
  315.  
  316. admin:password
  317. user1:password1
  318. user2:password2
  319. user3:password3
  320.  
  321. Basic Logic:
  322.  
  323. 1. User authorized --> Order Created (User able to see list of new orders belongs to him)
  324. 2. Admin authorized --> Order Approved (User able to see list of confirmed orders belongs to him)
  325. 3. Admin authorized --> Order Shipped (User able to see list of shipped orders belongs to him)
  326.  
  327.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement