Advertisement
Guest User

3

a guest
Jun 15th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.65 KB | None | 0 0
  1. package assignment;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class core{
  7.     private static Scanner input;
  8.     private String account;
  9.     private ArrayList<String> username = new ArrayList<String>();
  10.     private ArrayList<String> password = new ArrayList<String>();
  11.     private ArrayList<String> userOrder = new ArrayList<String>();
  12.     private ArrayList<String> shipStatus = new ArrayList<String>();
  13.     private ArrayList<String> orderStatus = new ArrayList<String>();
  14.     private ArrayList<Integer> item1 = new ArrayList<Integer>();
  15.     private ArrayList<Integer> item2 = new ArrayList<Integer>();
  16.     private ArrayList<Integer> item3 = new ArrayList<Integer>();
  17.    
  18.     public static void main(String[] args) {
  19.         int x = 0;
  20.         boolean bool = true;
  21.         core run = new core();
  22.         //run.readDat();
  23.         input = new Scanner(System.in);
  24.         do{
  25.             switch (x) {
  26.            
  27.             case 0:
  28.                 x = mainMenu();
  29.                 break;
  30.             case 1:
  31.                 x = run.admin();
  32.                 break;
  33.             case 2:
  34.                 x = run.user();
  35.                 break;
  36.             case 3:
  37.                 x = run.userLogin();
  38.                 break;
  39.             case 4:
  40.                 x = run.userReg();
  41.                 break;
  42.             case 5:
  43.                 x = run.userMenu();
  44.                 break;
  45.             case 6:
  46.                 x = run.userShop();
  47.                 bool = false;
  48.                 break;
  49.             case 7:
  50.                 run.userStatus();
  51.                 bool = false;
  52.                 break;
  53.             default:
  54.                 bool = false;
  55.                 System.out.println(x);
  56.                 break;
  57.             }
  58.         } while (bool);
  59.     }
  60.    
  61.     private static int mainMenu(){
  62.         System.out.println("Welcome to FreeStyler Online Apparels");
  63.         System.out.println("Login As:\n(1)Admin\n(2)User");
  64.         boolean bool = true;
  65.         String n;
  66.         do {
  67.             try{
  68.                 n = input.nextLine();
  69.             } catch (Exception e) {
  70.                 System.out.println("Invalid input, " + e);
  71.                 System.out.println("Login As:\n(1)Admin\n(2)User");
  72.                 continue;
  73.             }
  74.             if (n.equals("1")){
  75.                 bool = false;
  76.                 return 1;
  77.             }
  78.             else if (n.equals("2")){
  79.                 bool = false;
  80.                 return 2;
  81.             }
  82.             else {
  83.                 System.out.println("Invalid input, please try again");
  84.                 System.out.println("Login As:\n(1)Admin\n(2)User");
  85.                 continue;
  86.             }
  87.         }while(bool);
  88.         return 0;
  89.     }
  90.     private int admin(){
  91.         boolean bool = true;
  92.         String user, pass = null;
  93.         do {
  94.             System.out.println("-1 to back");
  95.             System.out.print("Username: ");
  96.             try {
  97.                 user = input.nextLine();
  98.             } catch (Exception e){
  99.                 System.out.println("Error: " + e);
  100.                 continue;
  101.             }
  102.             if (backtrack(user)){
  103.                 bool = false;
  104.                 return 0;
  105.             }
  106.             System.out.print("Password: ");
  107.             try{
  108.                 pass = input.nextLine();
  109.             } catch (Exception e){
  110.                 System.out.println("Error: " + e);
  111.             }
  112.            
  113.             if (backtrack(pass)){
  114.                 bool = false;
  115.                 return 0;
  116.             }
  117.             else if (user.equals("admin") && pass.equals("admin")){
  118.                 System.out.println("Login Successfully");
  119.                 bool = false;
  120.                 return 10;
  121.             }
  122.             else {
  123.                 System.out.println("Invalid username or password");
  124.             }
  125.         }while (bool);
  126.         return 0;
  127.     }
  128.    
  129.     private int user(){
  130.         String x;
  131.         System.out.println("-1 to back");
  132.         System.out.println("(1)Login\n(2)Register");
  133.         try{
  134.             x = input.nextLine();
  135.         }
  136.         catch (Exception e){
  137.             System.out.println("Invalid input, " + e);
  138.             return 2;
  139.         }
  140.        
  141.         if (x.equals("1")){
  142.             return 3;
  143.         }
  144.         else if (x.equals("2")){
  145.             return 4;
  146.         }
  147.         else if (x.equals("-1")){
  148.             return 0;
  149.         }
  150.         else {
  151.             System.out.println("Invalid input, please try again");
  152.             return 0;
  153.         }
  154.     }
  155.    
  156.     private int userLogin(){
  157.         boolean bool = true;
  158.         String user, pass = null;
  159.         loadDat();
  160.         do{
  161.             System.out.println("-1 to back");
  162.             System.out.print("Username: ");
  163.             try {
  164.                 user = input.nextLine();
  165.             } catch (Exception e){
  166.                 System.out.println("Error: " + e);
  167.                 continue;
  168.             }
  169.             if (backtrack(user)){
  170.                 bool = false;
  171.                 return 2;
  172.             }
  173.             System.out.print("Password: ");
  174.             try{
  175.                 pass = input.nextLine();
  176.             } catch (Exception e){
  177.                 System.out.println("Error: " + e);
  178.             }
  179.             if (backtrack(pass)){
  180.                 bool = false;
  181.                 return 2;
  182.             }
  183.             else {
  184.                 for (int i = 0; i < this.username.size(); i++) {
  185.                     if (user.equals(this.username.get(i)) && pass.equals(this.password.get(i))){
  186.                         System.out.println("Login Successfully");
  187.                         System.out.println();
  188.                         bool = false;
  189.                         account = this.username.get(i);
  190.                         return 5;
  191.                     }
  192.                 }
  193.                 System.out.println("Invalid username or password");
  194.             }  
  195.         }while(bool);
  196.         return 0;
  197.     }
  198.    
  199.     private int userReg(){
  200.         String user, pass;
  201.         boolean bool = true;
  202.         boolean bool2 = false;
  203.         boolean taken;
  204.         loadDat();
  205.         System.out.println("-1 to back");
  206.         System.out.println("Register as");
  207.        
  208.         do{
  209.             taken = false;
  210.             System.out.print("Username: ");
  211.             user = input.nextLine();
  212.             if (backtrack(user)){
  213.                 return 2;
  214.             }
  215.            
  216.             for (int i = 0; i < this.username.size(); i++) {
  217.                 if (user.equals(this.username.get(i))){
  218.                     System.out.println("Username taken, please take different username");
  219.                     taken = true;
  220.                     continue;
  221.                 }
  222.             }
  223.            
  224.             if (taken){
  225.                 continue;
  226.             }
  227.            
  228.             else if(user.contains(" ")){
  229.                 System.out.println("Username or password cannot contain space");
  230.                 continue;
  231.             }
  232.            
  233.             else {
  234.                 do {
  235.                     bool2 = false;
  236.                     System.out.print("Password: ");
  237.                     pass = input.nextLine();
  238.                     if (backtrack(pass)){
  239.                         return 2;
  240.                     }
  241.                     else if (pass.contains(" ")){
  242.                         System.out.println("Username or password cannot contain space");
  243.                         bool2 = true;
  244.                         continue;
  245.                     }
  246.                     else {
  247.                         addDat(user, pass);
  248.                         System.out.println("Account created successfully");
  249.                         bool = false;
  250.                         return 2;
  251.                     }
  252.                 }while(bool2);
  253.             }
  254.         }while(bool);
  255.         return 0;
  256.     }
  257.    
  258.     private int userMenu(){
  259.         boolean bool = true;
  260.         String x = null;
  261.         System.out.println("Welcome back, " + account);
  262.         do{
  263.            
  264.             System.out.println("(1)Shop\n(2)Status\n(3)Log Out");
  265.             try {
  266.                 x = input.nextLine();
  267.             } catch (Exception e) {
  268.                 System.out.println("Invalid input, " + e);
  269.             }
  270.             if (x.equals("1")){
  271.                 bool = false;
  272.                 return 6;
  273.             }
  274.             else if (x.equals("2")){
  275.                 bool = false;
  276.                 return 7;
  277.             }
  278.             else if (x.equals("3")){
  279.                 bool = false;
  280.                 return 0;
  281.             }
  282.             else {
  283.                 System.out.println("Invalid input, please try again");
  284.             }
  285.         }while(bool);
  286.         return 0;
  287.     }
  288.    
  289.     private int userShop(){
  290.         int x = 0;
  291.         int n = 0;
  292.         int item[] = {
  293.             0,
  294.             0,
  295.             0,
  296.         };
  297.         boolean bool, bool2, checkOut;
  298.         do {
  299.             checkOut = true;
  300.             do {
  301.                 bool = false;
  302.                 System.out.println("Item on sale, add order");
  303.                 System.out.println("-1 to back (Clear cart)");
  304.                 System.out.println("(1)item1\n(2)item2\n(3)item3\n(4)View item in cart\n(5)Check Out");
  305.                 System.out.print("Select an item to purchase: ");
  306.                 try {
  307.                     x = Integer.parseInt(input.nextLine());
  308.                 }
  309.                 catch (Exception e){
  310.                     System.out.println("Invalid input, " + e);
  311.                     bool = true;
  312.                     continue;
  313.                 }
  314.                 if (backtrack(x)){
  315.                     bool = false;
  316.                     return 5;
  317.                 }
  318.                
  319.                 else if (x <= 0 || x >= 6){
  320.                     System.out.println("Invalid input, please try again\n");
  321.                     bool = true;
  322.                 }
  323.                 else {
  324.                     break;
  325.                 }
  326.             }while(bool);
  327.             if (x > 0 && x < 4){
  328.                 do{
  329.                     bool2 = false;
  330.                     System.out.println("-1 to cancel");
  331.                     System.out.print("Quantity: ");
  332.                     try {
  333.                         n = Integer.parseInt(input.nextLine());
  334.                     }
  335.                     catch (Exception e){
  336.                         System.out.println("Invalid input, " + e + "\n");
  337.                         bool2 = true;
  338.                         continue;
  339.                     }
  340.                     if(n < 1){
  341.                         System.out.println("Quantity cannot less than 0");
  342.                         bool2 = true;
  343.                         continue;
  344.                     }
  345.                     else {
  346.                         System.out.println("item" + x + " (Amount: " + n + ") added to cart" );
  347.                     }
  348.                 }while(bool2);
  349.                 item[x - 1] += n;
  350.             }
  351.             else if (x == 4){
  352.                 for (int i = 0; i <= 3; i++) {
  353.                     System.out.println("item" + (i + 1) + " (Amount: " + item[i]);
  354.                 }
  355.                 continue;
  356.             }
  357.             else if (x == 5){
  358.                 checkOut = false;
  359.                 addOrder(account, "true", "true", item[0], item[1], item[2]);
  360.                 loadDat();
  361.                 System.out.println("Thank you for purchasing from FreeStyler Online Aparrels, your order number is Order #" + (userOrder.size() - 1));
  362.             }
  363.         }while(checkOut);
  364.         return 5;
  365.     }
  366.    
  367.     private int userStatus(){
  368.         loadOrder();
  369.         for (int i = 0; i < this.userOrder.size(); i++) {
  370.             if (this.userOrder.get(i).equals(account)){
  371.                 System.out.println("Order #" + i);
  372.                 System.out.println("item1 x " + item1.get(i));
  373.                 System.out.println("item2 x " + item2.get(i));
  374.                 System.out.println("item3 x " + item2.get(i));
  375.                 System.out.println("Order confirm: " + orderStatus.get(i));
  376.                 System.out.println("Shipping confirm: " + shipStatus.get(i));
  377.                 System.out.println();
  378.             }
  379.         }
  380.         return 0;
  381.     }
  382.    
  383.     private void loadDat(){
  384.         readWrite read = new readWrite();
  385.         read.openWrite("login.txt");
  386.         read.openRead("login.txt");
  387.         read.readDat();
  388.         username.clear();
  389.         password.clear();
  390.         for (int i = 0; i < read.user.size(); i++) {
  391.             this.username.add(read.user.get(i));
  392.             this.password.add(read.pass.get(i));
  393.         }
  394.         read.closeRead();
  395.     }
  396.    
  397.    
  398.     private void addDat(String user, String pass){
  399.         readWrite write = new readWrite();
  400.         write.openWrite("login.txt");
  401.         write.openRead("login.txt");
  402.         write.readDat();
  403.         write.closeRead();
  404.         write.user.add(user);
  405.         write.pass.add(pass);
  406.         write.openWrite("login.txt");
  407.         write.writeDat();
  408.         write.closeWrite();
  409.     }
  410.    
  411.     private void loadOrder(){
  412.         readWrite readOrder = new readWrite();
  413.         readOrder.openWrite("order.txt");
  414.         readOrder.openRead("order.txt");
  415.         readOrder.readStatus();
  416.         userOrder.clear();
  417.         orderStatus.clear();
  418.         shipStatus.clear();
  419.         item1.clear();
  420.         item2.clear();
  421.         item3.clear();
  422.         for (int i = 0; i < readOrder.userOrder.size(); i++) {
  423.             this.userOrder.add(readOrder.userOrder.get(i));
  424.             this.orderStatus.add(readOrder.orderStatus.get(i));
  425.             this.shipStatus.add(readOrder.shipStatus.get(i));
  426.             this.item1.add(readOrder.item1.get(i));
  427.             this.item2.add(readOrder.item2.get(i));
  428.             this.item3.add(readOrder.item3.get(i));
  429.         }
  430.     }
  431.    
  432.     private void addOrder(String user, String order, String ship, int item1, int item2, int item3){
  433.         readWrite write = new readWrite();
  434.         write.openWrite("order.txt");
  435.         write.openRead("order.txt");
  436.         write.readStatus();
  437.         write.closeRead();
  438.         write.userOrder.add(order);
  439.         write.orderStatus.add(order);
  440.         write.shipStatus.add(ship);
  441.         write.item1.add(item1);
  442.         write.item2.add(item2);
  443.         write.item3.add(item3);
  444.         write.openWrite("order.txt");
  445.         write.closeWrite();
  446.     }
  447.    
  448.     private boolean backtrack(String back){
  449.         if (back.equals("-1")){
  450.             return true;
  451.         }
  452.         else {
  453.             return false;
  454.         }
  455.     }
  456.    
  457.     private boolean backtrack(int back){
  458.         if (back == -1){
  459.             return true;
  460.         }
  461.         else {
  462.             return false;
  463.         }
  464.     }
  465. }
  466.  
  467.  
  468. package assignment;
  469.  
  470. import java.io.File;
  471. import java.io.FileNotFoundException;
  472. import java.io.IOException;
  473. import java.io.PrintWriter;
  474. import java.util.Scanner;
  475. import java.util.ArrayList;
  476.  
  477. public class readWrite {
  478.     private Scanner y;
  479.     protected ArrayList<String> user = new ArrayList<String>();
  480.     protected ArrayList<String> pass = new ArrayList<String>();
  481.     protected ArrayList<String> userOrder = new ArrayList<String>();
  482.     protected ArrayList<String> shipStatus = new ArrayList<String>();
  483.     protected ArrayList<String> orderStatus = new ArrayList<String>();
  484.     protected ArrayList<Integer> item1 = new ArrayList<Integer>();
  485.     protected ArrayList<Integer> item2 = new ArrayList<Integer>();
  486.     protected ArrayList<Integer> item3 = new ArrayList<Integer>();
  487.     File file;
  488.     PrintWriter pw;
  489.    
  490.     protected void openWrite(String fileName){
  491.         try {
  492.             file = new File(fileName);
  493.             if(!file.exists()){
  494.                 file.createNewFile();
  495.             }
  496.         }
  497.         catch (Exception e) {
  498.             System.out.println("Failed to create/open file " + e);
  499.         }
  500.        
  501.     }
  502.    
  503.     protected void writeDat(){
  504.         try {
  505.             pw = new PrintWriter(file);
  506.         }
  507.         catch (FileNotFoundException e) {
  508.             System.out.println("Failed to write file " + e);
  509.         }
  510.         for (int i = 0; i < this.user.size(); i++) {
  511.             pw.println(this.user.get(i));
  512.             pw.println(this.pass.get(i));
  513.         }
  514.     }
  515.    
  516.     protected void writeOrder(){
  517.         try{
  518.             pw = new PrintWriter(file);
  519.         }
  520.         catch (Exception e){
  521.             System.out.println("Failed to write file " + e);
  522.         }
  523.         for (int i = 0; i < this.userOrder.size(); i++) {
  524.             pw.println(this.userOrder.get(i));
  525.             pw.println(this.orderStatus.get(i));
  526.             pw.println(this.shipStatus.get(i));
  527.             pw.println(this.item1.get(i));
  528.             pw.println(this.item2.get(i));
  529.             pw.println(this.item3.get(i));
  530.         }
  531.     }
  532.    
  533.     protected void closeWrite(){
  534.         pw.close();
  535.     }
  536.    
  537.     protected void openRead(String fileName){
  538.         try {
  539.             y = new Scanner(new File(fileName));
  540.         }
  541.         catch (Exception e) {
  542.             System.out.println(e);
  543.         }
  544.     }
  545.    
  546.     protected void readDat(){
  547.         while(y.hasNext()){
  548.             this.user.add(y.next());
  549.             this.pass.add(y.next());
  550.         }
  551.     }
  552.    
  553.     protected void readStatus(){
  554.         while(y.hasNext()){
  555.             this.userOrder.add(y.next());
  556.             this.orderStatus.add(y.next());
  557.             this.shipStatus.add(y.next());
  558.             this.item1.add(Integer.parseInt(y.next()));
  559.             this.item2.add(Integer.parseInt(y.next()));
  560.             this.item3.add(Integer.parseInt(y.next()));
  561.         }
  562.     }
  563.    
  564.     protected void display(){
  565.         for (int i = 0; i < this.user.size(); i++) {
  566.             System.out.println(this.user.get(i));
  567.             System.out.println(this.pass.get(i));
  568.         }
  569.     }
  570.    
  571.     protected void closeRead(){
  572.         y.close();
  573.     }
  574. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement