Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * ClassName: Programming 1
- *
- * @author Sebastian Lawe
- * @version 05.11.2014
- *
- * Assignment: 5
- * Course: ADEV-1000
- * Section: 2
- * Date Created: 05/11/2014
- * Last Updated: 05/11/2014
- */
- import java.util.Scanner;
- public class JavaMart
- {
- public static void main(String[] args)
- {
- // Declare and initialize variable.
- int productsCounted = 0;
- double totalInventoryCost = 0;
- // Output header.
- System.out.println("Java Mart Inventory System\n--------------------------");
- // Get name.
- String name = getName();
- // Do once and repeat while anotherEntry is true.
- do {
- // Get SKU.
- String sku = getSKU();
- // Get description.
- String description = getDescription();
- // Add to totalInventoryCost
- totalInventoryCost += getQuantityOnHand() * getCostPerUnit();
- // Increment Count.
- productsCounted++;
- }while(anotherEntry());
- // Output report
- System.out.println("\nInventory Report\n--------------------------");
- System.out.println("Inventory performed by: " + name);
- System.out.println("Number of products counted: " + productsCounted);
- System.out.println("Total Inventory Cost: $" + totalInventoryCost);
- }
- /**
- * Gets a name from the user.
- *
- * @return - the name of the user.
- */
- private static String getName()
- {
- //Declare and initialize name variable.
- String name = "";
- //Loop if the received name is invalid. Set name to the users entered value.
- while(!validName(name = promptAndInput("Employee Name: ")));
- // Return the name.
- return name;
- }
- /**
- * Gets an SKU from the user.
- *
- * @return - the SKU.
- */
- private static String getSKU()
- {
- // Declare and initialize sku variable.
- String sku = "";
- // Loop if the received SKU is invalid. Set sku to the users entered value.
- while(!validSKU(sku = promptAndInput("\nEnter SKU number: ")));
- // Return sku.
- return sku;
- }
- /**
- * Gets a description from a user.
- *
- * @return
- */
- private static String getDescription()
- {
- // Declare and initialize description variable.
- String description = "";
- // Loop if the received description is invalid. Set description to the users entered value.
- while(!validDescription(description = promptAndInput("Enter description: ")));
- // Return description.
- return description;
- }
- /**
- * Gets the quantity on hand from the user.
- *
- * @return - the quantity on hand entered by user.
- */
- private static int getQuantityOnHand()
- {
- // Declare and initialize quantityOnHand variable.
- int quantityOnHand = 0;
- // Loop if the received number is invalid. Set quantityOnHand to the users entered value.
- while(!validNumber(quantityOnHand = Integer.parseInt(promptAndInput("Enter quantity on hand: "))));
- // Return result.
- return quantityOnHand;
- }
- /**
- * Gets the cost per unit from the user
- *
- * @return - the cost per unit entered by the user.
- */
- private static double getCostPerUnit()
- {
- // Declare and initialize variables
- double costPerUnit = 0;
- // Loop if the received number is invalid. Set costPerUnit to the users entered value.
- while(!validCostPerUnit(costPerUnit = Double.parseDouble(promptAndInput("Enter unit cost: "))));
- // Return result
- return costPerUnit;
- }
- /**
- * Checks to see if the name is valid.
- *
- * @param name - the name to validate.
- * @return - whether or not the name was valid.
- */
- private static boolean validName(String name){
- // Declare and initlialize boolean.
- boolean validEntry = false;
- // Check if length is greater than 0.
- if(name.length() > 0)
- {
- // Set valid entry to true.
- validEntry = true;
- }
- else
- {
- // Output error.
- System.out.println("\n**Error 01** - You must enter your name\n");
- }
- // Return result.
- return validEntry;
- }
- /**
- * Checks to see if the sku is valid.
- *
- * @param sku - the sku to validate.
- * @return - whether or not the sku was valid.
- */
- private static boolean validSKU(String sku)
- {
- // Declare and initialize boolean.
- boolean validEntry = false;
- // Check to see if sku length is equal to 5
- if(sku.length() == 5)
- {
- // Set valid entry to true.
- validEntry = true;
- }
- else
- {
- // Output error.
- System.out.println("\n**Error 02** - SKU number must be five characters");
- }
- // Return result;
- return validEntry;
- }
- /**
- * Checks to see if the description is valid.
- *
- * @param description - the description to validate.
- * @return - whether or not the description was valid.
- */
- private static boolean validDescription(String description)
- {
- // Declare and initialize boolean.
- boolean validEntry = true;
- // Check if description is less than or equal to 0.
- if(description.length() <= 0){
- // Output error.
- System.out.println("\n**Error 03** - Product description is required\n");
- // Set validEntry to false.
- validEntry = false;
- }
- else if(description.length() > 20)
- {
- //Output error.
- System.out.println("\n**Error 04** - Product description is more than 20 characters\n");
- // Set valid entry to false
- validEntry = false;
- }
- // Return result.
- return validEntry;
- }
- /**
- * Checks to see if the number is not negative
- *
- * @return - whether or not the number was negative.
- */
- private static boolean validNumber(int number)
- {
- // Declare and initialize boolean.
- boolean validEntry = false;
- // Check if number is greater or equal to 0
- if(number >= 0)
- {
- // Set validEntry to true.
- validEntry = true;
- }
- else
- {
- // Output error.
- System.out.println("\n**Error 05** - Quantity on hand is a negative value\n");
- }
- // Return validEntry.
- return validEntry;
- }
- /**
- * Checks if the entered value is a valid coat per unit.
- *
- * @param costPerUnit - the cost per unit.
- * @return - whether or not the cost per unit was valid.
- */
- private static boolean validCostPerUnit(double costPerUnit)
- {
- // Declare an initialize boolean.
- boolean validEntry = false;
- // Check if cost per unit is between 0.01 - 5000.00
- if(costPerUnit >= 0.01 && costPerUnit <= 5000.00)
- {
- // Set valid entry to true.
- validEntry = true;
- }
- else
- {
- // Output error.
- System.out.println("\n**Error 06** - Unit cost must be a value between .01 - 5000.00\n");
- }
- return validEntry;
- }
- /**
- * Checks to see if the response is a valid y/n response.
- *
- * @param response - the yes or no response.
- * @return - whether or not the response was a valid y/n answer
- */
- private static boolean validResponse(String response)
- {
- // Declare and initialize boolean.
- boolean validEntry = false;
- // Check if response is 'y' or 'n'
- if(response.toLowerCase().equals("y") || response.toLowerCase().equals("n"))
- {
- // Set validEntry to true.
- validEntry = true;
- }
- else
- {
- // Output error.
- System.out.println("\n**Error** - Invalid response");
- }
- // Return result.
- return validEntry;
- }
- /**
- * Prompts user and returns the users entry.
- *
- * @param prompt - what is being asked of the user.
- * @return - the users entry.
- */
- private static String promptAndInput(String prompt)
- {
- // Declare and initialize scanner object.
- Scanner scanner = new Scanner(System.in);
- // Output prompt.
- System.out.print(prompt);
- // Return users input.
- return scanner.nextLine();
- }
- /**
- * Asks the user if another entry is required.
- *
- * @return - the users reponse. True is yes, false is no.
- */
- private static boolean anotherEntry()
- {
- // Declare and initialize string.
- String yesOrNo = "";
- // Loop if the received response is invalid. Set yesOrNo to user input.
- while(!validResponse(yesOrNo = promptAndInput("\nEnter another item? (y/n): ")));
- // Return result.
- return yesOrNo.equals("y");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment