March 2018
Intermediate to advanced
208 pages
4h 52m
English
| | class Inventory { |
| | |
| | private List<Supply> supplies = new ArrayList<>(); |
| | |
| | int getQuantity(Supply supply) { |
| | if (supply == null) { |
| » | throw new NullPointerException("supply must not be null"); |
| | } |
| | |
| | int quantity = 0; |
| | for (Supply supplyInStock : supplies) { |
| | if (supply.equals(supplyInStock)) { |
| | quantity++; |
| | } |
| | } |
| | |
| » | return quantity; |
| | |
| | } |
| | } |
In the early days of programming, you had to do everything by yourself. In C, for instance, you still need to create a String using a char[] or implement your own list. You had to do this for all kinds of data structures and algorithms. Although this is a nice exercise, it’s also time-consuming and error-prone.
Times have changed. The Java API is ...