Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Zehra Baig
- //CSC-162-01
- //Lab 6-C
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class RetailItemDemo
- {
- //main method
- public static void main(String[] args)
- {
- Scanner reader = new Scanner(System.in);
- String desc;
- int units;
- double price;
- boolean noErrors;
- DecimalFormat dollar = new DecimalFormat("$#,###.00");
- //Creating RetailItem obj
- RetailItem retailItem = new RetailItem();
- //setting description
- System.out.print("Enter the item's description: ");
- desc = reader.nextLine();
- retailItem.setDescription(desc);
- //looping until the user becomes competent
- do
- {
- System.out.print("Enter the number of units on hand: ");
- units = reader.nextInt();
- try
- {
- retailItem.setUnitsOnHand(units);
- noErrors = true;
- }
- catch(NegativeUnitsException nue)
- {
- System.out.println(nue.getMessage());
- noErrors = false;
- }
- }while (!noErrors);
- //same as above except with price
- do
- {
- System.out.print("Enter the price of the item: ");
- price = reader.nextDouble();
- try
- {
- retailItem.setPrice(price);
- noErrors = true;
- }
- catch(NegativePriceException npe)
- {
- System.out.println(npe.getMessage());
- noErrors = false;
- }
- }while (!noErrors);
- //printing user's input
- System.out.println("\nDescription: " + retailItem.getDescription());
- System.out.println("Units on hand: " + retailItem.getUnitsOnHand());
- System.out.println("Price: " + dollar.format(retailItem.getPrice()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement