Advertisement
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<Integer> item1 = new ArrayList<Integer>();
- private ArrayList<Integer> item2 = new ArrayList<Integer>();
- private ArrayList<Integer> item3 = new ArrayList<Integer>();
- public static void main(String[] args) {
- int x = 0;
- boolean bool = true;
- core run = new core();
- //run.readDat();
- input = new Scanner(System.in);
- do{
- switch (x) {
- case 0:
- x = 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();
- bool = false;
- break;
- case 7:
- run.userStatus();
- bool = false;
- break;
- default:
- bool = false;
- System.out.println(x);
- break;
- }
- } while (bool);
- }
- private static 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;
- 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);
- return 2;
- }
- 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");
- 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(){
- int x = 0;
- int n = 0;
- int item[] = {
- 0,
- 0,
- 0,
- };
- boolean bool, bool2, checkOut;
- do {
- checkOut = true;
- do {
- bool = false;
- System.out.println("Item on sale, add order");
- System.out.println("-1 to back (Clear cart)");
- System.out.println("(1)item1\n(2)item2\n(3)item3\n(4)View item in cart\n(5)Check Out");
- System.out.print("Select an item to purchase: ");
- try {
- x = Integer.parseInt(input.nextLine());
- }
- catch (Exception e){
- System.out.println("Invalid input, " + e);
- bool = true;
- continue;
- }
- if (backtrack(x)){
- bool = false;
- return 5;
- }
- else if (x <= 0 || x >= 6){
- System.out.println("Invalid input, please try again\n");
- bool = true;
- }
- else {
- break;
- }
- }while(bool);
- if (x > 0 && x < 4){
- do{
- bool2 = false;
- System.out.println("-1 to cancel");
- System.out.print("Quantity: ");
- try {
- n = Integer.parseInt(input.nextLine());
- }
- catch (Exception e){
- System.out.println("Invalid input, " + e + "\n");
- bool2 = true;
- continue;
- }
- if(n < 1){
- System.out.println("Quantity cannot less than 0");
- bool2 = true;
- continue;
- }
- else {
- System.out.println("item" + x + " (Amount: " + n + ") added to cart" );
- }
- }while(bool2);
- item[x - 1] += n;
- }
- else if (x == 4){
- for (int i = 0; i <= 3; i++) {
- System.out.println("item" + (i + 1) + " (Amount: " + item[i]);
- }
- continue;
- }
- else if (x == 5){
- checkOut = false;
- addOrder(account, "true", "true", item[0], item[1], item[2]);
- loadDat();
- System.out.println("Thank you for purchasing from FreeStyler Online Aparrels, your order number is Order #" + (userOrder.size() - 1));
- }
- }while(checkOut);
- return 5;
- }
- private int userStatus(){
- loadOrder();
- for (int i = 0; i < this.userOrder.size(); i++) {
- if (this.userOrder.get(i).equals(account)){
- System.out.println("Order #" + i);
- System.out.println("item1 x " + item1.get(i));
- System.out.println("item2 x " + item2.get(i));
- System.out.println("item3 x " + item2.get(i));
- System.out.println("Order confirm: " + orderStatus.get(i));
- System.out.println("Shipping confirm: " + shipStatus.get(i));
- System.out.println();
- }
- }
- return 0;
- }
- 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();
- item1.clear();
- item2.clear();
- item3.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.item1.add(readOrder.item1.get(i));
- this.item2.add(readOrder.item2.get(i));
- this.item3.add(readOrder.item3.get(i));
- }
- }
- private void addOrder(String user, String order, String ship, int item1, int item2, int item3){
- readWrite write = new readWrite();
- write.openWrite("order.txt");
- write.openRead("order.txt");
- write.readStatus();
- write.closeRead();
- write.userOrder.add(order);
- write.orderStatus.add(order);
- write.shipStatus.add(ship);
- write.item1.add(item1);
- write.item2.add(item2);
- write.item3.add(item3);
- write.openWrite("order.txt");
- 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;
- }
- }
- }
- package assignment;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- 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<Integer> item1 = new ArrayList<Integer>();
- protected ArrayList<Integer> item2 = new ArrayList<Integer>();
- protected ArrayList<Integer> item3 = new ArrayList<Integer>();
- 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(this.item1.get(i));
- pw.println(this.item2.get(i));
- pw.println(this.item3.get(i));
- }
- }
- 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(){
- while(y.hasNext()){
- this.userOrder.add(y.next());
- this.orderStatus.add(y.next());
- this.shipStatus.add(y.next());
- this.item1.add(Integer.parseInt(y.next()));
- this.item2.add(Integer.parseInt(y.next()));
- this.item3.add(Integer.parseInt(y.next()));
- }
- }
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement