Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** ***********************************
- * Programmer: Your Name
- * classID: Your classID
- * Lab3: SalesTracker
- * CIS 2600: Business Application Programming
- * Fall 2017
- * Due date: XX/XX/XX
- * Date completed: XX/XX/XX
- **************************************
- * Program Explanation
- *
- * HARD: Dynamic Object Array = java util arraylist
- *
- *
- *
- *NOTE TO SELF: Next time, use multiple scanner objects to avoid having to
- *flush buffer.
- ************************************* */
- /*
- Remember to explain the code itself.
- Discussing how it accomplishes what it does
- is as important as noting what it does.
- */
- //=============================Watch the line==================================>
- package lab3;
- //import scanner class
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- /**
- *
- * @author
- *
- */
- public class CreateSalesperson {
- public static void main(String[] args) {
- //vars
- //MENU Nav
- String menuSelect = " ";
- String listSelect = " ";
- //ADD
- String userIdString = " ";
- String userName = " ";
- String userSales = " ";
- //We do not want to go over 20 objects for assignment
- final int ARRAYCAP = 20;
- //EDIT
- int userEdit = 0;
- String userEditID = " ";
- String userEditName = " ";
- String userEditAmount = " ";
- //DELETE
- int userDelete = 0;
- String confirmDelete = " ";
- //LOOP
- boolean reLoop;
- //create scanner object to obtain input using inputDevice
- Scanner inputDevice = new Scanner(System.in);
- //Creates an array list (named salespeopleList
- //to store objects(Salesperson)
- ArrayList<Salesperson> salespeopleList = new ArrayList<>(20);
- //Container Loop for entire program, infinite - only way out is sys.exit
- while (reLoop = true) {
- // -------------------------------------------------------------------
- System.out.println("\nWhat would you like to do today?");
- System.out.println("(A)dd a Record");
- System.out.println("(E)dit a Record");
- System.out.println("(D)elete a Record");
- System.out.println("(S)ort the List");
- System.out.println("(Q)uit");
- System.out.print("Please enter in your choice: ");
- //toUpperCase so that lowercase input still works
- menuSelect = inputDevice.nextLine().toUpperCase();
- while (!(menuSelect.equalsIgnoreCase("A")
- || menuSelect.equalsIgnoreCase("E")
- || menuSelect.equalsIgnoreCase("D")
- || menuSelect.equalsIgnoreCase("S")
- || menuSelect.equalsIgnoreCase("Q"))) {
- System.out.println("\nYou entered an incorrect value!\n "
- + "\nPlease try again using the corresponding "
- + "letter in parentheses: ");
- System.out.println("(A)dd a Record");
- System.out.println("(E)dit a Record");
- System.out.println("(D)elete a Record");
- System.out.println("(S)ort the List");
- System.out.println("(Q)uit");
- System.out.print("Please enter in your choice: ");
- menuSelect = inputDevice.nextLine().toUpperCase();
- }
- // -------------------------------------------------------------------
- switch (menuSelect) {
- //ADD--------------------------------------------------------------
- case "A":
- //While Array is less than 20, you can add
- if (salespeopleList.size() < ARRAYCAP) {
- System.out.print("\nPlease enter your UID: ");
- userIdString = inputDevice.nextLine();
- //convert int to string
- //userIdString = String.valueOf(userID);
- //ask again while length is anything but 8 digits
- while ((userIdString.length() != 8)) {
- System.out.print("\nPlease enter 8 digits for your UID: ");
- userIdString = inputDevice.nextLine();
- }
- // if (salespeopleList.getUserID().equals(userIdString)) {
- /*
- //for i=0 size of array list
- //arraylist index, is it equal to new user id, return true,
- //loop to ask
- //userIdString.compareTo();
- for(int i = 0; i<salespeopleList.size(); i++) {
- if (salespeopleList.getUserID() == userIdString) {
- */
- System.out.print("\nPlease enter your name: ");
- userName = inputDevice.nextLine();
- System.out.print("\nPlease enter your sales amount: $");
- userSales = inputDevice.nextLine();
- //
- //
- //Leave this here for testing 20
- //for (int count = 0; count < 20; ++count) {
- //
- Salesperson addPerson = new Salesperson(userIdString,
- userName, userSales);
- addPerson.setUserID(userIdString);
- addPerson.setUserName(userName);
- addPerson.setUserSales(userSales);
- //ADD TO NEXT ELEMENT IN ARRAY
- salespeopleList.add(addPerson);
- //}
- }//End If Checking for less than 20
- //If array is less than 20
- else {
- System.out.println("\nThe list has reached its capacity of"
- + " 20 set by the department manager. If you would like"
- + " to change the size of this list, adjust the "
- + "constant.");
- }
- break;//End case A
- //ADD--------------------------------------------------------------
- //EDIT-------------------------------------------------------------
- case "E":
- System.out.print("\nPlease enter the element in the List you "
- + "would like to change: ");
- userEdit = inputDevice.nextInt();
- Salesperson editPerson = salespeopleList.get(userEdit - 1);
- System.out.println("You are about to edit: "
- + editPerson.getUserID() + " "
- + editPerson.getUserName() + " "
- + editPerson.getUserSales());
- System.out.println("\nPlease enter new ID: ");
- //flush buffer
- inputDevice.nextLine();
- userEditID = inputDevice.nextLine();
- System.out.println("\nPlease enter new Name: ");
- userEditName = inputDevice.nextLine();
- System.out.println("\nPlease enter new Amount: ");
- userEditAmount = inputDevice.nextLine();
- salespeopleList.get(userEdit - 1).setUserID(userEditID);
- salespeopleList.get(userEdit - 1).setUserName(userEditName);
- salespeopleList.get(userEdit - 1).setUserSales(userEditAmount);
- break;
- //EDIT-------------------------------------------------------------
- //DELETE-----------------------------------------------------------
- case "D":
- System.out.print("\nPlease enter the element in the List you "
- + "would like to delete: ");
- userDelete = inputDevice.nextInt();
- Salesperson deletePerson = salespeopleList.get(userDelete - 1);
- System.out.println("\nYou are about to delete: "
- + deletePerson.getUserID() + " "
- + deletePerson.getUserName() + " "
- + deletePerson.getUserSales());
- System.out.println("Are you sure you want to delete this "
- + "record? (Y/N): ");
- //flush buffer
- inputDevice.nextLine();
- confirmDelete = inputDevice.nextLine().toUpperCase();
- if (confirmDelete.equals("Y")) {
- salespeopleList.remove(userDelete - 1);
- System.out.println("Successfully Deleted");
- }
- break;
- //DELETE-----------------------------------------------------------
- //SORT-------------------------------------------------------------
- case "S":
- System.out.println("\nWelcome to the sorter!");
- Salesperson Person;
- System.out.println("UID " + "Name " + "Sales_Amount ");
- for (int count = 0; count < salespeopleList.size(); count++)
- {
- Person = salespeopleList.get(count);
- System.out.println(Person.getUserID() + " "
- + Person.getUserName() + " "
- + Person.getUserSales());
- }
- /*
- Salesperson comparator = new Salesperson (" ", " ", " ");
- Collections.sort(salespeopleList, comparator);
- // Sorted List
- System.out.println("After Sorting:");
- for(String counter: salespeopleList){
- System.out.println(counter);
- }
- */
- Collections.sort(salespeopleList, (o1, o2) -> o1.getUserID().compareTo(o2.getUserID()));
- break;
- //SORT-------------------------------------------------------------
- //QUIT-------------------------------------------------------------
- case "Q":
- System.out.println("\nYou are now exiting the program."
- + " Goodbye.");
- //exit argument 0 non-error exit
- System.exit(0);
- //QUIT-------------------------------------------------------------
- default:
- }//END SWITCH
- //DISPLAY---------------------------------------------------------------
- System.out.println("\nWould you like to display your list? (Y/N): ");
- listSelect = inputDevice.nextLine().toUpperCase();
- if (listSelect.equals("Y")) {
- Salesperson Person;
- System.out.println("UID " + "Name " + "Sales_Amount ");
- for (int count = 0; count < salespeopleList.size(); count++) {
- Person = salespeopleList.get(count);
- System.out.println(Person.getUserID() + " "
- + Person.getUserName() + " "
- + Person.getUserSales());
- }
- System.out.print("\nPress enter to return to main menu");
- inputDevice.nextLine();//
- }
- //DISPLAY---------------------------------------------------------------
- }//RELOOP. GO BACK TO START.
- }//END MAIN
- }//END CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement