Advertisement
Sheero

RetailItemDemo

Apr 26th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. //Zehra Baig
  2. //CSC-162-01
  3. //Lab 6-C
  4.  
  5. import java.text.DecimalFormat;
  6. import java.util.Scanner;
  7.  
  8. public class RetailItemDemo
  9. {
  10.     //main method
  11.     public static void main(String[] args)
  12.     {
  13.         Scanner reader = new Scanner(System.in);
  14.         String desc;
  15.         int units;
  16.         double price;
  17.         boolean noErrors;
  18.         DecimalFormat dollar = new DecimalFormat("$#,###.00");
  19.        
  20.         //Creating RetailItem obj
  21.         RetailItem retailItem = new RetailItem();
  22.        
  23.         //setting description
  24.         System.out.print("Enter the item's description: ");
  25.         desc = reader.nextLine();
  26.         retailItem.setDescription(desc);
  27.        
  28.         //looping until the user becomes competent
  29.         do
  30.         {
  31.             System.out.print("Enter the number of units on hand: ");
  32.             units = reader.nextInt();
  33.  
  34.             try
  35.             {
  36.                 retailItem.setUnitsOnHand(units);
  37.                 noErrors = true;
  38.             }
  39.             catch(NegativeUnitsException nue)
  40.             {
  41.                 System.out.println(nue.getMessage());
  42.                 noErrors = false;
  43.             }
  44.         }while (!noErrors);
  45.        
  46.         //same as above except with price
  47.         do
  48.         {
  49.             System.out.print("Enter the price of the item: ");
  50.             price = reader.nextDouble();
  51.  
  52.             try
  53.             {
  54.                 retailItem.setPrice(price);
  55.                 noErrors = true;
  56.             }
  57.  
  58.             catch(NegativePriceException npe)
  59.             {
  60.                 System.out.println(npe.getMessage());
  61.                 noErrors = false;
  62.             }
  63.         }while (!noErrors);
  64.        
  65.         //printing user's input
  66.         System.out.println("\nDescription: " + retailItem.getDescription());
  67.         System.out.println("Units on hand: " + retailItem.getUnitsOnHand());
  68.         System.out.println("Price: " + dollar.format(retailItem.getPrice()));
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement