Created
March 3, 2016 15:46
-
-
Save SamuelSchwent/951a681b83f478e7b693 to your computer and use it in GitHub Desktop.
Java - Throwing and Handling Exceptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Challenge3; | |
| public class DisplayItems { | |
| public static void main(String[] args) throws InvalidUnitsException, InvalidPriceException { | |
| RetailItem item1 = new RetailItem("Jacket", 12, 59.95); | |
| RetailItem item2 = new RetailItem("Designer Jeans", 20, 34.95); | |
| RetailItem item3 = new RetailItem("Shirt", 20, -24.95); | |
| System.out.printf("Item 1: %s\nUnits: %d\nPrice: %.2f\n" | |
| + "", item1.getDescription(), item1.getUnitsOnHand(), item1.getPrice()); | |
| System.out.printf("\nItem 2: %s\nUnits: %d\nPrice: %.2f\n" | |
| + "", item2.getDescription(), item2.getUnitsOnHand(), item2.getPrice()); | |
| System.out.printf("\nItem 3: %s\nUnits: %d\nPrice: %.2f\n" | |
| + "", item3.getDescription(), item3.getUnitsOnHand(), item3.getPrice()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Challenge3; | |
| @SuppressWarnings("serial") | |
| public class InvalidPriceException extends Exception{ | |
| public InvalidPriceException() | |
| { | |
| super("ERROR: PRICE IS NEGITIVE"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Challenge3; | |
| @SuppressWarnings("serial") | |
| public class InvalidUnitsException extends Exception{ | |
| public InvalidUnitsException() | |
| { | |
| super("ERROR: UNITS IS NEGITIVE"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Challenge3; | |
| public class RetailItem { | |
| String description; | |
| int unitsOnHand; | |
| double price; | |
| public RetailItem(String description, int unitsOnHand, double price) throws InvalidUnitsException, InvalidPriceException | |
| { | |
| if (unitsOnHand < 0) | |
| throw new InvalidUnitsException(); | |
| if (price < 0) | |
| throw new InvalidPriceException(); | |
| this.description = description; | |
| this.unitsOnHand = unitsOnHand; | |
| this.price = price; | |
| } | |
| public String getDescription() | |
| { | |
| return description; | |
| } | |
| public int getUnitsOnHand() | |
| { | |
| return unitsOnHand; | |
| } | |
| public double getPrice() | |
| { | |
| return price; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment