Advertisement
Guest User

Untitled

a guest
May 12th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.43 KB | None | 0 0
  1. package DIS;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.util.*;
  6.  
  7. public class main
  8. {
  9.     public static void main(String args[]) throws FileNotFoundException, IOException
  10.     {
  11.             main mainInstance = new main();
  12.     }
  13.  
  14.     public void inventory() throws FileNotFoundException, IOException
  15.     {
  16.             //User command line interface is started up.
  17.             userAccounts.loadDatabase();
  18.             inventory.loadDatabase();
  19.             System.out.println("Welcome to the Defence Inventory System Ver. 1.3");
  20.  
  21.             //Login attempts are made until a proper username and password are received.
  22.             while (true)
  23.             {
  24.                 //Prompt user for username and password.
  25.                 System.out.println("Username: ");
  26.                 username = input.nextLine();
  27.                 System.out.println("Password: ");
  28.                 String password = input.nextLine();
  29.  
  30.                 //Information is passed to the userAccounts class on to determine if an account is valid.
  31.                 userInstance = userAccounts.accountCheck(username, password); //userInstance is a state int variable.
  32.  
  33.                 //Upon receiving confirmation of an admin login attempt handling is passed to the newly created adminInventory class.
  34.                 if (userInstance == 2)  //2 denotes an admin
  35.                 {
  36.                     if (rootCheck(password))
  37.                         break;
  38.                 }
  39.  
  40.                 //Upon receving confirmation of a successful user login the login loop breaks.
  41.                 if (userInstance == 1)  //1 denotes a user
  42.                     break;
  43.  
  44.                 System.out.println("Username and/or password was not found. Try again.");
  45.             }
  46.  
  47.             System.out.println("Successfully logged in as " + username);
  48.  
  49.             //User I/O (or further pa is passed off to the proper handling method.
  50.             if (rootAccess)
  51.                 this.adminIO();
  52.             else
  53.                 this.userIO();
  54.     }
  55.  
  56.     public void adminIO() throws IOException
  57.     {
  58.         System.out.println("What would you like to do? (Enter z at any time to return to this menu.)");
  59.         String tempString = null;
  60.         char command = 'a';
  61.  
  62.         while (command != 'q')
  63.         {
  64.             System.out.println("\t a. Check equipment in ");
  65.             System.out.println("\t b. Check equipment in ");
  66.             System.out.println("\t c. List all equipment");
  67.             System.out.println("\t d. List all equipment out");
  68.             System.out.println("\t e. List all equipment in");
  69.             System.out.println("\t f. List all out equipment past its due date");
  70.             System.out.println("\t g. Search for equipment");
  71.             System.out.println("\t h. Add equipment to the database");
  72.             System.out.println("\t i. Remove equipment from the database");
  73.             System.out.println("\t j. Edit equipment in the database");
  74.             System.out.println("\t k. Add a user account");
  75.             System.out.println("\t l. Remove a user account");
  76.             System.out.println("\t m. Quit the DIS");
  77.  
  78.             tempString = input.nextLine();
  79.             command = tempString.charAt(0);
  80.  
  81.             switch(command)
  82.             {
  83.                 case 'A':
  84.                 case 'a':
  85.                     checkIn();
  86.                     break;
  87.                 case 'B':
  88.                 case 'b':
  89.                     checkOut();
  90.                     break;
  91.                 case 'C':
  92.                 case 'c':
  93.                     listEquipment(0);
  94.                     break;
  95.                 case 'D':
  96.                 case 'd':
  97.                     listEquipment(1);
  98.                     break;
  99.                 case 'E':
  100.                 case 'e':
  101.                     listEquipment(2);
  102.                     break;
  103.                 case 'F':
  104.                 case 'f':
  105.                     listEquipment(3);
  106.                     break;
  107.                 case 'G':
  108.                 case 'g':
  109.                     searchEquipment(2);
  110.                     break;
  111.                 case 'H':
  112.                 case 'h':
  113.                     addEquipment();
  114.                     break;
  115.                 case 'I':
  116.                 case 'i':
  117.                     removeEquipment();
  118.                     break;
  119.                 case 'J':
  120.                 case 'j':
  121.                     editEquipment();
  122.                     break;
  123.                 case 'K':
  124.                 case 'k':
  125.                     addUser();
  126.                     break;
  127.                 case 'L':
  128.                 case 'l':
  129.                     removeUser();
  130.                     break;
  131.                 case 'M':
  132.                 case 'm':
  133.                     command = 'q';
  134.                     quitProgram();
  135.                     break;
  136.                 default:
  137.                     System.out.println("Invalid Command entered");
  138.             }
  139.         }
  140.         }
  141.  
  142.     public void userIO() throws IOException
  143.     {
  144.         System.out.println("What would you like to do?(Enter z at any time to return to this menu.)");
  145.         String tempString = null;
  146.         char command = 'a';
  147.  
  148.         while (command != 'q')
  149.         {
  150.             System.out.println("\t a. Check equipment in ");
  151.             System.out.println("\t b. Check equipment in ");
  152.             System.out.println("\t c. List all available equipment");
  153.             System.out.println("\t d. Search for equipment");
  154.             System.out.println("\t e. Quit the DIS");
  155.  
  156.             tempString = input.nextLine();
  157.             command = tempString.charAt(0);
  158.  
  159.             switch(command)
  160.             {
  161.                 case 'A':
  162.                 case 'a':
  163.                     checkIn();
  164.                     break;
  165.                 case 'B':
  166.                 case 'b':
  167.                     checkOut();
  168.                     break;
  169.                 case 'C':
  170.                 case 'c':
  171.                     listEquipment(1);
  172.                     break;
  173.                 case 'D':
  174.                 case 'd':
  175.                     searchEquipment(2);
  176.                     break;
  177.                 case 'E':
  178.                 case 'e':
  179.                     command = 'q';
  180.                     quitProgram();
  181.                     break;
  182.                 default:
  183.                     System.out.println("Invalid Command entered");
  184.             }
  185.         }
  186.     }
  187.  
  188.     public void checkIn()
  189.     {
  190.         String intendedIn = null;
  191.         System.out.println("Enter the name of the equipment you wish to turn in:");
  192.         intendedIn = input.nextLine();
  193.         if (menuCheck(intendedIn))
  194.         {
  195.             for (int x = 0; x < inventory.numRecords; x = x + 1)
  196.             {
  197.                 if (intendedIn.compareTo(inventory.getName(x)) == 0)
  198.                 {
  199.                     if (inventory.getStatus(x) == false)
  200.                     {
  201.                         System.out.println("Item " + inventory.getName(x) + "is already checked in.");
  202.                         return;
  203.                     }
  204.                     else
  205.                     {
  206.                         inventory.editStatus(false, x);
  207.                         inventory.editLoaner(null, x); // is this alright?
  208.                         System.out.println("Item " + inventory.getName(x) + "was successfully checked in");
  209.                         return;
  210.                     }
  211.                 }
  212.             }
  213.  
  214.             System.out.println("Equipment does not exist or was entered incorrectly, try again.");
  215.         }
  216.     }
  217.  
  218.     public void checkOut()
  219.     {
  220.         String intendedOut = null;
  221.         System.out.println("Enter the name of the equipment you wish to check out:");
  222.         intendedOut = input.nextLine();
  223.         if (menuCheck(intendedOut))
  224.         {
  225.             for (int x = 0; x < inventory.numRecords; x = x + 1)
  226.             {
  227.                 if (intendedOut.compareTo(inventory.getName(x)) == 0)
  228.                 {
  229.                     if (inventory.getStatus(x) == true)
  230.                     {
  231.                         System.out.println("Item " + inventory.getName(x) + "is already checked out.");
  232.                         return;
  233.                     }
  234.                     else
  235.                     {
  236.                         inventory.editStatus(true, x);
  237.                         inventory.editLoaner(username, x);
  238.                         System.out.println("Item " + inventory.getName(x) + "was successfully checked out");
  239.                         return;
  240.                     }
  241.                 }
  242.             }
  243.  
  244.             System.out.println("Equipment does not exist or was entered incorrectly, try again.");
  245.         }
  246.     }
  247.  
  248.     @SuppressWarnings("empty-statement")
  249.     public void listEquipment(int filter) //0 all, 1 in, 2 out, 3 late
  250.     {
  251.         Date time = new Date();
  252.         printHeader();
  253.         int x;
  254.         for (x = 0; x < inventory.numRecords; x = x + 1);
  255.         {
  256.             if (filter == 3)
  257.             {
  258.                 long currentTime = (time.getYear() + 1900)*1000 + (time.getMonth() + 1)*10 + time.getDay();
  259.                 if (currentTime > inventory.getDueDate(x));
  260.                     printRecord(x);
  261.             }
  262.             else if (filter == 2)
  263.             {
  264.                 if (inventory.getStatus(x) == true)
  265.                     printRecord(x);
  266.             }
  267.             else if (filter == 1)
  268.             {
  269.                 if (inventory.getStatus(x) == false)
  270.                     printRecord(x);
  271.             }
  272.             else if (filter == 0)
  273.             {
  274.                 printRecord(x);
  275.             }
  276.         }
  277.     }
  278.  
  279.     public void searchEquipment(int userType)
  280.     {
  281.         while(true)
  282.         {
  283.             System.out.println("Enter the name of the equipment you wish to find:");
  284.             String searchName = input.nextLine();
  285.             if (menuCheck(searchName))
  286.             {
  287.                 for (int x = 0; x < inventory.numRecords; x = x + 1)
  288.                 {
  289.                     if (searchName.compareTo(inventory.getName(x)) == 0)
  290.                     {
  291.                         System.out.println("Record found:");
  292.                         printHeader();
  293.                         printRecord(x);
  294.                         return;
  295.                     }
  296.                 }
  297.  
  298.                 System.out.println("Record not found, try again");
  299.             }
  300.             else
  301.             {
  302.                 return;
  303.             }
  304.         }
  305.     }
  306.  
  307.     public void addEquipment()
  308.     {
  309.         System.out.println("Specify the name of the equipment to be added:");
  310.         String name = input.nextLine();
  311.         if (menuCheck(name) == false)
  312.             return;
  313.  
  314.         System.out.println("Specify the quantity of the equipemtn to be added:");
  315.         String strQuantity = input.nextLine();
  316.                 if (menuCheck(strQuantity) == false)
  317.             return;
  318.         long quantity = Long.parseLong(strQuantity);
  319.  
  320.         System.out.println("Specify the serial number of the equipment to be added:");
  321.         String strSerial = input.nextLine();
  322.                 if (menuCheck(strSerial) == false)
  323.                     return;
  324.         long serial = Long.parseLong(strSerial);
  325.  
  326.         System.out.println("Specify the next maintence date of the equipment: (In the format \"dd/mm/yyyy\")");
  327.         String tempMaint = input.nextLine();
  328.         if(menuCheck(tempMaint) == false)
  329.             return;
  330.         String strMaint = tempMaint.substring(7) + tempMaint.substring(4, 6) + tempMaint.substring(1, 3);
  331.         //try
  332.         //{
  333.             long maint = Long.parseLong(strMaint);
  334.         //}
  335.         //catch (NumberFormatException INSERT SUMTIN)
  336.         //{
  337.             //System.out.prinln("NumberFormatException: " + INSERT SUMTIN.getMessage());
  338.         //}
  339.  
  340.         boolean status = false;
  341.  
  342.         String loaner = null; //is this alright?
  343.  
  344.         System.out.println("Specify the inventory serial number of the equipment to be added:");
  345.         String strInvtSerial = input.nextLine();
  346.                 if (menuCheck(strInvtSerial) == false)
  347.             return;
  348.         long invtSerial = Long.parseLong(strInvtSerial);
  349.  
  350.         System.out.println("Specify the due date the equipment: (In the format \"dd/mm/yyyy\"");
  351.         String tempDue = input.nextLine();
  352.         if (menuCheck(tempDue) == false)
  353.             return;
  354.         String strDue = tempDue.substring(7) + tempDue.substring(4, 6) + tempDue.substring(1, 3);
  355.         long dueDate = Long.parseLong(strDue);
  356.  
  357.         inventory.addRecord(name, quantity, serial, maint, status, loaner, invtSerial, dueDate);
  358.     }
  359.  
  360.     public void removeEquipment()
  361.     {
  362.         System.out.println("Specify the name of the equipment to be removed:");
  363.         String intendedRemoval = input.nextLine();
  364.         if(menuCheck(intendedRemoval))
  365.         {
  366.             for (int x = 0; x < inventory.numRecords; x = x + 1)
  367.             {
  368.                 if (intendedRemoval.compareTo(inventory.getName(x)) == 0)
  369.                 {
  370.                     inventory.removeRecord(x);
  371.                 }
  372.             }
  373.         }
  374.     }
  375.  
  376.     public void editEquipment()
  377.     {
  378.         int targetIndex = 0;
  379.         while(true)
  380.         {
  381.             System.out.println("Enter the name of the equipment to be edited:");
  382.             String editTarget = input.nextLine();
  383.             if (menuCheck(editTarget))
  384.             {
  385.                 for (int x = 0; x < inventory.numRecords; x = x + 1)
  386.                 {
  387.                     if (editTarget.compareTo(inventory.getName(x)) == 0)
  388.                     {
  389.                         targetIndex = x;
  390.                         System.out.println("Record found:");
  391.                         printHeader();
  392.                         printRecord(x);
  393.                         return;
  394.                     }
  395.                 }
  396.                 System.out.println("Record not found, try again");
  397.             }
  398.             else
  399.             {
  400.                 return;
  401.             }
  402.         }
  403.  
  404.         System.out.println("Specify the element to be edited: ");
  405.         System.out.println("\t a. The Name");
  406.         System.out.println("\t b. The Quantity");
  407.         System.out.println("\t c. The Serial Number");
  408.         System.out.println("\t d. Next Maintenence Date");
  409.         System.out.println("\t e. Check in/out");
  410.         System.out.println("\t f. Who it is lonaed to");
  411.         System.out.println("\t g. The Inventory Serial Number");
  412.         System.out.println("\t h. The Due Date");
  413.  
  414.         String tempString = input.nextLine();
  415.         char command = tempString.charAt(0);
  416.  
  417.         switch(command)
  418.         {
  419.             case 'A':
  420.             case 'a':
  421.                 System.out.println("Specify the new name of the equipment: ");
  422.                 String newName = input.nextLine();
  423.                 if (menuCheck(newName) == false)
  424.                     return;
  425.                 inventory.editName(newName, targetIndex);
  426.                 break;
  427.             case 'B':
  428.             case 'b':
  429.                 System.out.println("Specify the new quantity of the equipment: ");
  430.                 String tmpQuantity = input.nextLine();
  431.                 if (menuCheck(tmpQuantity) == false)
  432.                     return;
  433.                                  long newQuantity = Long.parseLong(tmpQuantity);
  434.                 inventory.editQuantity(newQuantity, targetIndex);
  435.                 break;
  436.             case 'C':
  437.             case 'c':
  438.                 System.out.println("Specify the new serial number of the equipment: ");
  439.                 String tmpSerial = input.nextLine();
  440.                 if (menuCheck(tmpSerial) == false)
  441.                     return;
  442.                                  long newSerial = Long.parseLong(tmpSerial);
  443.                 inventory.editSerial(newSerial, targetIndex);
  444.                 break;
  445.             case 'D':
  446.             case 'd':
  447.                 System.out.println("Specify the new maintenence date of the equipment: (In the format \"dd/mm/yyyy\")");
  448.                 String tempMaint = input.nextLine();
  449.                 if (menuCheck(tempMaint) == false)
  450.                     return;
  451.                 String strMaint = tempMaint.substring(7) + tempMaint.substring(4, 6) + tempMaint.substring(1, 3);
  452.                                 long newMaint = Long.parseLong(strMaint);
  453.                 inventory.editMaint(newMaint, targetIndex);
  454.                 break;
  455.             case 'E':
  456.             case 'e':
  457.                 while (true)
  458.                 {
  459.                     System.out.println("Specify the status of the equipment: (In the form of \"in\" or \"out\")");
  460.                     String tempStatus = input.nextLine();
  461.                     if (menuCheck(tempStatus) == false)
  462.                         return;
  463.                     String strStatus = tempStatus.toLowerCase();
  464.                     if (strStatus.compareTo("in") == 0)
  465.                     {
  466.                         inventory.editStatus(false, targetIndex);
  467.                         break; //does this break through the while(true) or just the if?
  468.                     }
  469.                     else if (strStatus.compareTo("out") == 0)
  470.                     {
  471.                         inventory.editStatus(true, targetIndex);
  472.                         break;
  473.                     }
  474.                     else
  475.                     {
  476.                         System.out.println("Please Enter only \"in\" or \"out\"");
  477.                     }
  478.  
  479.                 }
  480.                 break;
  481.             case 'F':
  482.             case 'f':
  483.                 System.out.println("Specify the new name of who was loaned the equipment: ");
  484.                 String newLoaner = input.nextLine();
  485.                 if (menuCheck(newLoaner) == false)
  486.                     return;
  487.                 inventory.editLoaner(newLoaner, targetIndex);
  488.                 break;
  489.             case 'G':
  490.             case 'g':
  491.                 System.out.println("Specify the new inventory serial number of the equipment: ");
  492.                 String tmpInvtSerial = input.nextLine();
  493.                 if (menuCheck(tmpInvtSerial) == false)
  494.                     return;
  495.                                  long newInvtSerial = Long.parseLong(tmpInvtSerial);
  496.                 inventory.editInvtSerial(newInvtSerial, targetIndex);
  497.                 break;
  498.             case 'H':
  499.             case 'h':
  500.                 System.out.println("Specify the new due date of the equipment: (In the format \"dd/mm/yyyy\")");
  501.                 String tempDue = input.nextLine();
  502.                 if (menuCheck(tempDue) == false)
  503.                     return;
  504.                 String strDueDate = tempDue.substring(7) + tempDue.substring(4, 6) + tempDue.substring(1, 3);
  505.                                 long newDueDate = Long.parseLong(strDueDate);
  506.                 inventory.editDueDate(newDueDate, targetIndex); //needs conversion to a string
  507.                 break;
  508.         }
  509.     }
  510.  
  511.     public void addUser()
  512.     {
  513.         String intendedUser = null;
  514.         String intendedPass = null;
  515.         String passCheck = null;
  516.  
  517.         System.out.println("Specify the username of the new user:");
  518.         intendedUser = input.nextLine();
  519.         if (menuCheck(intendedUser))
  520.         {
  521.             while(true)
  522.             {
  523.                 System.out.println("Specify the password of the new user:");
  524.                 intendedPass = input.nextLine();
  525.                 if (menuCheck(intendedPass))
  526.                 {
  527.                     System.out.println("Confirm the password of the new user:");
  528.                     passCheck = input.nextLine();
  529.                     if (menuCheck(passCheck))
  530.                     {
  531.                         if (intendedUser.compareTo(passCheck) == 0)
  532.                         {
  533.                              userAccounts.addUser(intendedUser, intendedPass);
  534.                         }
  535.                         else
  536.                         {
  537.                             System.out.println("Usernames did not match, try again.");
  538.                         }
  539.                     }
  540.                 }
  541.             }
  542.         }
  543.     }
  544.  
  545.     public void removeUser()
  546.     {
  547.  
  548.         String intendedTarget = null;
  549.         System.out.println("Specify the username of the account to be removed:");
  550.         intendedTarget = input.nextLine();
  551.         if (menuCheck(intendedTarget))
  552.         {
  553.             userAccounts.removeUser(intendedTarget);
  554.         }
  555.     }
  556.  
  557.     public void quitProgram() throws IOException
  558.     {
  559.         System.out.println("Closing DIS.");
  560.         userAccounts.saveDatabase();
  561.         inventory.saveInventory();
  562.     }
  563.  
  564.     public boolean menuCheck(String checkMe)
  565.     {
  566.         char check = checkMe.charAt(0);
  567.  
  568.         switch (check)
  569.         {
  570.             case 'Z':
  571.             case 'z':
  572.                 return false;
  573.             default:
  574.                 return true;
  575.         }
  576.     }
  577.  
  578.     public void printHeader()
  579.     {
  580.         Formatter fmt = new Formatter();
  581.  
  582.         fmt.format("%-17s", "Equipment Name");
  583.         System.out.print(fmt.toString());          //17 bytes long
  584.         fmt.format("%-9s", "Quantity");
  585.         System.out.print(fmt.toString());          //9 bytes long
  586.         fmt.format("%-9s", "Serial #");
  587.         System.out.print(fmt.toString());          //9 bytes long
  588.         fmt.format("%-13s", "Req. Maint.");
  589.         System.out.print(fmt.toString());          //13 bytes long
  590.         fmt.format("%-7s", "Status");
  591.         System.out.print(fmt.toString());          //7 bytes long
  592.         fmt.format("%-15s", "Loaned to");
  593.         System.out.print(fmt.toString());          //15 bytes long
  594.         fmt.format("%-15s", "Invt. Serial #");
  595.         System.out.print(fmt.toString());          //15 bytes long
  596.         fmt.format("%-11s", "Due Date");
  597.         System.out.println(fmt.toString());        //11 bytes long
  598.     }
  599.  
  600.     public void printRecord(int index)
  601.     {
  602.         Formatter fmt = new Formatter();
  603.  
  604.         fmt.format("%-17s", inventory.getName(index));
  605.         System.out.print(fmt.toString());
  606.         fmt.format("%-9d", inventory.getQuantity(index));
  607.         System.out.print(fmt.toString()); //needs more conversion probably, talk to Mr. V
  608.         fmt.format("%-09d", inventory.getSerial(index));
  609.         System.out.print(fmt.toString());
  610.  
  611.         long lgMaint = inventory.getMaint(index);
  612.         String tempMaint = Long.toString(lgMaint);
  613.         String strMaint = tempMaint.substring(7) + "/" + tempMaint.substring(5, 7) + "/" + tempMaint.substring(1, 5);
  614.         fmt.format("%-13s", strMaint);
  615.         System.out.print(fmt.toString());
  616.  
  617.         if (inventory.getStatus(index) == true)
  618.         {
  619.             fmt.format("%-7s", "Out");
  620.             System.out.print(fmt.toString());
  621.         }
  622.         else
  623.         {
  624.             fmt.format("$-7s", "In");
  625.             System.out.print(fmt.toString());
  626.         }
  627.  
  628.         fmt.format("%-15s", inventory.getLoaner(index));
  629.         System.out.print(fmt.toString());
  630.         fmt.format("-09d", inventory.getInvtSerial(index));
  631.         System.out.print(fmt.toString());
  632.  
  633.         long lgDue = inventory.getDueDate(index);
  634.         String tempDue = Long.toString(lgDue);
  635.         String strDue = tempDue.substring(7) + "/" + tempDue.substring(5, 7) + "/" + tempDue.substring(1, 5);
  636.         fmt.format("%-11s", strDue);
  637.         System.out.println(fmt.toString());
  638.     }
  639.  
  640.     public boolean rootCheck(String password)
  641.     {
  642.         if (password.compareTo(rootPassword) == 0)
  643.         {
  644.             rootAccess = true;
  645.             return true;
  646.         }
  647.                 else
  648.                 {
  649.                     return false;
  650.                 }
  651.     }
  652.  
  653.     public String username = null;
  654.     private boolean rootAccess = false;
  655.     private final String rootPassword = "1337haxor";
  656.     public int userInstance = 0; //0 denotes null (suprsing for Germans!).
  657.  
  658.     accountDatabase userAccounts = new accountDatabase();
  659.     inventoryDatabase inventory = new inventoryDatabase();
  660.     Scanner input = new Scanner(System.in);
  661. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement