weiqian1020

FreeStyler Online Aparrels 2

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