Advertisement
weiqian1020

core2

Jun 23rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 42.75 KB | None | 0 0
  1. package assignment;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. import javax.swing.JOptionPane;
  6.  
  7. public class core{
  8.     private static Scanner input;
  9.     private String account; //current logging account name
  10.     private ArrayList<String> username = new ArrayList<>();
  11.     private ArrayList<String> password = new ArrayList<>();
  12.     private ArrayList<String> userOrder = new ArrayList<>(); //username that order
  13.     private ArrayList<String> shipStatus = new ArrayList<>(); //current shipping status
  14.     private ArrayList<String> orderStatus = new ArrayList<>(); //current order status
  15.     private ArrayList<Top> top = new ArrayList<>(); //array for class Top, to makes iterating the order list easier
  16.     private ArrayList<Pant> pant = new ArrayList<>(); //array for class Pant, to makes iterating the order list easier
  17.     private ArrayList<Dress> dress = new ArrayList<>(); //array for class Dress, to makes iterating the order list easier
  18.     private ArrayList<Skirt> skirt = new ArrayList<>(); //array for class Skirt, to makes iterating the order list easier
  19.     private ArrayList<Integer> getUpdate = new ArrayList<>(); //array that in charge of notification
  20.     Top classTop = new Top(); //temporary class, so that it is able add into ArrayList
  21.     Pant classPant = new Pant(); //temporary class, so that it is able add into ArrayList
  22.     Dress classDress = new Dress(); //temporary class, so that it is able add into ArrayList
  23.     Skirt classSkirt = new Skirt(); //temporary class, so that it is able add into ArrayList
  24.     boolean r = false;
  25.    
  26.     public static void main(String[] args) {
  27.         int x = 0;
  28.         Math.floor(x);
  29.         core run = new core();
  30.         input = new Scanner(System.in);
  31.         do{
  32.             run.r = false;
  33.             switch (x) {
  34.            
  35.             case 0:
  36.                 x = run.mainMenu();
  37.                 break;
  38.             case 1:
  39.                 x = run.admin();
  40.                 run.r = true;
  41.                 break;
  42.             case 2:
  43.                 x = run.user();
  44.                 run.r = true;
  45.                 break;
  46.             case 3:
  47.                 x = run.userLogin();
  48.                 run.r = true;
  49.                 break;
  50.             case 4:
  51.                 x = run.userReg();
  52.                 run.r = true;
  53.                 break;
  54.             case 5:
  55.                 x = run.userMenu();
  56.                 run.r = true;
  57.                 break;
  58.             case 6:
  59.                 x = run.userShop();
  60.                 run.r = true;
  61.                 break;
  62.             case 7:
  63.                 x = run.userStatus();
  64.                 run.r = true;
  65.                 break;
  66.             case 8:
  67.                 x = run.adminMenu();
  68.                 run.r = true;
  69.                 break;
  70.             case 9:
  71.                 x = run.adminNewOrder();
  72.                 run.r = true;
  73.                 break;
  74.             case 10:
  75.                 x = run.adminAllOrder();
  76.                 run.r = true;
  77.                 break;
  78.             }
  79.         } while (run.r);
  80.     }
  81.    
  82.     private int mainMenu(){
  83.         int n;
  84.         String choice[] = {
  85.             "Admin",
  86.             "User"
  87.         };
  88.         n = JOptionPane.showOptionDialog(null, "Welcome to FreeStyler Online Apparels\nLogin As:", "FreeStyler Online Apparels", JOptionPane.DEFAULT_OPTION , JOptionPane.PLAIN_MESSAGE , null, choice, -1);
  89.         if (n == 0){
  90.             r = true;
  91.             return 1;
  92.         }
  93.         else if (n == 1){
  94.             r = true;
  95.             return 2;
  96.         }
  97.         return 0;
  98.     }
  99.     private int admin(){
  100.         String x, y;
  101.         do{
  102.             x = JOptionPane.showInputDialog(null, "Username", "Admin", JOptionPane.DEFAULT_OPTION);
  103.             if (x == null){
  104.                 return 0;
  105.             }
  106.             y = JOptionPane.showInputDialog(null, "Password", "Admin", JOptionPane.DEFAULT_OPTION);
  107.             if (y == null){
  108.                 return 0;
  109.             }
  110.             if (x.equals("admin") && y.equals("admin")){
  111.                 JOptionPane.showMessageDialog(null, "Login Successfully");
  112.                 return 8;
  113.             }
  114.             else {
  115.                 JOptionPane.showMessageDialog(null, "Username or password incorrect");
  116.             }
  117.         }while(true);
  118.     }
  119.    
  120.     private int user(){
  121.        String choice[] = {
  122.             "Login",
  123.             "Register"
  124.         };
  125.         int x;
  126.         x = JOptionPane.showOptionDialog(null, "", "FreeStyler Online Apparels", JOptionPane.DEFAULT_OPTION , JOptionPane.PLAIN_MESSAGE , null, choice, -1);
  127.         if (x == 0){
  128.             return 3;
  129.         }
  130.         else if (x == 1){
  131.             return 4;
  132.         }
  133.         else if (x == -1){
  134.             return 0;
  135.         }
  136.         return 0;
  137.     }
  138.    
  139.     private int adminMenu(){
  140.         int update = 0;
  141.         int x;
  142.         loadOrder();
  143.         for (int i = 0; i < userOrder.size(); i++) {
  144.                 if (orderStatus.get(i).equals("Pending")){
  145.                     update++;
  146.                 }
  147.             }
  148.         String choice[] = {
  149.             "View new orders [" + update +"]",
  150.             "View all orders",
  151.             "Log out"
  152.         };
  153.         if (update > 0 ){
  154.             x = JOptionPane.showOptionDialog(null, update + " new order! Click View new orders to view new order", "FreeStyler Online Apparels", JOptionPane.DEFAULT_OPTION , JOptionPane.PLAIN_MESSAGE , null, choice, -1);
  155.         }
  156.         else {
  157.             x = JOptionPane.showOptionDialog(null, "", "FreeStyler Online Apparels", JOptionPane.DEFAULT_OPTION , JOptionPane.PLAIN_MESSAGE , null, choice, -1);
  158.         }
  159.         if (x == -1){
  160.             return 0;
  161.         }
  162.         else if (x == 0){
  163.             return 9;
  164.         }
  165.         else if (x == 1){
  166.             return 10;
  167.         }
  168.         else if (x == 2){
  169.             return 0;
  170.         }
  171.         return 0;
  172.     }
  173.    
  174.     private int adminNewOrder(){
  175.        
  176.         boolean bool, bool2 = true;
  177.         String message, append;
  178.         String choice[] = {
  179.             "Previous",
  180.             "Next",
  181.             "Accept",
  182.             "Back"
  183.         };
  184.         do{
  185.             bool = false;
  186.             loadOrder();
  187.             int x = 1;
  188.             for (int i = 0; i < userOrder.size(); i++) {
  189.                 if (orderStatus.get(i).equals("Pending")){
  190.                     bool = true;
  191.                 }
  192.             }
  193.             if (bool){
  194.                 while (bool2){
  195.                     if (x == 1){
  196.                         for (int i = 0; i < userOrder.size(); i++) {
  197.                             message = "Order #" + i + "\nUsername that order: " + userOrder.get(i) + "\n";
  198.                             if(top.get(i).amount > 0){
  199.                                 append = top.get(i).group + " " + top.get(i).type + " " + top.get(i).color + " (" + top.get(i).size + ") @RM" + classTop.getPrice() + " each x " + top.get(i).amount + " = RM" + classTop.getPrice() * pant.get(i).amount + "\n";
  200.                                 message = message + append;
  201.                             }
  202.                             if(pant.get(i).amount > 0){
  203.                                 append = pant.get(i).group + " " + pant.get(i).type + " " + pant.get(i).color + " (" + pant.get(i).size + ") @RM" + classPant.getPrice() + " each x " + pant.get(i).amount + " = RM" + classPant.getPrice() * pant.get(i).amount + "\n";
  204.                                 message = message + append;
  205.                             }
  206.                             if(dress.get(i).amount > 0){
  207.                                 append = dress.get(i).group + " " + dress.get(i).type + " " + dress.get(i).color + " (" + dress.get(i).size + ") @RM" + classDress.getPrice() + " each x " + dress.get(i).amount + " = RM" + classDress.getPrice() * dress.get(i).amount + "\n";
  208.                                 message = message + append;
  209.                             }
  210.                             if(skirt.get(i).amount > 0){
  211.                                 append = skirt.get(i).group + " " + skirt.get(i).type + " " + skirt.get(i).color + " (" + skirt.get(i).size + ") @RM" + classSkirt.getPrice() + " each x " + skirt.get(i).amount + " = RM" + classSkirt.getPrice() * skirt.get(i).amount + "\n";
  212.                                 message = message + append;
  213.                             }
  214.                             x = JOptionPane.showOptionDialog(null, message, "FreeStyler Online Apparels", JOptionPane.DEFAULT_OPTION , JOptionPane.PLAIN_MESSAGE , null, choice, -1);
  215.                             if (x == -1){
  216.                                 return 8;
  217.                             }
  218.                             else if (x == 2){
  219.                                 setStatus(i);
  220.                                 JOptionPane.showMessageDialog(null, "Order " + i + " order and shipping accepted");
  221.                                 bool2 = false;
  222.                             }
  223.                             else if (x == 3){
  224.                                 return 8;
  225.                             }
  226.                             else if (x == 0){
  227.                                 break;
  228.                             }
  229.                         }
  230.                     }
  231.                     else if (x == 0){
  232.                         for (int i = userOrder.size() - 1; i >= 0; i--) {
  233.                             message = "Order #" + i + "\nUsername that order: " + userOrder.get(i) + "\n";
  234.                             if(top.get(i).amount > 0){
  235.                                 append = top.get(i).group + " " + top.get(i).type + " " + top.get(i).color + " (" + top.get(i).size + ") @RM" + classTop.getPrice() + " each x " + top.get(i).amount + " = RM" + classTop.getPrice() * pant.get(i).amount + "\n";
  236.                                 message = message + append;
  237.                             }
  238.                             if(pant.get(i).amount > 0){
  239.                                 append = pant.get(i).group + " " + pant.get(i).type + " " + pant.get(i).color + " (" + pant.get(i).size + ") @RM" + classPant.getPrice() + " each x " + pant.get(i).amount + " = RM" + classPant.getPrice() * pant.get(i).amount + "\n";
  240.                                 message = message + append;
  241.                             }
  242.                             if(dress.get(i).amount > 0){
  243.                                 append = dress.get(i).group + " " + dress.get(i).type + " " + dress.get(i).color + " (" + dress.get(i).size + ") @RM" + classDress.getPrice() + " each x " + dress.get(i).amount + " = RM" + classDress.getPrice() * dress.get(i).amount + "\n";
  244.                                 message = message + append;
  245.                             }
  246.                             if(skirt.get(i).amount > 0){
  247.                                 append = skirt.get(i).group + " " + skirt.get(i).type + " " + skirt.get(i).color + " (" + skirt.get(i).size + ") @RM" + classSkirt.getPrice() + " each x " + skirt.get(i).amount + " = RM" + classSkirt.getPrice() * skirt.get(i).amount + "\n";
  248.                                 message = message + append;
  249.                             }
  250.                             x = JOptionPane.showOptionDialog(null, message, "FreeStyler Online Apparels", JOptionPane.DEFAULT_OPTION , JOptionPane.PLAIN_MESSAGE , null, choice, -1);
  251.                             if (x == -1){
  252.                                 return 8;
  253.                             }
  254.                             else if (x == 2){
  255.                                 setStatus(i);
  256.                                 JOptionPane.showMessageDialog(null, "Order #" + i + " order and shipping accepted");
  257.                                 bool2 = false;
  258.                                 break;
  259.                             }
  260.                             else if (x == 3){
  261.                                 return 8;
  262.                             }
  263.                             else if (x == 1){
  264.                                 break;
  265.                             }
  266.                         }
  267.                     }
  268.                 }
  269.             }
  270.             else if (!bool){
  271.                 JOptionPane.showMessageDialog(null, "Empty");
  272.                 return 8;
  273.             }
  274.         }while(true);
  275.     }
  276.    
  277.     private int adminAllOrder(){
  278.         boolean bool = false, bool2 = true;
  279.         String choice[] = {
  280.             "Previous",
  281.             "Next",
  282.             "Back"
  283.         };
  284.         int x, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
  285.         float total;
  286.         String message, append;
  287.        
  288.         loadOrder();
  289.         x = 1;
  290.         if (orderStatus.size() >= 0){
  291.             bool = true;
  292.         }
  293.         if (bool){
  294.             while (bool2){
  295.                 if (x == 1){
  296.                     for (int i = 0; i < userOrder.size(); i++) {
  297.                         message = "Order #" + i + "\nUsername that order: " + userOrder.get(i) + "\n";
  298.                         if(top.get(i).amount > 0){
  299.                             sum1 = classTop.getPrice() * top.get(i).amount;
  300.                             append = top.get(i).group + " " + top.get(i).type + " " + top.get(i).color + " (" + top.get(i).size + ") @RM" + classTop.getPrice() + " each x " + top.get(i).amount + " = RM" + sum1 + "\n";
  301.                             message = message + append;
  302.                         }
  303.                         if(pant.get(i).amount > 0){
  304.                             sum2 = classPant.getPrice() * pant.get(i).amount;
  305.                             append = pant.get(i).group + " " + pant.get(i).type + " " + pant.get(i).color + " (" + pant.get(i).size + ") @RM" + classPant.getPrice() + " each x " + pant.get(i).amount + " = RM" + sum2 + "\n";
  306.                             message = message + append;
  307.                         }
  308.                         if(dress.get(i).amount > 0){
  309.                             sum3 = classDress.getPrice() * dress.get(i).amount;
  310.                             append = dress.get(i).group + " " + dress.get(i).type + " " + dress.get(i).color + " (" + dress.get(i).size + ") @RM" + classDress.getPrice() + " each x " + dress.get(i).amount + " = RM" + sum3 + "\n";
  311.                             message = message + append;
  312.                         }
  313.                         if(skirt.get(i).amount > 0){
  314.                             sum4 = classSkirt.getPrice() * skirt.get(i).amount;
  315.                             append = skirt.get(i).group + " " + skirt.get(i).type + " " + skirt.get(i).color + " (" + skirt.get(i).size + ") @RM" + classSkirt.getPrice() + " each x " + skirt.get(i).amount + " = RM" + sum4 + "\n";
  316.                             message = message + append;
  317.                         }
  318.                         total = sum1 + sum2 + sum3 + sum4;
  319.                         append = "Discount: ";
  320.                         message = message + append;
  321.                         if (top.get(i).amount + pant.get(i).amount + dress.get(i).amount + skirt.get(i).amount >= 10){
  322.                             append = "10%\n";
  323.                             message = message + append;
  324.                             total = total * (float)0.9;
  325.                             append = "Total = RM" + total;
  326.                             message = message + append;
  327.                         }
  328.                         else if (top.get(i).amount + pant.get(i).amount + dress.get(i).amount + skirt.get(i).amount < 10){
  329.                             append = "0%\n";
  330.                             message = message + append;
  331.                             append = "Total = RM" + total;
  332.                             message = message + append;
  333.                         }
  334.                         x = JOptionPane.showOptionDialog(null, message, "FreeStyler Online Apparels", JOptionPane.DEFAULT_OPTION , JOptionPane.PLAIN_MESSAGE , null, choice, -1);
  335.                         if (x == -1){
  336.                             return 8;
  337.                         }
  338.                         else if (x == 2){
  339.                             return 8;
  340.                         }
  341.                         else if (x == 0){
  342.                             break;
  343.                         }
  344.                     }
  345.                 }
  346.                 else if (x == 0){
  347.                     for (int i = userOrder.size() - 1; i >= 0; i--) {
  348.                         message = "Order #" + i + "\nUsername that order: " + userOrder.get(i) + "\n";
  349.                         if(top.get(i).amount > 0){
  350.                             append = top.get(i).group + " " + top.get(i).type + " " + top.get(i).color + " (" + top.get(i).size + ") @RM" + classTop.getPrice() + " each x " + top.get(i).amount + " = RM" + (classTop.getPrice() * top.get(i).amount) + "\n";
  351.                             message = message + append;
  352.                         }
  353.                         if(pant.get(i).amount > 0){
  354.                             append = pant.get(i).group + " " + pant.get(i).type + " " + pant.get(i).color + " (" + pant.get(i).size + ") @RM" + classPant.getPrice() + " each x " + pant.get(i).amount + " = RM" + (classPant.getPrice() * pant.get(i).amount) + "\n";
  355.                             message = message + append;
  356.                         }
  357.                         if(dress.get(i).amount > 0){
  358.                             append = dress.get(i).group + " " + dress.get(i).type + " " + dress.get(i).color + " (" + dress.get(i).size + ") @RM" + classDress.getPrice() + " each x " + dress.get(i).amount + " = RM" + (classDress.getPrice() * dress.get(i).amount) + "\n";
  359.                             message = message + append;
  360.                         }
  361.                         if(skirt.get(i).amount > 0){
  362.                             append = skirt.get(i).group + " " + skirt.get(i).type + " " + skirt.get(i).color + " (" + skirt.get(i).size + ") @RM" + classSkirt.getPrice() + " each x " + skirt.get(i).amount + " = RM" + (classSkirt.getPrice() * skirt.get(i).amount) + "\n";
  363.                             message = message + append;
  364.                         }
  365.                         total = sum1 + sum2 + sum3 + sum4;
  366.                         append = "Discount: ";
  367.                         message = message + append;
  368.                         if (top.get(i).amount + pant.get(i).amount + dress.get(i).amount + skirt.get(i).amount >= 10){
  369.                             append = "10%\n";
  370.                             message = message + append;
  371.                             total = total * (float)0.9;
  372.                             append = "Total = RM" + total;
  373.                             message = message + append;
  374.                         }
  375.                         else if (top.get(i).amount + pant.get(i).amount + dress.get(i).amount + skirt.get(i).amount < 10){
  376.                             append = "0%\n";
  377.                             message = message + append;
  378.                             append = "Total = RM" + total;
  379.                             message = message + append;
  380.                         }
  381.                         x = JOptionPane.showOptionDialog(null, message, "FreeStyler Online Apparels", JOptionPane.DEFAULT_OPTION , JOptionPane.PLAIN_MESSAGE , null, choice, -1);
  382.                         if (x == -1){
  383.                             return 8;
  384.                         }
  385.                         else if (x == 2){
  386.                             return 8;
  387.                         }
  388.                         else if (x == 1){
  389.                             break;
  390.                         }
  391.                     }
  392.                 }
  393.             }
  394.         }
  395.         else if (!bool){
  396.             JOptionPane.showMessageDialog(null, "Empty");
  397.             return 8;
  398.         }
  399.         return 0;
  400.     }
  401.    
  402.     private int userLogin(){
  403.         boolean bool = true;
  404.         String user, pass = null;
  405.         loadDat();
  406.         do{
  407.             System.out.println("-1 to back");
  408.             System.out.print("Username: ");
  409.             try {
  410.                 user = input.nextLine();
  411.             } catch (Exception e){
  412.                 System.out.println("Error: " + e);
  413.                 continue;
  414.             }
  415.             if (backtrack(user)){
  416.                 bool = false;
  417.                 return 2;
  418.             }
  419.             System.out.print("Password: ");
  420.             try{
  421.                 pass = input.nextLine();
  422.             } catch (Exception e){
  423.                 System.out.println("Error: " + e);
  424.             }
  425.             if (backtrack(pass)){
  426.                 bool = false;
  427.                 return 2;
  428.             }
  429.             else {
  430.                 for (int i = 0; i < this.username.size(); i++) {
  431.                     if (user.equals(this.username.get(i)) && pass.equals(this.password.get(i))){
  432.                         System.out.println("Login Successfully");
  433.                         System.out.println();
  434.                         bool = false;
  435.                         account = this.username.get(i);
  436.                         return 5;
  437.                     }
  438.                 }
  439.                 System.out.println("Invalid username or password");
  440.                 System.out.println();
  441.             }  
  442.         }while(bool);
  443.         return 0;
  444.     }
  445.    
  446.     private int userReg(){
  447.         String user, pass;
  448.         boolean bool = true;
  449.         boolean bool2 = false;
  450.         boolean taken;
  451.         loadDat();
  452.         System.out.println("-1 to back");
  453.         System.out.println("Register as");
  454.        
  455.         do{
  456.             taken = false;
  457.             System.out.print("Username: ");
  458.             user = input.nextLine();
  459.             if (backtrack(user)){
  460.                 return 2;
  461.             }
  462.            
  463.             for (int i = 0; i < this.username.size(); i++) {
  464.                 if (user.equals(this.username.get(i))){
  465.                     System.out.println("Username taken, please take different username");
  466.                     System.out.println();
  467.                     taken = true;
  468.                     continue;
  469.                 }
  470.             }
  471.            
  472.             if (taken){
  473.                 continue;
  474.             }
  475.            
  476.             else if(user.contains(" ")){
  477.                 System.out.println("Username or password cannot contain space");
  478.                 System.out.println();
  479.                 continue;
  480.             }
  481.            
  482.             else {
  483.                 do {
  484.                     bool2 = false;
  485.                     System.out.print("Password: ");
  486.                     pass = input.nextLine();
  487.                     if (backtrack(pass)){
  488.                         return 2;
  489.                     }
  490.                     else if (pass.contains(" ")){
  491.                         System.out.println("Username or password cannot contain space");
  492.                         System.out.println();
  493.                         bool2 = true;
  494.                         continue;
  495.                     }
  496.                     else {
  497.                         addDat(user, pass);
  498.                         System.out.println("Account created successfully");
  499.                         System.out.println();
  500.                         bool = false;
  501.                         return 2;
  502.                     }
  503.                 }while(bool2);
  504.             }
  505.         }while(bool);
  506.         return 0;
  507.     }
  508.    
  509.     private int userMenu(){
  510.         boolean bool = true;
  511.         String x = null;
  512.         System.out.println("Welcome back, " + account);
  513.         loadOrder();
  514.         for (int i = 0; i < userOrder.size(); i++) {
  515.             if (userOrder.get(i).equals(account) && getUpdate.get(i) == 1){
  516.                 System.out.println("Your order #" + i + " has been accepted.");
  517.                 System.out.println("Enter 2 to view the order status");
  518.                 setUpdate(i);
  519.             }
  520.         }
  521.         do{
  522.            
  523.             System.out.println("(1)Shop\n(2)Status\n(3)Log Out");
  524.             try {
  525.                 x = input.nextLine();
  526.             } catch (Exception e) {
  527.                 System.out.println("Invalid input, " + e);
  528.             }
  529.             if (x.equals("1")){
  530.                 bool = false;
  531.                 return 6;
  532.             }
  533.             else if (x.equals("2")){
  534.                 bool = false;
  535.                 return 7;
  536.             }
  537.             else if (x.equals("3")){
  538.                 bool = false;
  539.                 return 0;
  540.             }
  541.             else {
  542.                 System.out.println("Invalid input, please try again");
  543.             }
  544.         }while(bool);
  545.         return 0;
  546.     }
  547.    
  548.     private int userShop(){
  549.         boolean bool, bool2, bool3, checkOut;
  550.         int x, color, size = 0, amount, total;
  551.         String in;
  552.         String c, s, g, t;
  553.         do {
  554.             checkOut = false;
  555.             System.out.println("-1 to back, clear cart");
  556.             System.out.println("Men\tWomen\n(1)Top\t(3)Dress\n(2)Pant\t(4)Skirt\n(5)View cart\n(6)Checkout");
  557.             try {
  558.                 in = input.nextLine();
  559.                 x = Integer.parseInt(in);
  560.             }
  561.             catch(Exception e){
  562.                 System.out.println("Invalid input, " + e);
  563.                 System.out.println();
  564.                 continue;
  565.             }
  566.             if (x >= 1 && x <= 4){
  567.                 do{
  568.                     bool = true;
  569.                     System.out.println("-1 to back");
  570.                     System.out.println("Choose a color:");
  571.                     System.out.println("(1)White\n(2)Black\n(3)Red\n(4)Green\n(5)Blue");
  572.                     try {
  573.                         in = input.nextLine();
  574.                         color = Integer.parseInt(in);
  575.                     }
  576.                     catch (Exception e){
  577.                         System.out.println("Invalid input, " + e);
  578.                         System.out.println();
  579.                         continue;
  580.                     }
  581.                     if (color >= 1 && color <= 5){
  582.                         do {
  583.                             bool2 = true;
  584.                             System.out.println("-1 to back");
  585.                             System.out.println("Choose a size:");
  586.                             System.out.println("(1)S\n(2)M\n(3)L");
  587.                             try {
  588.                                 in = input.nextLine();
  589.                                 size = Integer.parseInt(in);
  590.                             }
  591.                             catch (Exception e){
  592.                                 System.out.println("Invalid input, " + e);
  593.                                 System.out.println();
  594.                                 continue;
  595.                             }
  596.                             if (size >= 1 && size <= 3){
  597.                                 do {
  598.                                     bool3 = true;
  599.                                     System.out.println("-1 to back");
  600.                                     System.out.println("Purchase total of 10 pieces will get 10% discount!");
  601.                                     switch (x) {
  602.                                     case 1:
  603.                                         System.out.print("Amount @RM" + classTop.getPrice() +" each: ");
  604.                                         break;
  605.                                     case 2:
  606.                                         System.out.print("Amount @RM" + classPant.getPrice() +" each: ");
  607.                                         break;
  608.                                     case 3:
  609.                                         System.out.print("Amount @RM" + classDress.getPrice() +" each: ");
  610.                                         break;
  611.                                     case 4:
  612.                                         System.out.print("Amount @RM" + classSkirt.getPrice() +" each: ");
  613.                                         break;
  614.                                     }
  615.                                     try {
  616.                                         in = input.nextLine();
  617.                                         amount = Integer.parseInt(in);
  618.                                     }
  619.                                     catch (Exception e){
  620.                                         System.out.println("Invalid input, " + e);
  621.                                         System.out.println();
  622.                                         continue;
  623.                                     }
  624.                                    
  625.                                     if (backtrack(amount)){
  626.                                         System.out.println();
  627.                                         bool3 = false;
  628.                                         break;
  629.                                     }
  630.                                     else if (amount < 1){
  631.                                         System.out.println("Amount cannot less than 1");
  632.                                         System.out.println();
  633.                                         continue;
  634.                                     }
  635.                                     else {
  636.                                         g = gGet(x);
  637.                                         c = cGet(color);
  638.                                         s = sGet(size);
  639.                                         t = tGet(x);
  640.                                         addCart(t, c, s, amount);
  641.                                         System.out.println();
  642.                                         System.out.printf("%s %s %s (%s) x %d added to cart\n", g, t, c, s, amount);
  643.                                         System.out.println();
  644.                                         bool = false;
  645.                                         bool2 = false;
  646.                                         bool3 = false;
  647.                                     }
  648.                                 } while (bool3);
  649.                             }
  650.                             else if(backtrack(size)){
  651.                                 bool2 = false;
  652.                                 break;
  653.                             }
  654.                             else {
  655.                                 System.out.println("Invalid input, please try again");
  656.                                 System.out.println();
  657.                             }
  658.                         }while(bool2);
  659.                     }
  660.                     else if (backtrack(color)){
  661.                         bool = false;
  662.                         break;
  663.                     }
  664.                     else {
  665.                         System.out.println("Invalid input, please try again");
  666.                         System.out.println();
  667.                     }
  668.                 }while(bool);
  669.             }
  670.             else if(x == 5){
  671.                 if (classTop.amount + classPant.amount + classDress.amount + classSkirt.amount > 0){
  672.                     if (classTop.amount >= 1){
  673.                         System.out.printf("%s %s %s (%s) @RM%d each x %d = RM%d\n", classTop.group, classTop.type, classTop.color, classTop.size, classTop.getPrice(), classTop.amount, (classTop.amount * classTop.getPrice()));
  674.                     }
  675.                     if (classPant.amount >= 1){
  676.                         System.out.printf("%s %s %s (%s) @RM%d each x %d = RM%d\n", classPant.group, classPant.type, classPant.color, classPant.size, classPant.getPrice(), classPant.amount, (classPant.amount * classPant.getPrice()));
  677.                     }
  678.                     if (classDress.amount >= 1){
  679.                         System.out.printf("%s %s %s (%s) @RM%d each x %d = RM%d\n", classDress.group, classDress.type, classDress.color, classDress.size, classDress.getPrice(), classDress.amount, (classDress.amount * classDress.getPrice()));
  680.                     }
  681.                     if (classSkirt.amount >= 1){
  682.                         System.out.printf("%s %s %s (%s) @RM%d each x %d = RM%d\n", classSkirt.group, classSkirt.type, classSkirt.color, classSkirt.size, classSkirt.getPrice(), classSkirt.amount, (classSkirt.amount * classSkirt.getPrice()));
  683.                     }
  684.                     total = (classTop.amount * classTop.getPrice()) + (classPant.amount * classPant.getPrice()) + (classDress.amount * classDress.getPrice()) + (classSkirt.amount * classSkirt.getPrice());
  685.                     Math.abs(total);
  686.                         System.out.print("Discount: ");
  687.                     if (classTop.amount + classPant.amount + classDress.amount + classSkirt.amount >= 10){
  688.                         System.out.println("10%");
  689.                         System.out.printf("Total = RM%.2f\n", total * 0.9);
  690.                     }
  691.                     else {
  692.                         System.out.println("0%");
  693.                         System.out.printf("Total = RM%d\n", total);
  694.                     }
  695.                 }
  696.                 else {
  697.                     System.out.println("No item in the cart");
  698.                 }
  699.                 System.out.println();
  700.                 System.out.println("Press Enter to continue");
  701.                 input.nextLine();
  702.             }
  703.             else if(x == 6){
  704.                 if (classTop.amount + classPant.amount + classDress.amount + classSkirt.amount > 0){
  705.                     addOrder(account);
  706.                     checkOut = true;
  707.                     loadOrder();
  708.                     total = (classTop.amount * classTop.getPrice()) + (classPant.amount * classPant.getPrice()) + (classDress.amount * classDress.getPrice()) + (classSkirt.amount * classSkirt.getPrice());
  709.                     System.out.println("The total is :" + total);
  710.                     System.out.println("Thanks\nYour order number is #" + (userOrder.size() - 1) + "\nPlease come again!\n");
  711.                     classTop.amount = 0;
  712.                     classPant.amount = 0;
  713.                     classDress.amount = 0;
  714.                     classSkirt.amount = 0;
  715.                     return 5;
  716.                 }
  717.                 else {
  718.                     System.out.println("Thanks, please come again\n");
  719.                     return 5;
  720.                 }
  721.             }
  722.             else if(backtrack(x)){
  723.                 classTop.amount = 0;
  724.                 classPant.amount = 0;
  725.                 classDress.amount = 0;
  726.                 classSkirt.amount = 0;
  727.                 return 5;
  728.             }
  729.             else {
  730.                 System.out.println("Invalid input, try again");
  731.             }
  732.         }while(!checkOut);
  733.         return 0;
  734.     }
  735.    
  736.     private int userStatus(){
  737.         loadOrder();
  738.         boolean order = false;
  739.         int total;
  740.         for (int i = 0; i < this.userOrder.size(); i++) {
  741.             if (this.userOrder.get(i).equals(account)){
  742.                 System.out.println();
  743.                 order = true;
  744.                 System.out.println("Order #" + i);
  745.                 if(((Top)top.get(i)).amount > 0){
  746.                     System.out.printf("%s %s %s (%s) @RM%d each x %d = RM%d\n", ((Top)top.get(i)).group, ((Top)top.get(i)).type, ((Top)top.get(i)).color, ((Top)top.get(i)).size, classTop.getPrice(), ((Top)top.get(i)).amount, (classTop.getPrice() * ((Top)top.get(i)).amount));
  747.                 }
  748.                 if(((Pant)pant.get(i)).amount > 0){
  749.                     System.out.printf("%s %s %s (%s) @RM%d each x %d = RM%d\n", ((Pant)pant.get(i)).group, ((Pant)pant.get(i)).type, ((Pant)pant.get(i)).color, ((Pant)pant.get(i)).size, classPant.getPrice(), ((Pant)pant.get(i)).amount, (classPant.getPrice() * ((Pant)pant.get(i)).amount));
  750.                 }
  751.                 if(((Dress)dress.get(i)).amount > 0){
  752.                     System.out.printf("%s %s %s (%s) @RM%d each x %d = RM%d\n", ((Dress)dress.get(i)).group, ((Dress)dress.get(i)).type, ((Dress)dress.get(i)).color, ((Dress)dress.get(i)).size, classDress.getPrice(), ((Dress)dress.get(i)).amount, (classDress.getPrice() * ((Dress)dress.get(i)).amount));
  753.                 }
  754.                 if(((Skirt)skirt.get(i)).amount > 0){
  755.                     System.out.printf("%s %s %s (%s) @RM%d each x %d = RM%d\n", ((Skirt)skirt.get(i)).group, ((Skirt)skirt.get(i)).type, ((Skirt)skirt.get(i)).color, ((Skirt)skirt.get(i)).size, classSkirt.getPrice(), ((Skirt)skirt.get(i)).amount, (classSkirt.getPrice() * ((Skirt)skirt.get(i)).amount));
  756.                 }
  757.                 total = (((Top)top.get(i)).amount * classTop.getPrice()) + (((Pant)pant.get(i)).amount * classPant.getPrice()) + (((Dress)dress.get(i)).amount * classDress.getPrice()) + (((Skirt)skirt.get(i)).amount * classSkirt.getPrice());
  758.                 Math.abs(total);
  759.                 System.out.print("Discount: ");
  760.                 if (((Top)top.get(i)).amount + ((Pant)pant.get(i)).amount + ((Dress)dress.get(i)).amount + ((Skirt)skirt.get(i)).amount >= 10){
  761.                     System.out.println("10%");
  762.                     System.out.printf("Total = RM%.2f\n", total * 0.9);
  763.                 }
  764.                 else {
  765.                     System.out.println("0%");
  766.                     System.out.printf("Total = RM%d\n", total);
  767.                 }
  768.                 System.out.println("Order confirm: " + orderStatus.get(i));
  769.                 System.out.println("Shipping confirm: " + shipStatus.get(i));
  770.             }
  771.         }
  772.         if (!order){
  773.             System.out.println();
  774.             System.out.println("Empty, you did not order anything in the past");
  775.         }
  776.         System.out.println("\nPress Enter to continue");
  777.         input.nextLine();
  778.         return 5;
  779.     }
  780.    
  781.     private void loadDat(){ //load or refresh username and password data into memory
  782.         readWrite read = new readWrite("login.txt");
  783.         read.openRead("login.txt");
  784.         read.readDat();
  785.         username.clear();
  786.         password.clear();
  787.         for (int i = 0; i < read.user.size(); i++) { //pass data from readWrite class
  788.             this.username.add(read.user.get(i));
  789.             this.password.add(read.pass.get(i));
  790.         }
  791.         read.closeRead();
  792.     }
  793.    
  794.    
  795.     private void addDat(String user, String pass){ //add new username and password data into memory
  796.         readWrite write = new readWrite("login.txt");
  797.         write.openRead("login.txt");
  798.         write.readDat();
  799.         write.closeRead();
  800.         write.user.add(user); //pass new username and password data to readWrite class
  801.         write.pass.add(pass);
  802.         write.writeDat();
  803.         write.closeWrite();
  804.     }
  805.    
  806.     private void loadOrder(){ //load or refresh orders data into memory
  807.         readWrite readOrder = new readWrite("order.txt");
  808.         readOrder.openRead("order.txt");
  809.         readOrder.readOrder();
  810.         userOrder.clear(); //clear all the existing data in the memory, to refresh
  811.         orderStatus.clear();
  812.         shipStatus.clear();
  813.         top.clear();
  814.         pant.clear();
  815.         dress.clear();
  816.         skirt.clear();
  817.         getUpdate.clear();
  818.         for (int i = 0; i < readOrder.userOrder.size(); i++) { //pass order data from readWrite class
  819.             this.userOrder.add(readOrder.userOrder.get(i));
  820.             this.orderStatus.add(readOrder.orderStatus.get(i));
  821.             this.shipStatus.add(readOrder.shipStatus.get(i));
  822.             this.top.add(readOrder.top.get(i));
  823.             this.pant.add(readOrder.pant.get(i));
  824.             this.dress.add(readOrder.dress.get(i));
  825.             this.skirt.add(readOrder.skirt.get(i));
  826.             this.getUpdate.add(readOrder.getUpdate.get(i));
  827.         }
  828.         readOrder.closeRead();
  829.     }
  830.    
  831.     private void addOrder(String user){ //add new data to readWrite class
  832.         readWrite write = new readWrite("order.txt");
  833.         write.openRead("order.txt");
  834.         write.readOrder();
  835.         write.closeRead();
  836.         write.userOrder.add(user); //pass new orders data to readWrite class
  837.         write.orderStatus.add("Pending");
  838.         write.shipStatus.add("Pending");
  839.         write.top.add(classTop);
  840.         write.pant.add(classPant);
  841.         write.dress.add(classDress);
  842.         write.skirt.add(classSkirt);
  843.         write.getUpdate.add(0);
  844.         write.writeOrder();
  845.         write.closeWrite();
  846.     }
  847.    
  848.     private void setStatus(int orderNumber){ //admin set order status and shipping status to "accepted"
  849.         readWrite write = new readWrite("order.txt");
  850.         write.openRead("order.txt");
  851.         write.readOrder();
  852.         write.closeRead();
  853.         write.orderStatus.set(orderNumber, "Accepted");
  854.         write.shipStatus.set(orderNumber, "Accepted");
  855.         write.getUpdate.set(orderNumber, 1);
  856.         write.writeOrder();
  857.         write.closeWrite();
  858.     }
  859.    
  860.     private void setUpdate(int orderNumber){ //admin tell user that his order has been confirmed
  861.         readWrite write = new readWrite("order.txt");
  862.         write.openRead("order.txt");
  863.         write.readOrder();
  864.         write.closeRead();
  865.         write.getUpdate.set(orderNumber, 0);
  866.         write.writeOrder();
  867.         write.closeWrite();
  868.     }
  869.    
  870.     private boolean backtrack(String back){ //check if input is -1, return to previous page
  871.         if (back.equals("-1")){
  872.             return true;
  873.         }
  874.         else {
  875.             return false;
  876.         }
  877.     }
  878.    
  879.     private boolean backtrack(int back){ //check if input is -1, return to previous page
  880.         if (back == -1){
  881.             return true;
  882.         }
  883.         else {
  884.             return false;
  885.         }
  886.     }
  887.    
  888.     private String gGet(int x){ //translate user input into string form
  889.         switch (x) {
  890.             case 1:
  891.             case 2:
  892.                 return "Men";
  893.             case 3:
  894.             case 4:
  895.                 return "Women";
  896.             default:
  897.                 return "Invalid";
  898.         }
  899.     }
  900.    
  901.     private String tGet(int x){ //translate user input into string form
  902.         switch (x) {
  903.             case 1:
  904.                 return "Top";
  905.             case 2:
  906.                 return "Pant";
  907.             case 3:
  908.                 return "Dress";
  909.             case 4:
  910.                 return "Skirt";
  911.             default:
  912.                 return "Invalid";
  913.         }
  914.     }
  915.    
  916.     private String cGet(int x){ //translate user input into string form
  917.         switch (x) {
  918.             case 1:
  919.                 return "White";
  920.             case 2:
  921.                 return "Black";
  922.             case 3:
  923.                 return "Red";
  924.             case 4:
  925.                 return "Green";
  926.             case 5:
  927.                 return "Blue";
  928.             default:
  929.                 return "Invalid";
  930.         }
  931.     }
  932.    
  933.     private String sGet(int x){ //translate user input into string form
  934.         switch (x) {
  935.             case 1:
  936.                 return "S";
  937.             case 2:
  938.                 return "M";
  939.             case 3:
  940.                 return "L";
  941.             default:
  942.                 return "Invalid";
  943.         }
  944.     }
  945.    
  946.     private void addCart(String t, String c, String s, int amount){ //add item into cart
  947.         switch (t) {
  948.             case "Top":
  949.                 classTop.amount = amount;
  950.                 classTop.color = c;
  951.                 classTop.size = s;
  952.                 classTop.group = "Men";
  953.                 break;
  954.             case "Pant":
  955.                 classPant.amount = amount;
  956.                 classPant.color = c;
  957.                 classPant.size = s;
  958.                 classPant.group = "Men";
  959.                 break;
  960.             case "Dress":
  961.                 classDress.amount = amount;
  962.                 classDress.color = c;
  963.                 classDress.size = s;
  964.                 classDress.group = "Women";
  965.                 break;
  966.             case "Skirt":
  967.                 classSkirt.amount = amount;
  968.                 classSkirt.color = c;
  969.                 classSkirt.size = s;
  970.                 classSkirt.group = "Women";
  971.                 break;
  972.             default:
  973.                 break;
  974.         }
  975.     }
  976. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement