Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.concurrent.TimeUnit;
- import static java.lang.System.out;
- import static java.lang.System.err;
- public class Main {
- /**
- main() launches start()
- start() is the main method to allow other methods to return to the start method without having to pass String[] args
- */
- public static void main(String[] args) throws Exception {
- start();
- }
- /**
- start() controls the initial page of the program
- prompts users to either register a user or login using an existing account
- if there are no users registered it will not attempt to log in
- if the register returns a null value (registration failed) it will not add it to the list of known users
- */
- public static void start() throws Exception {
- ArrayList<User> users = new ArrayList<>();
- Scanner scanner = new Scanner(System.in);
- while(true) {
- // print formatted front page
- out.print("+----------------------------+\n" +
- "| 1. Login |\n" +
- "| 2. Register |\n" +
- "+----------------------------+\n" +
- "\nPlease, select an option: ");
- // take in the option given and give it to the switch statement
- int option = 0;
- if (scanner.hasNextInt()) {
- option = scanner.nextInt();
- }
- switch (option) {
- // pass the users ArrayList to login() validation method
- // if no users exist abort the operation
- case 1:
- System.out.println(users.size());
- if(users.size() > 0) {
- login(users);
- } else {
- err.println("\nNo users are registered\n");
- }
- TimeUnit.MILLISECONDS.sleep(5);
- break;
- // initiate register() and create a user out of the parameters passed through the console
- // add the returned user to the users list, if the user returned is null remove the null value from the list
- case 2:
- User user = register();
- if(user != null) {
- users.add(user);
- }
- System.out.println(users.size());
- break;
- default:
- err.println("\nError: invalid choice\nTry typing a number 1 or 2\n");
- TimeUnit.MILLISECONDS.sleep(5);
- break;
- }
- }
- }
- /**
- a simple validation method to verify that the user being created has proper credentials
- returns the user created if values passed through console are all valid
- returns null if user fails any validation check
- */
- public static User register() throws Exception {
- Scanner scanner = new Scanner(System.in);
- System.out.print("\nPlease enter a valid email: ");
- String email = scanner.nextLine();
- // If email is valid
- if (email.contains("@") && email.contains(".")) {
- out.print("Please enter your first and last name: ");
- String name = scanner.nextLine();
- // If name is valid
- if (name.contains(" ")) {
- out.print("Please enter a password at least 6 characters long: ");
- String pwd = scanner.nextLine();
- // If password is valid
- if (pwd.length() >= 6) {
- User user = new User(email, name, pwd);
- out.println(" ");
- return user;
- // Error messages
- } else {
- err.println("\nInvalid password,, must be at least 6 characters long\n");
- TimeUnit.MILLISECONDS.sleep(5);
- }
- } else {
- err.println("\nInvalid name, needs to include a space\n");
- TimeUnit.MILLISECONDS.sleep(5);
- }
- } else {
- err.println("\nInvalid email, must contain a '@' and '.' character\n");
- TimeUnit.MILLISECONDS.sleep(5);
- }
- return null;
- }
- /**
- if the login method is invoked it will prompt the user for their login credentials
- 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
- */
- public static void login(ArrayList<User> users) throws Exception {
- // cust ArrayList stores customers created
- // log ArrayList stores support tickets created
- ArrayList<Customer> cust = new ArrayList<>();
- ArrayList<Support> log = new ArrayList<>();
- Scanner scanner = new Scanner(System.in);
- // prompt the user for their email and password for their account
- out.println("\nEnter your email: ");
- String email = scanner.nextLine();
- out.print("Enter your password: ");
- String pwd = scanner.nextLine();
- /**
- for users in list
- validate that email exists in list
- validate that the password give matches the of the email given
- */
- for(int i = 0; i < users.size(); i++){
- if(users.get(i).email.equals(email)){
- email = users.get(i).email;
- for(User user : users){
- if(user.getPwd().equals(pwd)){
- while (true) {
- // print the second screen
- out.print("\n+----------------------------+\n" +
- "| 1. Add new customer |\n" +
- "| 2. Remove customer |\n" +
- "| 3. List all customers |\n" +
- "| 4. Add support visit log |\n" +
- "| 5. View stats |\n" +
- "| 0. Logout |\n" +
- "+----------------------------+\n" +
- "\nPlease, select an option: ");
- // take in the option given and give it to the switch statement
- int option = 0;
- if (scanner.hasNextInt()) {
- option = scanner.nextInt();
- }
- switch (option) {
- case 0: // return to login start() / login screen
- System.out.println("\n[Logging out]\n");
- start();
- case 1: // branch to add() to take in customer credentials and store them in the cust ArrayList
- Customer customer = add();
- cust.add(customer);
- break;
- case 2: // branch to remove() to remove a given customer from cust ArrayList
- remove(cust);
- break;
- case 3: // branch to list() to list all users stored in the cust ArrayList and prints their information
- list(cust);
- break;
- case 4: // branch to support() to add a new support ticket and stores the ticket in the log ArrayList
- Support support = support();
- log.add(support);
- break;
- 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)
- view(log, cust);
- break;
- default:
- err.println("\nError: invalid choice\nTry typing a number 1 or 2\n");
- TimeUnit.MILLISECONDS.sleep(5);
- break;
- }
- }
- } else {
- // if password given is invalid
- err.println("\nPassword is incorrect\n");
- TimeUnit.MILLISECONDS.sleep(5);
- }
- }
- } else {
- // if email given is invalid
- System.out.println(users.get(i).email);
- err.println("\nEmail " + email + " does not exist\n");
- TimeUnit.MILLISECONDS.sleep(5);
- }
- }
- }
- /**
- collects information about a customer
- creates a customer object based on parameters passed through command line
- returns created customer to be added to cust ArrayList
- */
- private static Customer add() {
- // take input
- Scanner scanner = new Scanner(System.in);
- out.print("\n[Add a new customer]\n\nEnter the customer ID: ");
- String id = scanner.nextLine();
- out.print("Enter the customer name: ");
- String name = scanner.nextLine();
- out.print("Enter the customer email: ");
- String email = scanner.nextLine();
- out.print("Enter the customer phone: ");
- String phone = scanner.nextLine();
- out.print("Enter the address latitude: ");
- String latitude = scanner.nextLine();
- out.print("Enter the address longitude: ");
- String longitude = scanner.nextLine();
- out.println(" ");
- // create customer
- Customer customer = new Customer(id, name, email, phone, latitude, longitude);
- return customer;
- }
- /**
- prompts the user for an id of a customer to remove from the cust ArrayList
- if the id given exists in the cust ArrayList, remove it from the list
- */
- private static void remove(ArrayList<Customer> cust) {
- String id;
- // prompt user for the ID of the customer to remove
- Scanner scanner = new Scanner(System.in);
- out.print("\nEnter an ID to remove: ");
- id = scanner.nextLine();
- // for each customer in the cust ArrayList
- // if the id given equals the customer's id, remove that customer from the list
- for(int i = 0; i < cust.size(); i++){
- if(id == cust.get(i).id){
- cust.remove(i);
- }
- }
- }
- /**
- lists the information about every customer in the cust ArrayList
- */
- private static void list(ArrayList<Customer> cust) {
- System.out.println("\n[List all customers]");
- // for every customer in cust ArrayList, get that customer's credentials and print them
- for (int i = 0; i < cust.size(); i++) {
- out.println("\nCustomer ID: " + cust.get(i).id);
- out.println("Name: " + cust.get(i).name);
- out.println("Email: " + cust.get(i).email);
- out.println("Phone: " + cust.get(i).phone);
- // format latitude and longitude to be a google maps link
- out.println("Google maps link: https://www.google.com/maps/?q=" + cust.get(i).latitude + "," + cust.get(i).longitude + "\n");
- }
- }
- /**
- prompts user for input to create a new support ticket
- */
- private static Support support() {
- // collect informatino about the support ticket
- Scanner scanner = new Scanner(System.in);
- out.print("\n[Add a new support visit]\n" +
- "\nEnter the customer ID of the support: ");
- String id = scanner.nextLine();
- out.print("Enter the day: ");
- String day = scanner.nextLine();
- out.print("Enter the month: ");
- String month = scanner.nextLine();
- out.print("Enter the year: ");
- String year = scanner.nextLine();
- out.print("Enter the amount of support hours: ");
- int hours = scanner.nextInt();
- out.print("Enter the amount paid: ");
- /** For whatever reason the program will not run the scanner.nextLine() unless there are both of them
- So here's a piece of code that makes it work but I have no idea why this is happening */
- String weirdBud = scanner.nextLine();
- int paid = scanner.nextInt();
- out.println("\nNew support record added!");
- // create a new support ticket based on information given in command line
- Support support = new Support(id, day, month, year, hours, paid);
- // returns support ticket instantiated to be added to the log ArrayList
- return support;
- }
- /**
- prints the total number of hours in all support tickets
- prints the ID and name of the customer that has spent the most (money)
- */
- private static void view(ArrayList<Support> log, ArrayList<Customer> cust) {
- int time = 0;
- int count = 0;
- String id = "";
- String name = "";
- // for every support ticket create
- // add the sum of the time specified in each ticket
- // if support ticket being tested paid is larger than stored paid value (count) get the customer id on that ticket
- // for every customer in the cust ArrayList
- // check if the ID on the ticket is assigned to any customer in the cust ArrayList
- // 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
- for(int i = 0; i < log.size(); i++){
- time = time + log.get(i).hours;
- if(log.get(i).paid > count){
- count = log.get(i).paid;
- id = log.get(i).id;
- for(Customer customer : cust){
- if(customer.getId().equals(id)){
- name = cust.get(i).name;
- }
- }
- }
- }
- // print stats determined about the support ticket
- out.println("\n[View stats of support logs]\n" +
- "Total number of hours spent on support: " + time +"\n\n" +
- "Customer that has spent the most (money) on support:\nID: " + id + "\n" +
- "Name: " + name);
- }
- }
- /**
- support class stores information about support tickets created
- */
- class Support {
- String id;
- String day;
- String month;
- String year;
- int hours;
- int paid;
- public Support(String id, String day, String month, String year, int hours, int paid){
- this.id = id;
- this.day = day;
- this.month = month;
- this.year = year;
- this.hours = hours;
- this.paid = paid;
- }
- }
- /**
- user class stores information about users created for initial login screen
- */
- class User {
- String email;
- String name;
- String pwd;
- public User(String email, String name, String pwd){
- this.email = email;
- this.name = name;
- this.pwd = pwd;
- }
- public String getPwd() {
- return pwd;
- }
- }
- /**
- customer class stores information about customers to be stored in the cust ArrayList
- */
- class Customer {
- String id;
- String name;
- String email;
- String phone;
- String latitude;
- String longitude;
- public Customer(String id, String name, String email, String phone, String latitude, String longitude){
- this.id = id;
- this.name = name;
- this.email = email;
- this.phone = phone;
- this.latitude = latitude;
- this.longitude = longitude;
- }
- public String getId() {
- return id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement