├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── main.yml ├── .gitignore ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── CONTRIBUTING.md ├── LICENSE ├── NOTICE ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main └── java │ ├── _02_01 │ ├── before │ │ └── GenericsExample.java │ └── end │ │ └── GenericsExample.java │ ├── _02_02 │ ├── before │ │ └── GenericMethods.java │ └── end │ │ └── GenericMethods.java │ ├── _02_03 │ ├── before │ │ └── BoundedGenerics.java │ └── end │ │ └── BoundedGenerics.java │ ├── _02_04 │ ├── before │ │ ├── ClothingItem.java │ │ ├── ClothingSite.java │ │ ├── JacketItem.java │ │ └── ShirtItem.java │ └── end │ │ ├── ClothingItem.java │ │ ├── ClothingSite.java │ │ ├── JacketItem.java │ │ └── ShirtItem.java │ ├── _02_05 │ ├── before │ │ ├── ClothingItem.java │ │ ├── ClothingSite.java │ │ ├── JacketItem.java │ │ └── ShirtItem.java │ └── end │ │ ├── ClothingItem.java │ │ ├── ClothingSite.java │ │ ├── JacketItem.java │ │ └── ShirtItem.java │ ├── _02_06 │ ├── AdultTicket.java │ ├── ChildTicket.java │ ├── Ticket.java │ └── TicketMachine.java │ ├── _02_07 │ ├── before │ │ ├── AdultTicket.java │ │ ├── ChildTicket.java │ │ ├── Ticket.java │ │ └── TicketMachine.java │ └── end │ │ ├── AdultTicket.java │ │ ├── ChildTicket.java │ │ ├── Ticket.java │ │ └── TicketMachine.java │ ├── _03_02 │ ├── before │ │ └── LinkedListExample.java │ └── end │ │ └── LinkedListExample.java │ ├── _03_03 │ ├── before │ │ └── HashmapExample.java │ └── end │ │ └── HashmapExample.java │ ├── _03_04 │ ├── before │ │ └── LinkedHashmapExample.java │ └── end │ │ └── LinkedHashmapExample.java │ ├── _03_05 │ └── VotingSystem.java │ ├── _03_06 │ ├── before │ │ └── VotingSystem.java │ └── end │ │ └── VotingSystem.java │ ├── _04_01 │ ├── GoodMorningGreeting.java │ ├── Greeting.java │ ├── HelloWorldGreeting.java │ └── Main.java │ ├── _04_02 │ ├── before │ │ ├── GoodMorningGreeting.java │ │ ├── Greeting.java │ │ ├── HelloWorldGreeting.java │ │ └── Main.java │ └── end │ │ ├── Greeting.java │ │ └── Main.java │ ├── _04_03 │ ├── before │ │ ├── Main.java │ │ └── NumberUtils.java │ └── end │ │ ├── Main.java │ │ └── NumberUtils.java │ ├── _04_05 │ ├── before │ │ └── Main.java │ └── end │ │ └── Main.java │ ├── _04_06 │ ├── before │ │ └── StreamUtils.java │ └── end │ │ └── StreamUtils.java │ ├── _04_07 │ ├── before │ │ └── StreamUtils.java │ └── end │ │ └── StreamUtils.java │ ├── _05_02 │ ├── before │ │ └── Main.java │ └── end │ │ ├── Main.java │ │ └── ThreadExample.java │ ├── _05_03 │ ├── before │ │ ├── Main.java │ │ └── ThreadExample.java │ └── end │ │ ├── Main.java │ │ └── RunnableExample.java │ ├── _05_04 │ ├── before │ │ ├── Main.java │ │ └── RunnableExample.java │ └── end │ │ ├── Main.java │ │ └── RunnableExample.java │ ├── _05_05 │ ├── before │ │ ├── ATM.java │ │ ├── BankAccount.java │ │ └── Customer.java │ └── end │ │ ├── ATM.java │ │ ├── BankAccount.java │ │ └── Customer.java │ ├── _05_06 │ ├── StockChecker.java │ └── Store.java │ ├── _05_07 │ ├── before │ │ ├── StockChecker.java │ │ └── Store.java │ └── end │ │ ├── StockChecker.java │ │ └── Store.java │ ├── _06_02 │ ├── before │ │ └── ScannerExample.java │ └── end │ │ └── ScannerExample.java │ ├── _06_03 │ ├── before │ │ └── BufferedReaderExample.java │ ├── end │ │ └── BufferedReaderExample.java │ └── example.txt │ ├── _06_04 │ ├── before │ │ └── BufferedReaderExample.java │ ├── end │ │ └── BufferedReaderExample.java │ └── example.txt │ ├── _06_05 │ ├── BufferedReaderVsScanner.txt │ └── FileReaderExercise.java │ ├── _06_06 │ ├── BufferedReaderVsScanner.txt │ ├── before │ │ └── FileReaderExercise.java │ └── end │ │ └── FileReaderExercise.java │ ├── _07_02 │ ├── before │ │ └── FileCreationExample.java │ └── end │ │ └── FileCreationExample.java │ ├── _07_03 │ ├── begin │ │ └── DirectoriesExamples.java │ └── end │ │ └── DirectoriesExamples.java │ ├── _07_04 │ ├── before │ │ └── FileCopier.java │ ├── end │ │ └── FileCopier.java │ └── files │ │ └── a │ │ └── example.txt │ ├── _07_05 │ └── FilesExercise.java │ └── _07_06 │ ├── before │ └── FilesExercise.java │ └── end │ └── FilesExercise.java └── test └── java ├── _03_05 └── VotingSystemTest.java ├── _03_06 ├── before │ └── VotingSystemTest.java └── end │ └── VotingSystemTest.java ├── _04_06 ├── before │ └── StreamUtilsTest.java └── end │ └── StreamUtilsTest.java ├── _04_07 ├── before │ └── StreamUtilsTest.java └── end │ └── StreamUtilsTest.java ├── _06_05 └── FileReaderExerciseTest.java └── _06_06 ├── before └── FileReaderExerciseTest.java └── end └── FileReaderExerciseTest.java /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) denotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | ## Issue Overview 9 | 10 | 11 | ## Describe your environment 12 | 13 | 14 | ## Steps to Reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 4. 20 | 21 | ## Expected Behavior 22 | 23 | 24 | ## Current Behavior 25 | 26 | 27 | ## Possible Solution 28 | 29 | 30 | ## Screenshots / Video 31 | 32 | 33 | ## Related Issues 34 | 35 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Copy To Branches 2 | on: 3 | workflow_dispatch: 4 | jobs: 5 | copy-to-branches: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | with: 10 | fetch-depth: 0 11 | - name: Copy To Branches Action 12 | uses: planetoftheweb/copy-to-branches@v1.2 13 | env: 14 | key: main 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .tmp 4 | npm-debug.log 5 | .DS_Store 6 | .tmp 7 | .idea/ 8 | target/ 9 | pom.xml.tag 10 | pom.xml.releaseBackup 11 | pom.xml.versionsBackup 12 | pom.xml.next 13 | release.properties 14 | dependency-reduced-pom.xml 15 | buildNumber.properties 16 | .mvn/timing.properties 17 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar 18 | .mvn/wrapper/maven-wrapper.jar 19 | 20 | 21 | # Compiled class file 22 | *.class 23 | 24 | # Log file 25 | *.log 26 | 27 | # BlueJ files 28 | *.ctxt 29 | 30 | # Mobile Tools for Java (J2ME) 31 | .mtj.tmp/ 32 | 33 | # Package Files # 34 | *.jar 35 | *.war 36 | *.nar 37 | *.ear 38 | *.zip 39 | *.tar.gz 40 | *.rar 41 | 42 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 43 | hs_err_pid* -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/advanced-java-development-2515082/5135cc7744de98601483c5b7be2bf15e4c28ac7a/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.6/apache-maven-3.8.6-bin.zip 18 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar 19 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | Contribution Agreement 3 | ====================== 4 | 5 | This repository does not accept pull requests (PRs). All pull requests will be closed. 6 | 7 | However, if any contributions (through pull requests, issues, feedback or otherwise) are provided, as a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code (or otherwise providing feedback), you (and, if applicable, your employer) are licensing the submitted code (and/or feedback) to LinkedIn and the open source community subject to the BSD 2-Clause license. 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | LinkedIn Learning Exercise Files License Agreement 2 | ================================================== 3 | 4 | This License Agreement (the "Agreement") is a binding legal agreement 5 | between you (as an individual or entity, as applicable) and LinkedIn 6 | Corporation (“LinkedIn”). By downloading or using the LinkedIn Learning 7 | exercise files in this repository (“Licensed Materials”), you agree to 8 | be bound by the terms of this Agreement. If you do not agree to these 9 | terms, do not download or use the Licensed Materials. 10 | 11 | 1. License. 12 | - a. Subject to the terms of this Agreement, LinkedIn hereby grants LinkedIn 13 | members during their LinkedIn Learning subscription a non-exclusive, 14 | non-transferable copyright license, for internal use only, to 1) make a 15 | reasonable number of copies of the Licensed Materials, and 2) make 16 | derivative works of the Licensed Materials for the sole purpose of 17 | practicing skills taught in LinkedIn Learning courses. 18 | - b. Distribution. Unless otherwise noted in the Licensed Materials, subject 19 | to the terms of this Agreement, LinkedIn hereby grants LinkedIn members 20 | with a LinkedIn Learning subscription a non-exclusive, non-transferable 21 | copyright license to distribute the Licensed Materials, except the 22 | Licensed Materials may not be included in any product or service (or 23 | otherwise used) to instruct or educate others. 24 | 25 | 2. Restrictions and Intellectual Property. 26 | - a. You may not to use, modify, copy, make derivative works of, publish, 27 | distribute, rent, lease, sell, sublicense, assign or otherwise transfer the 28 | Licensed Materials, except as expressly set forth above in Section 1. 29 | - b. Linkedin (and its licensors) retains its intellectual property rights 30 | in the Licensed Materials. Except as expressly set forth in Section 1, 31 | LinkedIn grants no licenses. 32 | - c. You indemnify LinkedIn and its licensors and affiliates for i) any 33 | alleged infringement or misappropriation of any intellectual property rights 34 | of any third party based on modifications you make to the Licensed Materials, 35 | ii) any claims arising from your use or distribution of all or part of the 36 | Licensed Materials and iii) a breach of this Agreement. You will defend, hold 37 | harmless, and indemnify LinkedIn and its affiliates (and our and their 38 | respective employees, shareholders, and directors) from any claim or action 39 | brought by a third party, including all damages, liabilities, costs and 40 | expenses, including reasonable attorneys’ fees, to the extent resulting from, 41 | alleged to have resulted from, or in connection with: (a) your breach of your 42 | obligations herein; or (b) your use or distribution of any Licensed Materials. 43 | 44 | 3. Open source. This code may include open source software, which may be 45 | subject to other license terms as provided in the files. 46 | 47 | 4. Warranty Disclaimer. LINKEDIN PROVIDES THE LICENSED MATERIALS ON AN “AS IS” 48 | AND “AS AVAILABLE” BASIS. LINKEDIN MAKES NO REPRESENTATION OR WARRANTY, 49 | WHETHER EXPRESS OR IMPLIED, ABOUT THE LICENSED MATERIALS, INCLUDING ANY 50 | REPRESENTATION THAT THE LICENSED MATERIALS WILL BE FREE OF ERRORS, BUGS OR 51 | INTERRUPTIONS, OR THAT THE LICENSED MATERIALS ARE ACCURATE, COMPLETE OR 52 | OTHERWISE VALID. TO THE FULLEST EXTENT PERMITTED BY LAW, LINKEDIN AND ITS 53 | AFFILIATES DISCLAIM ANY IMPLIED OR STATUTORY WARRANTY OR CONDITION, INCLUDING 54 | ANY IMPLIED WARRANTY OR CONDITION OF MERCHANTABILITY OR FITNESS FOR A 55 | PARTICULAR PURPOSE, AVAILABILITY, SECURITY, TITLE AND/OR NON-INFRINGEMENT. 56 | YOUR USE OF THE LICENSED MATERIALS IS AT YOUR OWN DISCRETION AND RISK, AND 57 | YOU WILL BE SOLELY RESPONSIBLE FOR ANY DAMAGE THAT RESULTS FROM USE OF THE 58 | LICENSED MATERIALS TO YOUR COMPUTER SYSTEM OR LOSS OF DATA. NO ADVICE OR 59 | INFORMATION, WHETHER ORAL OR WRITTEN, OBTAINED BY YOU FROM US OR THROUGH OR 60 | FROM THE LICENSED MATERIALS WILL CREATE ANY WARRANTY OR CONDITION NOT 61 | EXPRESSLY STATED IN THESE TERMS. 62 | 63 | 5. Limitation of Liability. LINKEDIN SHALL NOT BE LIABLE FOR ANY INDIRECT, 64 | INCIDENTAL, SPECIAL, PUNITIVE, CONSEQUENTIAL OR EXEMPLARY DAMAGES, INCLUDING 65 | BUT NOT LIMITED TO, DAMAGES FOR LOSS OF PROFITS, GOODWILL, USE, DATA OR OTHER 66 | INTANGIBLE LOSSES . IN NO EVENT WILL LINKEDIN'S AGGREGATE LIABILITY TO YOU 67 | EXCEED $100. THIS LIMITATION OF LIABILITY SHALL: 68 | - i. APPLY REGARDLESS OF WHETHER (A) YOU BASE YOUR CLAIM ON CONTRACT, TORT, 69 | STATUTE, OR ANY OTHER LEGAL THEORY, (B) WE KNEW OR SHOULD HAVE KNOWN ABOUT 70 | THE POSSIBILITY OF SUCH DAMAGES, OR (C) THE LIMITED REMEDIES PROVIDED IN THIS 71 | SECTION FAIL OF THEIR ESSENTIAL PURPOSE; AND 72 | - ii. NOT APPLY TO ANY DAMAGE THAT LINKEDIN MAY CAUSE YOU INTENTIONALLY OR 73 | KNOWINGLY IN VIOLATION OF THESE TERMS OR APPLICABLE LAW, OR AS OTHERWISE 74 | MANDATED BY APPLICABLE LAW THAT CANNOT BE DISCLAIMED IN THESE TERMS. 75 | 76 | 6. Termination. This Agreement automatically terminates upon your breach of 77 | this Agreement or termination of your LinkedIn Learning subscription. On 78 | termination, all licenses granted under this Agreement will terminate 79 | immediately and you will delete the Licensed Materials. Sections 2-7 of this 80 | Agreement survive any termination of this Agreement. LinkedIn may discontinue 81 | the availability of some or all of the Licensed Materials at any time for any 82 | reason. 83 | 84 | 7. Miscellaneous. This Agreement will be governed by and construed in 85 | accordance with the laws of the State of California without regard to conflict 86 | of laws principles. The exclusive forum for any disputes arising out of or 87 | relating to this Agreement shall be an appropriate federal or state court 88 | sitting in the County of Santa Clara, State of California. If LinkedIn does 89 | not act to enforce a breach of this Agreement, that does not mean that 90 | LinkedIn has waived its right to enforce this Agreement. The Agreement does 91 | not create a partnership, agency relationship, or joint venture between the 92 | parties. Neither party has the power or authority to bind the other or to 93 | create any obligation or responsibility on behalf of the other. You may not, 94 | without LinkedIn’s prior written consent, assign or delegate any rights or 95 | obligations under these terms, including in connection with a change of 96 | control. Any purported assignment and delegation shall be ineffective. The 97 | Agreement shall bind and inure to the benefit of the parties, their respective 98 | successors and permitted assigns. If any provision of the Agreement is 99 | unenforceable, that provision will be modified to render it enforceable to the 100 | extent possible to give effect to the parties’ intentions and the remaining 101 | provisions will not be affected. This Agreement is the only agreement between 102 | you and LinkedIn regarding the Licensed Materials, and supersedes all prior 103 | agreements relating to the Licensed Materials. 104 | 105 | Last Updated: March 2019 106 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2023 LinkedIn Corporation 2 | All Rights Reserved. 3 | 4 | Licensed under the LinkedIn Learning Exercise File License (the "License"). 5 | See LICENSE in the project root for license information. 6 | 7 | ATTRIBUTIONS: 8 | 9 | JUnit 10 | https://github.com/junit-team/junit5 11 | License: Eclipse Public License - v 2.0 12 | https://www.eclipse.org/legal/epl-2.0/ 13 | 14 | Please note, this project may automatically load third party code from external 15 | repositories (for example, NPM modules, Composer packages, or other dependencies). 16 | If so, such third party code may be subject to other license terms than as set 17 | forth above. In addition, such third party code may also depend on and load 18 | multiple tiers of dependencies. Please review the applicable licenses of the 19 | additional dependencies. 20 | 21 | =-=-=-=-=-=-=-=-=-=-=-=-=-= 22 | 23 | "Eclipse Public License - v 2.0 24 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE (“AGREEMENT”). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 25 | 26 | 1. Definitions 27 | “Contribution” means: 28 | 29 | a) in the case of the initial Contributor, the initial content Distributed under this Agreement, and 30 | b) in the case of each subsequent Contributor: 31 | i) changes to the Program, and 32 | ii) additions to the Program; where such changes and/or additions to the Program originate from and are Distributed by that particular Contributor. A Contribution “originates” from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include changes or additions to the Program that are not Modified Works. 33 | “Contributor” means any person or entity that Distributes the Program. 34 | 35 | “Licensed Patents” mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program. 36 | 37 | “Program” means the Contributions Distributed in accordance with this Agreement. 38 | 39 | “Recipient” means anyone who receives the Program under this Agreement or any Secondary License (as applicable), including Contributors. 40 | 41 | “Derivative Works” shall mean any work, whether in Source Code or other form, that is based on (or derived from) the Program and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. 42 | 43 | “Modified Works” shall mean any work in Source Code or other form that results from an addition to, deletion from, or modification of the contents of the Program, including, for purposes of clarity any new file in Source Code form that contains any contents of the Program. Modified Works shall not include works that contain only declarations, interfaces, types, classes, structures, or files of the Program solely in each case in order to link to, bind by name, or subclass the Program or Modified Works thereof. 44 | 45 | “Distribute” means the acts of a) distributing or b) making available in any manner that enables the transfer of a copy. 46 | 47 | “Source Code” means the form of a Program preferred for making modifications, including but not limited to software source code, documentation source, and configuration files. 48 | 49 | “Secondary License” means either the GNU General Public License, Version 2.0, or any later versions of that license, including any exceptions or additional permissions as identified by the initial Contributor. 50 | 51 | 2. Grant of Rights 52 | a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, Distribute and sublicense the Contribution of such Contributor, if any, and such Derivative Works. 53 | 54 | b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in Source Code or other form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder. 55 | 56 | c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to Distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program. 57 | 58 | d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement. 59 | 60 | e) Notwithstanding the terms of any Secondary License, no Contributor makes additional grants to any Recipient (other than those set forth in this Agreement) as a result of such Recipient's receipt of the Program under the terms of a Secondary License (if permitted under the terms of Section 3). 61 | 62 | 3. Requirements 63 | 3.1 If a Contributor Distributes the Program in any form, then: 64 | 65 | a) the Program must also be made available as Source Code, in accordance with section 3.2, and the Contributor must accompany the Program with a statement that the Source Code for the Program is available under this Agreement, and informs Recipients how to obtain it in a reasonable manner on or through a medium customarily used for software exchange; and 66 | 67 | b) the Contributor may Distribute the Program under a license different than this Agreement, provided that such license: 68 | 69 | i) effectively disclaims on behalf of all other Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose; 70 | ii) effectively excludes on behalf of all other Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits; 71 | iii) does not attempt to limit or alter the recipients' rights in the Source Code under section 3.2; and 72 | iv) requires any subsequent distribution of the Program by any party to be under a license that satisfies the requirements of this section 3. 73 | 3.2 When the Program is Distributed as Source Code: 74 | 75 | a) it must be made available under this Agreement, or if the Program (i) is combined with other material in a separate file or files made available under a Secondary License, and (ii) the initial Contributor attached to the Source Code the notice described in Exhibit A of this Agreement, then the Program may be made available under the terms of such Secondary Licenses, and 76 | b) a copy of this Agreement must be included with each copy of the Program. 77 | 3.3 Contributors may not remove or alter any copyright, patent, trademark, attribution notices, disclaimers of warranty, or limitations of liability (“notices”) contained within the Program from any copy of the Program which they Distribute, provided that Contributors may add their own appropriate notices. 78 | 79 | 4. Commercial Distribution 80 | Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor (“Commercial Contributor”) hereby agrees to defend and indemnify every other Contributor (“Indemnified Contributor”) against any losses, damages and costs (collectively “Losses”) arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense. 81 | 82 | For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages. 83 | 84 | 5. No Warranty 85 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE PROGRAM IS PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations. 86 | 87 | 6. Disclaimer of Liability 88 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT PERMITTED BY APPLICABLE LAW, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 89 | 90 | 7. General 91 | If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. 92 | 93 | If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed. 94 | 95 | All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive. 96 | 97 | Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be Distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to Distribute the Program (including its Contributions) under the new version. 98 | 99 | Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved. Nothing in this Agreement is intended to be enforceable by any entity that is not a Contributor or Recipient. No third-party beneficiary rights are created under this Agreement. 100 | 101 | Exhibit A - Form of Secondary Licenses Notice 102 | “This Source Code may also be made available under the following Secondary Licenses when the conditions for such availability set forth in the Eclipse Public License, v. 2.0 are satisfied: {name license(s), version(s), and exceptions or additional permissions here}.” 103 | 104 | Simply including a copy of this Agreement, including this Exhibit A is not sufficient to license the Source Code under Secondary Licenses. 105 | 106 | If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. 107 | 108 | You may add additional accurate notices of copyright ownership." 109 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advanced Java Development 2 | This is the repository for the LinkedIn Learning course Advanced Java Development. The full course is available from [LinkedIn Learning][lil-course-url]. 3 | 4 | ![Advanced Java Development][lil-thumbnail-url] 5 | 6 | You’re comfortable with the basics of writing in Java, but you want to level up your skills and be more productive. In this course, Bethan Palmer has exactly what you need, with topics including generics, multi-threading, inputs and outputs, and more. After explaining the differences between versions of Java, Bethan dives into generics in Java, advanced data structures, and functional programming. She covers concurrency in Java, including how to use the Runnable interface, the ExecutorService, and synchronized methods. Bethan highlights topics around input and output like reading from System.in with a scanner, reading files with BufferedReader, and using try-with-resources. She also guides you through working with files in Java, creating a new file, working with directories, and copying files in Java. Each chapter includes challenges and solutions to reinforce what you’re learning. 7 | 8 | ## Instructions 9 | All of the code needed for this course is on the main branch. 10 | 11 | The package names correspond to the videos in this course. The naming convention is _CHAPTER#_MOVIE#. As an example, the package named _02_03 corresponds to the second chapter and the third video in that chapter. 12 | 13 | Some code examples will have a before and an end state. In this case the package will contain two more directories, `before` and `end`. The `before` directory will contain the code as it is at the beginning of the movie. The `end` directory contains the code as it is at the end of the movie. 14 | 15 | 16 | ## Installing 17 | 1. To use these exercise files, you must have Java 8 or higher installed. 18 | 2. Clone this repository into your local machine using the terminal (Mac), CMD (Windows), or a GUI tool like SourceTree. 19 | 3. Open the code in your favorite IDE. 20 | 21 | ### Instructor 22 | 23 | Bethan Palmer 24 | 25 | Software Developer 26 | 27 | 28 | 29 | Check out my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/bethan-palmer). 30 | 31 | [lil-course-url]: https://www.linkedin.com/learning/advanced-java-development?dApp=59033956 32 | [lil-thumbnail-url]: https://media.licdn.com/dms/image/C4E0DAQGw3sFhiEecwQ/learning-public-crop_675_1200/0/1670989829357?e=2147483647&v=beta&t=e1ZCDsQq3979dg7TJ-4fhKqP-uZrkUNshZ5x4DEXZO0 33 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Apache Maven Wrapper startup batch script, version 3.1.1 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | # e.g. to debug Maven itself, use 32 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | # ---------------------------------------------------------------------------- 35 | 36 | if [ -z "$MAVEN_SKIP_RC" ] ; then 37 | 38 | if [ -f /usr/local/etc/mavenrc ] ; then 39 | . /usr/local/etc/mavenrc 40 | fi 41 | 42 | if [ -f /etc/mavenrc ] ; then 43 | . /etc/mavenrc 44 | fi 45 | 46 | if [ -f "$HOME/.mavenrc" ] ; then 47 | . "$HOME/.mavenrc" 48 | fi 49 | 50 | fi 51 | 52 | # OS specific support. $var _must_ be set to either true or false. 53 | cygwin=false; 54 | darwin=false; 55 | mingw=false 56 | case "`uname`" in 57 | CYGWIN*) cygwin=true ;; 58 | MINGW*) mingw=true;; 59 | Darwin*) darwin=true 60 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 61 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 62 | if [ -z "$JAVA_HOME" ]; then 63 | if [ -x "/usr/libexec/java_home" ]; then 64 | JAVA_HOME="`/usr/libexec/java_home`"; export JAVA_HOME 65 | else 66 | JAVA_HOME="/Library/Java/Home"; export JAVA_HOME 67 | fi 68 | fi 69 | ;; 70 | esac 71 | 72 | if [ -z "$JAVA_HOME" ] ; then 73 | if [ -r /etc/gentoo-release ] ; then 74 | JAVA_HOME=`java-config --jre-home` 75 | fi 76 | fi 77 | 78 | # For Cygwin, ensure paths are in UNIX format before anything is touched 79 | if $cygwin ; then 80 | [ -n "$JAVA_HOME" ] && 81 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 82 | [ -n "$CLASSPATH" ] && 83 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 84 | fi 85 | 86 | # For Mingw, ensure paths are in UNIX format before anything is touched 87 | if $mingw ; then 88 | [ -n "$JAVA_HOME" ] && 89 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 90 | fi 91 | 92 | if [ -z "$JAVA_HOME" ]; then 93 | javaExecutable="`which javac`" 94 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 95 | # readlink(1) is not available as standard on Solaris 10. 96 | readLink=`which readlink` 97 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 98 | if $darwin ; then 99 | javaHome="`dirname \"$javaExecutable\"`" 100 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 101 | else 102 | javaExecutable="`readlink -f \"$javaExecutable\"`" 103 | fi 104 | javaHome="`dirname \"$javaExecutable\"`" 105 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 106 | JAVA_HOME="$javaHome" 107 | export JAVA_HOME 108 | fi 109 | fi 110 | fi 111 | 112 | if [ -z "$JAVACMD" ] ; then 113 | if [ -n "$JAVA_HOME" ] ; then 114 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 115 | # IBM's JDK on AIX uses strange locations for the executables 116 | JAVACMD="$JAVA_HOME/jre/sh/java" 117 | else 118 | JAVACMD="$JAVA_HOME/bin/java" 119 | fi 120 | else 121 | JAVACMD="`\\unset -f command; \\command -v java`" 122 | fi 123 | fi 124 | 125 | if [ ! -x "$JAVACMD" ] ; then 126 | echo "Error: JAVA_HOME is not defined correctly." >&2 127 | echo " We cannot execute $JAVACMD" >&2 128 | exit 1 129 | fi 130 | 131 | if [ -z "$JAVA_HOME" ] ; then 132 | echo "Warning: JAVA_HOME environment variable is not set." 133 | fi 134 | 135 | # traverses directory structure from process work directory to filesystem root 136 | # first directory with .mvn subdirectory is considered project base directory 137 | find_maven_basedir() { 138 | if [ -z "$1" ] 139 | then 140 | echo "Path not specified to find_maven_basedir" 141 | return 1 142 | fi 143 | 144 | basedir="$1" 145 | wdir="$1" 146 | while [ "$wdir" != '/' ] ; do 147 | if [ -d "$wdir"/.mvn ] ; then 148 | basedir=$wdir 149 | break 150 | fi 151 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 152 | if [ -d "${wdir}" ]; then 153 | wdir=`cd "$wdir/.."; pwd` 154 | fi 155 | # end of workaround 156 | done 157 | printf '%s' "$(cd "$basedir"; pwd)" 158 | } 159 | 160 | # concatenates all lines of a file 161 | concat_lines() { 162 | if [ -f "$1" ]; then 163 | echo "$(tr -s '\n' ' ' < "$1")" 164 | fi 165 | } 166 | 167 | BASE_DIR=$(find_maven_basedir "$(dirname $0)") 168 | if [ -z "$BASE_DIR" ]; then 169 | exit 1; 170 | fi 171 | 172 | MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}; export MAVEN_PROJECTBASEDIR 173 | if [ "$MVNW_VERBOSE" = true ]; then 174 | echo $MAVEN_PROJECTBASEDIR 175 | fi 176 | 177 | ########################################################################################## 178 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 179 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 180 | ########################################################################################## 181 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 182 | if [ "$MVNW_VERBOSE" = true ]; then 183 | echo "Found .mvn/wrapper/maven-wrapper.jar" 184 | fi 185 | else 186 | if [ "$MVNW_VERBOSE" = true ]; then 187 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 188 | fi 189 | if [ -n "$MVNW_REPOURL" ]; then 190 | wrapperUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar" 191 | else 192 | wrapperUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar" 193 | fi 194 | while IFS="=" read key value; do 195 | case "$key" in (wrapperUrl) wrapperUrl="$value"; break ;; 196 | esac 197 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 198 | if [ "$MVNW_VERBOSE" = true ]; then 199 | echo "Downloading from: $wrapperUrl" 200 | fi 201 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 202 | if $cygwin; then 203 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 204 | fi 205 | 206 | if command -v wget > /dev/null; then 207 | QUIET="--quiet" 208 | if [ "$MVNW_VERBOSE" = true ]; then 209 | echo "Found wget ... using wget" 210 | QUIET="" 211 | fi 212 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 213 | wget $QUIET "$wrapperUrl" -O "$wrapperJarPath" 214 | else 215 | wget $QUIET --http-user="$MVNW_USERNAME" --http-password="$MVNW_PASSWORD" "$wrapperUrl" -O "$wrapperJarPath" 216 | fi 217 | [ $? -eq 0 ] || rm -f "$wrapperJarPath" 218 | elif command -v curl > /dev/null; then 219 | QUIET="--silent" 220 | if [ "$MVNW_VERBOSE" = true ]; then 221 | echo "Found curl ... using curl" 222 | QUIET="" 223 | fi 224 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 225 | curl $QUIET -o "$wrapperJarPath" "$wrapperUrl" -f -L 226 | else 227 | curl $QUIET --user "$MVNW_USERNAME:$MVNW_PASSWORD" -o "$wrapperJarPath" "$wrapperUrl" -f -L 228 | fi 229 | [ $? -eq 0 ] || rm -f "$wrapperJarPath" 230 | else 231 | if [ "$MVNW_VERBOSE" = true ]; then 232 | echo "Falling back to using Java to download" 233 | fi 234 | javaSource="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 235 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" 236 | # For Cygwin, switch paths to Windows format before running javac 237 | if $cygwin; then 238 | javaSource=`cygpath --path --windows "$javaSource"` 239 | javaClass=`cygpath --path --windows "$javaClass"` 240 | fi 241 | if [ -e "$javaSource" ]; then 242 | if [ ! -e "$javaClass" ]; then 243 | if [ "$MVNW_VERBOSE" = true ]; then 244 | echo " - Compiling MavenWrapperDownloader.java ..." 245 | fi 246 | # Compiling the Java class 247 | ("$JAVA_HOME/bin/javac" "$javaSource") 248 | fi 249 | if [ -e "$javaClass" ]; then 250 | # Running the downloader 251 | if [ "$MVNW_VERBOSE" = true ]; then 252 | echo " - Running MavenWrapperDownloader.java ..." 253 | fi 254 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 255 | fi 256 | fi 257 | fi 258 | fi 259 | ########################################################################################## 260 | # End of extension 261 | ########################################################################################## 262 | 263 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 264 | 265 | # For Cygwin, switch paths to Windows format before running java 266 | if $cygwin; then 267 | [ -n "$JAVA_HOME" ] && 268 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 269 | [ -n "$CLASSPATH" ] && 270 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 271 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 272 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 273 | fi 274 | 275 | # Provide a "standardized" way to retrieve the CLI args that will 276 | # work with both Windows and non-Windows executions. 277 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 278 | export MAVEN_CMD_LINE_ARGS 279 | 280 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 281 | 282 | exec "$JAVACMD" \ 283 | $MAVEN_OPTS \ 284 | $MAVEN_DEBUG_OPTS \ 285 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 286 | "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 287 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 288 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Apache Maven Wrapper startup batch script, version 3.1.1 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 28 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 29 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 30 | @REM e.g. to debug Maven itself, use 31 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 32 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 33 | @REM ---------------------------------------------------------------------------- 34 | 35 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 36 | @echo off 37 | @REM set title of command window 38 | title %0 39 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 40 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 41 | 42 | @REM set %HOME% to equivalent of $HOME 43 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 44 | 45 | @REM Execute a user defined script before this one 46 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 47 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 48 | if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* 49 | if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* 50 | :skipRcPre 51 | 52 | @setlocal 53 | 54 | set ERROR_CODE=0 55 | 56 | @REM To isolate internal variables from possible post scripts, we use another setlocal 57 | @setlocal 58 | 59 | @REM ==== START VALIDATION ==== 60 | if not "%JAVA_HOME%" == "" goto OkJHome 61 | 62 | echo. 63 | echo Error: JAVA_HOME not found in your environment. >&2 64 | echo Please set the JAVA_HOME variable in your environment to match the >&2 65 | echo location of your Java installation. >&2 66 | echo. 67 | goto error 68 | 69 | :OkJHome 70 | if exist "%JAVA_HOME%\bin\java.exe" goto init 71 | 72 | echo. 73 | echo Error: JAVA_HOME is set to an invalid directory. >&2 74 | echo JAVA_HOME = "%JAVA_HOME%" >&2 75 | echo Please set the JAVA_HOME variable in your environment to match the >&2 76 | echo location of your Java installation. >&2 77 | echo. 78 | goto error 79 | 80 | @REM ==== END VALIDATION ==== 81 | 82 | :init 83 | 84 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 85 | @REM Fallback to current working directory if not found. 86 | 87 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 88 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 89 | 90 | set EXEC_DIR=%CD% 91 | set WDIR=%EXEC_DIR% 92 | :findBaseDir 93 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 94 | cd .. 95 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 96 | set WDIR=%CD% 97 | goto findBaseDir 98 | 99 | :baseDirFound 100 | set MAVEN_PROJECTBASEDIR=%WDIR% 101 | cd "%EXEC_DIR%" 102 | goto endDetectBaseDir 103 | 104 | :baseDirNotFound 105 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 106 | cd "%EXEC_DIR%" 107 | 108 | :endDetectBaseDir 109 | 110 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 111 | 112 | @setlocal EnableExtensions EnableDelayedExpansion 113 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 114 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 115 | 116 | :endReadAdditionalConfig 117 | 118 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | set WRAPPER_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar" 123 | 124 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 125 | IF "%%A"=="wrapperUrl" SET WRAPPER_URL=%%B 126 | ) 127 | 128 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 129 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 130 | if exist %WRAPPER_JAR% ( 131 | if "%MVNW_VERBOSE%" == "true" ( 132 | echo Found %WRAPPER_JAR% 133 | ) 134 | ) else ( 135 | if not "%MVNW_REPOURL%" == "" ( 136 | SET WRAPPER_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar" 137 | ) 138 | if "%MVNW_VERBOSE%" == "true" ( 139 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 140 | echo Downloading from: %WRAPPER_URL% 141 | ) 142 | 143 | powershell -Command "&{"^ 144 | "$webclient = new-object System.Net.WebClient;"^ 145 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 146 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 147 | "}"^ 148 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%WRAPPER_URL%', '%WRAPPER_JAR%')"^ 149 | "}" 150 | if "%MVNW_VERBOSE%" == "true" ( 151 | echo Finished downloading %WRAPPER_JAR% 152 | ) 153 | ) 154 | @REM End of extension 155 | 156 | @REM Provide a "standardized" way to retrieve the CLI args that will 157 | @REM work with both Windows and non-Windows executions. 158 | set MAVEN_CMD_LINE_ARGS=%* 159 | 160 | %MAVEN_JAVA_EXE% ^ 161 | %JVM_CONFIG_MAVEN_PROPS% ^ 162 | %MAVEN_OPTS% ^ 163 | %MAVEN_DEBUG_OPTS% ^ 164 | -classpath %WRAPPER_JAR% ^ 165 | "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ 166 | %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 167 | if ERRORLEVEL 1 goto error 168 | goto end 169 | 170 | :error 171 | set ERROR_CODE=1 172 | 173 | :end 174 | @endlocal & set ERROR_CODE=%ERROR_CODE% 175 | 176 | if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost 177 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 178 | if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" 179 | if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" 180 | :skipRcPost 181 | 182 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 183 | if "%MAVEN_BATCH_PAUSE%"=="on" pause 184 | 185 | if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% 186 | 187 | cmd /C exit /B %ERROR_CODE% 188 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | advanced-java-development-2515082 8 | advanced-java-development-2515082 9 | 1.0-SNAPSHOT 10 | 11 | 17 12 | 17 13 | 14 | 15 | 16 | 17 | org.junit.jupiter 18 | junit-jupiter-engine 19 | 5.9.0 20 | test 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/main/java/_02_01/before/GenericsExample.java: -------------------------------------------------------------------------------- 1 | package _02_01.before; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class GenericsExample { 7 | 8 | public static void main(String[] args) { 9 | 10 | List shapes = new ArrayList(); 11 | shapes.add("Circle"); 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/_02_01/end/GenericsExample.java: -------------------------------------------------------------------------------- 1 | package _02_01.end; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class GenericsExample { 7 | 8 | public static void main(String[] args) { 9 | 10 | List shapes = new ArrayList<>(); 11 | shapes.add("Circle"); 12 | System.out.println(shapes.get(0)); 13 | String circle = shapes.get(0); 14 | shapes.add("Rectangle"); 15 | String rectangle = shapes.get(1); 16 | 17 | 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/_02_02/before/GenericMethods.java: -------------------------------------------------------------------------------- 1 | package _02_02.before; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class GenericMethods { 7 | 8 | public static void main(String[] args) { 9 | String[] words = {"apple", "banana", "pear"}; 10 | Integer[] numbers = {1, 5, 7}; 11 | } 12 | 13 | 14 | 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/_02_02/end/GenericMethods.java: -------------------------------------------------------------------------------- 1 | package _02_02.end; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class GenericMethods { 7 | 8 | public static void main(String[] args) { 9 | String[] words = {"apple", "banana", "pear"}; 10 | Integer[] numbers = {1, 5, 7}; 11 | List wordsList = convertArrayToList(words); 12 | List numbersList = convertArrayToList(numbers); 13 | System.out.println(wordsList); 14 | System.out.println(numbersList); 15 | } 16 | 17 | private static List convertArrayToList(T[] array) { 18 | return Arrays.asList(array); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/_02_03/before/BoundedGenerics.java: -------------------------------------------------------------------------------- 1 | package _02_03.before; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class BoundedGenerics { 7 | 8 | public static void main(String[] args) { 9 | String[] words = {"apple", "banana", "pear"}; 10 | Integer[] numbers = {1, 5, 7}; 11 | List wordsList = convertArrayToList(words); 12 | List numbersList = convertArrayToList(numbers); 13 | System.out.println(wordsList); 14 | System.out.println(numbersList); 15 | } 16 | 17 | private static List convertArrayToList(T[] array) { 18 | return Arrays.asList(array); 19 | } 20 | 21 | 22 | } -------------------------------------------------------------------------------- /src/main/java/_02_03/end/BoundedGenerics.java: -------------------------------------------------------------------------------- 1 | package _02_03.end; 2 | 3 | import java.util.*; 4 | 5 | public class BoundedGenerics { 6 | 7 | public static void main(String[] args) { 8 | Double[] doubles = {2.0, 3.5, 14.7}; 9 | Integer[] numbers = {1, 5, 7}; 10 | List doublesList = convertArrayToList(doubles); 11 | List numbersList = convertArrayToList(numbers); 12 | System.out.println(doublesList); 13 | System.out.println(numbersList); 14 | } 15 | 16 | private static List convertArrayToList(T[] array) { 17 | return Arrays.asList(array); 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/_02_04/before/ClothingItem.java: -------------------------------------------------------------------------------- 1 | package _02_04.before; 2 | 3 | public abstract class ClothingItem { 4 | 5 | abstract int getPrice(); 6 | 7 | abstract String getName(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_02_04/before/ClothingSite.java: -------------------------------------------------------------------------------- 1 | package _02_04.before; 2 | 3 | public class ClothingSite { 4 | 5 | static void checkoutItem(ClothingItem item) { 6 | System.out.println("Item purchased: " + item.getName() + ", price: " + item.getPrice()); 7 | } 8 | 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/_02_04/before/JacketItem.java: -------------------------------------------------------------------------------- 1 | package _02_04.before; 2 | 3 | public class JacketItem extends ClothingItem { 4 | 5 | @Override 6 | int getPrice() { 7 | return 25; 8 | } 9 | 10 | @Override 11 | String getName() { 12 | return "Jacket"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_02_04/before/ShirtItem.java: -------------------------------------------------------------------------------- 1 | package _02_04.before; 2 | 3 | public class ShirtItem extends ClothingItem { 4 | 5 | @Override 6 | int getPrice() { 7 | return 10; 8 | } 9 | 10 | @Override 11 | String getName() { 12 | return "Shirt"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_02_04/end/ClothingItem.java: -------------------------------------------------------------------------------- 1 | package _02_04.end; 2 | 3 | public abstract class ClothingItem { 4 | 5 | abstract int getPrice(); 6 | 7 | abstract String getName(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_02_04/end/ClothingSite.java: -------------------------------------------------------------------------------- 1 | package _02_04.end; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class ClothingSite { 7 | 8 | public static void main(String[] args) { 9 | 10 | ShirtItem shirtItem = new ShirtItem(); 11 | checkoutItem(shirtItem); 12 | 13 | JacketItem jacketItem = new JacketItem(); 14 | checkoutItem(jacketItem); 15 | 16 | List clothingItems = new ArrayList<>(); 17 | clothingItems.add(shirtItem); 18 | clothingItems.add(jacketItem); 19 | 20 | checkoutAllItems(clothingItems); 21 | } 22 | 23 | static void checkoutItem(ClothingItem item) { 24 | System.out.println("Item purchased: " + item.getName() + ", price: " + item.getPrice()); 25 | } 26 | 27 | static void checkoutAllItems(List clothingItems) { 28 | 29 | for (ClothingItem clothingItem : clothingItems) { 30 | checkoutItem(clothingItem); 31 | } 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/_02_04/end/JacketItem.java: -------------------------------------------------------------------------------- 1 | package _02_04.end; 2 | 3 | public class JacketItem extends ClothingItem { 4 | 5 | @Override 6 | int getPrice() { 7 | return 25; 8 | } 9 | 10 | @Override 11 | String getName() { 12 | return "Jacket"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_02_04/end/ShirtItem.java: -------------------------------------------------------------------------------- 1 | package _02_04.end; 2 | 3 | public class ShirtItem extends ClothingItem { 4 | 5 | @Override 6 | int getPrice() { 7 | return 10; 8 | } 9 | 10 | @Override 11 | String getName() { 12 | return "Shirt"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_02_05/before/ClothingItem.java: -------------------------------------------------------------------------------- 1 | package _02_05.before; 2 | 3 | public abstract class ClothingItem { 4 | 5 | abstract int getPrice(); 6 | 7 | abstract String getName(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_02_05/before/ClothingSite.java: -------------------------------------------------------------------------------- 1 | package _02_05.before; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class ClothingSite { 7 | 8 | public static void main(String[] args) { 9 | 10 | ShirtItem shirtItem = new ShirtItem(); 11 | JacketItem jacketItem = new JacketItem(); 12 | 13 | List clothingItems = new ArrayList<>(); 14 | clothingItems.add(shirtItem); 15 | clothingItems.add(jacketItem); 16 | 17 | checkoutAllItems(clothingItems); 18 | 19 | } 20 | 21 | static void checkoutItem(ClothingItem item) { 22 | System.out.println("Item purchased: " + item.getName() + ", price: " + item.getPrice()); 23 | } 24 | 25 | static void checkoutAllItems(List clothingItems) { 26 | 27 | for (ClothingItem clothingItem : clothingItems) { 28 | checkoutItem(clothingItem); 29 | } 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/_02_05/before/JacketItem.java: -------------------------------------------------------------------------------- 1 | package _02_05.before; 2 | 3 | public class JacketItem extends ClothingItem { 4 | 5 | @Override 6 | int getPrice() { 7 | return 25; 8 | } 9 | 10 | @Override 11 | String getName() { 12 | return "Jacket"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_02_05/before/ShirtItem.java: -------------------------------------------------------------------------------- 1 | package _02_05.before; 2 | 3 | public class ShirtItem extends ClothingItem { 4 | 5 | @Override 6 | int getPrice() { 7 | return 10; 8 | } 9 | 10 | @Override 11 | String getName() { 12 | return "Shirt"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_02_05/end/ClothingItem.java: -------------------------------------------------------------------------------- 1 | package _02_05.end; 2 | 3 | public abstract class ClothingItem { 4 | 5 | abstract int getPrice(); 6 | 7 | abstract String getName(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_02_05/end/ClothingSite.java: -------------------------------------------------------------------------------- 1 | package _02_05.end; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class ClothingSite { 7 | 8 | public static void main(String[] args) { 9 | 10 | ShirtItem shirtItemOne = new ShirtItem(); 11 | ShirtItem shirtItemTwo = new ShirtItem(); 12 | 13 | List shirtItems = new ArrayList<>(); 14 | shirtItems.add(shirtItemOne); 15 | shirtItems.add(shirtItemTwo); 16 | 17 | checkoutAllItems(shirtItems); 18 | 19 | 20 | } 21 | 22 | static void checkoutItem(ClothingItem item) { 23 | System.out.println("Item purchased: " + item.getName() + ", price: " + item.getPrice()); 24 | } 25 | 26 | static void checkoutAllItems(List clothingItems) { 27 | 28 | for (ClothingItem clothingItem : clothingItems) { 29 | checkoutItem(clothingItem); 30 | } 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/_02_05/end/JacketItem.java: -------------------------------------------------------------------------------- 1 | package _02_05.end; 2 | 3 | public class JacketItem extends ClothingItem { 4 | 5 | @Override 6 | int getPrice() { 7 | return 25; 8 | } 9 | 10 | @Override 11 | String getName() { 12 | return "Jacket"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_02_05/end/ShirtItem.java: -------------------------------------------------------------------------------- 1 | package _02_05.end; 2 | 3 | public class ShirtItem extends ClothingItem { 4 | 5 | @Override 6 | int getPrice() { 7 | return 10; 8 | } 9 | 10 | @Override 11 | String getName() { 12 | return "Shirt"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_02_06/AdultTicket.java: -------------------------------------------------------------------------------- 1 | package _02_06; 2 | 3 | public class AdultTicket extends Ticket { 4 | 5 | @Override 6 | public int getPrice() { 7 | return 10; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | return "Adult ticket"; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/_02_06/ChildTicket.java: -------------------------------------------------------------------------------- 1 | package _02_06; 2 | 3 | public class ChildTicket extends Ticket { 4 | 5 | 6 | @Override 7 | public int getPrice() { 8 | return 5; 9 | } 10 | 11 | @Override 12 | public String toString() { 13 | return "Child ticket"; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/_02_06/Ticket.java: -------------------------------------------------------------------------------- 1 | package _02_06; 2 | 3 | public abstract class Ticket { 4 | public abstract int getPrice(); 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/_02_06/TicketMachine.java: -------------------------------------------------------------------------------- 1 | package _02_06; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import static java.util.Arrays.asList; 7 | 8 | public class TicketMachine { 9 | 10 | public static void main(String[] args) { 11 | 12 | List> destinationLists = asList( 13 | asList("New York", "Boston"), 14 | asList("Los Angeles", "San Francisco")); 15 | 16 | 17 | List> ticketLists = asList( 18 | asList(new AdultTicket(), new AdultTicket()), 19 | asList(new ChildTicket(), new ChildTicket()) 20 | ); 21 | 22 | List destinations = flattenList(destinationLists); 23 | System.out.println(destinations); 24 | 25 | // List tickets = flattenList(ticketLists); 26 | // System.out.println(tickets); 27 | 28 | // System.out.println("Total price: " + getTotalPrice(tickets)); 29 | 30 | // List adultTickets = asList(new AdultTicket(), new AdultTicket()); 31 | // System.out.println("Total price: " + getTotalPrice(adultTickets)); 32 | 33 | } 34 | 35 | 36 | //This should work with lists containing any type of object 37 | static List flattenList(List> nestedList) { 38 | 39 | List flattenedList = new ArrayList<>(); 40 | nestedList.forEach(flattenedList::addAll); 41 | return flattenedList; 42 | 43 | } 44 | 45 | // This should work with lists containing Ticket objects or any subclass of Ticket 46 | static int getTotalPrice(List tickets) { 47 | int totalPrice = 0; 48 | for (Ticket ticket : tickets) { 49 | totalPrice += ticket.getPrice(); 50 | } 51 | return totalPrice; 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/_02_07/before/AdultTicket.java: -------------------------------------------------------------------------------- 1 | package _02_07.before; 2 | 3 | public class AdultTicket extends Ticket { 4 | 5 | @Override 6 | public int getPrice() { 7 | return 10; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | return "Adult ticket"; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/_02_07/before/ChildTicket.java: -------------------------------------------------------------------------------- 1 | package _02_07.before; 2 | 3 | public class ChildTicket extends Ticket { 4 | 5 | 6 | @Override 7 | public int getPrice() { 8 | return 5; 9 | } 10 | 11 | @Override 12 | public String toString() { 13 | return "Child ticket"; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/_02_07/before/Ticket.java: -------------------------------------------------------------------------------- 1 | package _02_07.before; 2 | 3 | public abstract class Ticket { 4 | public abstract int getPrice(); 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/_02_07/before/TicketMachine.java: -------------------------------------------------------------------------------- 1 | package _02_07.before; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import static java.util.Arrays.asList; 7 | 8 | public class TicketMachine { 9 | 10 | public static void main(String[] args) { 11 | 12 | List> destinationLists = asList( 13 | asList("New York", "Boston"), 14 | asList("Los Angeles", "San Francisco")); 15 | 16 | 17 | List> ticketLists = asList( 18 | asList(new AdultTicket(), new AdultTicket()), 19 | asList(new ChildTicket(), new ChildTicket()) 20 | ); 21 | 22 | List destinations = flattenList(destinationLists); 23 | System.out.println(destinations); 24 | 25 | // List tickets = flattenList(ticketLists); 26 | // System.out.println(tickets); 27 | 28 | // System.out.println("Total price: " + getTotalPrice(tickets)); 29 | 30 | // List adultTickets = asList(new AdultTicket(), new AdultTicket()); 31 | // System.out.println("Total price: " + getTotalPrice(adultTickets)); 32 | 33 | } 34 | 35 | 36 | static List flattenList(List> nestedList) { 37 | 38 | List flattenedList = new ArrayList<>(); 39 | nestedList.forEach(flattenedList::addAll); 40 | return flattenedList; 41 | 42 | } 43 | 44 | 45 | static int getTotalPrice(List tickets) { 46 | int totalPrice = 0; 47 | for (Ticket ticket : tickets) { 48 | totalPrice += ticket.getPrice(); 49 | } 50 | return totalPrice; 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/_02_07/end/AdultTicket.java: -------------------------------------------------------------------------------- 1 | package _02_07.end; 2 | 3 | public class AdultTicket extends Ticket { 4 | 5 | @Override 6 | public int getPrice() { 7 | return 10; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | return "Adult ticket"; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/_02_07/end/ChildTicket.java: -------------------------------------------------------------------------------- 1 | package _02_07.end; 2 | 3 | public class ChildTicket extends Ticket { 4 | 5 | 6 | @Override 7 | public int getPrice() { 8 | return 5; 9 | } 10 | 11 | @Override 12 | public String toString() { 13 | return "Child ticket"; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/_02_07/end/Ticket.java: -------------------------------------------------------------------------------- 1 | package _02_07.end; 2 | 3 | public abstract class Ticket { 4 | public abstract int getPrice(); 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/_02_07/end/TicketMachine.java: -------------------------------------------------------------------------------- 1 | package _02_07.end; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import static java.util.Arrays.asList; 7 | 8 | public class TicketMachine { 9 | 10 | public static void main(String[] args) { 11 | 12 | List> destinationLists = asList( 13 | asList("New York", "Boston"), 14 | asList("Los Angeles", "San Francisco")); 15 | 16 | 17 | List> ticketLists = asList( 18 | asList(new AdultTicket(), new AdultTicket()), 19 | asList(new ChildTicket(), new ChildTicket()) 20 | ); 21 | 22 | List destinations = flattenList(destinationLists); 23 | System.out.println(destinations); 24 | 25 | List tickets = flattenList(ticketLists); 26 | System.out.println(tickets); 27 | 28 | 29 | System.out.println("Total price: " + getTotalPrice(tickets)); 30 | 31 | List adultTickets = asList(new AdultTicket(), new AdultTicket()); 32 | System.out.println("Total price: " + getTotalPrice(adultTickets)); 33 | 34 | } 35 | 36 | 37 | static List flattenList(List> nestedList) { 38 | 39 | List flattenedList = new ArrayList<>(); 40 | nestedList.forEach(flattenedList::addAll); 41 | return flattenedList; 42 | 43 | } 44 | 45 | 46 | static int getTotalPrice(List tickets) { 47 | int totalPrice = 0; 48 | for (Ticket ticket : tickets) { 49 | totalPrice += ticket.getPrice(); 50 | } 51 | return totalPrice; 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/_03_02/before/LinkedListExample.java: -------------------------------------------------------------------------------- 1 | package _03_02.before; 2 | 3 | public class LinkedListExample { 4 | 5 | public static void main(String[] args) { 6 | 7 | 8 | 9 | } 10 | 11 | 12 | 13 | 14 | 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/_03_02/end/LinkedListExample.java: -------------------------------------------------------------------------------- 1 | package _03_02.end; 2 | 3 | import java.util.LinkedList; 4 | 5 | public class LinkedListExample { 6 | 7 | public static void main(String[] args) { 8 | 9 | LinkedList list = new LinkedList<>(); 10 | list.add("first item"); 11 | list.add("second item"); 12 | System.out.println(list); 13 | 14 | System.out.println(list.getFirst()); 15 | System.out.println(list.getLast()); 16 | 17 | System.out.println(list.poll()); 18 | System.out.println(list); 19 | 20 | System.out.println(list.pop()); 21 | System.out.println(list); 22 | 23 | System.out.println(list.poll()); 24 | System.out.println(list.pop()); 25 | 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/_03_03/before/HashmapExample.java: -------------------------------------------------------------------------------- 1 | package _03_03.before; 2 | 3 | public class HashmapExample { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | 9 | 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/_03_03/end/HashmapExample.java: -------------------------------------------------------------------------------- 1 | package _03_03.end; 2 | 3 | import java.util.HashMap; 4 | 5 | public class HashmapExample { 6 | 7 | static HashMap basket = new HashMap<>(); 8 | 9 | public static void main(String[] args) { 10 | HashMap basket = new HashMap<>(); 11 | 12 | basket.put("apple", 1); 13 | System.out.println(basket.get("apple")); 14 | 15 | basket.put("apple", 2); 16 | System.out.println(basket.get("apple")); 17 | 18 | System.out.println(basket.containsKey("apple")); 19 | 20 | basket.merge("apple", 1, Integer::sum); 21 | basket.merge("orange", 1, Integer::sum); 22 | 23 | System.out.println(basket.get("apple")); 24 | System.out.println(basket.get("orange")); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/_03_04/before/LinkedHashmapExample.java: -------------------------------------------------------------------------------- 1 | package _03_04.before; 2 | 3 | import java.util.HashMap; 4 | 5 | public class LinkedHashmapExample { 6 | 7 | public static void main(String[] args) { 8 | HashMap basket = new HashMap<>(); 9 | 10 | basket.put("apple", 2); 11 | basket.put("orange", 1); 12 | basket.put("banana", 3); 13 | 14 | basket.forEach((key, value) -> System.out.println(key + ": " + value)); 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/_03_04/end/LinkedHashmapExample.java: -------------------------------------------------------------------------------- 1 | package _03_04.end; 2 | 3 | import java.util.LinkedHashMap; 4 | 5 | public class LinkedHashmapExample { 6 | 7 | public static void main(String[] args) { 8 | LinkedHashMap basket = new LinkedHashMap<>(10, 0.75f, true); 9 | 10 | basket.put("apple", 2); 11 | basket.put("orange", 1); 12 | basket.put("banana", 3); 13 | 14 | basket.get("apple"); 15 | 16 | basket.forEach((key, value) -> System.out.println(key + ": " + value)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/_03_05/VotingSystem.java: -------------------------------------------------------------------------------- 1 | package _03_05; 2 | 3 | import java.util.LinkedHashMap; 4 | 5 | public class VotingSystem { 6 | 7 | // Create a LinkedHashMap here, with the contestant name as the key 8 | // and the number of votes as the value. 9 | 10 | void vote(String contestant) { 11 | // Implement this method so that a new contestant is added to the map if it's 12 | // not already there. If it is already there, increment the value by one. 13 | } 14 | 15 | // This method should return the LinkedHashMap field you create at the top of 16 | // the class. 17 | public LinkedHashMap getVotes() { 18 | return new LinkedHashMap(); 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/_03_06/before/VotingSystem.java: -------------------------------------------------------------------------------- 1 | package _03_06.before; 2 | 3 | import java.util.LinkedHashMap; 4 | 5 | public class VotingSystem { 6 | 7 | // Create a LinkedHashMap here, with the contestant name as the key 8 | // and the number of votes as the value. 9 | 10 | void vote(String contestant) { 11 | // Implement this method so that a new contestant is added to the map if it's 12 | // not already there. If it is already there, increment the value by one. 13 | } 14 | 15 | // This method should return the LinkedHashMap field you create at the top of 16 | // the class. 17 | public LinkedHashMap getVotes() { 18 | return new LinkedHashMap(); 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/_03_06/end/VotingSystem.java: -------------------------------------------------------------------------------- 1 | package _03_06.end; 2 | 3 | import java.util.LinkedHashMap; 4 | 5 | public class VotingSystem { 6 | 7 | // Create a LinkedHashMap here, with the contestant name as the key 8 | // and the number of votes as the value. 9 | LinkedHashMap votes = new LinkedHashMap<>(); 10 | 11 | void vote(String contestant) { 12 | // Implement this method so that a new contestant is added to the map if it's 13 | // not already there, and set the value to 1. If it is already there, 14 | // increment the value by one. 15 | votes.merge(contestant, 1, Integer::sum); 16 | } 17 | 18 | // This method should return the LinkedHashMap field you create at the top of 19 | // the class. 20 | public LinkedHashMap getVotes() { 21 | return votes; 22 | } 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/_04_01/GoodMorningGreeting.java: -------------------------------------------------------------------------------- 1 | package _04_01.end; 2 | 3 | public class GoodMorningGreeting implements Greeting { 4 | 5 | @Override 6 | public void printMessage() { 7 | System.out.println("Good morning"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_04_01/Greeting.java: -------------------------------------------------------------------------------- 1 | package _04_01.end; 2 | 3 | @FunctionalInterface 4 | public interface Greeting { 5 | 6 | void printMessage(); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/_04_01/HelloWorldGreeting.java: -------------------------------------------------------------------------------- 1 | package _04_01.end; 2 | 3 | public class HelloWorldGreeting implements Greeting { 4 | 5 | @Override 6 | public void printMessage() { 7 | System.out.println("Hello World"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_04_01/Main.java: -------------------------------------------------------------------------------- 1 | package _04_01.end; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | HelloWorldGreeting helloWorldGreeting = new HelloWorldGreeting(); 7 | helloWorldGreeting.printMessage(); 8 | GoodMorningGreeting goodMorningGreeting = new GoodMorningGreeting(); 9 | goodMorningGreeting.printMessage(); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/_04_02/before/GoodMorningGreeting.java: -------------------------------------------------------------------------------- 1 | package _04_02.before; 2 | 3 | public class GoodMorningGreeting implements Greeting { 4 | @Override 5 | public void printMessage() { 6 | System.out.println("Good morning"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/_04_02/before/Greeting.java: -------------------------------------------------------------------------------- 1 | package _04_02.before; 2 | 3 | @FunctionalInterface 4 | public interface Greeting { 5 | 6 | void printMessage(); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/_04_02/before/HelloWorldGreeting.java: -------------------------------------------------------------------------------- 1 | package _04_02.before; 2 | 3 | public class HelloWorldGreeting implements Greeting { 4 | 5 | @Override 6 | public void printMessage() { 7 | System.out.println("Hello World"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_04_02/before/Main.java: -------------------------------------------------------------------------------- 1 | package _04_02.before; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | HelloWorldGreeting helloWorldGreeting = new HelloWorldGreeting(); 7 | helloWorldGreeting.printMessage(); 8 | GoodMorningGreeting goodMorningGreeting = new GoodMorningGreeting(); 9 | goodMorningGreeting.printMessage(); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/_04_02/end/Greeting.java: -------------------------------------------------------------------------------- 1 | package _04_02.end; 2 | 3 | @FunctionalInterface 4 | public interface Greeting { 5 | 6 | void printMessage(); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/_04_02/end/Main.java: -------------------------------------------------------------------------------- 1 | package _04_02.end; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Greeting helloWorldGreeting = () -> System.out.println("Hello World"); 7 | helloWorldGreeting.printMessage(); 8 | 9 | Greeting goodMorningGreeting = () -> System.out.println("Good morning"); 10 | goodMorningGreeting.printMessage(); 11 | 12 | Greeting goodAfternoonGreeting = () -> System.out.println("Good afternoon"); 13 | goodAfternoonGreeting.printMessage(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/_04_03/before/Main.java: -------------------------------------------------------------------------------- 1 | package _04_03.before; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) { 9 | List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/_04_03/before/NumberUtils.java: -------------------------------------------------------------------------------- 1 | package _04_03.before; 2 | 3 | public class NumberUtils { 4 | 5 | static void evenOrOdd(int number) { 6 | if(number % 2 == 0) { 7 | System.out.println(number + " is even"); 8 | } else { 9 | System.out.println(number + " is odd"); 10 | } 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/_04_03/end/Main.java: -------------------------------------------------------------------------------- 1 | package _04_03.end; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) { 9 | List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7); 10 | numbers.forEach(NumberUtils::evenOrOdd); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/_04_03/end/NumberUtils.java: -------------------------------------------------------------------------------- 1 | package _04_03.end; 2 | 3 | public class NumberUtils { 4 | 5 | static void evenOrOdd(int number) { 6 | if(number % 2 == 0) { 7 | System.out.println(number + " is even"); 8 | } else { 9 | System.out.println(number + " is odd"); 10 | } 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/_04_05/before/Main.java: -------------------------------------------------------------------------------- 1 | package _04_05.before; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Collections; 6 | import java.util.List; 7 | 8 | public class Main { 9 | 10 | public static void main(String[] args) { 11 | 12 | List fruit = Arrays.asList("apple", "pineapple", "banana", "pear", "strawberry"); 13 | List capitalisedFruit = processWithoutStreams(fruit); 14 | System.out.println(capitalisedFruit); 15 | } 16 | 17 | static List processWithoutStreams(List fruit) { 18 | List capitalisedFruit = new ArrayList<>(); 19 | for (String item : fruit) { 20 | item = item.toUpperCase(); 21 | if(item.startsWith("P")) 22 | { 23 | capitalisedFruit.add(item); 24 | } 25 | } 26 | Collections.sort(capitalisedFruit); 27 | return capitalisedFruit; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/_04_05/end/Main.java: -------------------------------------------------------------------------------- 1 | package _04_05.end; 2 | 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) { 9 | 10 | List fruit = Arrays.asList("apple", "pineapple", "banana", "pear", "strawberry"); 11 | List capitalisedFruit = processWithoutStreams(fruit); 12 | System.out.println(capitalisedFruit); 13 | } 14 | 15 | static List processWithoutStreams(List fruit) { 16 | List capitalisedFruit = new ArrayList<>(); 17 | for (String item : fruit) { 18 | item = item.toUpperCase(); 19 | if(item.startsWith("P")) 20 | { 21 | capitalisedFruit.add(item); 22 | } 23 | } 24 | Collections.sort(capitalisedFruit); 25 | return capitalisedFruit; 26 | } 27 | 28 | static List processWithStreams(List fruit) { 29 | return fruit.stream() 30 | .map(String::toUpperCase) 31 | .filter(item -> item.startsWith("P")) 32 | .sorted() 33 | .collect(Collectors.toList()); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/_04_06/before/StreamUtils.java: -------------------------------------------------------------------------------- 1 | package _04_06.before; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | public class StreamUtils { 7 | 8 | 9 | // This method should a list containing the same elements as the original, 10 | // but sorted alphabetically. 11 | List sorted(List list) { 12 | return list; 13 | } 14 | 15 | // This method should return a list only containing items 16 | // from the original list that begin with T. 17 | List beginningWithT(List list) { 18 | return list; 19 | } 20 | 21 | // This method should a list containing the same elements as the original, 22 | // but every String should be all capitalized. 23 | List upperCase(List list) { 24 | return list; 25 | } 26 | 27 | 28 | // This method should sort the elements in the list alphabetically, 29 | // filter the elements to only get the words beginning with T, and 30 | // capitalise every string. 31 | List everythingCombined(List list) { 32 | return list; 33 | } 34 | 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/_04_06/end/StreamUtils.java: -------------------------------------------------------------------------------- 1 | package _04_06.end; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | public class StreamUtils { 7 | 8 | 9 | // This method should a list containing the same elements as the original, 10 | // but sorted alphabetically. 11 | List sorted(List list) { 12 | return list.stream().sorted().collect(Collectors.toList()); 13 | } 14 | 15 | // This method should return a list only containing items 16 | // from the original list that begin with T. 17 | List beginningWithT(List list) { 18 | return list; 19 | } 20 | 21 | // This method should a list containing the same elements as the original, 22 | // but every String should be all capitalized. 23 | List upperCase(List list) { 24 | return list; 25 | } 26 | 27 | 28 | // This method should sort the elements in the list alphabetically, 29 | // filter the elements to only get the words beginning with T, and 30 | // capitalise every string. 31 | List everythingCombined(List list) { 32 | return list; 33 | } 34 | 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/_04_07/before/StreamUtils.java: -------------------------------------------------------------------------------- 1 | package _04_07.before; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | public class StreamUtils { 7 | 8 | 9 | // This method should a list containing the same elements as the original, 10 | // but sorted alphabetically. 11 | List sorted(List list) { 12 | return list.stream().sorted().collect(Collectors.toList()); 13 | } 14 | 15 | // This method should return a list only containing items 16 | // from the original list that begin with T. 17 | List beginningWithT(List list) { 18 | return list; 19 | } 20 | 21 | // This method should a list containing the same elements as the original, 22 | // but every String should be all capitalized. 23 | List upperCase(List list) { 24 | return list; 25 | } 26 | 27 | 28 | // This method should sort the elements in the list alphabetically, 29 | // filter the elements to only get the words beginning with T, and 30 | // capitalise every string. 31 | List everythingCombined(List list) { 32 | return list; 33 | } 34 | 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/_04_07/end/StreamUtils.java: -------------------------------------------------------------------------------- 1 | package _04_07.end; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | public class StreamUtils { 7 | 8 | 9 | // This method should a list containing the same elements as the original, 10 | // but sorted alphabetically. 11 | List sorted(List list) { 12 | return list.stream().sorted().collect(Collectors.toList()); 13 | } 14 | 15 | // This method should return a list only containing items 16 | // from the original list that begin with T. 17 | List beginningWithT(List list) { 18 | return list.stream() 19 | .filter(item -> item.startsWith("T")) 20 | .collect(Collectors.toList()); 21 | } 22 | 23 | // This method should a list containing the same elements as the original, 24 | // but every String should be all capitalized. 25 | List upperCase(List list) { 26 | return list.stream() 27 | .map(String::toUpperCase) 28 | .collect(Collectors.toList()); 29 | } 30 | 31 | 32 | // This method should sort the elements in the list alphabetically, 33 | // filter the elements to only get the words beginning with T, and 34 | // capitalise every string. 35 | List everythingCombined(List list) { 36 | return list.stream() 37 | .sorted() 38 | .filter(item -> item.startsWith("T")) 39 | .map(String::toUpperCase) 40 | .collect(Collectors.toList()); 41 | } 42 | 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/_05_02/before/Main.java: -------------------------------------------------------------------------------- 1 | package _05_02.before; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_05_02/end/Main.java: -------------------------------------------------------------------------------- 1 | package _05_02.end; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Thread threadOne = new ThreadExample(); 7 | Thread threadTwo = new ThreadExample(); 8 | 9 | threadOne.setName("First thread"); 10 | threadTwo.setName("Second thread"); 11 | 12 | threadOne.start(); 13 | threadTwo.start(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/_05_02/end/ThreadExample.java: -------------------------------------------------------------------------------- 1 | package _05_02.end; 2 | 3 | public class ThreadExample extends Thread { 4 | 5 | 6 | @Override 7 | public void run() { 8 | System.out.println("Hello world from " + this.getName()); 9 | } 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/_05_03/before/Main.java: -------------------------------------------------------------------------------- 1 | package _05_03.before; 2 | 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | 8 | Thread threadOne = new ThreadExample(); 9 | Thread threadTwo = new ThreadExample(); 10 | 11 | threadOne.setName("First thread"); 12 | threadTwo.setName("Second thread"); 13 | 14 | threadOne.start(); 15 | threadTwo.start(); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/_05_03/before/ThreadExample.java: -------------------------------------------------------------------------------- 1 | package _05_03.before; 2 | 3 | public class ThreadExample extends Thread { 4 | 5 | 6 | @Override 7 | public void run() { 8 | System.out.println("Hello World from " + this.getName()); 9 | } 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/_05_03/end/Main.java: -------------------------------------------------------------------------------- 1 | package _05_03.end; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | Thread threadOne = new Thread(new RunnableExample()); 8 | Thread threadTwo = new Thread(() -> System.out.println("Hello world from a lambda style runnable")); 9 | 10 | threadOne.setName("First thread"); 11 | threadTwo.setName("Second thread"); 12 | 13 | threadOne.start(); 14 | threadTwo.start(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/_05_03/end/RunnableExample.java: -------------------------------------------------------------------------------- 1 | package _05_03.end; 2 | 3 | public class RunnableExample implements Runnable { 4 | 5 | @Override 6 | public void run() { 7 | System.out.println("Hello world from a runnable"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_05_04/before/Main.java: -------------------------------------------------------------------------------- 1 | package _05_04.before; 2 | 3 | import _05_03.end.RunnableExample; 4 | 5 | import java.util.concurrent.ExecutorService; 6 | import java.util.concurrent.Executors; 7 | 8 | public class Main { 9 | 10 | public static void main(String[] args) { 11 | 12 | ExecutorService executorService = Executors.newFixedThreadPool(2); 13 | executorService.submit(new _05_04.before.RunnableExample()); 14 | executorService.submit(() -> System.out.println("Hello world from a runnable running in an ExecutorService")); 15 | 16 | executorService.shutdown(); 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/_05_04/before/RunnableExample.java: -------------------------------------------------------------------------------- 1 | package _05_04.before; 2 | 3 | public class RunnableExample implements Runnable { 4 | 5 | @Override 6 | public void run() { 7 | System.out.println("Hello world from a runnable"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_05_04/end/Main.java: -------------------------------------------------------------------------------- 1 | package _05_04.end; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) { 9 | Runnable runnable = new RunnableExample(); 10 | 11 | ExecutorService executorService = Executors.newFixedThreadPool(2); 12 | executorService.submit(new RunnableExample()); 13 | executorService.submit(() -> System.out.println("Hello from a runnable running in an ExecutorService")); 14 | 15 | executorService.shutdown(); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/_05_04/end/RunnableExample.java: -------------------------------------------------------------------------------- 1 | package _05_04.end; 2 | 3 | public class RunnableExample implements Runnable { 4 | 5 | @Override 6 | public void run() { 7 | System.out.println("Hello world from a runnable"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_05_05/before/ATM.java: -------------------------------------------------------------------------------- 1 | package _05_05.before; 2 | 3 | public class ATM { 4 | 5 | public void withdraw(BankAccount account, int amount) { 6 | int balance = account.getBalance(); 7 | if (balance - amount < 0) { 8 | System.out.println("Transaction denied"); 9 | } else { 10 | System.out.println("Handling transaction..."); 11 | account.debit(amount); 12 | System.out.println("$" + amount + " withdrawn"); 13 | } 14 | System.out.println("Current balance: " + account.getBalance()); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/_05_05/before/BankAccount.java: -------------------------------------------------------------------------------- 1 | package _05_05.before; 2 | 3 | public class BankAccount { 4 | 5 | private int balance = 100; 6 | 7 | void debit(int amount) { 8 | balance -= amount; 9 | } 10 | 11 | public int getBalance() { 12 | return balance; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/_05_05/before/Customer.java: -------------------------------------------------------------------------------- 1 | package _05_05.before; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | public class Customer { 7 | 8 | public static void main(String[] args) { 9 | ATM atm = new ATM(); 10 | BankAccount account = new BankAccount(); 11 | 12 | ExecutorService executorService = Executors.newFixedThreadPool(2); 13 | 14 | executorService.submit(() -> atm.withdraw(account, 100)); 15 | executorService.submit(() -> atm.withdraw(account, 100)); 16 | 17 | executorService.shutdown(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/_05_05/end/ATM.java: -------------------------------------------------------------------------------- 1 | package _05_05.end; 2 | 3 | public class ATM { 4 | 5 | public synchronized void withdraw(BankAccount account, int amount) { 6 | int balance = account.getBalance(); 7 | if (balance - amount < 0) { 8 | System.out.println("Transaction denied"); 9 | } else { 10 | System.out.println("Handling transaction..."); 11 | account.debit(amount); 12 | System.out.println("$" + amount + " withdrawn"); 13 | } 14 | System.out.println("Current balance: " + account.getBalance()); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/_05_05/end/BankAccount.java: -------------------------------------------------------------------------------- 1 | package _05_05.end; 2 | 3 | public class BankAccount { 4 | 5 | private int balance = 100; 6 | 7 | void debit(int amount) { 8 | balance -= amount; 9 | } 10 | 11 | public int getBalance() { 12 | return balance; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/_05_05/end/Customer.java: -------------------------------------------------------------------------------- 1 | package _05_05.end; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | public class Customer { 7 | 8 | public static void main(String[] args) { 9 | ATM atm = new ATM(); 10 | BankAccount account = new BankAccount(); 11 | 12 | ExecutorService executorService = Executors.newFixedThreadPool(2); 13 | 14 | executorService.submit(() -> atm.withdraw(account, 100)); 15 | executorService.submit(() -> atm.withdraw(account, 100)); 16 | 17 | executorService.shutdown(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/_05_06/StockChecker.java: -------------------------------------------------------------------------------- 1 | package _05_06; 2 | 3 | public class StockChecker { 4 | 5 | private int stock = 20; 6 | 7 | public int getStock() { 8 | return stock; 9 | } 10 | 11 | public void updateStock(int amount) { 12 | stock -= amount; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_05_06/Store.java: -------------------------------------------------------------------------------- 1 | package _05_06; 2 | 3 | public class Store { 4 | 5 | // Make this method a synchronized method 6 | static void purchase(StockChecker stockChecker, int amount) { 7 | int stock = stockChecker.getStock(); 8 | if(stock - amount < 0) { 9 | System.out.println("Out of stock"); 10 | } else { 11 | System.out.println("Item is in stock"); 12 | stockChecker.updateStock(amount); 13 | System.out.println(amount + " items purchased"); 14 | } 15 | System.out.println("Current stock: " + stockChecker.getStock()); 16 | } 17 | 18 | public static void main(String[] args) { 19 | 20 | // Create a new StockChecker object. 21 | 22 | // Create an ExecutorService with a fixed thread pool of 4 threads 23 | 24 | // Submit 4 tasks to the executor service. 25 | 26 | // In each task you should call the purchase method and pass in 20, 27 | // to represent a customer purchasing 20 items. 28 | 29 | // Shut down the executor service. 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/_05_07/before/StockChecker.java: -------------------------------------------------------------------------------- 1 | package _05_07.before; 2 | 3 | public class StockChecker { 4 | 5 | private int stock = 20; 6 | 7 | public int getStock() { 8 | return stock; 9 | } 10 | 11 | public void updateStock(int amount) { 12 | stock -= amount; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_05_07/before/Store.java: -------------------------------------------------------------------------------- 1 | package _05_07.before; 2 | 3 | public class Store { 4 | 5 | // Make this method a synchronized method 6 | static void purchase(StockChecker stockChecker, int amount) { 7 | int stock = stockChecker.getStock(); 8 | if(stock - amount < 0) { 9 | System.out.println("Out of stock"); 10 | } else { 11 | System.out.println("Item is in stock"); 12 | stockChecker.updateStock(amount); 13 | System.out.println(amount + " items purchased"); 14 | } 15 | System.out.println("Current stock: " + stockChecker.getStock()); 16 | } 17 | 18 | public static void main(String[] args) { 19 | 20 | // Create a new StockChecker object 21 | 22 | // Create an ExecutorService with a fixed thread pool of 4 threads 23 | 24 | // Submit 4 tasks to the executor service. 25 | // In each task you should call the purchase method and pass in 20, 26 | // to represent a customer purchasing 20 items. 27 | 28 | // Shut down the executor service. 29 | 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/_05_07/end/StockChecker.java: -------------------------------------------------------------------------------- 1 | package _05_07.end; 2 | 3 | public class StockChecker { 4 | 5 | private int stock = 20; 6 | 7 | public int getStock() { 8 | return stock; 9 | } 10 | 11 | public void updateStock(int amount) { 12 | stock -= amount; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/_05_07/end/Store.java: -------------------------------------------------------------------------------- 1 | package _05_07.end; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | public class Store { 7 | 8 | // Make this method a synchronized method 9 | static synchronized void purchase(StockChecker stockChecker, int amount) { 10 | int stock = stockChecker.getStock(); 11 | if(stock - amount < 0) { 12 | System.out.println("Not enough stock"); 13 | } else { 14 | System.out.println("Item is in stock"); 15 | stockChecker.updateStock(amount); 16 | System.out.println(amount + " items purchased"); 17 | } 18 | System.out.println("Current stock: " + stockChecker.getStock()); 19 | } 20 | 21 | public static void main(String[] args) { 22 | // Create a new StockChecker object 23 | StockChecker stockChecker = new StockChecker(); 24 | 25 | // Create an ExecutorService with a fixed thread pool of 4 threads 26 | ExecutorService executorService = Executors.newFixedThreadPool(5); 27 | 28 | // Submit 4 tasks to the executor service. 29 | // In each task you should call the purchase method and pass in 20, 30 | // to represent a customer purchasing 20 items. 31 | executorService.submit(() -> purchase(stockChecker, 20)); 32 | executorService.submit(() -> purchase(stockChecker, 20)); 33 | executorService.submit(() -> purchase(stockChecker, 20)); 34 | executorService.submit(() -> purchase(stockChecker, 20)); 35 | 36 | // Shut down the executor service. 37 | executorService.shutdown(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/_06_02/before/ScannerExample.java: -------------------------------------------------------------------------------- 1 | package _06_02.before; 2 | 3 | public class ScannerExample { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_06_02/end/ScannerExample.java: -------------------------------------------------------------------------------- 1 | package _06_02.end; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ScannerExample { 6 | 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | System.out.print("Please enter your name: "); 10 | String name = scanner.nextLine(); 11 | System.out.println(name); 12 | System.out.print("Please enter you age: "); 13 | int age = scanner.nextInt(); 14 | System.out.println(age); 15 | scanner.nextLine(); 16 | System.out.print("Please enter your occupation: "); 17 | String occupation = scanner.nextLine(); 18 | System.out.println(occupation); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/_06_03/before/BufferedReaderExample.java: -------------------------------------------------------------------------------- 1 | package _06_03.before; 2 | 3 | public class BufferedReaderExample { 4 | 5 | public static void main(String[] args) { 6 | 7 | 8 | } 9 | } -------------------------------------------------------------------------------- /src/main/java/_06_03/end/BufferedReaderExample.java: -------------------------------------------------------------------------------- 1 | package _06_03.end; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileReader; 5 | import java.io.IOException; 6 | 7 | public class BufferedReaderExample { 8 | 9 | public static void main(String[] args) { 10 | try { 11 | BufferedReader reader = new BufferedReader(new FileReader("src/main/java/_06_03/example.txt")); 12 | System.out.println("First line:"); 13 | String firstLine = reader.readLine(); 14 | System.out.println(firstLine); 15 | System.out.println("Every other line:"); 16 | StringBuilder stringBuilder = new StringBuilder(); 17 | reader.lines().forEach(line -> stringBuilder.append(line + " ")); 18 | System.out.println(stringBuilder); 19 | } catch (IOException e) { 20 | e.printStackTrace(); 21 | } 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/_06_03/example.txt: -------------------------------------------------------------------------------- 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. 2 | Pellentesque venenatis, odio in aliquam gravida, sem ipsum eleifend turpis. 3 | Sed scelerisque, felis vel consectetur viverra, velit arcu porttitor leo, vitae aliquet nibh turpis nec nibh. 4 | Vivamus feugiat posuere tempus. Duis semper neque at ante blandit fermentum. 5 | Quisque eleifend efficitur neque. Integer gravida lacus non porttitor suscipit. 6 | Cras pretium mattis risus vitae maximus. Cras sed erat non magna porta vulputate. 7 | Nam eget quam lacinia, volutpat purus id, auctor nunc. 8 | Vestibulum efficitur orci elit, ut egestas magna congue et. 9 | Ut convallis accumsan enim, eget consequat arcu dapibus lacinia. 10 | Etiam elementum est enim, non vulputate massa accumsan vel. 11 | Suspendisse imperdiet viverra velit, nec lacinia justo cursus sed. -------------------------------------------------------------------------------- /src/main/java/_06_04/before/BufferedReaderExample.java: -------------------------------------------------------------------------------- 1 | package _06_04.before; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileReader; 5 | import java.io.IOException; 6 | 7 | public class BufferedReaderExample { 8 | 9 | public static void main(String[] args) { 10 | try { 11 | BufferedReader reader = new BufferedReader(new FileReader("src/main/java/_06_03/example.txt")); 12 | System.out.println("First line:"); 13 | String firstLine = reader.readLine(); 14 | System.out.println(firstLine); 15 | System.out.println("Every other line:"); 16 | StringBuilder stringBuilder = new StringBuilder(); 17 | reader.lines().forEach(stringBuilder::append); 18 | System.out.println(stringBuilder); 19 | } catch (IOException e) { 20 | e.printStackTrace(); 21 | } 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/_06_04/end/BufferedReaderExample.java: -------------------------------------------------------------------------------- 1 | package _06_04.end; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileReader; 5 | import java.io.IOException; 6 | 7 | public class BufferedReaderExample { 8 | 9 | public static void main(String[] args) { 10 | try(BufferedReader reader = new BufferedReader(new FileReader("src/main/java/_06_03/example.txt"))) { 11 | System.out.println("First line:"); 12 | String firstLine = reader.readLine(); 13 | System.out.println(firstLine); 14 | System.out.println("Every other line:"); 15 | StringBuilder stringBuilder = new StringBuilder(); 16 | reader.lines().forEach(line -> stringBuilder.append(line + " ")); 17 | System.out.println(stringBuilder); 18 | } catch (IOException e) { 19 | e.printStackTrace(); 20 | } 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/_06_04/example.txt: -------------------------------------------------------------------------------- 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. 2 | Pellentesque venenatis, odio in aliquam gravida, sem ipsum eleifend turpis. 3 | Sed scelerisque, felis vel consectetur viverra, velit arcu porttitor leo, vitae aliquet nibh turpis nec nibh. 4 | Vivamus feugiat posuere tempus. Duis semper neque at ante blandit fermentum. 5 | Quisque eleifend efficitur neque. Integer gravida lacus non porttitor suscipit. 6 | Cras pretium mattis risus vitae maximus. Cras sed erat non magna porta vulputate. 7 | Nam eget quam lacinia, volutpat purus id, auctor nunc. 8 | Vestibulum efficitur orci elit, ut egestas magna congue et. 9 | Ut convallis accumsan enim, eget consequat arcu dapibus lacinia. 10 | Etiam elementum est enim, non vulputate massa accumsan vel. 11 | Suspendisse imperdiet viverra velit, nec lacinia justo cursus sed. -------------------------------------------------------------------------------- /src/main/java/_06_05/BufferedReaderVsScanner.txt: -------------------------------------------------------------------------------- 1 | Scanners and BufferedReaders can perform similar operations in Java. 2 | For example both can be used to read input from the terminal, or read input from a file. 3 | However they work slightly differently. 4 | The Scanner class parses tokens from the stream, whereas BufferedReader just reads the input line by line. 5 | This means that it is slightly faster to read input using a BufferedReader, as it doesn't parse the content. 6 | But Scanner is better if you do want to parse the content. For example you can split text into sentences rather than into lines, if you use the Scanner to separate text at each period. 7 | Another difference is that BufferedReader is thread-safe, whereas Scanner is not. -------------------------------------------------------------------------------- /src/main/java/_06_05/FileReaderExercise.java: -------------------------------------------------------------------------------- 1 | package _06_05; 2 | 3 | public class FileReaderExercise { 4 | 5 | // This method should return the first line of BufferedReaderVsScanner.txt. 6 | String getFirstLine() { 7 | String firstLine = ""; 8 | 9 | return firstLine; 10 | } 11 | 12 | // This method should return all of the content of BufferedReaderVsScanner.txt 13 | // as a single String. 14 | String getWholeFile() { 15 | String wholeFile = ""; 16 | 17 | return wholeFile; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/_06_06/BufferedReaderVsScanner.txt: -------------------------------------------------------------------------------- 1 | Scanners and BufferedReaders can perform similar operations in Java. 2 | For example both can be used to read input from the terminal, or read input from a file. 3 | However they work slightly differently. 4 | The Scanner class parses tokens from the stream, whereas BufferedReader just reads the input line by line. 5 | This means that it is slightly faster to read input using a BufferedReader, as it doesn't parse the content. 6 | But Scanner is better if you do want to parse the content. For example you can split text into sentences rather than into lines, if you use the Scanner to separate text at each period. 7 | Another difference is that BufferedReader is thread-safe, whereas Scanner is not. -------------------------------------------------------------------------------- /src/main/java/_06_06/before/FileReaderExercise.java: -------------------------------------------------------------------------------- 1 | package _06_06.before; 2 | 3 | public class FileReaderExercise { 4 | 5 | // This method should return the first line of BufferedReaderVsScanner.txt. 6 | String getFirstLine() { 7 | String firstLine = ""; 8 | 9 | return firstLine; 10 | } 11 | 12 | // This method should return all of the content of BufferedReaderVsScanner.txt 13 | // as a single String. 14 | String getWholeFile() { 15 | String wholeFile = ""; 16 | 17 | return wholeFile; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/_06_06/end/FileReaderExercise.java: -------------------------------------------------------------------------------- 1 | package _06_06.end; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileReader; 5 | import java.io.IOException; 6 | 7 | public class FileReaderExercise { 8 | 9 | String getFirstLine() { 10 | String firstLine = ""; 11 | try(BufferedReader reader = new BufferedReader(new FileReader("src/main/java/_06_06/BufferedReaderVsScanner.txt"))) { 12 | firstLine = reader.readLine(); 13 | } catch (IOException e) { 14 | e.printStackTrace(); 15 | } 16 | return firstLine; 17 | } 18 | 19 | String getWholeFile() { 20 | String wholeFile = ""; 21 | try(BufferedReader reader = new BufferedReader(new FileReader("src/main/java/_06_06/BufferedReaderVsScanner.txt"))) { 22 | StringBuilder stringBuilder = new StringBuilder(); 23 | reader.lines().forEach(line -> stringBuilder.append(line + " ")); 24 | wholeFile = stringBuilder.toString(); 25 | } catch (IOException e) { 26 | e.printStackTrace(); 27 | } 28 | return wholeFile; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/_07_02/before/FileCreationExample.java: -------------------------------------------------------------------------------- 1 | package _07_02.before; 2 | 3 | public class FileCreationExample { 4 | 5 | public static void main(String[] args) { 6 | } 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/_07_02/end/FileCreationExample.java: -------------------------------------------------------------------------------- 1 | package _07_02.end; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | import java.nio.file.Path; 6 | import java.nio.file.Paths; 7 | 8 | public class FileCreationExample { 9 | 10 | public static void main(String[] args) { 11 | 12 | Path path = Paths.get("src/main/java/_07_02/example.txt"); 13 | try { 14 | if(Files.notExists(path)) { 15 | Files.createFile(path); 16 | } 17 | } catch (IOException e) { 18 | e.printStackTrace(); 19 | } 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/_07_03/begin/DirectoriesExamples.java: -------------------------------------------------------------------------------- 1 | package _07_03.begin; 2 | 3 | public class DirectoriesExamples { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_07_03/end/DirectoriesExamples.java: -------------------------------------------------------------------------------- 1 | package _07_03.end; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | import java.nio.file.Path; 6 | import java.nio.file.Paths; 7 | 8 | public class DirectoriesExamples { 9 | 10 | public static void main(String[] args) { 11 | 12 | try { 13 | Files.list(Paths.get(".")) 14 | .filter(file -> !Files.isDirectory(file)) 15 | .forEach(System.out::println); 16 | 17 | if(Files.notExists(Paths.get("src/main/java/_07_03/example"))) { 18 | Files.createDirectory(Paths.get("src/main/java/_07_03/example")); 19 | } 20 | 21 | } catch (IOException e) { 22 | e.printStackTrace(); 23 | } 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/_07_04/before/FileCopier.java: -------------------------------------------------------------------------------- 1 | package _07_04.before; 2 | 3 | public class FileCopier { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/_07_04/end/FileCopier.java: -------------------------------------------------------------------------------- 1 | package _07_04.end; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | import java.nio.file.Path; 6 | import java.nio.file.Paths; 7 | 8 | public class FileCopier { 9 | 10 | public static void main(String[] args) { 11 | 12 | Path pathToFile = Paths.get("src/main/java/_07_04/files/a/example.txt"); 13 | Path pathToNewLocation = Paths.get("src/main/java/_07_04/files/b/example.txt"); 14 | try { 15 | if(Files.notExists(pathToNewLocation)) { 16 | Files.copy(pathToFile, pathToNewLocation); 17 | } 18 | } catch (IOException e) { 19 | e.printStackTrace(); 20 | } 21 | 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/_07_04/files/a/example.txt: -------------------------------------------------------------------------------- 1 | Hello world -------------------------------------------------------------------------------- /src/main/java/_07_05/FilesExercise.java: -------------------------------------------------------------------------------- 1 | package _07_05; 2 | 3 | public class FilesExercise { 4 | 5 | public static void main(String[] args) { 6 | // Create a new empty file called example.txt inside folderA 7 | 8 | // Copy the file to folderB 9 | 10 | // List the contents of folderB to check that your file is in there 11 | 12 | } 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/_07_06/before/FilesExercise.java: -------------------------------------------------------------------------------- 1 | package _07_06.before; 2 | 3 | public class FilesExercise { 4 | 5 | public static void main(String[] args) { 6 | // Create a new empty file called example.txt inside folderA 7 | 8 | // Copy the file to folderB 9 | 10 | // List the contents of folderB to check that your file is in there 11 | 12 | } 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/_07_06/end/FilesExercise.java: -------------------------------------------------------------------------------- 1 | package _07_06.end; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | import java.nio.file.Path; 6 | import java.nio.file.Paths; 7 | 8 | public class FilesExercise { 9 | 10 | public static void main(String[] args) { 11 | try { 12 | // Create a new empty file called example.txt inside folderA 13 | Path path = Paths.get("src/main/java/_07_06/folderA/example.txt"); 14 | if(Files.notExists(path)) { 15 | Files.createFile(path); 16 | } 17 | 18 | // Copy the file to folderB 19 | Path newPath = Paths.get("src/main/java/_07_06/folderB/example.txt"); 20 | if(Files.notExists(newPath)) { 21 | Files.copy(path, newPath); 22 | } 23 | 24 | // List the contents of folderB to check that your file is in there 25 | Files.list(Paths.get("src/main/java/_07_06/folderB")); 26 | 27 | } catch (IOException e) { 28 | e.printStackTrace(); 29 | } 30 | 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/_03_05/VotingSystemTest.java: -------------------------------------------------------------------------------- 1 | package _03_05; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class VotingSystemTest { 8 | 9 | VotingSystem votingSystem = new VotingSystem(); 10 | 11 | @Test 12 | public void testVote() { 13 | 14 | votingSystem.vote("Contestant One"); 15 | votingSystem.vote("Contestant One"); 16 | votingSystem.vote("Contestant Two"); 17 | votingSystem.vote("Contestant One"); 18 | votingSystem.vote("Contestant Three"); 19 | votingSystem.vote("Contestant Three"); 20 | 21 | assertEquals(3, votingSystem.getVotes().get("Contestant One")); 22 | assertEquals(1, votingSystem.getVotes().get("Contestant Two")); 23 | assertEquals(2, votingSystem.getVotes().get("Contestant Three")); 24 | 25 | } 26 | 27 | 28 | @Test 29 | public void testOrderOfVotes() { 30 | votingSystem.vote("Contestant One"); 31 | votingSystem.vote("Contestant One"); 32 | votingSystem.vote("Contestant Two"); 33 | votingSystem.vote("Contestant One"); 34 | votingSystem.vote("Contestant Three"); 35 | votingSystem.vote("Contestant Three"); 36 | 37 | assertEquals("Contestant One", votingSystem.getVotes().keySet().iterator().next()); 38 | } 39 | 40 | 41 | 42 | } -------------------------------------------------------------------------------- /src/test/java/_03_06/before/VotingSystemTest.java: -------------------------------------------------------------------------------- 1 | package _03_06.before; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class VotingSystemTest { 8 | 9 | VotingSystem votingSystem = new VotingSystem(); 10 | 11 | @Test 12 | public void testVote() { 13 | 14 | votingSystem.vote("Contestant One"); 15 | votingSystem.vote("Contestant One"); 16 | votingSystem.vote("Contestant Two"); 17 | votingSystem.vote("Contestant One"); 18 | votingSystem.vote("Contestant Three"); 19 | votingSystem.vote("Contestant Three"); 20 | 21 | assertEquals(3, votingSystem.getVotes().get("Contestant One")); 22 | assertEquals(1, votingSystem.getVotes().get("Contestant Two")); 23 | assertEquals(2, votingSystem.getVotes().get("Contestant Three")); 24 | 25 | } 26 | 27 | 28 | @Test 29 | public void testOrderOfVotes() { 30 | votingSystem.vote("Contestant One"); 31 | votingSystem.vote("Contestant One"); 32 | votingSystem.vote("Contestant Two"); 33 | votingSystem.vote("Contestant One"); 34 | votingSystem.vote("Contestant Three"); 35 | votingSystem.vote("Contestant Three"); 36 | 37 | assertEquals("Contestant One", votingSystem.getVotes().keySet().iterator().next()); 38 | } 39 | 40 | 41 | 42 | } -------------------------------------------------------------------------------- /src/test/java/_03_06/end/VotingSystemTest.java: -------------------------------------------------------------------------------- 1 | package _03_06.end; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class VotingSystemTest { 8 | 9 | VotingSystem votingSystem = new VotingSystem(); 10 | 11 | @Test 12 | public void testVote() { 13 | 14 | votingSystem.vote("Contestant One"); 15 | votingSystem.vote("Contestant One"); 16 | votingSystem.vote("Contestant Two"); 17 | votingSystem.vote("Contestant One"); 18 | votingSystem.vote("Contestant Three"); 19 | votingSystem.vote("Contestant Three"); 20 | 21 | assertEquals(3, votingSystem.getVotes().get("Contestant One")); 22 | assertEquals(1, votingSystem.getVotes().get("Contestant Two")); 23 | assertEquals(2, votingSystem.getVotes().get("Contestant Three")); 24 | 25 | } 26 | 27 | 28 | @Test 29 | public void testOrderOfVotes() { 30 | votingSystem.vote("Contestant One"); 31 | votingSystem.vote("Contestant One"); 32 | votingSystem.vote("Contestant Two"); 33 | votingSystem.vote("Contestant One"); 34 | votingSystem.vote("Contestant Three"); 35 | votingSystem.vote("Contestant Three"); 36 | 37 | assertEquals("Contestant One", votingSystem.getVotes().keySet().iterator().next()); 38 | } 39 | 40 | 41 | 42 | } -------------------------------------------------------------------------------- /src/test/java/_04_06/before/StreamUtilsTest.java: -------------------------------------------------------------------------------- 1 | package _04_06.before; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | class StreamUtilsTest { 11 | StreamUtils streamUtils = new StreamUtils(); 12 | private List weekdays = Arrays.asList("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"); 13 | 14 | @Test 15 | public void testSorted() { 16 | List actual = streamUtils.sorted(weekdays); 17 | 18 | List expected = Arrays.asList("Friday", "Monday", "Thursday", "Tuesday", "Wednesday"); 19 | 20 | assertEquals(expected, actual); 21 | } 22 | 23 | 24 | @Test 25 | public void testBeginningWithT() { 26 | List actual = streamUtils.beginningWithT(weekdays); 27 | 28 | List expected = Arrays.asList("Tuesday", "Thursday"); 29 | 30 | assertEquals(expected, actual); 31 | } 32 | 33 | @Test 34 | public void testUpperCase() { 35 | List actual = streamUtils.upperCase(weekdays); 36 | 37 | List expected = Arrays.asList("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"); 38 | 39 | assertEquals(expected, actual); 40 | } 41 | 42 | @Test 43 | public void testEverythingCombined() { 44 | List actual = streamUtils.everythingCombined(weekdays); 45 | 46 | List expected = Arrays.asList("THURSDAY", "TUESDAY"); 47 | 48 | assertEquals(expected, actual); 49 | } 50 | 51 | 52 | } -------------------------------------------------------------------------------- /src/test/java/_04_06/end/StreamUtilsTest.java: -------------------------------------------------------------------------------- 1 | package _04_06.end; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.*; 9 | 10 | class StreamUtilsTest { 11 | 12 | StreamUtils streamUtils = new StreamUtils(); 13 | private List weekdays = Arrays.asList("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"); 14 | 15 | @Test 16 | public void testSorted() { 17 | List actual = streamUtils.sorted(weekdays); 18 | 19 | List expected = Arrays.asList("Friday", "Monday", "Thursday", "Tuesday", "Wednesday"); 20 | 21 | assertEquals(expected, actual); 22 | } 23 | 24 | 25 | @Test 26 | public void testBeginningWithT() { 27 | List actual = streamUtils.beginningWithT(weekdays); 28 | 29 | List expected = Arrays.asList("Tuesday", "Thursday"); 30 | 31 | assertEquals(expected, actual); 32 | } 33 | 34 | @Test 35 | public void testUpperCase() { 36 | List actual = streamUtils.upperCase(weekdays); 37 | 38 | List expected = Arrays.asList("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"); 39 | 40 | assertEquals(expected, actual); 41 | } 42 | 43 | @Test 44 | public void testEverythingCombined() { 45 | List actual = streamUtils.everythingCombined(weekdays); 46 | 47 | List expected = Arrays.asList("THURSDAY", "TUESDAY"); 48 | 49 | assertEquals(expected, actual); 50 | } 51 | 52 | 53 | } -------------------------------------------------------------------------------- /src/test/java/_04_07/before/StreamUtilsTest.java: -------------------------------------------------------------------------------- 1 | package _04_07.before; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | class StreamUtilsTest { 11 | 12 | StreamUtils streamUtils = new StreamUtils(); 13 | private List weekdays = Arrays.asList("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"); 14 | 15 | @Test 16 | public void testSorted() { 17 | List actual = streamUtils.sorted(weekdays); 18 | 19 | List expected = Arrays.asList("Friday", "Monday", "Thursday", "Tuesday", "Wednesday"); 20 | 21 | assertEquals(expected, actual); 22 | } 23 | 24 | 25 | @Test 26 | public void testBeginningWithT() { 27 | List actual = streamUtils.beginningWithT(weekdays); 28 | 29 | List expected = Arrays.asList("Tuesday", "Thursday"); 30 | 31 | assertEquals(expected, actual); 32 | } 33 | 34 | @Test 35 | public void testUpperCase() { 36 | List actual = streamUtils.upperCase(weekdays); 37 | 38 | List expected = Arrays.asList("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"); 39 | 40 | assertEquals(expected, actual); 41 | } 42 | 43 | @Test 44 | public void testEverythingCombined() { 45 | List actual = streamUtils.everythingCombined(weekdays); 46 | 47 | List expected = Arrays.asList("THURSDAY", "TUESDAY"); 48 | 49 | assertEquals(expected, actual); 50 | } 51 | 52 | 53 | } -------------------------------------------------------------------------------- /src/test/java/_04_07/end/StreamUtilsTest.java: -------------------------------------------------------------------------------- 1 | package _04_07.end; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | class StreamUtilsTest { 11 | 12 | StreamUtils streamUtils = new StreamUtils(); 13 | private List weekdays = Arrays.asList("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"); 14 | 15 | @Test 16 | public void testSorted() { 17 | List actual = streamUtils.sorted(weekdays); 18 | 19 | List expected = Arrays.asList("Friday", "Monday", "Thursday", "Tuesday", "Wednesday"); 20 | 21 | assertEquals(expected, actual); 22 | } 23 | 24 | 25 | @Test 26 | public void testBeginningWithT() { 27 | List actual = streamUtils.beginningWithT(weekdays); 28 | 29 | List expected = Arrays.asList("Tuesday", "Thursday"); 30 | 31 | assertEquals(expected, actual); 32 | } 33 | 34 | @Test 35 | public void testUpperCase() { 36 | List actual = streamUtils.upperCase(weekdays); 37 | 38 | List expected = Arrays.asList("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"); 39 | 40 | assertEquals(expected, actual); 41 | } 42 | 43 | @Test 44 | public void testEverythingCombined() { 45 | List actual = streamUtils.everythingCombined(weekdays); 46 | 47 | List expected = Arrays.asList("THURSDAY", "TUESDAY"); 48 | 49 | assertEquals(expected, actual); 50 | } 51 | 52 | 53 | } -------------------------------------------------------------------------------- /src/test/java/_06_05/FileReaderExerciseTest.java: -------------------------------------------------------------------------------- 1 | package _06_05; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.*; 6 | 7 | class FileReaderExerciseTest { 8 | 9 | FileReaderExercise fileReaderExercise = new FileReaderExercise(); 10 | 11 | @Test 12 | public void testGetFirstLine() { 13 | String expected = "Scanners and BufferedReaders can perform similar operations in Java."; 14 | String actual = fileReaderExercise.getFirstLine(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | @Test 19 | public void testGetWholeFile() { 20 | String expected = "For example both can be used to read input from the terminal, or read input from a file." + 21 | "However they work slightly differently." + 22 | "The Scanner class parses tokens from the stream, whereas BufferedReader just reads the input line by line." + 23 | "This means that it is slightly faster to read input using a BufferedReader, as it doesn't parse the content." + 24 | "But Scanner is better if you do want to parse the content. For example you can split text into sentences rather than into lines, if you use the Scanner to separate text at each period." + 25 | "Another difference is that BufferedReader is thread-safe, whereas Scanner is not."; 26 | String actual = fileReaderExercise.getWholeFile(); 27 | assertEquals(expected, actual); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/test/java/_06_06/before/FileReaderExerciseTest.java: -------------------------------------------------------------------------------- 1 | package _06_06.before; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class FileReaderExerciseTest { 8 | 9 | FileReaderExercise fileReaderExercise = new FileReaderExercise(); 10 | 11 | @Test 12 | public void testGetFirstLine() { 13 | String expected = "Scanners and BufferedReaders can perform similar operations in Java."; 14 | String actual = fileReaderExercise.getFirstLine(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | @Test 19 | public void testGetWholeFile() { 20 | String expected = "Scanners and BufferedReaders can perform similar operations in Java. " + 21 | "For example both can be used to read input from the terminal, or read input from a file. " + 22 | "However they work slightly differently. " + 23 | "The Scanner class parses tokens from the stream, whereas BufferedReader just reads the input line by line. " + 24 | "This means that it is slightly faster to read input using a BufferedReader, as it doesn't parse the content. " + 25 | "But Scanner is better if you do want to parse the content. For example you can split text into sentences rather than into lines, if you use the Scanner to separate text at each period. " + 26 | "Another difference is that BufferedReader is thread-safe, whereas Scanner is not. "; 27 | String actual = fileReaderExercise.getWholeFile(); 28 | assertEquals(expected, actual); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /src/test/java/_06_06/end/FileReaderExerciseTest.java: -------------------------------------------------------------------------------- 1 | package _06_06.end; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class FileReaderExerciseTest { 8 | 9 | FileReaderExercise fileReaderExercise = new FileReaderExercise(); 10 | 11 | @Test 12 | public void testGetFirstLine() { 13 | String expected = "Scanners and BufferedReaders can perform similar operations in Java."; 14 | String actual = fileReaderExercise.getFirstLine(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | @Test 19 | public void testGetWholeFile() { 20 | String expected = "Scanners and BufferedReaders can perform similar operations in Java. " + 21 | "For example both can be used to read input from the terminal, or read input from a file. " + 22 | "However they work slightly differently. " + 23 | "The Scanner class parses tokens from the stream, whereas BufferedReader just reads the input line by line. " + 24 | "This means that it is slightly faster to read input using a BufferedReader, as it doesn't parse the content. " + 25 | "But Scanner is better if you do want to parse the content. For example you can split text into sentences rather than into lines, if you use the Scanner to separate text at each period. " + 26 | "Another difference is that BufferedReader is thread-safe, whereas Scanner is not. "; 27 | String actual = fileReaderExercise.getWholeFile(); 28 | assertEquals(expected, actual); 29 | } 30 | 31 | } --------------------------------------------------------------------------------