Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.stream.Stream;
- public class freestyler {
- public static void main(String[] args) {
- String[] auth = authorization.authorize();
- if (auth[1].equals("true")) {
- int action = 0;
- if (!auth[0].equals("admin")) {
- Object[] options = { "Create New", "Show New", "Show confirmed", "Show shipped" };
- action = JOptionPane.showOptionDialog(null, "What you want to do with orders?", "Select",
- JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
- null, options, options[0]);
- }
- switch (auth[0]){ // Control Structure
- case "user1" :
- user user1 = new user("user1"); //Object reference
- if (action == 0) {
- user1.newOrder();
- } else {
- order.getOrders("user1", action);
- }
- break;
- case "user2" :
- user user2 = new user("user2");
- if (action == 0) {
- user2.newOrder();
- } else {
- order.getOrders("user2", action);
- }
- break;
- case "user3" :
- user user3 = new user("user3");
- if (action == 0) {
- user3.newOrder();
- } else {
- order.getOrders("user3", action);
- }
- break;
- default:
- admin admin = new admin ();
- Object[] tasks = {"Show New", "Approve", "Ship", "Last admin" };
- String last_user = System.getProperty("user.name");
- int todo = JOptionPane.showOptionDialog(null, "What you want to do with orders?", "Select",
- JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
- null, tasks, tasks[0]);
- switch (todo) {
- case 1:
- admin.approveOrder(last_user);
- break;
- case 2:
- admin.shipOrder(last_user);
- break;
- case 3:
- System.out.println("Last accessed by: " + admin.last_admin());
- break;
- default:
- order.getOrders("admin", todo + 1);
- }
- }
- } else {
- System.exit(0);
- }
- }
- }
- class admin {
- private String last_access = "James";
- static Scanner admin_input = new Scanner(System.in);
- String last_admin() { // Accessor
- return this.last_access;
- }
- void approveOrder(String a) { // Mutator
- this.last_access = a;
- System.out.printf("Enter number of order for approval: ");
- String number = "#" + admin_input.next();
- order.editOrders(number, 0);
- }
- void shipOrder(String a) {
- this.last_access = a;
- System.out.printf("Enter number of order for shipping: ");
- String number = "#" + admin_input.next();
- order.editOrders(number, 1);
- }
- }
- class user {
- private String objName;
- public user(String objN) {
- this.objName = objN;
- } // Constructor
- String[] selectItem() {
- double price = 10 + Math.random() * 140; // Math 1
- Object[] categories = { "Men", "Women", "Children" }; // Array
- Object category = JOptionPane.showOptionDialog(null,
- "Select category", "Category",
- JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
- categories, categories[0]);
- Object[] types = { "Top", "Pants", "Skirts", "Dress", "Others"};
- Object type = JOptionPane.showOptionDialog(null,
- "Select type", "Type",
- JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
- types, types[0]);
- Object[] colors = { "White", "Black", "Gray", "Blue", "Red", "Green", "Yellow"};
- Object color = JOptionPane.showOptionDialog(null,
- "Select color", "Color",
- JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
- colors, colors[0]);
- Object[] sizes = {"S", "M", "L", "XL"};
- Object size = JOptionPane.showOptionDialog(null,
- "Select size", "Size",
- JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
- sizes, sizes[0]);
- String[] item = {category.toString(), type.toString(), color.toString(), size.toString(), Double.toString(Math.round(price*100.000)/100.000)}; // String 1 + Math 2
- return item;
- }
- ArrayList<String[]> order() {
- ArrayList<String[]> item = new ArrayList<>();
- item.add(selectItem());
- while (true) {
- if (JOptionPane.showConfirmDialog(null,
- "Order more items?", "More items", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
- item.add(selectItem());
- } else {
- break;
- }
- }
- return item;
- }
- ArrayList<String[]> final_order () {
- System.out.println("Start your order");
- System.out.println("You have ordered: ");
- int i = 0;
- ArrayList<String[]> toReturn = order();
- for (String[] orderList: toReturn) {
- i++;
- System.out.printf("%d. ", i);
- for (String orderItem : orderList) {
- System.out.printf("%s ", orderItem);
- }
- System.out.printf("\n");
- }
- if (i >= 10) {
- System.out.println("Enjoy your 10% discount for bulk order");
- }
- return toReturn;
- }
- void payment() {
- double total = 0;
- int i = 0;
- ArrayList<String[]> toProceed = final_order();
- for (String[] orderList: toProceed) {
- i++;
- double price = Double.parseDouble(orderList[orderList.length-1]);
- total += price;
- }
- if (i >= 10) {
- total = total / 100.000 * 90.000;
- }
- System.out.println("Total price: " + total);
- if (JOptionPane.showConfirmDialog(null,
- "Proceed with payment?", "Payment", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
- System.out.println("Order sent to approval");
- order.createNew(objName, toProceed, total);
- } else {
- System.out.println("Order cancelled");
- System.exit(0);
- }
- }
- void newOrder() {
- payment();
- }
- }
- class order {
- static Random rand = new Random();
- static StringBuilder strBuilder = new StringBuilder();
- static Boolean createNew (String user, ArrayList<String[]> items, double price) {
- int i = rand.nextInt(999999999) + 100000000;
- float float_price = (float) price; //Typecasting
- try (Writer writer = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream("database.txt", true), "utf-8"))) {
- writer.write("#" + i + " " + user + " " + float_price + " 1" + " 0" + " 0" + System.lineSeparator());
- for (String[] order: items) {
- writer.write("$ ");
- for (String curItem: order) {
- writer.write(curItem + " ");
- }
- writer.write(System.lineSeparator());
- }
- }catch(Exception ex) {
- System.out.println("Something Wrong");
- System.exit(0);
- }
- return true;
- }
- static Boolean getOrders(String user, Integer k) {
- ArrayList<String> txt = new ArrayList<>();
- Path path = Paths.get("database.txt");
- try (Stream<String> lines = Files.lines(path)) {
- lines.forEach(s -> txt.add(s));
- }catch(IOException ex) {
- System.out.println("Something Wrong");
- System.exit(0);
- }
- switch (k) {
- case 1:
- System.out.println("Your new orders: ");
- break;
- case 2:
- System.out.println("Your confirmed orders: ");
- break;
- case 3:
- System.out.println("Your shipped orders: ");
- break;
- }
- for (String curStr : txt) {
- String firstChar = curStr.substring(0, 1);
- if (firstChar.equals("#")) {
- String[] splited = curStr.split("\\s+"); // String 2
- if (splited[1].equals(user) || user.equals("admin")) {
- if (splited[k+2].equals("1")) {
- System.out.println(curStr);
- }
- }
- }
- }
- return true;
- }
- static Boolean editOrders(String number, Integer k) {
- ArrayList<String> txt = new ArrayList<>();
- ArrayList<String> newtxt = new ArrayList<>();
- String temp;
- Path path = Paths.get("database.txt");
- try (Stream<String> lines = Files.lines(path)) {
- lines.forEach(s -> txt.add(s));
- }catch(IOException ex) {
- System.out.println("Something Wrong");
- System.exit(0);
- }
- for (String curStr : txt) {
- temp = curStr;
- String firstChar = curStr.substring(0, 1);
- if (firstChar.equals("#")) {
- String[] splited = curStr.split("\\s+");
- if (splited[0].equals(number)) {
- if (splited[k + 4].equals("0")) {
- splited[k + 4] = "1";
- splited[3] = "0";
- for (String updatedStr : splited) {
- strBuilder.append(updatedStr).append(" ");
- }
- String newUpdatedStr = strBuilder.toString();
- temp = newUpdatedStr;
- } else {
- System.out.println("Something Wrong");
- System.exit(0);
- }
- }
- }
- newtxt.add(temp);
- }
- try (Writer writer = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream("database.txt", false), "utf-8"))) {
- for (String curStr : newtxt) {
- writer.write(curStr);
- writer.write(System.lineSeparator());
- }
- }catch(Exception ex) {
- System.out.println("Something Wrong");
- System.exit(0);
- }
- return true;
- }
- }
- class authorization {
- static Scanner user_input = new Scanner(System.in);
- static private String[] login = {"user1", "user2", "user3", "admin"};
- static private String[] password = {"password1", "password2", "password3", "password"};
- static String[] authorize (){
- Boolean authorized = false;
- System.out.printf("Please enter your username: ");
- String uLogin = user_input.next();
- System.out.printf("Please enter your password: ");
- String uPassword = user_input.next();
- for (int i = 0; i <= login.length-1; i++) {
- if (uLogin.equals(login[i]) && uPassword.equals(password[i])) {
- authorized = true;
- }
- }
- if (authorized) {
- System.out.println("Authorization completed");
- } else {
- System.out.println("Authorization failed");
- System.exit(0);
- }
- String[] toReturn = {uLogin, String.valueOf(authorized)};
- return toReturn;
- }
- }
- /* README
- Credentials:
- admin:password
- user1:password1
- user2:password2
- user3:password3
- Basic Logic:
- 1. User authorized --> Order Created (User able to see list of new orders belongs to him)
- 2. Admin authorized --> Order Approved (User able to see list of confirmed orders belongs to him)
- 3. Admin authorized --> Order Shipped (User able to see list of shipped orders belongs to him)
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement