Advertisement
Guest User

Untitled

a guest
Aug 13th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.91 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. import static java.lang.System.out;
  6. import static java.lang.System.err;
  7.  
  8. public class Main {
  9.  
  10.     /**
  11.     main() launches start()
  12.     start() is the main method to allow other methods to return to the start method without having to pass String[] args
  13.      */
  14.     public static void main(String[] args) throws Exception {
  15.  
  16.         start();
  17.     }
  18.  
  19.     /**
  20.     start() controls the initial page of the program
  21.     prompts users to either register a user or login using an existing account
  22.     if there are no users registered it will not attempt to log in
  23.     if the register returns a null value (registration failed) it will not add it to the list of known users
  24.      */
  25.     public static void start() throws Exception {
  26.  
  27.         ArrayList<User> users = new ArrayList<>();
  28.         Scanner scanner = new Scanner(System.in);
  29.  
  30.         while(true) {
  31.  
  32.  
  33.             // print formatted front page
  34.             out.print("+----------------------------+\n" +
  35.                     "| 1. Login                   |\n" +
  36.                     "| 2. Register                |\n" +
  37.                     "+----------------------------+\n" +
  38.                     "\nPlease, select an option: ");
  39.  
  40.             // take in the option given and give it to the switch statement
  41.             int option = 0;
  42.             if (scanner.hasNextInt()) {
  43.                 option = scanner.nextInt();
  44.             }
  45.  
  46.             switch (option) {
  47.                 // pass the users ArrayList to login() validation method
  48.                 // if no users exist abort the operation
  49.                 case 1:
  50.                     System.out.println(users.size());
  51.                     if(users.size() > 0) {
  52.                         login(users);
  53.                     } else {
  54.                         err.println("\nNo users are registered\n");
  55.                     }
  56.                     TimeUnit.MILLISECONDS.sleep(5);
  57.                     break;
  58.                 // initiate register() and create a user out of the parameters passed through the console
  59.                 // add the returned user to the users list, if the user returned is null remove the null value from the list
  60.                 case 2:
  61.                     User user = register();
  62.                     if(user != null) {
  63.                         users.add(user);
  64.                     }
  65.                     System.out.println(users.size());
  66.                     break;
  67.                 default:
  68.                     err.println("\nError: invalid choice\nTry typing a number 1 or 2\n");
  69.                     TimeUnit.MILLISECONDS.sleep(5);
  70.                     break;
  71.             }
  72.         }
  73.     }
  74.  
  75.     /**
  76.     a simple validation method to verify that the user being created has proper credentials
  77.     returns the user created if values passed through console are all valid
  78.     returns null if user fails any validation check
  79.      */
  80.     public static User register() throws Exception {
  81.  
  82.         Scanner scanner = new Scanner(System.in);
  83.  
  84.         System.out.print("\nPlease enter a valid email: ");
  85.         String email = scanner.nextLine();
  86.  
  87.         // If email is valid
  88.         if (email.contains("@") && email.contains(".")) {
  89.             out.print("Please enter your first and last name: ");
  90.             String name = scanner.nextLine();
  91.  
  92.             // If name is valid
  93.             if (name.contains(" ")) {
  94.                 out.print("Please enter a password at least 6 characters long: ");
  95.                 String pwd = scanner.nextLine();
  96.  
  97.                 // If password is valid
  98.                 if (pwd.length() >= 6) {
  99.                     User user = new User(email, name, pwd);
  100.                     out.println(" ");
  101.                     return user;
  102.  
  103.                 // Error messages
  104.                 } else {
  105.                     err.println("\nInvalid password,, must be at least 6 characters long\n");
  106.                     TimeUnit.MILLISECONDS.sleep(5);
  107.                 }
  108.             } else {
  109.                 err.println("\nInvalid name, needs to include a space\n");
  110.                 TimeUnit.MILLISECONDS.sleep(5);
  111.             }
  112.         } else {
  113.             err.println("\nInvalid email, must contain a '@' and '.' character\n");
  114.             TimeUnit.MILLISECONDS.sleep(5);
  115.         }
  116.         return null;
  117.     }
  118.  
  119.     /**
  120.     if the login method is invoked it will prompt the user for their login credentials
  121.     it will then scan through the list of known users to validate that both the email exists and that the password give matches that email's stored password
  122.      */
  123.     public static void login(ArrayList<User> users) throws Exception {
  124.  
  125.         // cust ArrayList stores customers created
  126.         // log ArrayList stores support tickets created
  127.         ArrayList<Customer> cust = new ArrayList<>();
  128.         ArrayList<Support> log = new ArrayList<>();
  129.  
  130.         Scanner scanner = new Scanner(System.in);
  131.  
  132.         // prompt the user for their email and password for their account
  133.         out.println("\nEnter your email: ");
  134.         String email = scanner.nextLine();
  135.         out.print("Enter your password: ");
  136.         String pwd = scanner.nextLine();
  137.  
  138.         /**
  139.         for users in list
  140.         validate that email exists in list
  141.         validate that the password give matches the of the email given
  142.          */
  143.         for(int i = 0; i < users.size(); i++){
  144.             if(users.get(i).email.equals(email)){
  145.                 email = users.get(i).email;
  146.                 for(User user : users){
  147.                     if(user.getPwd().equals(pwd)){
  148.  
  149.                         while (true) {
  150.  
  151.                             // print the second screen
  152.                             out.print("\n+----------------------------+\n" +
  153.                                     "| 1. Add new customer        |\n" +
  154.                                     "| 2. Remove customer         |\n" +
  155.                                     "| 3. List all customers      |\n" +
  156.                                     "| 4. Add support visit log   |\n" +
  157.                                     "| 5. View stats              |\n" +
  158.                                     "| 0. Logout                  |\n" +
  159.                                     "+----------------------------+\n" +
  160.                                     "\nPlease, select an option: ");
  161.  
  162.                             // take in the option given and give it to the switch statement
  163.                             int option = 0;
  164.                             if (scanner.hasNextInt()) {
  165.                                 option = scanner.nextInt();
  166.                             }
  167.  
  168.                             switch (option) {
  169.                                 case 0: // return to login start() / login screen
  170.                                     System.out.println("\n[Logging out]\n");
  171.                                     start();
  172.                                 case 1: // branch to add() to take in customer credentials and store them in the cust ArrayList
  173.                                     Customer customer = add();
  174.                                     cust.add(customer);
  175.                                     break;
  176.                                 case 2: // branch to remove() to remove a given customer from cust ArrayList
  177.                                     remove(cust);
  178.                                     break;
  179.                                 case 3: // branch to list() to list all users stored in the cust ArrayList and prints their information
  180.                                     list(cust);
  181.                                     break;
  182.                                 case 4: // branch to support() to add a new support ticket and stores the ticket in the log ArrayList
  183.                                     Support support = support();
  184.                                     log.add(support);
  185.                                     break;
  186.                                 case 5: // branch to view() and prints stats about the total number of hours spent on support and the user that has spent the most (money)
  187.                                     view(log, cust);
  188.                                     break;
  189.                                 default:
  190.                                     err.println("\nError: invalid choice\nTry typing a number 1 or 2\n");
  191.                                     TimeUnit.MILLISECONDS.sleep(5);
  192.                                     break;
  193.                                 }
  194.                             }
  195.                         } else {
  196.                         // if password given is invalid
  197.                         err.println("\nPassword is incorrect\n");
  198.                         TimeUnit.MILLISECONDS.sleep(5);
  199.                     }
  200.                     }
  201.                 } else {
  202.                 // if email given is invalid
  203.                 System.out.println(users.get(i).email);
  204.                 err.println("\nEmail " + email + " does not exist\n");
  205.                 TimeUnit.MILLISECONDS.sleep(5);
  206.             }
  207.             }
  208.         }
  209.  
  210.     /**
  211.     collects information about a customer
  212.     creates a customer object based on parameters passed through command line
  213.     returns created customer to be added to cust ArrayList
  214.      */
  215.     private static Customer add() {
  216.  
  217.         // take input
  218.         Scanner scanner = new Scanner(System.in);
  219.         out.print("\n[Add a new customer]\n\nEnter the customer ID: ");
  220.         String id = scanner.nextLine();
  221.         out.print("Enter the customer name: ");
  222.         String name = scanner.nextLine();
  223.         out.print("Enter the customer email: ");
  224.         String email = scanner.nextLine();
  225.         out.print("Enter the customer phone: ");
  226.         String phone = scanner.nextLine();
  227.         out.print("Enter the address latitude: ");
  228.         String latitude = scanner.nextLine();
  229.         out.print("Enter the address longitude: ");
  230.         String longitude = scanner.nextLine();
  231.         out.println(" ");
  232.  
  233.         // create customer
  234.         Customer customer = new Customer(id, name, email, phone, latitude, longitude);
  235.  
  236.         return customer;
  237.     }
  238.  
  239.     /**
  240.     prompts the user for an id of a customer to remove from the cust ArrayList
  241.     if the id given exists in the cust ArrayList, remove it from the list
  242.      */
  243.     private static void remove(ArrayList<Customer> cust) {
  244.  
  245.         String id;
  246.  
  247.         // prompt user for the ID of the customer to remove
  248.         Scanner scanner = new Scanner(System.in);
  249.         out.print("\nEnter an ID to remove: ");
  250.         id = scanner.nextLine();
  251.  
  252.         // for each customer in the cust ArrayList
  253.         // if the id given equals the customer's id, remove that customer from the list
  254.         for(int i = 0; i < cust.size(); i++){
  255.             if(id == cust.get(i).id){
  256.                 cust.remove(i);
  257.             }
  258.         }
  259.     }
  260.  
  261.     /**
  262.     lists the information about every customer in the cust ArrayList
  263.      */
  264.     private static void list(ArrayList<Customer> cust) {
  265.  
  266.         System.out.println("\n[List all customers]");
  267.  
  268.         // for every customer in cust ArrayList, get that customer's credentials and print them
  269.         for (int i = 0; i < cust.size(); i++) {
  270.             out.println("\nCustomer ID: " + cust.get(i).id);
  271.             out.println("Name: " + cust.get(i).name);
  272.             out.println("Email: " + cust.get(i).email);
  273.             out.println("Phone: " + cust.get(i).phone);
  274.  
  275.             // format latitude and longitude to be a google maps link
  276.             out.println("Google maps link: https://www.google.com/maps/?q=" + cust.get(i).latitude + "," + cust.get(i).longitude + "\n");
  277.         }
  278.     }
  279.  
  280.     /**
  281.     prompts user for input to create a new support ticket
  282.      */
  283.     private static Support support() {
  284.  
  285.         // collect informatino about the support ticket
  286.         Scanner scanner = new Scanner(System.in);
  287.         out.print("\n[Add a new support visit]\n" +
  288.                 "\nEnter the customer ID of the support: ");
  289.         String id = scanner.nextLine();
  290.         out.print("Enter the day: ");
  291.         String day = scanner.nextLine();
  292.         out.print("Enter the month: ");
  293.         String month = scanner.nextLine();
  294.         out.print("Enter the year: ");
  295.         String year = scanner.nextLine();
  296.         out.print("Enter the amount of support hours: ");
  297.         int hours = scanner.nextInt();
  298.         out.print("Enter the amount paid: ");
  299.  
  300.  
  301.         /** For whatever reason the program will not run the scanner.nextLine() unless there are both of them
  302.             So here's a piece of code that makes it work but I have no idea why this is happening */
  303.         String weirdBud = scanner.nextLine();
  304.  
  305.  
  306.         int paid = scanner.nextInt();
  307.         out.println("\nNew support record added!");
  308.  
  309.         // create a new support ticket based on information given in command line
  310.         Support support = new Support(id, day, month, year, hours, paid);
  311.  
  312.         // returns support ticket instantiated to be added to the log ArrayList
  313.         return support;
  314.     }
  315.  
  316.     /**
  317.     prints the total number of hours in all support tickets
  318.     prints the ID and name of the customer that has spent the most (money)
  319.      */
  320.     private static void view(ArrayList<Support> log, ArrayList<Customer> cust) {
  321.  
  322.         int time = 0;
  323.         int count = 0;
  324.         String id = "";
  325.         String name = "";
  326.  
  327.         // for every support ticket create
  328.         // add the sum of the time specified in each ticket
  329.         // if support ticket being tested paid is larger than stored paid value (count) get the customer id on that ticket
  330.         // for every customer in the cust ArrayList
  331.         // check if the ID on the ticket is assigned to any customer in the cust ArrayList
  332.         // if it is, set ID equal to the ID of that customer and name equal to the name of the customer associated with that ID
  333.         for(int i = 0; i < log.size(); i++){
  334.             time = time + log.get(i).hours;
  335.             if(log.get(i).paid > count){
  336.                 count = log.get(i).paid;
  337.                 id = log.get(i).id;
  338.                 for(Customer customer : cust){
  339.                     if(customer.getId().equals(id)){
  340.                         name = cust.get(i).name;
  341.                     }
  342.                 }
  343.             }
  344.         }
  345.  
  346.         // print stats determined about the support ticket
  347.         out.println("\n[View stats of support logs]\n" +
  348.                 "Total number of hours spent on support: " + time +"\n\n" +
  349.                 "Customer that has spent the most (money) on support:\nID: " + id + "\n" +
  350.                 "Name: " + name);
  351.     }
  352. }
  353.  
  354. /**
  355. support class stores information about support tickets created
  356.  */
  357. class Support {
  358.  
  359.     String id;
  360.     String day;
  361.     String month;
  362.     String year;
  363.     int hours;
  364.     int paid;
  365.  
  366.     public Support(String id, String day, String month, String year, int hours, int paid){
  367.         this.id = id;
  368.         this.day = day;
  369.         this.month = month;
  370.         this.year = year;
  371.         this.hours = hours;
  372.         this.paid = paid;
  373.     }
  374. }
  375.  
  376. /**
  377. user class stores information about users created for initial login screen
  378.  */
  379. class User {
  380.  
  381.     String email;
  382.     String name;
  383.     String pwd;
  384.  
  385.     public User(String email, String name, String pwd){
  386.         this.email = email;
  387.         this.name = name;
  388.         this.pwd = pwd;
  389.     }
  390.  
  391.     public String getPwd() {
  392.         return pwd;
  393.     }
  394. }
  395.  
  396. /**
  397. customer class stores information about customers to be stored in the cust ArrayList
  398.  */
  399. class Customer {
  400.  
  401.     String id;
  402.     String name;
  403.     String email;
  404.     String phone;
  405.     String latitude;
  406.     String longitude;
  407.  
  408.     public Customer(String id, String name, String email, String phone, String latitude, String longitude){
  409.         this.id = id;
  410.         this.name = name;
  411.         this.email = email;
  412.         this.phone = phone;
  413.         this.latitude = latitude;
  414.         this.longitude = longitude;
  415.     }
  416.  
  417.     public String getId() {
  418.         return id;
  419.     }
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement