Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DIS;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.*;
- public class main
- {
- public static void main(String args[]) throws FileNotFoundException, IOException
- {
- main mainInstance = new main();
- }
- public void inventory() throws FileNotFoundException, IOException
- {
- //User command line interface is started up.
- userAccounts.loadDatabase();
- inventory.loadDatabase();
- System.out.println("Welcome to the Defence Inventory System Ver. 1.3");
- //Login attempts are made until a proper username and password are received.
- while (true)
- {
- //Prompt user for username and password.
- System.out.println("Username: ");
- username = input.nextLine();
- System.out.println("Password: ");
- String password = input.nextLine();
- //Information is passed to the userAccounts class on to determine if an account is valid.
- userInstance = userAccounts.accountCheck(username, password); //userInstance is a state int variable.
- //Upon receiving confirmation of an admin login attempt handling is passed to the newly created adminInventory class.
- if (userInstance == 2) //2 denotes an admin
- {
- if (rootCheck(password))
- break;
- }
- //Upon receving confirmation of a successful user login the login loop breaks.
- if (userInstance == 1) //1 denotes a user
- break;
- System.out.println("Username and/or password was not found. Try again.");
- }
- System.out.println("Successfully logged in as " + username);
- //User I/O (or further pa is passed off to the proper handling method.
- if (rootAccess)
- this.adminIO();
- else
- this.userIO();
- }
- public void adminIO() throws IOException
- {
- System.out.println("What would you like to do? (Enter z at any time to return to this menu.)");
- String tempString = null;
- char command = 'a';
- while (command != 'q')
- {
- System.out.println("\t a. Check equipment in ");
- System.out.println("\t b. Check equipment in ");
- System.out.println("\t c. List all equipment");
- System.out.println("\t d. List all equipment out");
- System.out.println("\t e. List all equipment in");
- System.out.println("\t f. List all out equipment past its due date");
- System.out.println("\t g. Search for equipment");
- System.out.println("\t h. Add equipment to the database");
- System.out.println("\t i. Remove equipment from the database");
- System.out.println("\t j. Edit equipment in the database");
- System.out.println("\t k. Add a user account");
- System.out.println("\t l. Remove a user account");
- System.out.println("\t m. Quit the DIS");
- tempString = input.nextLine();
- command = tempString.charAt(0);
- switch(command)
- {
- case 'A':
- case 'a':
- checkIn();
- break;
- case 'B':
- case 'b':
- checkOut();
- break;
- case 'C':
- case 'c':
- listEquipment(0);
- break;
- case 'D':
- case 'd':
- listEquipment(1);
- break;
- case 'E':
- case 'e':
- listEquipment(2);
- break;
- case 'F':
- case 'f':
- listEquipment(3);
- break;
- case 'G':
- case 'g':
- searchEquipment(2);
- break;
- case 'H':
- case 'h':
- addEquipment();
- break;
- case 'I':
- case 'i':
- removeEquipment();
- break;
- case 'J':
- case 'j':
- editEquipment();
- break;
- case 'K':
- case 'k':
- addUser();
- break;
- case 'L':
- case 'l':
- removeUser();
- break;
- case 'M':
- case 'm':
- command = 'q';
- quitProgram();
- break;
- default:
- System.out.println("Invalid Command entered");
- }
- }
- }
- public void userIO() throws IOException
- {
- System.out.println("What would you like to do?(Enter z at any time to return to this menu.)");
- String tempString = null;
- char command = 'a';
- while (command != 'q')
- {
- System.out.println("\t a. Check equipment in ");
- System.out.println("\t b. Check equipment in ");
- System.out.println("\t c. List all available equipment");
- System.out.println("\t d. Search for equipment");
- System.out.println("\t e. Quit the DIS");
- tempString = input.nextLine();
- command = tempString.charAt(0);
- switch(command)
- {
- case 'A':
- case 'a':
- checkIn();
- break;
- case 'B':
- case 'b':
- checkOut();
- break;
- case 'C':
- case 'c':
- listEquipment(1);
- break;
- case 'D':
- case 'd':
- searchEquipment(2);
- break;
- case 'E':
- case 'e':
- command = 'q';
- quitProgram();
- break;
- default:
- System.out.println("Invalid Command entered");
- }
- }
- }
- public void checkIn()
- {
- String intendedIn = null;
- System.out.println("Enter the name of the equipment you wish to turn in:");
- intendedIn = input.nextLine();
- if (menuCheck(intendedIn))
- {
- for (int x = 0; x < inventory.numRecords; x = x + 1)
- {
- if (intendedIn.compareTo(inventory.getName(x)) == 0)
- {
- if (inventory.getStatus(x) == false)
- {
- System.out.println("Item " + inventory.getName(x) + "is already checked in.");
- return;
- }
- else
- {
- inventory.editStatus(false, x);
- inventory.editLoaner(null, x); // is this alright?
- System.out.println("Item " + inventory.getName(x) + "was successfully checked in");
- return;
- }
- }
- }
- System.out.println("Equipment does not exist or was entered incorrectly, try again.");
- }
- }
- public void checkOut()
- {
- String intendedOut = null;
- System.out.println("Enter the name of the equipment you wish to check out:");
- intendedOut = input.nextLine();
- if (menuCheck(intendedOut))
- {
- for (int x = 0; x < inventory.numRecords; x = x + 1)
- {
- if (intendedOut.compareTo(inventory.getName(x)) == 0)
- {
- if (inventory.getStatus(x) == true)
- {
- System.out.println("Item " + inventory.getName(x) + "is already checked out.");
- return;
- }
- else
- {
- inventory.editStatus(true, x);
- inventory.editLoaner(username, x);
- System.out.println("Item " + inventory.getName(x) + "was successfully checked out");
- return;
- }
- }
- }
- System.out.println("Equipment does not exist or was entered incorrectly, try again.");
- }
- }
- @SuppressWarnings("empty-statement")
- public void listEquipment(int filter) //0 all, 1 in, 2 out, 3 late
- {
- Date time = new Date();
- printHeader();
- int x;
- for (x = 0; x < inventory.numRecords; x = x + 1);
- {
- if (filter == 3)
- {
- long currentTime = (time.getYear() + 1900)*1000 + (time.getMonth() + 1)*10 + time.getDay();
- if (currentTime > inventory.getDueDate(x));
- printRecord(x);
- }
- else if (filter == 2)
- {
- if (inventory.getStatus(x) == true)
- printRecord(x);
- }
- else if (filter == 1)
- {
- if (inventory.getStatus(x) == false)
- printRecord(x);
- }
- else if (filter == 0)
- {
- printRecord(x);
- }
- }
- }
- public void searchEquipment(int userType)
- {
- while(true)
- {
- System.out.println("Enter the name of the equipment you wish to find:");
- String searchName = input.nextLine();
- if (menuCheck(searchName))
- {
- for (int x = 0; x < inventory.numRecords; x = x + 1)
- {
- if (searchName.compareTo(inventory.getName(x)) == 0)
- {
- System.out.println("Record found:");
- printHeader();
- printRecord(x);
- return;
- }
- }
- System.out.println("Record not found, try again");
- }
- else
- {
- return;
- }
- }
- }
- public void addEquipment()
- {
- System.out.println("Specify the name of the equipment to be added:");
- String name = input.nextLine();
- if (menuCheck(name) == false)
- return;
- System.out.println("Specify the quantity of the equipemtn to be added:");
- String strQuantity = input.nextLine();
- if (menuCheck(strQuantity) == false)
- return;
- long quantity = Long.parseLong(strQuantity);
- System.out.println("Specify the serial number of the equipment to be added:");
- String strSerial = input.nextLine();
- if (menuCheck(strSerial) == false)
- return;
- long serial = Long.parseLong(strSerial);
- System.out.println("Specify the next maintence date of the equipment: (In the format \"dd/mm/yyyy\")");
- String tempMaint = input.nextLine();
- if(menuCheck(tempMaint) == false)
- return;
- String strMaint = tempMaint.substring(7) + tempMaint.substring(4, 6) + tempMaint.substring(1, 3);
- //try
- //{
- long maint = Long.parseLong(strMaint);
- //}
- //catch (NumberFormatException INSERT SUMTIN)
- //{
- //System.out.prinln("NumberFormatException: " + INSERT SUMTIN.getMessage());
- //}
- boolean status = false;
- String loaner = null; //is this alright?
- System.out.println("Specify the inventory serial number of the equipment to be added:");
- String strInvtSerial = input.nextLine();
- if (menuCheck(strInvtSerial) == false)
- return;
- long invtSerial = Long.parseLong(strInvtSerial);
- System.out.println("Specify the due date the equipment: (In the format \"dd/mm/yyyy\"");
- String tempDue = input.nextLine();
- if (menuCheck(tempDue) == false)
- return;
- String strDue = tempDue.substring(7) + tempDue.substring(4, 6) + tempDue.substring(1, 3);
- long dueDate = Long.parseLong(strDue);
- inventory.addRecord(name, quantity, serial, maint, status, loaner, invtSerial, dueDate);
- }
- public void removeEquipment()
- {
- System.out.println("Specify the name of the equipment to be removed:");
- String intendedRemoval = input.nextLine();
- if(menuCheck(intendedRemoval))
- {
- for (int x = 0; x < inventory.numRecords; x = x + 1)
- {
- if (intendedRemoval.compareTo(inventory.getName(x)) == 0)
- {
- inventory.removeRecord(x);
- }
- }
- }
- }
- public void editEquipment()
- {
- int targetIndex = 0;
- while(true)
- {
- System.out.println("Enter the name of the equipment to be edited:");
- String editTarget = input.nextLine();
- if (menuCheck(editTarget))
- {
- for (int x = 0; x < inventory.numRecords; x = x + 1)
- {
- if (editTarget.compareTo(inventory.getName(x)) == 0)
- {
- targetIndex = x;
- System.out.println("Record found:");
- printHeader();
- printRecord(x);
- return;
- }
- }
- System.out.println("Record not found, try again");
- }
- else
- {
- return;
- }
- }
- System.out.println("Specify the element to be edited: ");
- System.out.println("\t a. The Name");
- System.out.println("\t b. The Quantity");
- System.out.println("\t c. The Serial Number");
- System.out.println("\t d. Next Maintenence Date");
- System.out.println("\t e. Check in/out");
- System.out.println("\t f. Who it is lonaed to");
- System.out.println("\t g. The Inventory Serial Number");
- System.out.println("\t h. The Due Date");
- String tempString = input.nextLine();
- char command = tempString.charAt(0);
- switch(command)
- {
- case 'A':
- case 'a':
- System.out.println("Specify the new name of the equipment: ");
- String newName = input.nextLine();
- if (menuCheck(newName) == false)
- return;
- inventory.editName(newName, targetIndex);
- break;
- case 'B':
- case 'b':
- System.out.println("Specify the new quantity of the equipment: ");
- String tmpQuantity = input.nextLine();
- if (menuCheck(tmpQuantity) == false)
- return;
- long newQuantity = Long.parseLong(tmpQuantity);
- inventory.editQuantity(newQuantity, targetIndex);
- break;
- case 'C':
- case 'c':
- System.out.println("Specify the new serial number of the equipment: ");
- String tmpSerial = input.nextLine();
- if (menuCheck(tmpSerial) == false)
- return;
- long newSerial = Long.parseLong(tmpSerial);
- inventory.editSerial(newSerial, targetIndex);
- break;
- case 'D':
- case 'd':
- System.out.println("Specify the new maintenence date of the equipment: (In the format \"dd/mm/yyyy\")");
- String tempMaint = input.nextLine();
- if (menuCheck(tempMaint) == false)
- return;
- String strMaint = tempMaint.substring(7) + tempMaint.substring(4, 6) + tempMaint.substring(1, 3);
- long newMaint = Long.parseLong(strMaint);
- inventory.editMaint(newMaint, targetIndex);
- break;
- case 'E':
- case 'e':
- while (true)
- {
- System.out.println("Specify the status of the equipment: (In the form of \"in\" or \"out\")");
- String tempStatus = input.nextLine();
- if (menuCheck(tempStatus) == false)
- return;
- String strStatus = tempStatus.toLowerCase();
- if (strStatus.compareTo("in") == 0)
- {
- inventory.editStatus(false, targetIndex);
- break; //does this break through the while(true) or just the if?
- }
- else if (strStatus.compareTo("out") == 0)
- {
- inventory.editStatus(true, targetIndex);
- break;
- }
- else
- {
- System.out.println("Please Enter only \"in\" or \"out\"");
- }
- }
- break;
- case 'F':
- case 'f':
- System.out.println("Specify the new name of who was loaned the equipment: ");
- String newLoaner = input.nextLine();
- if (menuCheck(newLoaner) == false)
- return;
- inventory.editLoaner(newLoaner, targetIndex);
- break;
- case 'G':
- case 'g':
- System.out.println("Specify the new inventory serial number of the equipment: ");
- String tmpInvtSerial = input.nextLine();
- if (menuCheck(tmpInvtSerial) == false)
- return;
- long newInvtSerial = Long.parseLong(tmpInvtSerial);
- inventory.editInvtSerial(newInvtSerial, targetIndex);
- break;
- case 'H':
- case 'h':
- System.out.println("Specify the new due date of the equipment: (In the format \"dd/mm/yyyy\")");
- String tempDue = input.nextLine();
- if (menuCheck(tempDue) == false)
- return;
- String strDueDate = tempDue.substring(7) + tempDue.substring(4, 6) + tempDue.substring(1, 3);
- long newDueDate = Long.parseLong(strDueDate);
- inventory.editDueDate(newDueDate, targetIndex); //needs conversion to a string
- break;
- }
- }
- public void addUser()
- {
- String intendedUser = null;
- String intendedPass = null;
- String passCheck = null;
- System.out.println("Specify the username of the new user:");
- intendedUser = input.nextLine();
- if (menuCheck(intendedUser))
- {
- while(true)
- {
- System.out.println("Specify the password of the new user:");
- intendedPass = input.nextLine();
- if (menuCheck(intendedPass))
- {
- System.out.println("Confirm the password of the new user:");
- passCheck = input.nextLine();
- if (menuCheck(passCheck))
- {
- if (intendedUser.compareTo(passCheck) == 0)
- {
- userAccounts.addUser(intendedUser, intendedPass);
- }
- else
- {
- System.out.println("Usernames did not match, try again.");
- }
- }
- }
- }
- }
- }
- public void removeUser()
- {
- String intendedTarget = null;
- System.out.println("Specify the username of the account to be removed:");
- intendedTarget = input.nextLine();
- if (menuCheck(intendedTarget))
- {
- userAccounts.removeUser(intendedTarget);
- }
- }
- public void quitProgram() throws IOException
- {
- System.out.println("Closing DIS.");
- userAccounts.saveDatabase();
- inventory.saveInventory();
- }
- public boolean menuCheck(String checkMe)
- {
- char check = checkMe.charAt(0);
- switch (check)
- {
- case 'Z':
- case 'z':
- return false;
- default:
- return true;
- }
- }
- public void printHeader()
- {
- Formatter fmt = new Formatter();
- fmt.format("%-17s", "Equipment Name");
- System.out.print(fmt.toString()); //17 bytes long
- fmt.format("%-9s", "Quantity");
- System.out.print(fmt.toString()); //9 bytes long
- fmt.format("%-9s", "Serial #");
- System.out.print(fmt.toString()); //9 bytes long
- fmt.format("%-13s", "Req. Maint.");
- System.out.print(fmt.toString()); //13 bytes long
- fmt.format("%-7s", "Status");
- System.out.print(fmt.toString()); //7 bytes long
- fmt.format("%-15s", "Loaned to");
- System.out.print(fmt.toString()); //15 bytes long
- fmt.format("%-15s", "Invt. Serial #");
- System.out.print(fmt.toString()); //15 bytes long
- fmt.format("%-11s", "Due Date");
- System.out.println(fmt.toString()); //11 bytes long
- }
- public void printRecord(int index)
- {
- Formatter fmt = new Formatter();
- fmt.format("%-17s", inventory.getName(index));
- System.out.print(fmt.toString());
- fmt.format("%-9d", inventory.getQuantity(index));
- System.out.print(fmt.toString()); //needs more conversion probably, talk to Mr. V
- fmt.format("%-09d", inventory.getSerial(index));
- System.out.print(fmt.toString());
- long lgMaint = inventory.getMaint(index);
- String tempMaint = Long.toString(lgMaint);
- String strMaint = tempMaint.substring(7) + "/" + tempMaint.substring(5, 7) + "/" + tempMaint.substring(1, 5);
- fmt.format("%-13s", strMaint);
- System.out.print(fmt.toString());
- if (inventory.getStatus(index) == true)
- {
- fmt.format("%-7s", "Out");
- System.out.print(fmt.toString());
- }
- else
- {
- fmt.format("$-7s", "In");
- System.out.print(fmt.toString());
- }
- fmt.format("%-15s", inventory.getLoaner(index));
- System.out.print(fmt.toString());
- fmt.format("-09d", inventory.getInvtSerial(index));
- System.out.print(fmt.toString());
- long lgDue = inventory.getDueDate(index);
- String tempDue = Long.toString(lgDue);
- String strDue = tempDue.substring(7) + "/" + tempDue.substring(5, 7) + "/" + tempDue.substring(1, 5);
- fmt.format("%-11s", strDue);
- System.out.println(fmt.toString());
- }
- public boolean rootCheck(String password)
- {
- if (password.compareTo(rootPassword) == 0)
- {
- rootAccess = true;
- return true;
- }
- else
- {
- return false;
- }
- }
- public String username = null;
- private boolean rootAccess = false;
- private final String rootPassword = "1337haxor";
- public int userInstance = 0; //0 denotes null (suprsing for Germans!).
- accountDatabase userAccounts = new accountDatabase();
- inventoryDatabase inventory = new inventoryDatabase();
- Scanner input = new Scanner(System.in);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement