Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.65 KB | None | 0 0
  1. /** ***********************************
  2.  * Programmer: Your Name
  3.  * classID: Your classID
  4.  * Lab3: SalesTracker
  5.  * CIS 2600: Business Application Programming
  6.  * Fall 2017
  7.  * Due date: XX/XX/XX
  8.  * Date completed: XX/XX/XX
  9.  **************************************
  10.  * Program Explanation
  11.  *
  12.  * HARD: Dynamic Object Array = java util arraylist
  13.  *
  14.  *
  15.  *
  16.  *NOTE TO SELF: Next time, use multiple scanner objects to avoid having to
  17.  *flush buffer.
  18.  ************************************* */
  19.  
  20. /*
  21. Remember to explain the code itself.
  22. Discussing how it accomplishes what it does
  23. is as important as noting what it does.
  24.  */
  25. //=============================Watch the line==================================>
  26. package lab3;
  27.  
  28. //import scanner class
  29. import java.util.Scanner;
  30. import java.util.ArrayList;
  31. import java.util.Collections;
  32. import java.util.Comparator;
  33.  
  34. /**
  35.  *
  36.  * @author
  37.  *
  38.  */
  39. public class CreateSalesperson {
  40.  
  41.     public static void main(String[] args) {
  42.  
  43.         //vars
  44.         //MENU Nav
  45.         String menuSelect = " ";
  46.         String listSelect = " ";
  47.  
  48.         //ADD
  49.         String userIdString = " ";
  50.         String userName = " ";
  51.         String userSales = " ";
  52.         //We do not want to go over 20 objects for assignment
  53.         final int ARRAYCAP = 20;
  54.  
  55.         //EDIT
  56.         int userEdit = 0;
  57.         String userEditID = " ";
  58.         String userEditName = " ";
  59.         String userEditAmount = " ";
  60.  
  61.         //DELETE
  62.         int userDelete = 0;
  63.         String confirmDelete = " ";
  64.  
  65.         //LOOP
  66.         boolean reLoop;
  67.  
  68.         //create scanner object to obtain input using inputDevice
  69.         Scanner inputDevice = new Scanner(System.in);
  70.  
  71.         //Creates an array list (named salespeopleList
  72.         //to store objects(Salesperson)
  73.         ArrayList<Salesperson> salespeopleList = new ArrayList<>(20);
  74.  
  75.         //Container Loop for entire program, infinite - only way out is sys.exit
  76.         while (reLoop = true) {
  77.  
  78.             // -------------------------------------------------------------------
  79.             System.out.println("\nWhat would you like to do today?");
  80.             System.out.println("(A)dd a Record");
  81.             System.out.println("(E)dit a Record");
  82.             System.out.println("(D)elete a Record");
  83.             System.out.println("(S)ort the List");
  84.             System.out.println("(Q)uit");
  85.             System.out.print("Please enter in your choice: ");
  86.             //toUpperCase so that lowercase input still works
  87.             menuSelect = inputDevice.nextLine().toUpperCase();
  88.  
  89.             while (!(menuSelect.equalsIgnoreCase("A")
  90.                     || menuSelect.equalsIgnoreCase("E")
  91.                     || menuSelect.equalsIgnoreCase("D")
  92.                     || menuSelect.equalsIgnoreCase("S")
  93.                     || menuSelect.equalsIgnoreCase("Q"))) {
  94.  
  95.                 System.out.println("\nYou entered an incorrect value!\n "
  96.                         + "\nPlease try again using the corresponding "
  97.                         + "letter in parentheses: ");
  98.                 System.out.println("(A)dd a Record");
  99.                 System.out.println("(E)dit a Record");
  100.                 System.out.println("(D)elete a Record");
  101.                 System.out.println("(S)ort the List");
  102.                 System.out.println("(Q)uit");
  103.                 System.out.print("Please enter in your choice: ");
  104.                 menuSelect = inputDevice.nextLine().toUpperCase();
  105.  
  106.             }
  107.             // -------------------------------------------------------------------
  108.  
  109.             switch (menuSelect) {
  110.                 //ADD--------------------------------------------------------------
  111.                 case "A":
  112.  
  113.                     //While Array is less than 20, you can add
  114.                     if (salespeopleList.size() < ARRAYCAP) {
  115.  
  116.                         System.out.print("\nPlease enter your UID: ");
  117.                         userIdString = inputDevice.nextLine();
  118.  
  119.                         //convert int to string
  120.                         //userIdString = String.valueOf(userID);
  121.                         //ask again while length is anything but 8 digits
  122.                         while ((userIdString.length() != 8)) {
  123.                             System.out.print("\nPlease enter 8 digits for your UID: ");
  124.                             userIdString = inputDevice.nextLine();
  125.  
  126.                         }
  127.  
  128.                        // if (salespeopleList.getUserID().equals(userIdString)) {
  129.  
  130.                        
  131.                        
  132.                        
  133.                        
  134.                        
  135.                        
  136.                        
  137.                             /*
  138.                 //for i=0 size of array list
  139.                 //arraylist index, is it equal to new user id, return true,
  140.                
  141.                
  142.                 //loop to ask
  143.                 //userIdString.compareTo();
  144.                
  145.                 for(int i = 0; i<salespeopleList.size(); i++) {
  146.                    
  147.             if (salespeopleList.getUserID() == userIdString) {
  148.                              */
  149.                            
  150.                            
  151.                            
  152.                            
  153.                        
  154.                    
  155.  
  156.                     System.out.print("\nPlease enter your name: ");
  157.                     userName = inputDevice.nextLine();
  158.  
  159.                     System.out.print("\nPlease enter your sales amount: $");
  160.                     userSales = inputDevice.nextLine();
  161.  
  162.                     //
  163.                     //
  164.                     //Leave this here for testing 20
  165.                     //for (int count = 0; count < 20; ++count) {
  166.                     //
  167.                     Salesperson addPerson = new Salesperson(userIdString,
  168.                             userName, userSales);
  169.                     addPerson.setUserID(userIdString);
  170.                     addPerson.setUserName(userName);
  171.                     addPerson.setUserSales(userSales);
  172.  
  173.                     //ADD TO NEXT ELEMENT IN ARRAY
  174.                     salespeopleList.add(addPerson);
  175.                 //}
  176.  
  177.             }//End If Checking for less than 20
  178.  
  179.             //If array is less than 20
  180.             else {
  181.                     System.out.println("\nThe list has reached its capacity of"
  182.                         + " 20 set by the department manager. If you would like"
  183.                             + " to change the size of this list, adjust the "
  184.                             + "constant.");
  185.             }
  186.  
  187.             break;//End case A
  188.             //ADD--------------------------------------------------------------    
  189.  
  190.             //EDIT-------------------------------------------------------------
  191.        
  192.         case "E":
  193.                
  194.                 System.out.print("\nPlease enter the element in the List you "
  195.                         + "would like to change: ");
  196.                 userEdit = inputDevice.nextInt();
  197.                
  198.                  
  199.                
  200.                
  201.                
  202.                 Salesperson editPerson = salespeopleList.get(userEdit - 1);
  203.                
  204.                
  205.                 System.out.println("You are about to edit: "
  206.                         + editPerson.getUserID() + " "
  207.                         + editPerson.getUserName() + " "
  208.                         + editPerson.getUserSales());
  209.                
  210.                
  211.                 System.out.println("\nPlease enter new ID: ");
  212.                 //flush buffer
  213.                 inputDevice.nextLine();
  214.                 userEditID = inputDevice.nextLine();
  215.                
  216.                 System.out.println("\nPlease enter new Name: ");
  217.                 userEditName = inputDevice.nextLine();
  218.                
  219.                 System.out.println("\nPlease enter new Amount: ");
  220.                 userEditAmount = inputDevice.nextLine();
  221.                
  222.                
  223.                 salespeopleList.get(userEdit - 1).setUserID(userEditID);
  224.                 salespeopleList.get(userEdit - 1).setUserName(userEditName);
  225.                 salespeopleList.get(userEdit - 1).setUserSales(userEditAmount);
  226.                
  227.  
  228.                 break;
  229.             //EDIT-------------------------------------------------------------    
  230.                
  231.             //DELETE-----------------------------------------------------------    
  232.             case "D":
  233.                 System.out.print("\nPlease enter the element in the List you "
  234.                         + "would like to delete: ");
  235.                 userDelete = inputDevice.nextInt();
  236.                
  237.                
  238.                
  239.                 Salesperson deletePerson = salespeopleList.get(userDelete - 1);
  240.                
  241.                
  242.                 System.out.println("\nYou are about to delete: "
  243.                         + deletePerson.getUserID() + " "
  244.                         + deletePerson.getUserName() + " "
  245.                         + deletePerson.getUserSales());
  246.                
  247.                 System.out.println("Are you sure you want to delete this "
  248.                         + "record? (Y/N): ");
  249.                 //flush buffer
  250.                 inputDevice.nextLine();
  251.                 confirmDelete = inputDevice.nextLine().toUpperCase();
  252.                
  253.                
  254.                 if (confirmDelete.equals("Y")) {
  255.                 salespeopleList.remove(userDelete - 1);
  256.                 System.out.println("Successfully Deleted");
  257.                 }
  258.                
  259.                
  260.                
  261.                
  262.                
  263.                
  264.                
  265.                 break;
  266.             //DELETE-----------------------------------------------------------    
  267.                
  268.                
  269.                
  270.                
  271.                
  272.                
  273.             //SORT-------------------------------------------------------------    
  274.             case "S":
  275.                 System.out.println("\nWelcome to the sorter!");
  276.                
  277.        
  278.  
  279.            Salesperson Person;
  280.            System.out.println("UID " + "Name " + "Sales_Amount ");
  281.             for (int count = 0; count < salespeopleList.size(); count++)
  282.                
  283.             {
  284.                 Person = salespeopleList.get(count);
  285.                 System.out.println(Person.getUserID() + " "
  286.                         + Person.getUserName() + " "
  287.                         + Person.getUserSales());
  288.  
  289.             }
  290.            
  291.            
  292.            /*
  293.        Salesperson comparator = new Salesperson (" ", " ", " ");
  294.        Collections.sort(salespeopleList, comparator);
  295.  
  296.        // Sorted List
  297.        System.out.println("After Sorting:");
  298.        for(String counter: salespeopleList){
  299.             System.out.println(counter);
  300.         }
  301.                
  302.                */
  303.             Collections.sort(salespeopleList, (o1, o2) -> o1.getUserID().compareTo(o2.getUserID()));    
  304.  
  305.                
  306.                
  307.                
  308.                
  309.                
  310.                
  311.                 break;    
  312.             //SORT-------------------------------------------------------------
  313.                
  314.                
  315.                
  316.                
  317.                
  318.                
  319.                
  320.             //QUIT-------------------------------------------------------------    
  321.             case "Q":
  322.                 System.out.println("\nYou are now exiting the program."
  323.                         + " Goodbye.");
  324.                 //exit argument 0 non-error exit
  325.                 System.exit(0);
  326.             //QUIT-------------------------------------------------------------
  327.             default:
  328.                
  329.                
  330.                
  331.                
  332.                
  333.                
  334.  
  335.         }//END SWITCH
  336.  
  337.  
  338.         //DISPLAY---------------------------------------------------------------
  339.         System.out.println("\nWould you like to display your list? (Y/N): ");
  340.         listSelect = inputDevice.nextLine().toUpperCase();
  341.  
  342.         if (listSelect.equals("Y")) {
  343.             Salesperson Person;
  344.  
  345.             System.out.println("UID " + "Name " + "Sales_Amount ");
  346.             for (int count = 0; count < salespeopleList.size(); count++) {
  347.                 Person = salespeopleList.get(count);
  348.                 System.out.println(Person.getUserID() + " "
  349.                         + Person.getUserName() + " "
  350.                         + Person.getUserSales());
  351.  
  352.             }
  353.  
  354.             System.out.print("\nPress enter to return to main menu");
  355.             inputDevice.nextLine();//
  356.         }
  357.         //DISPLAY---------------------------------------------------------------
  358.  
  359.     }//RELOOP. GO BACK TO START.
  360.  
  361. }//END MAIN
  362.  
  363. }//END CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement