Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class core{
- private static Scanner input;
- private String account;
- private ArrayList<String> username = new ArrayList<String>();
- private ArrayList<String> password = new ArrayList<String>();
- private ArrayList<String> userOrder = new ArrayList<String>();
- private ArrayList<String> shipStatus = new ArrayList<String>();
- private ArrayList<String> orderStatus = new ArrayList<String>();
- private ArrayList<Top> top = new ArrayList<Top>();
- private ArrayList<Pant> pant = new ArrayList<Pant>();
- private ArrayList<Dress> dress = new ArrayList<Dress>();
- private ArrayList<Skirt> skirt = new ArrayList<Skirt>();
- Top tempT = new Top();
- Pant tempP = new Pant();
- Dress tempD = new Dress();
- Skirt tempS = new Skirt();
- public static void main(String[] args) {
- int x = 0;
- boolean bool = true;
- core run = new core();
- input = new Scanner(System.in);
- do{
- switch (x) {
- case 0:
- x = run.mainMenu();
- break;
- case 1:
- x = run.admin();
- break;
- case 2:
- x = run.user();
- break;
- case 3:
- x = run.userLogin();
- break;
- case 4:
- x = run.userReg();
- break;
- case 5:
- x = run.userMenu();
- break;
- case 6:
- x = run.userShop();
- break;
- case 7:
- x = run.userStatus();
- break;
- default:
- bool = false;
- System.out.println(x);
- break;
- }
- } while (bool);
- }
- private int mainMenu(){
- System.out.println("Welcome to FreeStyler Online Apparels");
- System.out.println("Login As:\n(1)Admin\n(2)User");
- boolean bool = true;
- String n;
- do {
- try{
- n = input.nextLine();
- } catch (Exception e) {
- System.out.println("Invalid input, " + e);
- System.out.println("Login As:\n(1)Admin\n(2)User");
- continue;
- }
- if (n.equals("1")){
- bool = false;
- return 1;
- }
- else if (n.equals("2")){
- bool = false;
- return 2;
- }
- else {
- System.out.println("Invalid input, please try again");
- System.out.println("Login As:\n(1)Admin\n(2)User");
- continue;
- }
- }while(bool);
- return 0;
- }
- private int admin(){
- boolean bool = true;
- String user, pass = null;
- do {
- System.out.println("-1 to back");
- System.out.print("Username: ");
- try {
- user = input.nextLine();
- } catch (Exception e){
- System.out.println("Error: " + e);
- continue;
- }
- if (backtrack(user)){
- bool = false;
- return 0;
- }
- System.out.print("Password: ");
- try{
- pass = input.nextLine();
- } catch (Exception e){
- System.out.println("Error: " + e);
- }
- if (backtrack(pass)){
- bool = false;
- return 0;
- }
- else if (user.equals("admin") && pass.equals("admin")){
- System.out.println("Login Successfully");
- bool = false;
- return 10;
- }
- else {
- System.out.println("Invalid username or password");
- }
- }while (bool);
- return 0;
- }
- private int user(){
- String x;
- boolean bool;
- do {
- bool = false;
- System.out.println("-1 to back");
- System.out.println("(1)Login\n(2)Register");
- try{
- x = input.nextLine();
- }
- catch (Exception e){
- System.out.println("Invalid input, " + e);
- continue;
- }
- if (x.equals("1")){
- return 3;
- }
- else if (x.equals("2")){
- return 4;
- }
- else if (x.equals("-1")){
- return 0;
- }
- else {
- System.out.println("Invalid input, please try again");
- bool = true;
- continue;
- }
- }while(bool);
- return 0;
- }
- private int userLogin(){
- boolean bool = true;
- String user, pass = null;
- loadDat();
- do{
- System.out.println("-1 to back");
- System.out.print("Username: ");
- try {
- user = input.nextLine();
- } catch (Exception e){
- System.out.println("Error: " + e);
- continue;
- }
- if (backtrack(user)){
- bool = false;
- return 2;
- }
- System.out.print("Password: ");
- try{
- pass = input.nextLine();
- } catch (Exception e){
- System.out.println("Error: " + e);
- }
- if (backtrack(pass)){
- bool = false;
- return 2;
- }
- else {
- for (int i = 0; i < this.username.size(); i++) {
- if (user.equals(this.username.get(i)) && pass.equals(this.password.get(i))){
- System.out.println("Login Successfully");
- System.out.println();
- bool = false;
- account = this.username.get(i);
- return 5;
- }
- }
- System.out.println("Invalid username or password");
- }
- }while(bool);
- return 0;
- }
- private int userReg(){
- String user, pass;
- boolean bool = true;
- boolean bool2 = false;
- boolean taken;
- loadDat();
- System.out.println("-1 to back");
- System.out.println("Register as");
- do{
- taken = false;
- System.out.print("Username: ");
- user = input.nextLine();
- if (backtrack(user)){
- return 2;
- }
- for (int i = 0; i < this.username.size(); i++) {
- if (user.equals(this.username.get(i))){
- System.out.println("Username taken, please take different username");
- taken = true;
- continue;
- }
- }
- if (taken){
- continue;
- }
- else if(user.contains(" ")){
- System.out.println("Username or password cannot contain space");
- continue;
- }
- else {
- do {
- bool2 = false;
- System.out.print("Password: ");
- pass = input.nextLine();
- if (backtrack(pass)){
- return 2;
- }
- else if (pass.contains(" ")){
- System.out.println("Username or password cannot contain space");
- bool2 = true;
- continue;
- }
- else {
- addDat(user, pass);
- System.out.println("Account created successfully");
- bool = false;
- return 2;
- }
- }while(bool2);
- }
- }while(bool);
- return 0;
- }
- private int userMenu(){
- boolean bool = true;
- String x = null;
- System.out.println("Welcome back, " + account);
- do{
- System.out.println("(1)Shop\n(2)Status\n(3)Log Out");
- try {
- x = input.nextLine();
- } catch (Exception e) {
- System.out.println("Invalid input, " + e);
- }
- if (x.equals("1")){
- bool = false;
- return 6;
- }
- else if (x.equals("2")){
- bool = false;
- return 7;
- }
- else if (x.equals("3")){
- bool = false;
- return 0;
- }
- else {
- System.out.println("Invalid input, please try again");
- }
- }while(bool);
- return 0;
- }
- private int userShop(){
- boolean bool, bool2, bool3, checkOut;
- int x, color, size = 0, amount;
- String in;
- String c, s, g, t;
- do {
- checkOut = false;
- System.out.println("-1 to back, clear cart");
- System.out.println("Men\tWomen\n(1)Top\t(3)Dress\n(2)Pant\t(4)Skirt\n(5)View cart\n(6)Checkout");
- try {
- in = input.nextLine();
- x = Integer.parseInt(in);
- }
- catch(Exception e){
- System.out.println("Invalid input, " + e);
- continue;
- }
- if (x >= 1 && x <= 4){
- do{
- bool = true;
- System.out.println("-1 to back");
- System.out.println("Choose a color:");
- System.out.println("(1)White\n(2)Black\n(3)Red\n(4)Green\n(5)Blue");
- try {
- in = input.nextLine();
- color = Integer.parseInt(in);
- }
- catch (Exception e){
- System.out.println("Invalid input, " + e);
- continue;
- }
- if (color >= 1 && color <= 5){
- do {
- bool2 = true;
- System.out.println("-1 to back");
- System.out.println("Choose a size:");
- System.out.println("(1)S\n(2)M\n(3)L");
- try {
- in = input.nextLine();
- size = Integer.parseInt(in);
- }
- catch (Exception e){
- System.out.println("Invalid input, " + e);
- continue;
- }
- if (size >= 1 && size <= 3){
- do {
- bool3 = true;
- System.out.println("-1 to back");
- System.out.print("Amount: ");
- try {
- in = input.nextLine();
- amount = Integer.parseInt(in);
- }
- catch (Exception e){
- System.out.println("Invalid input, " + e);
- continue;
- }
- if (amount < 1){
- System.out.println("Amount cannot less than 1");
- continue;
- }
- else if (backtrack(amount)){
- bool3 = false;
- break;
- }
- else {
- g = gGet(x);
- c = cGet(color);
- s = sGet(size);
- t = tGet(x);
- addCart(t, c, s, amount);
- System.out.printf("%s %s %s (%s) x %d added to cart\n\n", g, t, c, s, amount);
- bool = false;
- bool2 = false;
- bool3 = false;
- }
- } while (bool3);
- }
- else if(backtrack(size)){
- bool2 = false;
- break;
- }
- else {
- System.out.println("Invalid input, please try again");
- }
- }while(bool2);
- }
- else if (backtrack(color)){
- bool = false;
- break;
- }
- else {
- System.out.println("Invalid input, please try again");
- }
- }while(bool);
- }
- else if(x == 5){
- if (tempT.amount >= 1){
- System.out.printf("%s %s %s (%s) x %d\n", tempT.group, tempT.type, tempT.color, tempT.size, tempT.amount);
- }
- if (tempP.amount >= 1){
- System.out.printf("%s %s %s (%s) x %d\n", tempP.group, tempP.type, tempP.color, tempP.size, tempP.amount);
- }
- if (tempD.amount >= 1){
- System.out.printf("%s %s %s (%s) x %d\n", tempD.group, tempD.type, tempD.color, tempD.size, tempD.amount);
- }
- if (tempS.amount >= 1){
- System.out.printf("%s %s %s (%s) x %d\n", tempS.group, tempS.type, tempS.color, tempS.size, tempS.amount);
- }
- System.out.println();
- System.out.println("Press Enter to continue\n");
- input.nextLine();
- }
- else if(x == 6){
- addOrder(account);
- checkOut = true;
- loadOrder();
- System.out.println("Thanks\nYour order number is #" + userOrder.size() + "\nPlease come again!\n");
- return 5;
- }
- else if(backtrack(x)){
- tempT.amount = 0;
- tempP.amount = 0;
- tempD.amount = 0;
- tempS.amount = 0;
- return 5;
- }
- else {
- System.out.println("Invalid input, try again");
- }
- }while(!checkOut);
- return 0;
- }
- private int userStatus(){
- loadOrder();
- boolean order = false;
- for (int i = 0; i < this.userOrder.size(); i++) {
- if (this.userOrder.get(i).equals(account)){
- System.out.println();
- order = true;
- System.out.println("Order #" + i);
- if(((Top)top.get(i)).amount > 0){
- System.out.printf("%s %s %s (%s) x %d\n", ((Top)top.get(i)).group, ((Top)top.get(i)).type, ((Top)top.get(i)).color, ((Top)top.get(i)).size, ((Top)top.get(i)).amount);
- }
- if(((Pant)pant.get(i)).amount > 0){
- System.out.printf("%s %s %s (%s) x %d\n", ((Pant)pant.get(i)).group, ((Pant)pant.get(i)).type, ((Pant)pant.get(i)).color, ((Pant)pant.get(i)).size, ((Pant)pant.get(i)).amount);
- }
- if(((Dress)dress.get(i)).amount > 0){
- System.out.printf("%s %s %s (%s) x %d\n", ((Dress)dress.get(i)).group, ((Dress)dress.get(i)).type, ((Dress)dress.get(i)).color, ((Dress)dress.get(i)).size, ((Dress)dress.get(i)).amount);
- }
- if(((Skirt)skirt.get(i)).amount > 0){
- System.out.printf("%s %s %s (%s) x %d\n", ((Skirt)skirt.get(i)).group, ((Skirt)skirt.get(i)).type, ((Skirt)skirt.get(i)).color, ((Skirt)skirt.get(i)).size, ((Skirt)skirt.get(i)).amount);
- }
- System.out.println("Order confirm: " + orderStatus.get(i));
- System.out.println("Shipping confirm: " + shipStatus.get(i));
- }
- }
- if (!order){
- System.out.println();
- System.out.println("Empty, you did not order anything in the past");
- }
- System.out.println("\nPress Enter to continue");
- input.nextLine();
- return 5;
- }
- private void loadDat(){
- readWrite read = new readWrite();
- read.openWrite("login.txt");
- read.openRead("login.txt");
- read.readDat();
- username.clear();
- password.clear();
- for (int i = 0; i < read.user.size(); i++) {
- this.username.add(read.user.get(i));
- this.password.add(read.pass.get(i));
- }
- read.closeRead();
- }
- private void addDat(String user, String pass){
- readWrite write = new readWrite();
- write.openWrite("login.txt");
- write.openRead("login.txt");
- write.readDat();
- write.closeRead();
- write.user.add(user);
- write.pass.add(pass);
- write.openWrite("login.txt");
- write.writeDat();
- write.closeWrite();
- }
- private void loadOrder(){
- readWrite readOrder = new readWrite();
- readOrder.openWrite("order.txt");
- readOrder.openRead("order.txt");
- readOrder.readStatus();
- userOrder.clear();
- orderStatus.clear();
- shipStatus.clear();
- top.clear();
- pant.clear();
- dress.clear();
- skirt.clear();
- for (int i = 0; i < readOrder.userOrder.size(); i++) {
- this.userOrder.add(readOrder.userOrder.get(i));
- this.orderStatus.add(readOrder.orderStatus.get(i));
- this.shipStatus.add(readOrder.shipStatus.get(i));
- this.top.add(readOrder.top.get(i));
- this.pant.add(readOrder.pant.get(i));
- this.dress.add(readOrder.dress.get(i));
- this.skirt.add(readOrder.skirt.get(i));
- }
- }
- private void addOrder(String user){
- readWrite write = new readWrite();
- write.openWrite("order.txt");
- write.openRead("order.txt");
- write.readStatus();
- write.closeRead();
- write.userOrder.add(user);
- write.orderStatus.add("Pending");
- write.shipStatus.add("Pending");
- write.top.add(tempT);
- write.pant.add(tempP);
- write.dress.add(tempD);
- write.skirt.add(tempS);
- write.openWrite("order.txt");
- write.writeOrder();
- write.closeWrite();
- }
- private boolean backtrack(String back){
- if (back.equals("-1")){
- return true;
- }
- else {
- return false;
- }
- }
- private boolean backtrack(int back){
- if (back == -1){
- return true;
- }
- else {
- return false;
- }
- }
- private String gGet(int x){
- if (x == 1 || x == 2){
- return "Men";
- }
- else if (x == 3 || x == 4){
- return "Women";
- }
- else {
- return "Invalid";
- }
- }
- private String tGet(int x){
- if (x == 1){
- return "Top";
- }
- else if (x == 2){
- return "Pant";
- }
- else if (x == 3){
- return "Dress";
- }
- else if (x == 4){
- return "Skirt";
- }
- else {
- return "Invalid";
- }
- }
- private String cGet(int x){
- if (x == 1){
- return "White";
- }
- else if (x == 2){
- return "Black";
- }
- else if (x == 3){
- return "Red";
- }
- else if (x == 4){
- return "Green";
- }
- else if (x == 5){
- return "Blue";
- }
- else {
- return "Invalid";
- }
- }
- private String sGet(int x){
- if (x == 1){
- return "S";
- }
- else if (x == 2){
- return "M";
- }
- else if (x == 3){
- return "L";
- }
- else {
- return "Invalid";
- }
- }
- private void addCart(String t, String c, String s, int amount){
- if (t.equals("Top")){
- tempT.amount = amount;
- tempT.color = c;
- tempT.size = s;
- tempT.group = "Men";
- }
- else if (t.equals("Pant")){
- tempP.amount = amount;
- tempP.color = c;
- tempP.size = s;
- tempP.group = "Men";
- }
- else if (t.equals("Dress")){
- tempD.amount = amount;
- tempD.color = c;
- tempD.size = s;
- tempD.group = "Women";
- }
- else if (t.equals("Skirt")){
- tempS.amount = amount;
- tempS.color = c;
- tempS.size = s;
- tempS.group = "Women";
- }
- }
- }
- package assignment;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class readWrite {
- private Scanner y;
- protected ArrayList<String> user = new ArrayList<String>();
- protected ArrayList<String> pass = new ArrayList<String>();
- protected ArrayList<String> userOrder = new ArrayList<String>();
- protected ArrayList<String> shipStatus = new ArrayList<String>();
- protected ArrayList<String> orderStatus = new ArrayList<String>();
- protected ArrayList<Top> top = new ArrayList<Top>();
- protected ArrayList<Pant> pant = new ArrayList<Pant>();
- protected ArrayList<Dress> dress = new ArrayList<Dress>();
- protected ArrayList<Skirt> skirt = new ArrayList<Skirt>();
- File file;
- PrintWriter pw;
- protected void openWrite(String fileName){
- try {
- file = new File(fileName);
- if(!file.exists()){
- file.createNewFile();
- }
- }
- catch (Exception e) {
- System.out.println("Failed to create/open file " + e);
- }
- }
- protected void writeDat(){
- try {
- pw = new PrintWriter(file);
- }
- catch (FileNotFoundException e) {
- System.out.println("Failed to write file " + e);
- }
- for (int i = 0; i < this.user.size(); i++) {
- pw.println(this.user.get(i));
- pw.println(this.pass.get(i));
- }
- }
- protected void writeOrder(){
- try{
- pw = new PrintWriter(file);
- }
- catch (Exception e){
- System.out.println("Failed to write file " + e);
- }
- for (int i = 0; i < this.userOrder.size(); i++) {
- pw.println(this.userOrder.get(i));
- pw.println(this.orderStatus.get(i));
- pw.println(this.shipStatus.get(i));
- pw.println(((Top)top.get(i)).amount + " " + ((Top)top.get(i)).color + " " + ((Top)top.get(i)).size + " " + ((Top)top.get(i)).group);
- pw.println(((Pant)pant.get(i)).amount + " " + ((Pant)pant.get(i)).color + " " + ((Pant)pant.get(i)).size + " " + ((Pant)pant.get(i)).group);
- pw.println(((Dress)dress.get(i)).amount + " " + ((Dress)dress.get(i)).color + " " + ((Dress)dress.get(i)).size + " " + ((Dress)dress.get(i)).group);
- pw.println(((Skirt)skirt.get(i)).amount + " " + ((Skirt)skirt.get(i)).color + " " + ((Skirt)skirt.get(i)).size + " " + ((Skirt)skirt.get(i)).group);
- }
- }
- protected void closeWrite(){
- pw.close();
- }
- protected void openRead(String fileName){
- try {
- y = new Scanner(new File(fileName));
- }
- catch (Exception e) {
- System.out.println(e);
- }
- }
- protected void readDat(){
- while(y.hasNext()){
- this.user.add(y.next());
- this.pass.add(y.next());
- }
- }
- protected void readStatus(){
- Top t = new Top();
- Pant p = new Pant();
- Dress d = new Dress();
- Skirt s = new Skirt();
- while(y.hasNext()){
- this.userOrder.add(y.next());
- this.orderStatus.add(y.next());
- this.shipStatus.add(y.next());
- t.amount = Integer.parseInt(y.next());
- t.color = y.next();
- t.size = y.next();
- t.group = y.next();
- this.top.add(t);
- p.amount = Integer.parseInt(y.next());
- p.color = y.next();
- p.size = y.next();
- p.group = y.next();
- this.pant.add(p);
- d.amount = Integer.parseInt(y.next());
- d.color = y.next();
- d.size = y.next();
- d.group = y.next();
- this.dress.add(d);
- s.amount = Integer.parseInt(y.next());
- s.color = y.next();
- s.size = y.next();
- s.group = y.next();
- this.skirt.add(s);
- }
- }
- protected void display(){
- for (int i = 0; i < this.user.size(); i++) {
- System.out.println(this.user.get(i));
- System.out.println(this.pass.get(i));
- }
- }
- protected void closeRead(){
- y.close();
- }
- }
- package assignment;
- public class Clothing {
- int amount;
- String color, size, group, type;
- }
Advertisement
Add Comment
Please, Sign In to add comment