SebastianLawe

Untitled

Feb 24th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.21 KB | None | 0 0
  1. /**
  2.  * ClassName: Programming 1
  3.  *
  4.  * @author Sebastian Lawe
  5.  * @version 05.11.2014
  6.  *
  7.  * Assignment: 5
  8.  * Course: ADEV-1000
  9.  * Section: 2
  10.  * Date Created: 05/11/2014
  11.  * Last Updated: 05/11/2014
  12.  */
  13. import java.util.Scanner;
  14. public class JavaMart
  15. {
  16.     public static void main(String[] args)
  17.     {
  18.         // Declare and initialize variable.
  19.         int productsCounted = 0;
  20.         double totalInventoryCost = 0;
  21.  
  22.         // Output header.
  23.         System.out.println("Java Mart Inventory System\n--------------------------");
  24.  
  25.         // Get name.
  26.         String name = getName();
  27.  
  28.         // Do once and repeat while anotherEntry is true.
  29.         do {
  30.             // Get SKU.
  31.             String sku = getSKU();
  32.  
  33.             // Get description.
  34.             String description = getDescription();
  35.  
  36.             // Add to totalInventoryCost
  37.             totalInventoryCost += getQuantityOnHand() * getCostPerUnit();
  38.  
  39.             // Increment Count.
  40.             productsCounted++;
  41.         }while(anotherEntry());
  42.  
  43.         // Output report
  44.         System.out.println("\nInventory Report\n--------------------------");
  45.         System.out.println("Inventory performed by: " + name);
  46.         System.out.println("Number of products counted: " + productsCounted);
  47.         System.out.println("Total Inventory Cost: $" +  totalInventoryCost);
  48.  
  49.     }
  50.  
  51.     /**
  52.      * Gets a name from the user.
  53.      *
  54.      * @return - the name of the user.
  55.      */
  56.     private static String getName()
  57.     {
  58.          //Declare and initialize name variable.
  59.          String name = "";
  60.  
  61.          //Loop if the received name is invalid. Set name to the users entered value.
  62.          while(!validName(name = promptAndInput("Employee Name: ")));
  63.  
  64.          // Return the name.
  65.          return name;
  66.     }
  67.  
  68.     /**
  69.      * Gets an SKU from the user.
  70.      *
  71.      * @return - the SKU.
  72.      */
  73.     private static String getSKU()
  74.     {
  75.         // Declare and initialize sku variable.
  76.         String sku = "";
  77.  
  78.         // Loop if the received SKU is invalid. Set sku to the users entered value.
  79.         while(!validSKU(sku = promptAndInput("\nEnter SKU number: ")));
  80.  
  81.         // Return sku.
  82.         return sku;
  83.     }
  84.  
  85.     /**
  86.      * Gets a description from a user.
  87.      *
  88.      * @return
  89.      */
  90.     private static String getDescription()
  91.     {
  92.         // Declare and initialize description variable.
  93.         String description = "";
  94.  
  95.         // Loop if the received description is invalid.  Set description to the users entered value.
  96.         while(!validDescription(description = promptAndInput("Enter description: ")));
  97.  
  98.         // Return description.
  99.         return description;
  100.     }
  101.  
  102.     /**
  103.      * Gets the quantity on hand from the user.
  104.      *
  105.      * @return - the quantity on hand entered by user.
  106.      */
  107.     private static int getQuantityOnHand()
  108.     {
  109.         // Declare and initialize quantityOnHand variable.
  110.         int quantityOnHand = 0;
  111.  
  112.         // Loop if the received number is invalid. Set quantityOnHand to the users entered value.
  113.         while(!validNumber(quantityOnHand = Integer.parseInt(promptAndInput("Enter quantity on hand: "))));
  114.  
  115.         // Return result.
  116.         return quantityOnHand;
  117.     }
  118.  
  119.     /**
  120.      * Gets the cost per unit from the user
  121.      *
  122.      * @return - the cost per unit entered by the user.
  123.      */
  124.     private static double getCostPerUnit()
  125.     {
  126.         // Declare and initialize variables
  127.         double costPerUnit = 0;
  128.  
  129.         // Loop if the received number is invalid. Set costPerUnit to the users entered value.
  130.         while(!validCostPerUnit(costPerUnit = Double.parseDouble(promptAndInput("Enter unit cost: "))));
  131.  
  132.         // Return result
  133.         return costPerUnit;
  134.     }
  135.  
  136.     /**
  137.      * Checks to see if the name is valid.
  138.      *
  139.      * @param name - the name to validate.
  140.      * @return - whether or not the name was valid.
  141.      */
  142.     private static boolean validName(String name){
  143.         // Declare and initlialize boolean.
  144.         boolean validEntry = false;
  145.  
  146.         // Check if length is greater than 0.
  147.         if(name.length() > 0)
  148.         {
  149.             // Set valid entry to true.
  150.             validEntry = true;
  151.         }
  152.         else
  153.         {
  154.             // Output error.
  155.             System.out.println("\n**Error 01** - You must enter your name\n");
  156.         }
  157.  
  158.         // Return result.
  159.         return validEntry;
  160.     }
  161.  
  162.     /**
  163.      * Checks to see if the sku is valid.
  164.      *
  165.      * @param sku - the sku to validate.
  166.      * @return  - whether or not the sku was valid.
  167.      */
  168.     private static boolean validSKU(String sku)
  169.     {
  170.         // Declare and initialize boolean.
  171.         boolean validEntry = false;
  172.  
  173.         // Check to see if sku length is equal to 5
  174.         if(sku.length() == 5)
  175.         {
  176.             // Set valid entry to true.
  177.             validEntry = true;
  178.         }
  179.         else
  180.         {
  181.             // Output error.
  182.             System.out.println("\n**Error 02** - SKU number must be five characters");
  183.         }
  184.  
  185.         // Return result;
  186.         return validEntry;
  187.     }
  188.  
  189.     /**
  190.      * Checks to see if the description is valid.
  191.      *
  192.      * @param description - the description to validate.
  193.      * @return - whether or not the description was valid.
  194.      */
  195.     private static boolean validDescription(String description)
  196.     {
  197.         // Declare and initialize boolean.
  198.         boolean validEntry = true;
  199.  
  200.         // Check if description is less than or equal to 0.
  201.         if(description.length() <= 0){
  202.             // Output error.
  203.             System.out.println("\n**Error 03** - Product description is required\n");
  204.  
  205.             // Set validEntry to false.
  206.             validEntry = false;
  207.         }
  208.         else if(description.length() > 20)
  209.         {
  210.             //Output error.
  211.             System.out.println("\n**Error 04** - Product description is more than 20 characters\n");
  212.  
  213.             // Set valid entry to false
  214.             validEntry = false;
  215.         }
  216.  
  217.         // Return result.
  218.         return validEntry;
  219.     }
  220.  
  221.     /**
  222.      * Checks to see if the number is not negative
  223.      *
  224.      * @return - whether or not the number was negative.
  225.      */
  226.     private static boolean validNumber(int number)
  227.     {
  228.         // Declare and initialize boolean.
  229.         boolean validEntry = false;
  230.  
  231.         // Check if number is greater or equal to 0
  232.         if(number >= 0)
  233.         {
  234.             // Set validEntry to true.
  235.             validEntry = true;
  236.         }
  237.         else
  238.         {
  239.             // Output error.
  240.             System.out.println("\n**Error 05** - Quantity on hand is a negative value\n");
  241.         }
  242.  
  243.         // Return validEntry.
  244.         return validEntry;
  245.     }
  246.  
  247.     /**
  248.      * Checks if the entered value is a valid coat per unit.
  249.      *
  250.      * @param costPerUnit - the cost per unit.
  251.      * @return - whether or not the cost per unit was valid.
  252.      */
  253.     private static boolean validCostPerUnit(double costPerUnit)
  254.     {
  255.         // Declare an initialize boolean.
  256.         boolean validEntry = false;
  257.  
  258.         // Check if cost per unit is between 0.01 - 5000.00
  259.         if(costPerUnit >= 0.01 && costPerUnit <= 5000.00)
  260.         {
  261.             // Set valid entry to true.
  262.             validEntry = true;
  263.         }
  264.         else
  265.         {
  266.             // Output error.
  267.             System.out.println("\n**Error 06** - Unit cost must be a value between .01 - 5000.00\n");
  268.         }
  269.         return validEntry;
  270.     }
  271.  
  272.     /**
  273.      * Checks to see if the response is a valid y/n response.
  274.      *
  275.      * @param response - the yes or no response.
  276.      * @return - whether or not the response was a valid y/n answer
  277.      */
  278.     private static boolean validResponse(String response)
  279.     {
  280.         // Declare and initialize boolean.
  281.         boolean validEntry = false;
  282.  
  283.         // Check if response is 'y' or 'n'
  284.         if(response.toLowerCase().equals("y") || response.toLowerCase().equals("n"))
  285.         {
  286.             // Set validEntry to true.
  287.             validEntry = true;
  288.         }
  289.         else
  290.         {
  291.             // Output error.
  292.             System.out.println("\n**Error** - Invalid response");
  293.         }
  294.  
  295.         // Return result.
  296.         return validEntry;
  297.     }
  298.     /**
  299.      * Prompts user and returns the users entry.
  300.      *
  301.      * @param prompt - what is being asked of the user.
  302.      * @return - the users entry.
  303.      */
  304.     private static String promptAndInput(String prompt)
  305.     {
  306.         // Declare and initialize scanner object.
  307.         Scanner scanner = new Scanner(System.in);
  308.  
  309.         // Output prompt.
  310.         System.out.print(prompt);
  311.  
  312.         // Return users input.
  313.         return scanner.nextLine();
  314.     }
  315.  
  316.     /**
  317.      * Asks the user if another entry is required.
  318.      *
  319.      * @return - the users reponse. True is yes, false is no.
  320.      */
  321.     private static boolean anotherEntry()
  322.     {
  323.         // Declare and initialize string.
  324.         String yesOrNo = "";
  325.  
  326.         // Loop if the received response is invalid. Set yesOrNo to user input.
  327.         while(!validResponse(yesOrNo = promptAndInput("\nEnter another item? (y/n): ")));
  328.  
  329.         // Return result.
  330.         return yesOrNo.equals("y");
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment