├── .DS_Store ├── .gitattributes ├── 9781484241424.jpg ├── Contributing.md ├── LICENSE.txt ├── README.md ├── ReadMe.txt └── javaprogramdesign ├── .DS_Store ├── chapter01 ├── bank01 │ ├── BankProgram.java │ └── ReadMe.txt ├── bank02 │ ├── Bank.java │ ├── BankClient.java │ ├── BankProgram.java │ └── ReadMe.txt ├── bank03 │ ├── AlternativeBankAccount.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ └── ReadMe.txt └── bank04 │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ └── ReadMe.txt ├── chapter02 ├── bank05 │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── ReadMe.txt │ └── SavingsAccount.java ├── bank06 │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── ReadMe.txt │ └── SavingsAccount.java └── comparable │ ├── CompareBankAccounts.java │ ├── CompareSavingsAccounts.java │ └── ReadMe.txt ├── chapter03 ├── bank07 │ ├── AbstractBankAccount.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── InterestChecking.java │ ├── ReadMe.txt │ └── SavingsAccount.java ├── bank08 │ ├── AbstractBankAccount.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── InterestChecking.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── SavedBankInfo.java │ └── SavingsAccount.java ├── bank09 │ ├── AbstractBankAccount.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── InterestChecking.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── SavedBankInfo.java │ └── SavingsAccount.java ├── bytestream │ ├── ByteArrayInputStream.java │ ├── EncryptDecrypt.java │ └── ReadMe.txt ├── rangelist │ ├── RangeList.java │ ├── RangeListTest.java │ └── ReadMe.txt └── thread │ ├── ReadLine.java │ ├── ReadMe.txt │ └── ThreadTest.java ├── chapter04 ├── bank10 │ ├── AbstractBankAccount.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── DepositCmd.java │ ├── Domestic.java │ ├── Foreign.java │ ├── InputCommand.java │ ├── InterestChecking.java │ ├── InterestCmd.java │ ├── LoanCmd.java │ ├── NewCmd.java │ ├── OwnerStrategy.java │ ├── QuitCmd.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── SavedBankInfo.java │ ├── SavingsAccount.java │ ├── SelectCmd.java │ ├── SetForeignCmd.java │ └── ShowCmd.java ├── bank11 │ ├── AbstractBankAccount.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── DepositCmd.java │ ├── Domestic.java │ ├── Foreign.java │ ├── InputCommand.java │ ├── InterestChecking.java │ ├── InterestCmd.java │ ├── LoanCmd.java │ ├── NewCmd.java │ ├── OwnerStrategy.java │ ├── QuitCmd.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── SavedBankInfo.java │ ├── SavingsAccount.java │ ├── SelectCmd.java │ ├── SetForeignCmd.java │ ├── ShowCmd.java │ └── TypeStrategy.java ├── comparator │ ├── AcctByMinBal.java │ ├── ComparatorBankAccounts.java │ └── ReadMe.txt ├── intprocessor │ ├── IntProcessorStrategy.java │ ├── IntProcessorTemplate.java │ ├── ReadMe.txt │ ├── TestClientStrategy.java │ └── TestClientTemplate.java └── thread │ ├── ReadLine.java │ ├── ReadMe.txt │ └── RunnableThreadTest.java ├── chapter05 ├── bank12 │ ├── AbstractBankAccount.java │ ├── AccountFactory.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── InputCommand.java │ ├── InputCommands.java │ ├── InterestChecking.java │ ├── OwnerStrategy.java │ ├── Owners.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── SavedBankInfo.java │ └── SavingsAccount.java ├── bank13 │ ├── AbstractBankAccount.java │ ├── AccountFactory.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── InputCommand.java │ ├── InputCommands.java │ ├── InterestChecking.java │ ├── InterestCheckingFactory.java │ ├── OwnerStrategy.java │ ├── Owners.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── RegularCheckingFactory.java │ ├── SavedBankInfo.java │ ├── SavingsAccount.java │ └── SavingsFactory.java ├── bank14 │ ├── AbstractBankAccount.java │ ├── AccountFactories.java │ ├── AccountFactory.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── InputCommand.java │ ├── InputCommands.java │ ├── InterestChecking.java │ ├── OwnerStrategy.java │ ├── Owners.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── SavedBankInfo.java │ └── SavingsAccount.java └── thread │ ├── PriorityThreadFactory.java │ └── ReadMe.txt ├── chapter06 ├── bank15 │ ├── AbstractBankAccount.java │ ├── AccountFactories.java │ ├── AccountFactory.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── InputCommand.java │ ├── InputCommands.java │ ├── InterestChecking.java │ ├── IteratorAccountStats.java │ ├── IteratorStatProgram.java │ ├── MaxBalanceVisitor.java │ ├── OwnerStrategy.java │ ├── Owners.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── SavedBankInfo.java │ ├── SavingsAccount.java │ ├── StreamAccountStats.java │ ├── StreamStatProgram.java │ └── Visitor.java └── iteration │ ├── NoDuplicates.java │ ├── PrimeCollection.java │ ├── PrimeIterator.java │ ├── RandomIterator.java │ └── ReadMe.txt ├── chapter07 ├── bank16 │ ├── AbstractBankAccount.java │ ├── AccountFactories.java │ ├── AccountFactory.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankAccountAdapter.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── FBIAcctInfo.java │ ├── FBIClient.java │ ├── InputCommand.java │ ├── InputCommands.java │ ├── InterestChecking.java │ ├── IteratorAccountStats.java │ ├── IteratorStatProgram.java │ ├── Loan.java │ ├── LoanAdapter.java │ ├── MaxBalanceVisitor.java │ ├── OwnerStrategy.java │ ├── Owners.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── SavedBankInfo.java │ ├── SavingsAccount.java │ ├── StreamAccountStats.java │ ├── StreamStatProgram.java │ └── Visitor.java └── iostream │ ├── FilePrefix.java │ ├── InputStreamReader.java │ ├── ObjectStreamTest.java │ ├── OutputStreamWriter.java │ ├── ReadMe.txt │ └── StringReader.java ├── chapter08 ├── bank17 │ ├── AbstractBankAccount.java │ ├── AccountFactories.java │ ├── AccountFactory.java │ ├── AuthorizerWrapper.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankAccountAdapter.java │ ├── BankAccountWrapper.java │ ├── BankClient.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── CollateralAuthorizer.java │ ├── CreditScoreAuthorizer.java │ ├── FBIAcctInfo.java │ ├── FBIClient.java │ ├── GoodCustomerAuthorizer.java │ ├── InputCommand.java │ ├── InputCommands.java │ ├── InterestChecking.java │ ├── IteratorAccountStats.java │ ├── IteratorStatProgram.java │ ├── Loan.java │ ├── LoanAdapter.java │ ├── LoanAuthorizer.java │ ├── MaxBalanceVisitor.java │ ├── OwnerStrategy.java │ ├── Owners.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── SavedBankInfo.java │ ├── SavingsAccount.java │ ├── StreamAccountStats.java │ ├── StreamStatProgram.java │ ├── SuspiciousAccount.java │ ├── UnmodifiableAccount.java │ ├── UnmodifiableBankIterator.java │ └── Visitor.java ├── collectionstream │ ├── FilterIterator.java │ ├── IteratorTest.java │ ├── MapIterator.java │ ├── ReadMe.txt │ ├── SimpleStream.java │ └── SimpleStreamTest.java └── iostream │ ├── DecoratedEncryptDecrypt.java │ ├── DoubledChars.java │ ├── InputStreamEfficiency.java │ ├── ProgressMonitorFileRead.java │ └── ReadMe.txt ├── chapter09 ├── cookbook │ ├── BasicFood.java │ ├── Cookbook.java │ ├── FoodItem.java │ ├── FoodIterator.java │ ├── ReadMe.txt │ └── Recipe.java ├── fx │ ├── AccountCreationWindow.java │ ├── NodeTraversal.java │ ├── PrintNodeInformation.java │ └── ReadMe.txt └── predicate │ ├── AndPredicate.java │ ├── CompositePredicate.java │ ├── CompositePredicateTest.java │ ├── OrPredicate.java │ └── ReadMe.txt ├── chapter10 ├── bank18 │ ├── AbstractBankAccount.java │ ├── AccountFactories.java │ ├── AccountFactory.java │ ├── Auditor.java │ ├── AuthorizerWrapper.java │ ├── Bank.java │ ├── BankAccount.java │ ├── BankAccountAdapter.java │ ├── BankAccountWrapper.java │ ├── BankClient.java │ ├── BankEvent.java │ ├── BankObserver.java │ ├── BankProgram.java │ ├── CheckingAccount.java │ ├── CollateralAuthorizer.java │ ├── CreditScoreAuthorizer.java │ ├── FBIAcctInfo.java │ ├── FBIClient.java │ ├── GoodCustomerAuthorizer.java │ ├── InputCommand.java │ ├── InputCommands.java │ ├── InterestChecking.java │ ├── IteratorAccountStats.java │ ├── IteratorStatProgram.java │ ├── Loan.java │ ├── LoanAdapter.java │ ├── LoanAuthorizer.java │ ├── MaxBalanceVisitor.java │ ├── OwnerStrategy.java │ ├── Owners.java │ ├── ReadMe.txt │ ├── RegularChecking.java │ ├── SavedBankInfo.java │ ├── SavingsAccount.java │ ├── StreamAccountStats.java │ ├── StreamStatProgram.java │ ├── SuspiciousAccount.java │ ├── UnmodifiableAccount.java │ ├── UnmodifiableBankIterator.java │ └── Visitor.java └── fx │ ├── AccountCreationWindow.java │ └── ReadMe.txt └── chapter11 ├── bank19 ├── AbstractBankAccount.java ├── AccountFactories.java ├── AccountFactory.java ├── AllController.java ├── AllController2.java ├── AllView.java ├── AllView2.java ├── AllView3.java ├── Auditor.java ├── AuthorizerWrapper.java ├── Bank.java ├── BankAccount.java ├── BankAccountAdapter.java ├── BankAccountWrapper.java ├── BankClient.java ├── BankEvent.java ├── BankObserver.java ├── BankProgram.java ├── CheckingAccount.java ├── CollateralAuthorizer.java ├── CreationController.java ├── CreationView.java ├── CreditScoreAuthorizer.java ├── FBIAcctInfo.java ├── FBIClient.java ├── FxBankProgram.java ├── GoodCustomerAuthorizer.java ├── InfoController.java ├── InfoView.java ├── InputCommand.java ├── InputCommands.java ├── InputController.java ├── InterestChecking.java ├── IteratorAccountStats.java ├── IteratorStatProgram.java ├── Loan.java ├── LoanAdapter.java ├── LoanAuthorizer.java ├── MaxBalanceVisitor.java ├── ObservableMapAdapter.java ├── OwnerStrategy.java ├── Owners.java ├── ReadMe.txt ├── RegularChecking.java ├── SavedBankInfo.java ├── SavingsAccount.java ├── StreamStatController.java ├── StreamStatModel.java ├── StreamStatProgram.java ├── StreamStatView.java ├── SuspiciousAccount.java ├── UnmodifiableAccount.java ├── UnmodifiableBankIterator.java └── Visitor.java ├── count1 ├── .DS_Store ├── Count1.java └── ReadMe.txt ├── count2 ├── Count2.java ├── CountController.java ├── CountModel.java ├── CountView.java └── ReadMe.txt └── count3 ├── Count3.java ├── CountController.java ├── CountModel.java ├── CountObserver.java ├── CountView.java ├── ReadMe.txt ├── WatcherController.java └── WatcherView.java /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-prog-design/b8028b786f81ed3b2369105ccc94b7171b5be599/.DS_Store -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /9781484241424.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-prog-design/b8028b786f81ed3b2369105ccc94b7171b5be599/9781484241424.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Java Program Design*](https://www.apress.com/9781484241424) by Edward Sciore (Apress, 2018). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484241424.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. -------------------------------------------------------------------------------- /ReadMe.txt: -------------------------------------------------------------------------------- 1 | Source Code for the book 2 | Java Program Design: Principles, Polymorphism, and Patterns 3 | by Edward Sciore 4 | 5 | The folder "javaprogramdesign" contains a subfolder for each chapter. 6 | The source code for each chapter is organized by topic, 7 | with one folder per topic. Each topic folder has a file named ReadMe.txt that 8 | explains its Java code and its purpose. 9 | 10 | The package of each Java file corresponds to its location in the folder hierarchy. 11 | It should therefore be possible to run the code by simply downloading the folder 12 | "javaprogramdesign" to your Java source code directory. 13 | 14 | 15 | -------------------------------------------------------------------------------- /javaprogramdesign/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-prog-design/b8028b786f81ed3b2369105ccc94b7171b5be599/javaprogramdesign/.DS_Store -------------------------------------------------------------------------------- /javaprogramdesign/chapter01/bank01/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 1 2 | 3 | The initial, non-object-oriented banking demo. 4 | 5 | This program consists of a single class, named BankProgram. This class holds a map 6 | that stores the balances of several savings accounts held by a bank. Each element 7 | in the map is a key-value pair. The key is an integer that denotes the account number, 8 | and its value is the balance of that account, in cents. 9 | 10 | The program runs as a loop that executes commands from the console. There are seven commands: 11 | 0) quit, which exits the loop. 12 | 1) new, which inserts a new element into the map, assigning it a new account number 13 | and an initial balance of 0. 14 | 3) get, which prints the balance of a given account. 15 | 4) deposit, which increases the balance of a given account by a specified number of cents. 16 | 5) authorizeloan, which determines whether an account has enough money to be used as 17 | collateral for a loan. The criterion is that the account must contain at least half 18 | of the loan amount. 19 | 6) show, which prints the balance of every account. 20 | 7) interest, which increases the balance of each account by a fixed interest rate. 21 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter01/bank02/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter01.bank02; 2 | 3 | public class BankProgram { 4 | public static void main(String[] args) { 5 | BankClient client = new BankClient(); 6 | client.run(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter01/bank02/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 2 2 | 3 | This version refactors version 1 into three classes: 4 | 5 | The class Bank is responsible for the banking information. 6 | 7 | The class BankClient is responsible for I/O processing. 8 | 9 | The class BankProgram is responsible for putting everything together. 10 | Here it doesn't do much, but it will in later versions. 11 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter01/bank03/AlternativeBankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter01.bank03; 2 | 3 | // This is a version of BankAccount that uses 4 | // public variables in lieu of methods. 5 | // It is an example of bad design. 6 | 7 | public class AlternativeBankAccount { 8 | public int acctnum; 9 | public int balance = 0; 10 | public boolean isforeign = false; 11 | 12 | public AlternativeBankAccount(int a) { 13 | acctnum = a; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter01/bank03/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter01.bank03; 2 | 3 | public class BankAccount { 4 | private int acctnum; 5 | private int balance = 0; 6 | private boolean isforeign = false; 7 | 8 | public BankAccount(int a) { 9 | acctnum = a; 10 | } 11 | 12 | public int getAcctNum() { 13 | return acctnum; 14 | } 15 | 16 | public int getBalance() { 17 | return balance; 18 | } 19 | 20 | public void setBalance(int amt) { 21 | balance = amt; 22 | } 23 | 24 | public boolean isForeign() { 25 | return isforeign; 26 | } 27 | 28 | public void setForeign(boolean b) { 29 | isforeign = b; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter01/bank03/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter01.bank03; 2 | 3 | public class BankProgram { 4 | public static void main(String[] args) { 5 | BankClient client = new BankClient(); 6 | client.run(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter01/bank03/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 3 2 | 3 | Introduces the new class BankAccount. The class Bank is 4 | modified to contain a map of BankAccount objects. 5 | 6 | In addition to the balance, an account also keeps track of 7 | whether the account owner is foreign or domestic. 8 | 9 | BankClient gets a new command (command 7) to allow 10 | the user to set the account ownership to foreign or domestic. 11 | The newAccount command is modified to request the initial ownership. 12 | 13 | Bank's toString method is modified to also display the ownership 14 | of each account. -------------------------------------------------------------------------------- /javaprogramdesign/chapter01/bank04/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter01.bank04; 2 | 3 | public class BankAccount { 4 | private double rate = 0.01; 5 | private int acctnum; 6 | private int balance = 0; 7 | private boolean isforeign = false; 8 | 9 | public BankAccount(int acctnum) { 10 | this.acctnum = acctnum; 11 | } 12 | 13 | public int getAcctNum() { 14 | return acctnum; 15 | } 16 | 17 | public int getBalance() { 18 | return balance; 19 | } 20 | 21 | public boolean isForeign() { 22 | return isforeign; 23 | } 24 | 25 | public void setForeign(boolean b) { 26 | isforeign = b; 27 | } 28 | 29 | public void deposit(int amt) { 30 | balance += amt; 31 | } 32 | 33 | public boolean hasEnoughCollateral(int loanamt) { 34 | return balance >= loanamt / 2; 35 | } 36 | 37 | public String toString() { 38 | return "Account " + acctnum + ": balance=" + balance 39 | + ", is " + (isforeign ? "foreign" : "domestic"); 40 | } 41 | 42 | public void addInterest() { 43 | balance += (int) (balance * rate); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter01/bank04/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter01.bank04; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | HashMap accounts = new HashMap<>(); 8 | Bank bank = new Bank(accounts, 0); 9 | Scanner scanner = new Scanner(System.in); 10 | BankClient client = new BankClient(scanner, bank); 11 | client.run(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter01/bank04/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 4 2 | 3 | This version improves the BankAccount class by giving it 4 | more responsibilities (interest rate, and the methods 5 | deposit, toString, addInterest, and hasEnoughCollateral). 6 | 7 | The Bank class is modified so that it delegates to 8 | these new methods. 9 | 10 | The classes have been modified to uses dependency injection: 11 | The Bank gets its map of accounts and its nextacct value via its constructor. 12 | The BankClient gets its input stream and Bank object via its constructor. 13 | The BankProgram is responsible for creating these things. 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter02/bank05/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter02.bank05; 2 | 3 | public interface BankAccount { 4 | public abstract int getAcctNum(); 5 | public abstract int getBalance(); 6 | public abstract boolean isForeign(); 7 | public abstract void setForeign(boolean isforeign); 8 | public abstract void deposit(int amt); 9 | public abstract boolean hasEnoughCollateral(int loanamt); 10 | public abstract String toString(); 11 | } 12 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter02/bank05/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter02.bank05; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | HashMap accounts = new HashMap<>(); 8 | Bank bank = new Bank(accounts, 0); 9 | Scanner scanner = new Scanner(System.in); 10 | BankClient client = new BankClient(scanner, bank); 11 | client.run(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter02/bank05/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter02.bank05; 2 | 3 | public class CheckingAccount implements BankAccount { 4 | private int acctnum; 5 | private int balance = 0; 6 | private boolean isforeign = false; 7 | 8 | public CheckingAccount(int acctnum) { 9 | this.acctnum = acctnum; 10 | } 11 | 12 | public int getAcctNum() { 13 | return acctnum; 14 | } 15 | 16 | public int getBalance() { 17 | return balance; 18 | } 19 | 20 | public boolean isForeign() { 21 | return isforeign; 22 | } 23 | 24 | public void setForeign(boolean b) { 25 | isforeign = b; 26 | } 27 | 28 | public void deposit(int amt) { 29 | balance += amt; 30 | } 31 | 32 | public boolean hasEnoughCollateral(int loanamt) { 33 | return balance >= 2 * loanamt / 3; 34 | } 35 | 36 | public String toString() { 37 | return "Checking account " + acctnum + ": balance=" + balance 38 | + ", is " + (isforeign ? "foreign" : "domestic"); 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter02/bank05/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 5 2 | 3 | This version introduces the interface BankAccount, with 4 | implementing classes SavingsAccount and CheckingAccount. 5 | 6 | The BankAccount method headers all have the keywords 7 | "public abstract". These keywords are optional, and are 8 | omitted from subsequent versions of the interface. 9 | 10 | The class SavingsAccount is a copy of the version 4 BankAccount 11 | code except that it has "implements BankAccount" in its header. 12 | 13 | The class CheckingAccount is similar to SavingsAccount, except 14 | that it has different code for the hasEnoughCollateral and toString 15 | methods. It also does not have an addInterest method. 16 | 17 | The class BankClient is modified so that the user specifies a 18 | savings account or checking account when creating a new account. 19 | 20 | The class Bank is modified so that its newAccount constructor 21 | has an argument denoting the account type, and creates a 22 | new SavingsAccount or CheckingAccount object as appropriate. 23 | 24 | 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter02/bank05/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter02.bank05; 2 | 3 | public class SavingsAccount implements BankAccount { 4 | private double rate = 0.01; 5 | private int acctnum; 6 | private int balance = 0; 7 | private boolean isforeign = false; 8 | 9 | public SavingsAccount(int acctnum) { 10 | this.acctnum = acctnum; 11 | } 12 | 13 | public int getAcctNum() { 14 | return acctnum; 15 | } 16 | 17 | public int getBalance() { 18 | return balance; 19 | } 20 | 21 | public boolean isForeign() { 22 | return isforeign; 23 | } 24 | 25 | public void setForeign(boolean b) { 26 | isforeign = b; 27 | } 28 | 29 | public void deposit(int amt) { 30 | balance += amt; 31 | } 32 | 33 | public boolean hasEnoughCollateral(int loanamt) { 34 | return balance >= loanamt / 2; 35 | } 36 | 37 | public String toString() { 38 | return "Savings account " + acctnum + ": balance=" + balance 39 | + ", is " + (isforeign ? "foreign" : "domestic"); 40 | } 41 | 42 | public void addInterest() { 43 | balance += (int) (balance * rate); 44 | } 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter02/bank06/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter02.bank06; 2 | 3 | public interface BankAccount extends Comparable { 4 | int getAcctNum(); 5 | int getBalance(); 6 | void deposit(int amt); 7 | boolean isForeign(); 8 | void setForeign(boolean isforeign); 9 | boolean hasEnoughCollateral(int loanamt); 10 | String toString(); 11 | void addInterest(); 12 | 13 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 14 | BankAccount ba = new SavingsAccount(acctnum); 15 | ba.deposit(n); 16 | return ba; 17 | } 18 | 19 | default boolean isEmpty() { 20 | return getBalance() == 0; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter02/bank06/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter02.bank06; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | Map accounts = new HashMap<>(); 8 | Bank bank = new Bank(accounts, 0); 9 | Scanner scanner = new Scanner(System.in); 10 | BankClient client = new BankClient(scanner, bank); 11 | client.run(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter02/bank06/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 6 2 | 3 | The BankAccount interface now has the method addInterest, 4 | to support transparency. It also extends Comparable, 5 | and contains a static method createSavingsWithDeposit 6 | and a default method isEmpty. 7 | 8 | The CheckingAccount class now has the addInterest method, and 9 | both CheckingAccount and SavingsAccount have a compareTo method. 10 | They also both explicitly implement Comparable. This 11 | is redundant, given that BankAccount also implements Comparable, 12 | but is not illegal. Future versions will drop this. 13 | 14 | The Bank's addInterest method has been modified to be transparent. 15 | 16 | The variables named "accounts" in Bank and BankProgram 17 | have been modified to be a Map instead of a HashMap. 18 | 19 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter02/comparable/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Testing the Comparable Interface. 2 | 3 | CompareSavingsAccounts compares SavingsAccount objects. 4 | CompareBankAccounts compares objects that implement BankAccount. 5 | Both work with version 6 (or higher) of the banking demo. 6 | 7 | Both programs compare bank accounts by their natural order. 8 | They each create a list of accounts and then calculate the account 9 | having the largest balance. The programs calculate the largest balance 10 | in two ways. 11 | 12 | The first way is use the method findMax to do a linear search of the list. 13 | The second way is to use the method Collections.max from the Java library. 14 | 15 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank07/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank07; 2 | 3 | public interface BankAccount extends Comparable { 4 | int getAcctNum(); 5 | int getBalance(); 6 | void deposit(int amt); 7 | boolean isForeign(); 8 | void setForeign(boolean isforeign); 9 | boolean hasEnoughCollateral(int loanamt); 10 | String toString(); 11 | void addInterest(); 12 | 13 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 14 | BankAccount ba = new SavingsAccount(acctnum); 15 | ba.deposit(n); 16 | return ba; 17 | } 18 | 19 | default boolean isEmpty() { 20 | return getBalance() == 0; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank07/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank07; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | Map accounts = new HashMap<>(); 8 | Bank bank = new Bank(accounts, 0); 9 | Scanner scanner = new Scanner(System.in); 10 | BankClient client = new BankClient(scanner, bank); 11 | client.run(); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank07/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank07; 2 | 3 | public class CheckingAccount extends AbstractBankAccount { 4 | 5 | public CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | public boolean hasEnoughCollateral(int loanamt) { 10 | return balance >= 2 * loanamt / 3; 11 | } 12 | 13 | public String toString() { 14 | return "Checking account " + acctnum + ": balance=" + balance 15 | + ", is " + (isforeign ? "foreign" : "domestic"); 16 | } 17 | 18 | public void addInterest() { 19 | // do nothing 20 | } 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank07/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank07; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | private double rate = 0.01; 5 | 6 | public InterestChecking(int acctnum) { 7 | super(acctnum); 8 | } 9 | 10 | public String toString() { 11 | return "Interest checking account " + acctnum + ": balance=" + balance 12 | + ", is " + (isforeign ? "foreign" : "domestic"); 13 | } 14 | 15 | public void addInterest() { 16 | balance += (int) (balance * rate); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank07/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 7. 2 | 3 | AbstractBankAccount is an abstract class that contains 4 | the common variables and methods of SavingsAccount and 5 | CheckingAccount, namely the variables acctnum and balance, 6 | and the methods getAcctNum, getBalance, deposit, compareTo, 7 | and equals. 8 | 9 | AbstractBankAccount also gets abstract methods for toString, 10 | hasEnoughCollateral, and addInterest. 11 | 12 | CheckingAccount has the new subclass InterestChecking. 13 | 14 | The processCommand method in BankClient is modified to 15 | support requests for new interest checking accounts. 16 | 17 | The newAcount method in Bank is modified to create new 18 | InterestChecking objects. 19 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank07/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank07; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | private double rate = 0.01; 5 | 6 | public SavingsAccount(int acctnum) { 7 | super(acctnum); 8 | } 9 | 10 | public boolean hasEnoughCollateral(int loanamt) { 11 | return balance >= loanamt / 2; 12 | } 13 | 14 | public String toString() { 15 | return "Savings account " + acctnum + ": balance=" + balance 16 | + ", is " + (isforeign ? "foreign" : "domestic"); 17 | } 18 | 19 | public void addInterest() { 20 | balance += (int) (balance * rate); 21 | } 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank08/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank08; 2 | 3 | public interface BankAccount extends Comparable { 4 | int getAcctNum(); 5 | int getBalance(); 6 | void deposit(int amt); 7 | boolean isForeign(); 8 | void setForeign(boolean isforeign); 9 | boolean hasEnoughCollateral(int loanamt); 10 | String toString(); 11 | void addInterest(); 12 | 13 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 14 | BankAccount ba = new SavingsAccount(acctnum); 15 | ba.deposit(n); 16 | return ba; 17 | } 18 | 19 | default boolean isEmpty() { 20 | return getBalance() == 0; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank08/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank08; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Scanner scanner = new Scanner(System.in); 12 | BankClient client = new BankClient(scanner, bank); 13 | client.run(); 14 | info.saveMap(accounts, bank.nextAcctNum()); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank08/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank08; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | public boolean hasEnoughCollateral(int loanamt) { 10 | return balance >= 2 * loanamt / 3; 11 | } 12 | 13 | public abstract String toString(); 14 | public abstract void addInterest(); 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank08/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank08; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | private double rate = 0.01; 5 | 6 | public InterestChecking(int acctnum) { 7 | super(acctnum); 8 | } 9 | 10 | public String toString() { 11 | return "Interest checking account " + acctnum + ": balance=" + balance 12 | + ", is " + (isforeign ? "foreign" : "domestic"); 13 | } 14 | 15 | public void addInterest() { 16 | balance += (int) (balance * rate); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank08/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 8. 2 | 3 | CheckingAccount is now an abstract class, having 4 | the subclasses RegularChecking and InterestChecking. 5 | 6 | CheckingAccount keeps the implementation of the method 7 | hasEnoughCollateral. The methods toString and addInterest 8 | are abstract, and are implemented in its subclasses. 9 | 10 | Bank's newAccount method is modified so that type==2 creates 11 | a new RegularChecking object. 12 | 13 | BankProgram has been modified so that it reads the bank's initial 14 | account information from a file when the program begins, 15 | and writes the account information to the file when the program ends. 16 | It uses the class SavedBankInfo to interact with the file. 17 | The public method nextAcctNum is added to Bank, 18 | so that the value of its nextAcct variable can be saved. 19 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank08/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank08; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | public RegularChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | public String toString() { 9 | return "Regular checking account " + acctnum + ": balance=" + balance 10 | + ", is " + (isforeign ? "foreign" : "domestic"); 11 | } 12 | 13 | public void addInterest() { 14 | // do nothing 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank08/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank08; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | private double rate = 0.01; 5 | 6 | public SavingsAccount(int acctnum) { 7 | super(acctnum); 8 | } 9 | 10 | public boolean hasEnoughCollateral(int loanamt) { 11 | return balance >= loanamt / 2; 12 | } 13 | 14 | public String toString() { 15 | return "Savings account " + acctnum + ": balance=" + balance 16 | + ", is " + (isforeign ? "foreign" : "domestic"); 17 | } 18 | 19 | public void addInterest() { 20 | balance += (int) (balance * rate); 21 | } 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank09/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank09; 2 | 3 | public interface BankAccount extends Comparable { 4 | int getAcctNum(); 5 | int getBalance(); 6 | void deposit(int amt); 7 | boolean isForeign(); 8 | void setForeign(boolean isforeign); 9 | boolean hasEnoughCollateral(int loanamt); 10 | String toString(); 11 | void addInterest(); 12 | 13 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 14 | BankAccount ba = new SavingsAccount(acctnum); 15 | ba.deposit(n); 16 | return ba; 17 | } 18 | 19 | default boolean isEmpty() { 20 | return getBalance() == 0; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank09/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank09; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Scanner scanner = new Scanner(System.in); 12 | BankClient client = new BankClient(scanner, bank); 13 | client.run(); 14 | info.saveMap(accounts, bank.nextAcctNum()); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank09/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank09; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected double collateralRatio() { 10 | return 2.0 / 3.0; 11 | } 12 | 13 | protected abstract String accountType(); 14 | protected abstract double interestRate(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank09/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank09; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | public InterestChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | protected double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank09/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 9. 2 | 3 | The AbstractBankAccount class has been redesigned to use the 4 | template pattern. In particular, the class now partially implements 5 | the methods hasEnoughCollateral, toString, and applyInterest, 6 | using the abstract methods collateralRatio, accountType, 7 | and interestRate. 8 | 9 | SavingsAccount is rewritten to implement those abstract methods. 10 | 11 | CheckingAccount is rewritten so implement collateralRatio, 12 | and declares accountType and interestRate to be abstract. 13 | 14 | RegularChecking and InterestChecking are rewritten to implement 15 | accountType and interestRate. 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank09/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank09; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | 5 | public RegularChecking(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected String accountType() { 10 | return "Regular checking"; 11 | } 12 | 13 | protected double interestRate() { 14 | return 0.0; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bank09/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bank09; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | public SavingsAccount(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected double collateralRatio() { 9 | return 1.0 / 2.0; 10 | } 11 | 12 | protected String accountType() { 13 | return "Savings"; 14 | } 15 | 16 | protected double interestRate() { 17 | return 0.01; 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bytestream/ByteArrayInputStream.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bytestream; 2 | 3 | import java.io.*; 4 | 5 | // A simplified version of the class in the Java library. 6 | 7 | public class ByteArrayInputStream extends InputStream { 8 | 9 | private byte[] a; 10 | private int pos = 0; 11 | 12 | public ByteArrayInputStream(byte[] a) { 13 | this.a = a; 14 | } 15 | 16 | public int read() throws IOException { 17 | if (pos >= a.length) 18 | return -1; 19 | else { 20 | pos++; 21 | return a[pos-1]; 22 | } 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bytestream/EncryptDecrypt.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.bytestream; 2 | import java.io.*; 3 | 4 | public class EncryptDecrypt { 5 | public static void main(String[] args) throws IOException { 6 | int offset = 26; // any value will do 7 | encrypt("mobydick.txt", "encrypted.txt", offset); 8 | encrypt("encrypted.txt", "decrypted.txt", -offset); 9 | } 10 | 11 | private static void encrypt(String source, String output, int offset) 12 | throws IOException { 13 | try ( InputStream is = new FileInputStream(source); 14 | OutputStream os = new FileOutputStream(output) ) { 15 | int x; 16 | while ((x = is.read()) >= 0) { 17 | byte b = (byte) x; 18 | b += offset; 19 | os.write(b); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/bytestream/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 3 ByteStream Examples 2 | 3 | ByteArrayInputStream is a simplified version of the 4 | Java library class of the same name, which illustrates 5 | how you can write a class that extends InputStream. 6 | 7 | EncryptDecrpyt illustrates the basic use of the InputStream 8 | methods. 9 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/rangelist/RangeList.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.rangelist; 2 | 3 | import java.util.*; 4 | 5 | public class RangeList extends AbstractList { 6 | private int limit; 7 | 8 | public RangeList(int limit) { 9 | this.limit = limit; 10 | } 11 | 12 | public int size() { 13 | return limit; 14 | } 15 | 16 | public Integer get(int n) { 17 | return n; 18 | } 19 | 20 | public static void main(String[] args) { 21 | List L = new RangeList(20); 22 | 23 | // print the list using get() 24 | for (int i=0; i L = new RangeList(20); 8 | for (int x : L) 9 | System.out.print(x + " "); 10 | System.out.println(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/rangelist/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 3 RangeList Example 2 | 3 | The class RangeList implements List, and denotes a 4 | list of integers from 0 to N-1 for some N. 5 | 6 | The class RangeListTest tests RangeList. 7 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/thread/ReadLine.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.thread; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReadLine extends Thread { 6 | private boolean done = false; 7 | 8 | public void run() { 9 | Scanner sc = new Scanner(System.in); 10 | String s = sc.nextLine(); 11 | sc.close(); 12 | done = true; 13 | } 14 | 15 | public boolean isDone() { 16 | return done; 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/thread/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 3 Thread Example 2 | 3 | The class ReadLine blocks until an input line is entered. 4 | It then sets the variable done to true. 5 | 6 | ThreadTest runs ReadLine in a separate thread, and starts 7 | printing integers. It stops when ReadLine's variable done 8 | is true. 9 | 10 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter03/thread/ThreadTest.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter03.thread; 2 | 3 | public class ThreadTest { 4 | public static void main(String[] args) { 5 | ReadLine r = new ReadLine(); 6 | r.start(); 7 | int i = 0; 8 | while(!r.isDone()) { 9 | System.out.println(i); 10 | i++; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | public interface BankAccount extends Comparable { 4 | int getAcctNum(); 5 | int getBalance(); 6 | void deposit(int amt); 7 | boolean isForeign(); 8 | void setForeign(boolean isforeign); 9 | boolean hasEnoughCollateral(int loanamt); 10 | String toString(); 11 | void addInterest(); 12 | int fee(); 13 | 14 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 15 | BankAccount ba = new SavingsAccount(acctnum); 16 | ba.deposit(n); 17 | return ba; 18 | } 19 | 20 | default boolean isEmpty() { 21 | return getBalance() == 0; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Scanner scanner = new Scanner(System.in); 12 | BankClient client = new BankClient(scanner, bank); 13 | client.run(); 14 | info.saveMap(accounts, bank.nextAcctNum()); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected double collateralRatio() { 10 | return 2.0 / 3.0; 11 | } 12 | 13 | protected abstract String accountType(); 14 | protected abstract double interestRate(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/DepositCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DepositCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.print("Enter deposit amount: "); 8 | int amt = sc.nextInt(); 9 | bank.deposit(current, amt); 10 | return current; 11 | } 12 | 13 | public String toString() { 14 | return "deposit"; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/Domestic.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | public class Domestic implements OwnerStrategy { 4 | public boolean isForeign() { 5 | return false; 6 | } 7 | 8 | public int fee() { 9 | return 0; 10 | } 11 | 12 | public String toString() { 13 | return "domestic"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/Foreign.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | public class Foreign implements OwnerStrategy { 4 | public boolean isForeign() { 5 | return true; 6 | } 7 | 8 | public int fee() { 9 | return 500; 10 | } 11 | 12 | public String toString() { 13 | return "foreign"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/InputCommand.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | import java.util.Scanner; 4 | 5 | public interface InputCommand { 6 | int execute(Scanner sc, Bank bank, int current); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | public InterestChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | protected double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/InterestCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class InterestCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | bank.addInterest(); 8 | return current; 9 | } 10 | 11 | public String toString() { 12 | return "interest"; 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/LoanCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LoanCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.print("Enter loan amount: "); 8 | int amt = sc.nextInt(); 9 | boolean ok = bank.authorizeLoan(current, amt); 10 | if (ok) 11 | System.out.println("Your loan is approved"); 12 | else 13 | System.out.println("Your loan is denied"); 14 | return current; 15 | } 16 | 17 | public String toString() { 18 | return "loan"; 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/NewCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class NewCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.print("Enter account type(1=savings, 2=checking, 3=interest checking): "); 8 | int type = sc.nextInt(); 9 | System.out.print("Enter 1 for foreign, 2 for domestic: "); 10 | int val = sc.nextInt(); 11 | boolean isforeign = (val == 1); 12 | current = bank.newAccount(type, isforeign); 13 | System.out.println("Your new account number is " + current); 14 | return current; 15 | } 16 | 17 | public String toString() { 18 | return "new"; 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/OwnerStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | public interface OwnerStrategy { 4 | boolean isForeign(); 5 | int fee(); 6 | } 7 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/QuitCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class QuitCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | sc.close(); 8 | System.out.println("Goodbye!"); 9 | return -1; 10 | } 11 | 12 | public String toString() { 13 | return "quit"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | 5 | public RegularChecking(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected String accountType() { 10 | return "Regular checking"; 11 | } 12 | 13 | protected double interestRate() { 14 | return 0.0; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | public SavingsAccount(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected double collateralRatio() { 9 | return 1.0 / 2.0; 10 | } 11 | 12 | protected String accountType() { 13 | return "Savings"; 14 | } 15 | 16 | protected double interestRate() { 17 | return 0.01; 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/SelectCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SelectCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.print("Enter account#: "); 8 | current = sc.nextInt(); 9 | int balance = bank.getBalance(current); 10 | System.out.println("The balance of account " + current + " is " + balance); 11 | return current; 12 | } 13 | 14 | public String toString() { 15 | return "select"; 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/SetForeignCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SetForeignCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.print("Enter 1 for foreign, 2 for domestic: "); 8 | int val = sc.nextInt(); 9 | boolean isforeign = (val == 1); 10 | bank.setForeign(current, isforeign); 11 | return current; 12 | } 13 | 14 | public String toString() { 15 | return "setforeign"; 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank10/ShowCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ShowCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.println(bank.toString()); 8 | return current; 9 | } 10 | 11 | public String toString() { 12 | return "Show"; 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | public interface BankAccount extends Comparable { 4 | int getAcctNum(); 5 | int getBalance(); 6 | void deposit(int amt); 7 | boolean isForeign(); 8 | void setForeign(boolean isforeign); 9 | boolean hasEnoughCollateral(int loanamt); 10 | String toString(); 11 | void addInterest(); 12 | int fee(); 13 | 14 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 15 | TypeStrategy ts = new SavingsAccount(); 16 | BankAccount ba = new AbstractBankAccount(acctnum, ts); 17 | ba.deposit(n); 18 | return ba; 19 | } 20 | 21 | default boolean isEmpty() { 22 | return getBalance() == 0; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Scanner scanner = new Scanner(System.in); 12 | BankClient client = new BankClient(scanner, bank); 13 | client.run(); 14 | info.saveMap(accounts, bank.nextAcctNum()); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/DepositCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DepositCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.print("Enter deposit amount: "); 8 | int amt = sc.nextInt(); 9 | bank.deposit(current, amt); 10 | return current; 11 | } 12 | 13 | public String toString() { 14 | return "deposit"; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/Domestic.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | public class Domestic implements OwnerStrategy { 4 | public boolean isForeign() { 5 | return false; 6 | } 7 | 8 | public String toString() { 9 | return "domestic"; 10 | } 11 | 12 | public int fee() { 13 | return 0; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/Foreign.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | public class Foreign implements OwnerStrategy { 4 | public boolean isForeign() { 5 | return true; 6 | } 7 | 8 | public int fee() { 9 | return 500; 10 | } 11 | 12 | public String toString() { 13 | return "foreign"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/InputCommand.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | import java.util.Scanner; 4 | 5 | public interface InputCommand { 6 | int execute(Scanner sc, Bank bank, int current); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | public class InterestChecking implements TypeStrategy { 4 | public double collateralRatio() { 5 | return 2.0 / 3.0; 6 | } 7 | 8 | public String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | public double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/InterestCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | import java.util.Scanner; 4 | 5 | public class InterestCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | bank.addInterest(); 8 | return current; 9 | } 10 | 11 | public String toString() { 12 | return "interest"; 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/LoanCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LoanCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.print("Enter loan amount: "); 8 | int amt = sc.nextInt(); 9 | boolean ok = bank.authorizeLoan(current, amt); 10 | if (ok) 11 | System.out.println("Your loan is approved"); 12 | else 13 | System.out.println("Your loan is denied"); 14 | return current; 15 | } 16 | 17 | public String toString() { 18 | return "loan"; 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/NewCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | import java.util.Scanner; 4 | 5 | public class NewCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.print("Enter account type(1=savings, 2=checking, 3=interest checking): "); 8 | int type = sc.nextInt(); 9 | boolean isforeign = requestForeign(sc); 10 | current = bank.newAccount(type, isforeign); 11 | System.out.println("Your new account number is " + current); 12 | return current; 13 | } 14 | 15 | public String toString() { 16 | return "new"; 17 | } 18 | 19 | private boolean requestForeign(Scanner sc) { 20 | System.out.print("Enter 1 for foreign, 2 for domestic: "); 21 | int val = sc.nextInt(); 22 | return (val == 1); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/OwnerStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | public interface OwnerStrategy { 4 | boolean isForeign(); 5 | int fee(); 6 | } 7 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/QuitCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | import java.util.Scanner; 4 | 5 | public class QuitCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | sc.close(); 8 | System.out.println("Goodbye!"); 9 | return -1; 10 | } 11 | 12 | public String toString() { 13 | return "quit"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 11. 2 | 3 | AbstractBankAccount is no longer an abstract class and has no 4 | subclasses. Instead, the information that was provided by the 5 | subclasses is now provided by the strategy interface 6 | TypeStrategy and its implementing subclasses SavingsType, 7 | RegularCheckingType, and InterestCheckingType. 8 | 9 | The TypeStrategy methods implement the same 3 methods that 10 | were previously abstract in AbstractBankAccount: 11 | collateralRatio, accountType, and interestRate. 12 | 13 | AbstractBankAccount has a variable ts that holds its 14 | TypeStrategy object. Its methods collateralRatio, accountType, 15 | and interestRate call the corresponding TypeStrategy methods. 16 | 17 | The Bank's newAccount method has been modified to create the proper 18 | TypeStrategy object and to pass it to the AbstractBankAccount constructor. 19 | 20 | The BankAccount static method createSavingsWithDeposit 21 | and the SavedBankInfo methods have been modified similarly. 22 | 23 | This version of the demo is a dead end. Subsequent versions 24 | are based on version 10. 25 | 26 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | public class RegularChecking implements TypeStrategy { 4 | public double collateralRatio() { 5 | return 2.0 / 3.0; 6 | } 7 | 8 | public String accountType() { 9 | return "Regular checking"; 10 | } 11 | 12 | public double interestRate() { 13 | return 0.0; 14 | } 15 | } -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | public class SavingsAccount implements TypeStrategy { 4 | public double collateralRatio() { 5 | return 1.0 / 2.0; 6 | } 7 | 8 | public String accountType() { 9 | return "Savings"; 10 | } 11 | 12 | public double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/SelectCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SelectCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.print("Enter account#: "); 8 | current = sc.nextInt(); 9 | int balance = bank.getBalance(current); 10 | System.out.println("The balance of account " + current + " is " + balance); 11 | return current; 12 | } 13 | 14 | public String toString() { 15 | return "select"; 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/SetForeignCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SetForeignCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | bank.setForeign(current, requestForeign(sc)); 8 | return current; 9 | } 10 | 11 | public String toString() { 12 | return "setforeign"; 13 | } 14 | 15 | private boolean requestForeign(Scanner sc) { 16 | System.out.print("Enter 1 for foreign, 2 for domestic: "); 17 | int val = sc.nextInt(); 18 | return (val == 1); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/ShowCmd.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ShowCmd implements InputCommand { 6 | public int execute(Scanner sc, Bank bank, int current) { 7 | System.out.println(bank.toString()); 8 | return current; 9 | } 10 | 11 | public String toString() { 12 | return "Show"; 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/bank11/TypeStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.bank11; 2 | 3 | public interface TypeStrategy { 4 | double collateralRatio(); 5 | String accountType(); 6 | double interestRate(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/comparator/AcctByMinBal.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.comparator; 2 | 3 | import java.util.Comparator; 4 | import javaprogramdesign.chapter04.bank10.BankAccount; 5 | 6 | public class AcctByMinBal implements Comparator { 7 | public int compare(BankAccount ba1, BankAccount ba2) { 8 | int bal1 = ba1.getBalance(); 9 | int bal2 = ba2.getBalance(); 10 | if (bal1 == bal2) 11 | return ba1.getAcctNum() - ba2.getAcctNum(); 12 | else 13 | return bal2 - bal1; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/comparator/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Testing the Comparator Interface. 2 | 3 | The program ComparatorBankAccounts revises the program 4 | CompareBankccounts from Chapter 2. It imports the BankAccount 5 | definitions from version 10 of the banking demo. 6 | 7 | The class AcctByMinBal implements Comparator, and is the 8 | basic way to create a comparator. 9 | 10 | ComparatorBankAccounts defines four Comparator interfaces. The first uses 11 | AcctByMinBal. The second uses an anonymous inner class. The third and 12 | fourth use lambda expressions. 13 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/intprocessor/IntProcessorStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.intprocessor; 2 | 3 | public class IntProcessorStrategy { 4 | private Operation op; 5 | 6 | public IntProcessorStrategy(Operation s) { 7 | this.op = s; 8 | } 9 | public void operateOn(int x) { 10 | int y = f(x); 11 | System.out.println(x + " becomes " + y); 12 | } 13 | private int f(int x) { 14 | return op.f(x); 15 | } 16 | } 17 | 18 | interface Operation { 19 | public int f(int x); 20 | } 21 | 22 | class AddOneStrategy implements Operation { 23 | public int f(int x) { 24 | return x+1; 25 | } 26 | } 27 | 28 | class AddTwoStrategy implements Operation { 29 | public int f(int x) { 30 | return x+2; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/intprocessor/IntProcessorTemplate.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.intprocessor; 2 | 3 | public abstract class IntProcessorTemplate { 4 | public void operateOn(int x) { 5 | int y = f(x); 6 | System.out.println(x + " becomes " + y); 7 | } 8 | protected abstract int f(int x); 9 | } 10 | 11 | class AddOneTemplate extends IntProcessorTemplate { 12 | protected int f(int x) { 13 | return x+1; 14 | } 15 | } 16 | 17 | class AddTwoTemplate extends IntProcessorTemplate { 18 | protected int f(int x) { 19 | return x+2; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/intprocessor/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 4 IntProcessor Example 2 | 3 | The two example programs illustrate the commonalities of 4 | the template and strategy patterns. 5 | 6 | The IntProcessorTemplate class has two subclasses, 7 | AddOneTemplate and AddTwoTemplate, each of which implements 8 | the abstract method f. 9 | 10 | The IntProcessorStrategy class has a variable op, 11 | which references a class that implements the strategy 12 | interface Operation. The classes AddOneStrategy and 13 | AddTwoStrategy implement Operation. 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/intprocessor/TestClientStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.intprocessor; 2 | 3 | public class TestClientStrategy { 4 | public static void main(String[] args) { 5 | Operation op1 = new AddOneStrategy(); 6 | Operation op2 = new AddTwoStrategy(); 7 | IntProcessorStrategy p1 = new IntProcessorStrategy(op1); 8 | IntProcessorStrategy p2 = new IntProcessorStrategy(op2); 9 | p1.operateOn(6); p2.operateOn(6); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/intprocessor/TestClientTemplate.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.intprocessor; 2 | 3 | public class TestClientTemplate { 4 | public static void main(String[] args) { 5 | IntProcessorTemplate p1 = new AddOneTemplate(); 6 | IntProcessorTemplate p2 = new AddTwoTemplate(); 7 | p1.operateOn(6); p2.operateOn(6); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/thread/ReadLine.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.thread; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReadLine implements Runnable { 6 | private boolean done = false; 7 | 8 | public void run() { 9 | Scanner sc = new Scanner(System.in); 10 | String s = sc.nextLine(); 11 | sc.close(); 12 | done = true; 13 | } 14 | 15 | public boolean isDone() { 16 | return done; 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/thread/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 4 Thread Example 2 | 3 | This code revises the chapter 3 thread example to use 4 | the strategy pattern. That is, it implements ReadLine as a Runnable 5 | instead of as a subclass of Thread. 6 | 7 | One of the points of the example is that the two examples 8 | have very similar code even though they use very different 9 | implementation techniques. 10 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter04/thread/RunnableThreadTest.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter04.thread; 2 | 3 | public class RunnableThreadTest { 4 | public static void main(String[] args) { 5 | ReadLine r = new ReadLine(); 6 | Thread t = new Thread(r); 7 | t.start(); 8 | int i = 0; 9 | while(!r.isDone()) { 10 | System.out.println(i); 11 | i++; 12 | } 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/AccountFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank12; 2 | 3 | public interface AccountFactory { 4 | static BankAccount createSavings(int acctnum) { 5 | return new SavingsAccount(acctnum); 6 | } 7 | 8 | static BankAccount createRegularChecking(int acctnum) { 9 | return new RegularChecking(acctnum); 10 | } 11 | 12 | static BankAccount createInterestChecking(int acctnum) { 13 | return new InterestChecking(acctnum); 14 | } 15 | 16 | static BankAccount createAccount(int type, int acctnum) { 17 | BankAccount ba; 18 | if (type == 1) 19 | ba = createSavings(acctnum); 20 | else if (type == 2) 21 | ba = createRegularChecking(acctnum); 22 | else 23 | ba = createInterestChecking(acctnum); 24 | return ba; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank12; 2 | 3 | public interface BankAccount extends Comparable { 4 | int getAcctNum(); 5 | int getBalance(); 6 | void deposit(int amt); 7 | boolean isForeign(); 8 | void setForeign(boolean isforeign); 9 | boolean hasEnoughCollateral(int loanamt); 10 | String toString(); 11 | void addInterest(); 12 | int fee(); 13 | 14 | default boolean isEmpty() { 15 | return getBalance() == 0; 16 | } 17 | 18 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 19 | BankAccount ba = AccountFactory.createSavings(acctnum); 20 | ba.deposit(n); 21 | return ba; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank12; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Scanner scanner = new Scanner(System.in); 12 | BankClient client = new BankClient(scanner, bank); 13 | client.run(); 14 | info.saveMap(accounts, bank.nextAcctNum()); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank12; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected double collateralRatio() { 10 | return 2.0 / 3.0; 11 | } 12 | 13 | protected abstract String accountType(); 14 | protected abstract double interestRate(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/InputCommand.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank12; 2 | 3 | import java.util.Scanner; 4 | 5 | public interface InputCommand { 6 | int execute(Scanner sc, Bank bank, int current); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank12; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | public InterestChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | protected double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/OwnerStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank12; 2 | 3 | public interface OwnerStrategy { 4 | boolean isForeign(); 5 | int fee(); 6 | } 7 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/Owners.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank12; 2 | 3 | public enum Owners implements OwnerStrategy { 4 | DOMESTIC(false,0,"domestic"), FOREIGN(true,500,"foreign"); 5 | 6 | private boolean isforeign; 7 | private int fee; 8 | private String name; 9 | 10 | private Owners(boolean isforeign, int fee, String name) { 11 | this.isforeign = isforeign; 12 | this.fee = fee; 13 | this.name = name; 14 | } 15 | 16 | public boolean isForeign() { 17 | return isforeign; 18 | } 19 | 20 | public int fee() { 21 | return fee; 22 | } 23 | 24 | public String toString() { 25 | return name; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 12 2 | 3 | The version revises Version 10. It uses enums to simplify the 4 | handling of input commands and the OwnerStrategy hierarchy. 5 | 6 | The strategy classes Domestic and Foreign are replaced by the enum 7 | Owners. The AbstractBankAccount constructor and its method 8 | setForeign are modified to use these enums. 9 | 10 | The eight InputCommand strategy classes QuitCmd, NewCmd, etc. 11 | have been replaced by the single enum InputCommands. 12 | The global variable "commands" in BankClient is now initialized 13 | by calling InputCommands.values. 14 | 15 | The new interface AccountFactory contains a static factory method 16 | for each of the three types of bank account: 17 | createSavings, createRegularChecking, and createInterestChecking. 18 | It also has a static method createAccount, which uses the type 19 | number to determine which account to create. 20 | 21 | The method createSavingsWithDeposit in BankAccount has been 22 | rewritten to mention AccountFactory.createSavings. 23 | 24 | The methods newAccount in Bank and readAccount in SavedBankInfo 25 | are modified to call the createAccount method in AccountFactory. 26 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank12; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | 5 | public RegularChecking(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected String accountType() { 10 | return "Regular checking"; 11 | } 12 | 13 | protected double interestRate() { 14 | return 0.0; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank12/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank12; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | public SavingsAccount(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected double collateralRatio() { 9 | return 1.0 / 2.0; 10 | } 11 | 12 | protected String accountType() { 13 | return "Savings"; 14 | } 15 | 16 | protected double interestRate() { 17 | return 0.01; 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/AccountFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public interface AccountFactory { 4 | BankAccount create(int acctnum); 5 | 6 | static BankAccount createAccount(int type, int acctnum) { 7 | AccountFactory af; 8 | if (type == 1) 9 | af = new SavingsFactory(); 10 | else if (type == 2) 11 | af = new RegularCheckingFactory(); 12 | else 13 | af = new InterestCheckingFactory(); 14 | return af.create(acctnum); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public interface BankAccount extends Comparable { 4 | int getAcctNum(); 5 | int getBalance(); 6 | void deposit(int amt); 7 | boolean isForeign(); 8 | void setForeign(boolean isforeign); 9 | boolean hasEnoughCollateral(int loanamt); 10 | String toString(); 11 | void addInterest(); 12 | int fee(); 13 | 14 | default boolean isEmpty() { 15 | return getBalance() == 0; 16 | } 17 | 18 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 19 | AccountFactory af = new SavingsFactory(); 20 | BankAccount ba = af.create(acctnum); 21 | ba.deposit(n); 22 | return ba; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Scanner scanner = new Scanner(System.in); 12 | BankClient client = new BankClient(scanner, bank); 13 | client.run(); 14 | info.saveMap(accounts, bank.nextAcctNum()); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected double collateralRatio() { 10 | return 2.0 / 3.0; 11 | } 12 | 13 | protected abstract String accountType(); 14 | protected abstract double interestRate(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/InputCommand.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | import java.util.Scanner; 4 | 5 | public interface InputCommand { 6 | int execute(Scanner sc, Bank bank, int current); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | public InterestChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | protected double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/InterestCheckingFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public class InterestCheckingFactory implements AccountFactory { 4 | public BankAccount create(int acctnum) { 5 | return new InterestChecking(acctnum); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/OwnerStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public interface OwnerStrategy { 4 | boolean isForeign(); 5 | String location(); 6 | int fee(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/Owners.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public enum Owners implements OwnerStrategy { 4 | DOMESTIC(false,0,"domestic"), FOREIGN(true,500,"foreign"); 5 | 6 | private boolean isforeign; 7 | private int fee; 8 | private String location; 9 | 10 | private Owners(boolean isforeign, int fee, String location) { 11 | this.isforeign = isforeign; 12 | this.fee = fee; 13 | this.location = location; 14 | } 15 | 16 | public boolean isForeign() { 17 | return isforeign; 18 | } 19 | 20 | public int fee() { 21 | return fee; 22 | } 23 | 24 | public String location() { 25 | return location; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 13. 2 | 3 | AccountFactory has a new non-static create method. 4 | 5 | There are three factory classes that implement AccountFactory: 6 | SavingsFactory, RegularCheckingFactory, and InterestCheckingFactory. 7 | 8 | The static factory methods in AccountFactory are revised to use 9 | the factory objects. 10 | 11 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | 5 | public RegularChecking(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected String accountType() { 10 | return "Regular checking"; 11 | } 12 | 13 | protected double interestRate() { 14 | return 0.0; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/RegularCheckingFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public class RegularCheckingFactory implements AccountFactory { 4 | public BankAccount create(int acctnum) { 5 | return new RegularChecking(acctnum); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | public SavingsAccount(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected double collateralRatio() { 9 | return 1.0 / 2.0; 10 | } 11 | 12 | protected String accountType() { 13 | return "Savings"; 14 | } 15 | 16 | protected double interestRate() { 17 | return 0.01; 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank13/SavingsFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank13; 2 | 3 | public class SavingsFactory implements AccountFactory { 4 | public BankAccount create(int acctnum) { 5 | return new SavingsAccount(acctnum); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/AccountFactories.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | public enum AccountFactories implements AccountFactory { 4 | SAVINGS("Savings", 5 | acctnum -> new SavingsAccount(acctnum)), 6 | REGULAR_CHECKING("Regular checking", 7 | acctnum -> new RegularChecking(acctnum)), 8 | INTEREST_CHECKING("Interest checking", 9 | acctnum -> new InterestChecking(acctnum)), 10 | NEW_CUSTOMER("New Customer Savings", 11 | acctnum -> { BankAccount result = new SavingsAccount(acctnum); 12 | result.deposit(1000); // $10 for free! 13 | return result; }); 14 | 15 | private String name; 16 | private AccountFactory af; 17 | 18 | private AccountFactories(String name, AccountFactory af) { 19 | this.name = name; 20 | this.af = af; 21 | } 22 | 23 | public BankAccount create(int acctnum) { 24 | return af.create(acctnum); 25 | } 26 | 27 | public String toString() { 28 | return name; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/AccountFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | public interface AccountFactory { 4 | BankAccount create(int acctnum); 5 | 6 | static AccountFactory[] factories = AccountFactories.values(); 7 | 8 | static BankAccount createAccount(int type, int acctnum) { 9 | AccountFactory af = factories[type-1]; //account types begin at 1 10 | return af.create(acctnum); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | public interface BankAccount extends Comparable { 4 | int getAcctNum(); 5 | int getBalance(); 6 | void deposit(int amt); 7 | boolean isForeign(); 8 | void setForeign(boolean isforeign); 9 | boolean hasEnoughCollateral(int loanamt); 10 | String toString(); 11 | void addInterest(); 12 | int fee(); 13 | 14 | default boolean isEmpty() { 15 | return getBalance() == 0; 16 | } 17 | 18 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 19 | BankAccount ba = AccountFactories.SAVINGS.create(acctnum); 20 | ba.deposit(n); 21 | return ba; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Scanner scanner = new Scanner(System.in); 12 | BankClient client = new BankClient(scanner, bank); 13 | client.run(); 14 | info.saveMap(accounts, bank.nextAcctNum()); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected double collateralRatio() { 10 | return 2.0 / 3.0; 11 | } 12 | 13 | protected abstract String accountType(); 14 | protected abstract double interestRate(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/InputCommand.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | import java.util.Scanner; 4 | 5 | public interface InputCommand { 6 | int execute(Scanner sc, Bank bank, int current); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | public InterestChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | protected double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/OwnerStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | public interface OwnerStrategy { 4 | boolean isForeign(); 5 | String location(); 6 | int fee(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/Owners.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | public enum Owners implements OwnerStrategy { 4 | DOMESTIC(false,0,"domestic"), FOREIGN(true,500,"foreign"); 5 | 6 | private boolean isforeign; 7 | private int fee; 8 | private String location; 9 | 10 | private Owners(boolean isforeign, int fee, String location) { 11 | this.isforeign = isforeign; 12 | this.fee = fee; 13 | this.location = location; 14 | } 15 | 16 | public boolean isForeign() { 17 | return isforeign; 18 | } 19 | 20 | public int fee() { 21 | return fee; 22 | } 23 | 24 | public String location() { 25 | return location; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 14. 2 | 3 | The three AccountFactory subclasses have been replaced by 4 | the enum AccountFactories. 5 | 6 | The AccountFactory static methods have been revised to 7 | use AccountFactories. The createAccount method has been 8 | revised to index into the array AccountFactories.values 9 | instead of using an if statement. 10 | 11 | InputCommands is rewritten to also use AccountFactories.values 12 | to create its "new account type" message. The NEW command prints 13 | that message to the user. 14 | 15 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | 5 | public RegularChecking(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected String accountType() { 10 | return "Regular checking"; 11 | } 12 | 13 | protected double interestRate() { 14 | return 0.0; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/bank14/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.bank14; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | public SavingsAccount(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected double collateralRatio() { 9 | return 1.0 / 2.0; 10 | } 11 | 12 | protected String accountType() { 13 | return "Savings"; 14 | } 15 | 16 | protected double interestRate() { 17 | return 0.01; 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/thread/PriorityThreadFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter05.thread; 2 | import java.util.concurrent.ThreadFactory; 3 | 4 | public class PriorityThreadFactory implements ThreadFactory { 5 | 6 | private int priority; 7 | public PriorityThreadFactory(int p) { 8 | priority = p; 9 | } 10 | 11 | public Thread newThread(Runnable r) { 12 | Thread t = new Thread(r); 13 | t.setPriority(priority); 14 | return t; 15 | } 16 | 17 | public static void main(String[] args) { 18 | ThreadFactory important = new PriorityThreadFactory(9); 19 | ThreadFactory menial = new PriorityThreadFactory(1); 20 | Runnable r = ()->{}; 21 | 22 | Thread t1 = important.newThread(r); 23 | Thread t2 = menial.newThread(r); 24 | System.out.println(t1.getPriority()); 25 | System.out.println(t2.getPriority()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter05/thread/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 5 ThreadFactory Example 2 | 3 | The class PriorityThreadFactory demonstrates how to use the 4 | ThreadFactory interface. Each PriorityThreadFactory object 5 | has an associated priority. Each Thread object it creates 6 | will have that priority. 7 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/AccountFactories.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | public enum AccountFactories implements AccountFactory { 4 | SAVINGS("Savings", 5 | acctnum -> new SavingsAccount(acctnum)), 6 | REGULAR_CHECKING("Regular checking", 7 | acctnum -> new RegularChecking(acctnum)), 8 | INTEREST_CHECKING("Interest checking", 9 | acctnum -> new InterestChecking(acctnum)), 10 | NEW_CUSTOMER("New Customer Savings", 11 | acctnum -> { BankAccount result = new SavingsAccount(acctnum); 12 | result.deposit(1000); // $10 for free! 13 | return result; }); 14 | 15 | private String name; 16 | private AccountFactory af; 17 | 18 | private AccountFactories(String name, AccountFactory af) { 19 | this.name = name; 20 | this.af = af; 21 | } 22 | 23 | public BankAccount create(int acctnum) { 24 | return af.create(acctnum); 25 | } 26 | 27 | public String toString() { 28 | return name; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/AccountFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | public interface AccountFactory { 4 | BankAccount create(int acctnum); 5 | 6 | static AccountFactory[] factories = AccountFactories.values(); 7 | 8 | static BankAccount createAccount(int type, int acctnum) { 9 | AccountFactory af = factories[type-1]; //account types begin at 1 10 | return af.create(acctnum); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | public interface BankAccount extends Comparable { 4 | int getAcctNum(); 5 | int getBalance(); 6 | void deposit(int amt); 7 | boolean isForeign(); 8 | void setForeign(boolean isforeign); 9 | boolean hasEnoughCollateral(int loanamt); 10 | String toString(); 11 | void addInterest(); 12 | int fee(); 13 | 14 | default boolean isEmpty() { 15 | return getBalance() == 0; 16 | } 17 | 18 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 19 | BankAccount ba = AccountFactories.SAVINGS.create(acctnum); 20 | ba.deposit(n); 21 | return ba; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Scanner scanner = new Scanner(System.in); 12 | BankClient client = new BankClient(scanner, bank); 13 | client.run(); 14 | info.saveMap(accounts, bank.nextAcctNum()); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected double collateralRatio() { 10 | return 2.0 / 3.0; 11 | } 12 | 13 | protected abstract String accountType(); 14 | protected abstract double interestRate(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/InputCommand.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | import java.util.Scanner; 4 | 5 | public interface InputCommand { 6 | int execute(Scanner sc, Bank bank, int current); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | public InterestChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | protected double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/MaxBalanceVisitor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | public class MaxBalanceVisitor implements Visitor { 4 | private int max = 0; 5 | 6 | public void accept(BankAccount ba) { 7 | int bal = ba.getBalance(); 8 | if (bal > max) 9 | max = bal; 10 | } 11 | 12 | public Integer result() { 13 | return max; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/OwnerStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | public interface OwnerStrategy { 4 | boolean isForeign(); 5 | String location(); 6 | int fee(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/Owners.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | public enum Owners implements OwnerStrategy { 4 | DOMESTIC(false,0,"domestic"), FOREIGN(true,500,"foreign"); 5 | 6 | private boolean isforeign; 7 | private int fee; 8 | private String location; 9 | 10 | private Owners(boolean isforeign, int fee, String location) { 11 | this.isforeign = isforeign; 12 | this.fee = fee; 13 | this.location = location; 14 | } 15 | 16 | public boolean isForeign() { 17 | return isforeign; 18 | } 19 | 20 | public int fee() { 21 | return fee; 22 | } 23 | 24 | public String location() { 25 | return location; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 15. 2 | 3 | Bank has the methods iterator and stream. 4 | 5 | There are two new classes, IteratorAcctStats and 6 | StreamAcctStats, which are clients of Bank. 7 | The methods in these classes support two tasks: 8 | to print some selected accounts, and to find the maximum 9 | balance of some selected accounts. 10 | 11 | There is a new interface, Visitor, which supports the 12 | visitor pattern by adding the method "result" to the 13 | Consumer interface. 14 | MaxBalanceVistor is an example class that implements Visitor. 15 | 16 | The programs IteratorStatProgram and StreamStatProgram 17 | test these two classes. -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | 5 | public RegularChecking(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected String accountType() { 10 | return "Regular checking"; 11 | } 12 | 13 | protected double interestRate() { 14 | return 0.0; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | public SavingsAccount(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected double collateralRatio() { 9 | return 1.0 / 2.0; 10 | } 11 | 12 | protected String accountType() { 13 | return "Savings"; 14 | } 15 | 16 | protected double interestRate() { 17 | return 0.01; 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/StreamAccountStats.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | import java.util.function.*; 4 | import java.util.stream.Stream; 5 | 6 | public class StreamAccountStats { 7 | private Bank bank; 8 | 9 | public StreamAccountStats(Bank b) { 10 | bank = b; 11 | } 12 | 13 | public void printAccounts6(Predicate pred) { 14 | Stream s1 = bank.stream(); 15 | Stream s2 = s1.filter(pred); 16 | s2.forEach(ba->System.out.println(ba)); 17 | } 18 | 19 | public void printAccounts7(Predicate pred) { 20 | bank.stream() 21 | .filter(pred) 22 | .forEach(ba->System.out.println(ba)); 23 | } 24 | 25 | public int maxBalance6(Predicate pred) { 26 | return bank.stream() 27 | .filter(pred) 28 | .map(ba->ba.getBalance()) 29 | .reduce(0, (x,y)->Math.max(x,y)); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/StreamStatProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | import java.util.Map; 4 | import java.util.function.Predicate; 5 | 6 | public class StreamStatProgram { 7 | public static void main(String[] args) { 8 | SavedBankInfo info = new SavedBankInfo("bank.info"); 9 | Map accounts = info.getAccounts(); 10 | int nextacct = info.nextAcctNum(); 11 | Bank bank = new Bank(accounts, nextacct); 12 | StreamAccountStats stats = new StreamAccountStats(bank); 13 | Predicate pred = ba -> ba.fee() == 0; 14 | System.out.println("The max balance of the domestic accounts is " + stats.maxBalance6(pred)); 15 | 16 | System.out.println("Here are the domestic accounts."); 17 | stats.printAccounts6(pred); 18 | System.out.println("Here are the domestic accounts again."); 19 | stats.printAccounts7(pred); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/bank15/Visitor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.bank15; 2 | 3 | import java.util.function.Consumer; 4 | 5 | public interface Visitor extends Consumer { 6 | R result(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/iteration/NoDuplicates.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.iteration; 2 | 3 | import java.util.*; 4 | 5 | public class NoDuplicates { 6 | 7 | public static boolean noDuplicates(List L) { 8 | Iterator iter = L.iterator(); 9 | while (iter.hasNext()) { 10 | String s = iter.next(); 11 | if (!isUnique(L, s)) 12 | return false; 13 | } 14 | return true; 15 | } 16 | 17 | private static boolean isUnique(List L, String s) { 18 | int count = 0; 19 | Iterator iter = L.iterator(); 20 | while (iter.hasNext()) 21 | if (iter.next().equals(s)) 22 | count++; 23 | return count == 1; 24 | } 25 | 26 | public static void main(String[] args) { 27 | List L = Arrays.asList("a", "b", "c", "b", "e"); 28 | boolean b = noDuplicates(L); 29 | System.out.println(b); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/iteration/PrimeCollection.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.iteration; 2 | 3 | import java.util.*; 4 | 5 | public class PrimeCollection extends AbstractCollection { 6 | private int size; 7 | 8 | public PrimeCollection(int size) { 9 | this.size = size; 10 | } 11 | 12 | public int size() { 13 | return size; 14 | } 15 | 16 | public Iterator iterator() { 17 | return new PrimeIterator(size); 18 | } 19 | 20 | public static void main(String[] args) { 21 | Collection c = new PrimeCollection(10); 22 | // print an iterator for c 23 | for (int i : c) 24 | System.out.print(i + " "); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/iteration/PrimeIterator.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.iteration; 2 | import java.util.*; 3 | 4 | public class PrimeIterator implements Iterator { 5 | private int current = 1; 6 | private int total; 7 | private int count = 0; 8 | 9 | public PrimeIterator(int total) { 10 | this.total = total; 11 | } 12 | 13 | public boolean hasNext() { 14 | return count < total; 15 | } 16 | 17 | public Integer next() { 18 | current++; 19 | while (!isPrime(current)) 20 | current++; 21 | count++; 22 | return current; 23 | } 24 | 25 | private boolean isPrime(int n) { 26 | for (int i=2; i*i<=n; i++) 27 | if (n%i == 0) 28 | return false; 29 | return true; 30 | } 31 | 32 | public static void main(String[] args) { 33 | Iterator iter = new PrimeIterator(100); 34 | for (int i=0; i<100; i++) 35 | System.out.println(iter.next()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/iteration/RandomIterator.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter06.iteration; 2 | import java.util.*; 3 | 4 | public class RandomIterator implements Iterator { 5 | private Random rand = new Random(); 6 | 7 | public boolean hasNext() { 8 | return true; 9 | } 10 | 11 | public Integer next() { 12 | return rand.nextInt(); 13 | } 14 | 15 | // Generate random numbers until a duplicate occurs. 16 | public static void main(String[] args) { 17 | Iterator iter = new RandomIterator(); 18 | Set nums = new HashSet<>(); 19 | boolean dupNotFound = true; 20 | while (dupNotFound) 21 | dupNotFound = nums.add(iter.next()); 22 | System.out.println(nums.size()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter06/iteration/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 6 Iteration Examples 2 | 3 | NoDuplicates shows the need to separate an iterator from its iterable. 4 | Its isUnique method creates a new iterator each time it is called. 5 | 6 | RandomIterator and PrimeIterator illustrate some simple Iterator 7 | classes. RandomIterator's main method prints the number of 8 | integers it has to generate until a repeat occurs. PrimeIterator's 9 | main method prints the first 100 primes. 10 | 11 | PrimeCollection creates a Collection class by extending 12 | AbstractCollection. It uses the PrimeIterator class as its 13 | implementation of the iterator method. 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/AccountFactories.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | public enum AccountFactories implements AccountFactory { 4 | SAVINGS("Savings", 5 | acctnum -> new SavingsAccount(acctnum)), 6 | REGULAR_CHECKING("Regular checking", 7 | acctnum -> new RegularChecking(acctnum)), 8 | INTEREST_CHECKING("Interest checking", 9 | acctnum -> new InterestChecking(acctnum)), 10 | NEW_CUSTOMER("New Customer Savings", 11 | acctnum -> { BankAccount result = new SavingsAccount(acctnum); 12 | result.deposit(1000); // $10 for free! 13 | return result; }); 14 | 15 | private String name; 16 | private AccountFactory af; 17 | 18 | private AccountFactories(String name, AccountFactory af) { 19 | this.name = name; 20 | this.af = af; 21 | } 22 | 23 | public BankAccount create(int acctnum) { 24 | return af.create(acctnum); 25 | } 26 | 27 | public String toString() { 28 | return name; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/AccountFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | public interface AccountFactory { 4 | BankAccount create(int acctnum); 5 | 6 | static AccountFactory[] factories = AccountFactories.values(); 7 | 8 | static BankAccount createAccount(int type, int acctnum) { 9 | AccountFactory af = factories[type-1]; //account types begin at 1 10 | return af.create(acctnum); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | import java.io.Serializable; 4 | 5 | public interface BankAccount extends Comparable, Serializable { 6 | int getAcctNum(); 7 | int getBalance(); 8 | void deposit(int amt); 9 | boolean isForeign(); 10 | void setForeign(boolean isforeign); 11 | boolean hasEnoughCollateral(int loanamt); 12 | String toString(); 13 | void addInterest(); 14 | int fee(); 15 | 16 | default boolean isEmpty() { 17 | return getBalance() == 0; 18 | } 19 | 20 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 21 | BankAccount ba = AccountFactories.SAVINGS.create(acctnum); 22 | ba.deposit(n); 23 | return ba; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/BankAccountAdapter.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | public class BankAccountAdapter implements FBIAcctInfo { 4 | private BankAccount ba; 5 | 6 | public BankAccountAdapter(BankAccount ba) { 7 | this.ba = ba; 8 | } 9 | 10 | public int balance() { 11 | return ba.getBalance() / 100; 12 | } 13 | 14 | public boolean isForeign() { 15 | return ba.isForeign(); 16 | } 17 | 18 | public String acctType() { 19 | return "deposit"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank16.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Scanner scanner = new Scanner(System.in); 12 | BankClient client = new BankClient(scanner, bank); 13 | client.run(); 14 | info.saveMap(accounts, bank.nextAcctNum()); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected double collateralRatio() { 10 | return 2.0 / 3.0; 11 | } 12 | 13 | protected abstract String accountType(); 14 | protected abstract double interestRate(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/FBIAcctInfo.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | interface FBIAcctInfo { 4 | int balance(); 5 | boolean isForeign(); 6 | String acctType(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/FBIClient.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | import java.util.*; 4 | 5 | public class FBIClient { 6 | public static void main(String[] args) { 7 | Bank b = getBank(); 8 | 9 | // put account info into a single list 10 | List L = new ArrayList<>(); 11 | for (BankAccount ba : b) 12 | L.add(new BankAccountAdapter(ba)); 13 | for (Loan ln : b.loans()) 14 | L.add(new LoanAdapter(ln)); 15 | 16 | // then process the list 17 | int count = 0; 18 | for (FBIAcctInfo a : L) 19 | if (a.isForeign() && a.balance() > 1000.0) 20 | count++; 21 | System.out.println("The count is " + count); 22 | } 23 | 24 | private static Bank getBank() { 25 | SavedBankInfo info = new SavedBankInfo("bank16.info"); 26 | Map accounts = info.getAccounts(); 27 | int nextacct = info.nextAcctNum(); 28 | return new Bank(accounts, nextacct); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/InputCommand.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | import java.util.Scanner; 4 | 5 | public interface InputCommand { 6 | int execute(Scanner sc, Bank bank, int current); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | public InterestChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | protected double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/LoanAdapter.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | public class LoanAdapter implements FBIAcctInfo { 4 | private Loan ln; 5 | 6 | public LoanAdapter(Loan ln) { 7 | this.ln = ln; 8 | } 9 | 10 | public int balance() { 11 | return (int) (ln.remainingPrincipal() / 100); 12 | } 13 | 14 | public boolean isForeign() { 15 | return !ln.isDomestic(); 16 | } 17 | 18 | public String acctType() { 19 | return "loan"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/MaxBalanceVisitor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | public class MaxBalanceVisitor implements Visitor { 4 | private int max = 0; 5 | 6 | public void accept(BankAccount ba) { 7 | int bal = ba.getBalance(); 8 | if (bal > max) 9 | max = bal; 10 | } 11 | 12 | public Integer result() { 13 | return max; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/OwnerStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | import java.io.Serializable; 4 | 5 | public interface OwnerStrategy extends Serializable { 6 | boolean isForeign(); 7 | String location(); 8 | int fee(); 9 | } 10 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/Owners.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | public enum Owners implements OwnerStrategy { 4 | DOMESTIC(false,0,"domestic"), FOREIGN(true,500,"foreign"); 5 | 6 | private boolean isforeign; 7 | private int fee; 8 | private String location; 9 | 10 | private Owners(boolean isforeign, int fee, String location) { 11 | this.isforeign = isforeign; 12 | this.fee = fee; 13 | this.location = location; 14 | } 15 | 16 | public boolean isForeign() { 17 | return isforeign; 18 | } 19 | 20 | public int fee() { 21 | return fee; 22 | } 23 | 24 | public String location() { 25 | return location; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 16. 2 | 3 | This version contains a new implementation of SavedBankInfo that 4 | uses object streams. To make this possible, BankAccount and 5 | OwnerStrategy are modified so that they extend Serializable. 6 | 7 | This version contains new files to support the FBI scenario. 8 | There is a new interface FBIAcctInfo, and new 9 | classes Loan, BankAccountAdapter, LoanAdapter and FBIClient. 10 | The class Bank has a dummy method loans, which returns an empty list. 11 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | 5 | public RegularChecking(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected String accountType() { 10 | return "Regular checking"; 11 | } 12 | 13 | protected double interestRate() { 14 | return 0.0; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | public SavingsAccount(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected double collateralRatio() { 9 | return 1.0 / 2.0; 10 | } 11 | 12 | protected String accountType() { 13 | return "Savings"; 14 | } 15 | 16 | protected double interestRate() { 17 | return 0.01; 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/StreamAccountStats.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | import java.util.function.*; 4 | import java.util.stream.Stream; 5 | 6 | public class StreamAccountStats { 7 | private Bank bank; 8 | 9 | public StreamAccountStats(Bank b) { 10 | bank = b; 11 | } 12 | 13 | public void printAccounts6(Predicate pred) { 14 | Stream s = bank.stream(); 15 | s = s.filter(pred); 16 | s.forEach(ba->System.out.println(ba)); 17 | } 18 | 19 | public void printAccounts7(Predicate pred) { 20 | bank.stream() 21 | .filter(pred) 22 | .forEach(ba->System.out.println(ba)); 23 | } 24 | 25 | public int maxBalance6(Predicate pred) { 26 | return bank.stream() 27 | .filter(pred) 28 | .map(ba->ba.getBalance()) 29 | .reduce(0, (x,y)->Math.max(x,y)); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/StreamStatProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | import java.util.Map; 4 | import java.util.function.Predicate; 5 | 6 | public class StreamStatProgram { 7 | public static void main(String[] args) { 8 | SavedBankInfo info = new SavedBankInfo("bank.info"); 9 | Map accounts = info.getAccounts(); 10 | int nextacct = info.nextAcctNum(); 11 | Bank bank = new Bank(accounts, nextacct); 12 | StreamAccountStats stats = new StreamAccountStats(bank); 13 | Predicate pred = ba -> ba.fee() == 0; 14 | System.out.println("The max balance of the domestic accounts is " + stats.maxBalance6(pred)); 15 | 16 | System.out.println("Here are the domestic accounts."); 17 | stats.printAccounts6(pred); 18 | System.out.println("Here are the domestic accounts again."); 19 | stats.printAccounts7(pred); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/bank16/Visitor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.bank16; 2 | 3 | import java.util.function.Consumer; 4 | 5 | public interface Visitor extends Consumer { 6 | R result(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/iostream/FilePrefix.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.iostream; 2 | 3 | import java.io.*; 4 | 5 | public class FilePrefix { 6 | public static void main(String[] args) throws IOException { 7 | try (Reader r = new FileReader("mobydick.txt"); 8 | Writer w = new FileWriter("shortmoby.txt")) { 9 | for (int i=0; i<500; i++) { 10 | int x = r.read(); 11 | if (x < 0) 12 | break; 13 | char c = (char) x; 14 | w.write(c); 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/iostream/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 7 I/O Stream Examples 2 | 3 | The FilePrefix class is a simple demonstration of the Reader 4 | and Writer methods. 5 | 6 | The InputStreamReader, OutputStreamWriter, and StringReader classes 7 | are simple versions of those adapter classes in the Java library. 8 | 9 | ObjectStreamTest is a simple demonstration of the 10 | ObjectInputStream and ObjectOutputStream methods. 11 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter07/iostream/StringReader.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter07.iostream; 2 | 3 | import java.io.*; 4 | 5 | public class StringReader extends Reader { 6 | private String s; 7 | private int pos = 0; 8 | 9 | public StringReader(String s) throws IOException { 10 | this.s = s; 11 | } 12 | 13 | public int read(char[] cbuf, int off, int len) 14 | throws IOException { 15 | if (pos >= s.length()) 16 | return -1; 17 | int count=0; 18 | while (count new SavingsAccount(acctnum)), 6 | REGULAR_CHECKING("Regular checking", 7 | acctnum -> new RegularChecking(acctnum)), 8 | INTEREST_CHECKING("Interest checking", 9 | acctnum -> new InterestChecking(acctnum)), 10 | NEW_CUSTOMER("New Customer Savings", 11 | acctnum -> { BankAccount result = new SavingsAccount(acctnum); 12 | result.deposit(1000); // $10 for free! 13 | return result; }); 14 | 15 | private String name; 16 | private AccountFactory af; 17 | 18 | private AccountFactories(String name, AccountFactory af) { 19 | this.name = name; 20 | this.af = af; 21 | } 22 | 23 | public BankAccount create(int acctnum) { 24 | return af.create(acctnum); 25 | } 26 | 27 | public String toString() { 28 | return name; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/AccountFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public interface AccountFactory { 4 | BankAccount create(int acctnum); 5 | 6 | static AccountFactory[] factories = AccountFactories.values(); 7 | 8 | static BankAccount createAccount(int type, int acctnum) { 9 | AccountFactory af = factories[type-1]; //account types begin at 1 10 | return af.create(acctnum); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/AuthorizerWrapper.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public abstract class AuthorizerWrapper implements LoanAuthorizer { 4 | protected LoanAuthorizer auth; 5 | 6 | protected AuthorizerWrapper(LoanAuthorizer auth) { 7 | this.auth = auth; 8 | } 9 | 10 | public boolean authorizeLoan(int amt) { 11 | return auth.authorizeLoan(amt); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.io.Serializable; 4 | 5 | public interface BankAccount extends Comparable, Serializable { 6 | int getAcctNum(); 7 | int getBalance(); 8 | void deposit(int amt); 9 | boolean isForeign(); 10 | void setForeign(boolean isforeign); 11 | boolean hasEnoughCollateral(int loanamt); 12 | String toString(); 13 | void addInterest(); 14 | int fee(); 15 | 16 | default boolean isEmpty() { 17 | return getBalance() == 0; 18 | } 19 | 20 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 21 | BankAccount ba = AccountFactories.SAVINGS.create(acctnum); 22 | ba.deposit(n); 23 | return ba; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/BankAccountAdapter.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public class BankAccountAdapter implements FBIAcctInfo { 4 | private BankAccount ba; 5 | 6 | public BankAccountAdapter(BankAccount ba) { 7 | this.ba = ba; 8 | } 9 | 10 | public int balance() { 11 | return ba.getBalance() / 100; 12 | } 13 | 14 | public boolean isForeign() { 15 | return ba.isForeign(); 16 | } 17 | 18 | public String acctType() { 19 | return "deposit"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank17.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Scanner scanner = new Scanner(System.in); 12 | BankClient client = new BankClient(scanner, bank); 13 | client.run(); 14 | info.saveMap(accounts, bank.nextAcctNum()); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected double collateralRatio() { 10 | return 2.0 / 3.0; 11 | } 12 | 13 | protected abstract String accountType(); 14 | protected abstract double interestRate(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/CollateralAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public class CollateralAuthorizer implements LoanAuthorizer { 4 | private BankAccount ba; 5 | 6 | public CollateralAuthorizer(BankAccount ba) { 7 | this.ba = ba; 8 | } 9 | 10 | public boolean authorizeLoan(int amt) { 11 | return ba.hasEnoughCollateral(amt); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/CreditScoreAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.util.Random; 4 | 5 | public class CreditScoreAuthorizer extends AuthorizerWrapper { 6 | private int score; 7 | 8 | public CreditScoreAuthorizer(LoanAuthorizer auth) { 9 | super(auth); 10 | // For simplicity, mock up the credit score 11 | // associated with the owner of the bank account. 12 | Random rnd = new Random(); 13 | this.score = 300 + rnd.nextInt(500); 14 | } 15 | 16 | public boolean authorizeLoan(int amt) { 17 | if (score > 700 && amt < 1000000) 18 | return true; 19 | else if (score < 500) 20 | return false; 21 | else 22 | return auth.authorizeLoan(amt); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/FBIAcctInfo.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | interface FBIAcctInfo { 4 | int balance(); 5 | boolean isForeign(); 6 | String acctType(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/FBIClient.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.util.*; 4 | 5 | public class FBIClient { 6 | public static void main(String[] args) { 7 | Bank b = getBank(); 8 | 9 | // put account info into a single list 10 | List L = new ArrayList<>(); 11 | for (BankAccount ba : b) 12 | L.add(new BankAccountAdapter(ba)); 13 | for (Loan ln : b.loans()) 14 | L.add(new LoanAdapter(ln)); 15 | 16 | // then process the list 17 | int count = 0; 18 | for (FBIAcctInfo a : L) 19 | if (a.isForeign() && a.balance() > 1000.0) 20 | count++; 21 | System.out.println("The count is " + count); 22 | } 23 | 24 | private static Bank getBank() { 25 | SavedBankInfo info = new SavedBankInfo("bank17.info"); 26 | Map accounts = info.getAccounts(); 27 | int nextacct = info.nextAcctNum(); 28 | return new Bank(accounts, nextacct); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/GoodCustomerAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.util.Random; 4 | 5 | public class GoodCustomerAuthorizer extends AuthorizerWrapper { 6 | private boolean isgood; 7 | 8 | public GoodCustomerAuthorizer(LoanAuthorizer auth) { 9 | super(auth); 10 | // For simplicity, mock up the customer status 11 | // associated with the owner of the bank account. 12 | Random rnd = new Random(); 13 | isgood = rnd.nextBoolean(); 14 | } 15 | 16 | public boolean authorizeLoan(int amt) { 17 | if (isgood && amt < 200000) 18 | return true; 19 | else 20 | return auth.authorizeLoan(amt); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/InputCommand.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.util.Scanner; 4 | 5 | public interface InputCommand { 6 | int execute(Scanner sc, Bank bank, int current); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | public InterestChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | protected double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/LoanAdapter.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public class LoanAdapter implements FBIAcctInfo { 4 | private Loan ln; 5 | 6 | public LoanAdapter(Loan ln) { 7 | this.ln = ln; 8 | } 9 | 10 | public int balance() { 11 | return (int) (ln.remainingPrincipal() / 100); 12 | } 13 | 14 | public boolean isForeign() { 15 | return !ln.isDomestic(); 16 | } 17 | 18 | public String acctType() { 19 | return "loan"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/LoanAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public interface LoanAuthorizer { 4 | boolean authorizeLoan(int amt); 5 | 6 | static LoanAuthorizer getAuthorizer(BankAccount ba) { 7 | LoanAuthorizer auth = new CollateralAuthorizer(ba); 8 | auth = new CreditScoreAuthorizer(auth); 9 | return new GoodCustomerAuthorizer(auth); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/MaxBalanceVisitor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public class MaxBalanceVisitor implements Visitor { 4 | private int max = 0; 5 | 6 | public void accept(BankAccount ba) { 7 | int bal = ba.getBalance(); 8 | if (bal > max) 9 | max = bal; 10 | } 11 | 12 | public Integer result() { 13 | return max; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/OwnerStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.io.Serializable; 4 | 5 | public interface OwnerStrategy extends Serializable { 6 | boolean isForeign(); 7 | String location(); 8 | int fee(); 9 | } 10 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/Owners.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public enum Owners implements OwnerStrategy { 4 | DOMESTIC(false,0,"domestic"), FOREIGN(true,500,"foreign"); 5 | 6 | private boolean isforeign; 7 | private int fee; 8 | private String location; 9 | 10 | private Owners(boolean isforeign, int fee, String location) { 11 | this.isforeign = isforeign; 12 | this.fee = fee; 13 | this.location = location; 14 | } 15 | 16 | public boolean isForeign() { 17 | return isforeign; 18 | } 19 | 20 | public int fee() { 21 | return fee; 22 | } 23 | 24 | public String location() { 25 | return location; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 17. 2 | 3 | This version modifies version 16 to enable three forms of decoration. 4 | 5 | First, bank accounts are decorated. There is a new abstract class 6 | BankAccountWrapper and decorator subclasses UnmodifiableAccount 7 | and SuspiciousAccount. 8 | 9 | Second, there is a new class UnmodifiableBankIterator, 10 | which decorates Iterator. 11 | 12 | Third, there is a decorator hierarchy to do loan authorization. 13 | There is a new interface LoanAuthorizer, a wrapper class 14 | AuthorizerWrapper, a base class CollateralAuthorizer, and 15 | decorator classes GoodCustomerAuthorizer and CreditScoreAuthorizer. 16 | 17 | The class Bank is modified in several ways. Its method iterator 18 | is modified to return an unmodifiable iterator of unmodifiable 19 | bank accounts. It also has a new method makeSuspicious, 20 | which decorates a given account with a SuspiciousAccount wrapper. 21 | Finally, its authorizeLoan method is modified to use a 22 | LoanAuthorizer decorator. 23 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | 5 | public RegularChecking(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected String accountType() { 10 | return "Regular checking"; 11 | } 12 | 13 | protected double interestRate() { 14 | return 0.0; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | public SavingsAccount(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected double collateralRatio() { 9 | return 1.0 / 2.0; 10 | } 11 | 12 | protected String accountType() { 13 | return "Savings"; 14 | } 15 | 16 | protected double interestRate() { 17 | return 0.01; 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/StreamAccountStats.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.util.function.*; 4 | import java.util.stream.Stream; 5 | 6 | public class StreamAccountStats { 7 | private Bank bank; 8 | 9 | public StreamAccountStats(Bank b) { 10 | bank = b; 11 | } 12 | 13 | public void printAccounts6(Predicate pred) { 14 | Stream s = bank.stream(); 15 | s = s.filter(pred); 16 | s.forEach(ba->System.out.println(ba)); 17 | } 18 | 19 | public void printAccounts7(Predicate pred) { 20 | bank.stream() 21 | .filter(pred) 22 | .forEach(ba->System.out.println(ba)); 23 | } 24 | 25 | public int maxBalance6(Predicate pred) { 26 | return bank.stream() 27 | .filter(pred) 28 | .map(ba->ba.getBalance()) 29 | .reduce(0, (x,y)->Math.max(x,y)); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/StreamStatProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.util.Map; 4 | import java.util.function.Predicate; 5 | 6 | public class StreamStatProgram { 7 | public static void main(String[] args) { 8 | SavedBankInfo info = new SavedBankInfo("bank.info"); 9 | Map accounts = info.getAccounts(); 10 | int nextacct = info.nextAcctNum(); 11 | Bank bank = new Bank(accounts, nextacct); 12 | StreamAccountStats stats = new StreamAccountStats(bank); 13 | Predicate pred = ba -> ba.fee() == 0; 14 | System.out.println("The max balance of the domestic accounts is " + stats.maxBalance6(pred)); 15 | 16 | System.out.println("Here are the domestic accounts."); 17 | stats.printAccounts6(pred); 18 | System.out.println("Here are the domestic accounts again."); 19 | stats.printAccounts7(pred); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/SuspiciousAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.util.Date; 4 | 5 | public class SuspiciousAccount extends BankAccountWrapper { 6 | public SuspiciousAccount(BankAccount ba) { 7 | super(ba); 8 | } 9 | 10 | public void deposit(int amt) { 11 | Date d = new Date(); 12 | String msg = "On " + d + " account #" + ba.getAcctNum() + " deposited " + amt; 13 | System.out.println(msg); 14 | ba.deposit(amt); 15 | } 16 | 17 | public String toString() { 18 | return "## " + ba.toString(); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/UnmodifiableAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | public class UnmodifiableAccount extends BankAccountWrapper { 4 | 5 | public UnmodifiableAccount(BankAccount ba) { 6 | super(ba); 7 | } 8 | 9 | public void deposit(int amt) { 10 | throw new UnsupportedOperationException(); 11 | } 12 | 13 | public void addInterest() { 14 | throw new UnsupportedOperationException(); 15 | } 16 | 17 | public void setForeign(boolean isforeign) { 18 | throw new UnsupportedOperationException(); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/UnmodifiableBankIterator.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.util.Iterator; 4 | 5 | public class UnmodifiableBankIterator implements Iterator { 6 | private Iterator iter; 7 | 8 | public UnmodifiableBankIterator(Iterator iter) { 9 | this.iter = iter; 10 | } 11 | 12 | public boolean hasNext() { 13 | return iter.hasNext(); 14 | } 15 | 16 | public BankAccount next() { 17 | return new UnmodifiableAccount(iter.next()); 18 | } 19 | 20 | public void remove() { 21 | throw new UnsupportedOperationException(); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/bank17/Visitor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.bank17; 2 | 3 | import java.util.function.Consumer; 4 | 5 | public interface Visitor extends Consumer { 6 | R result(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/collectionstream/FilterIterator.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.collectionstream; 2 | 3 | import java.util.*; 4 | import java.util.function.Predicate; 5 | 6 | public class FilterIterator implements Iterator { 7 | private Iterator iter; 8 | private Predicate pred; 9 | private T nextvalue; 10 | private boolean found = false; 11 | 12 | public FilterIterator(Iterator iter, Predicate pred) { 13 | this.iter = iter; 14 | this.pred = pred; 15 | } 16 | 17 | public boolean hasNext() { 18 | while (!found && iter.hasNext()) { 19 | T t = iter.next(); 20 | if (pred.test(t)) { 21 | nextvalue = t; 22 | found = true; 23 | } 24 | } 25 | return found; 26 | } 27 | 28 | public T next() { 29 | hasNext(); // just to be safe 30 | if (!found) 31 | throw new NoSuchElementException(); 32 | found = false; 33 | return nextvalue; 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/collectionstream/IteratorTest.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.collectionstream; 2 | 3 | import java.util.*; 4 | 5 | public class IteratorTest { 6 | public static void main(String[] args) { 7 | Collection c = Arrays.asList("a", "bb", "ccc", "dddd"); 8 | 9 | Iterator i1, i2, i3, i4; 10 | i1 = c.iterator(); 11 | i2 = new FilterIterator(i1, s->s.length() > 1); 12 | i3 = new FilterIterator(i2, s->s.length() < 4); 13 | i4 = new MapIterator(i3, s->s.toUpperCase()); 14 | 15 | while (i4.hasNext()) { 16 | String s = i4.next(); 17 | System.out.println(s); 18 | } 19 | 20 | Iterator j1, j2, j3; 21 | Iterator j4; 22 | j1 = c.iterator(); 23 | j2 = new FilterIterator(j1, s->s.length() > 1); 24 | j3 = new FilterIterator(j2, s->s.length() < 4); 25 | j4 = new MapIterator(j3, s->s.length()); 26 | int max = -1; 27 | while (j4.hasNext()) { 28 | Integer n = j4.next(); 29 | if (n > max) 30 | max = n; 31 | } 32 | System.out.println("The max length is " + max); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/collectionstream/MapIterator.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.collectionstream; 2 | 3 | import java.util.Iterator; 4 | import java.util.function.Function; 5 | 6 | public class MapIterator implements Iterator { 7 | private Iterator iter; 8 | private Function f; 9 | 10 | public MapIterator(Iterator iter, Function f) { 11 | this.iter = iter; 12 | this.f = f; 13 | } 14 | 15 | public boolean hasNext() { 16 | return iter.hasNext(); 17 | } 18 | 19 | public R next() { 20 | T t = iter.next(); 21 | return f.apply(t); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/collectionstream/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 8 Collection Stream Examples 2 | 3 | The classes FilterIterator and MapIterator are decorated 4 | iterators. The test class is IteratorTest. 5 | 6 | The class SimpleStream uses the decorated iterators to implement 7 | a simplified Stream class. The test class is SimpleStreamTest, 8 | which is analogous to IteratorTest. 9 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/collectionstream/SimpleStream.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.collectionstream; 2 | 3 | import java.util.function.*; 4 | import java.util.*; 5 | 6 | public class SimpleStream { 7 | Iterator iter; 8 | 9 | public SimpleStream(Iterator iter) { 10 | this.iter = iter; 11 | } 12 | 13 | public Iterator iterator() { 14 | return iter; 15 | } 16 | 17 | public SimpleStream filter(Predicate pred) { 18 | Iterator newiter = new FilterIterator(iter, pred); 19 | return new SimpleStream(newiter); 20 | } 21 | 22 | public SimpleStream map(Function f) { 23 | Iterator newiter = new MapIterator(iter, f); 24 | return new SimpleStream(newiter); 25 | } 26 | 27 | public void forEach(Consumer cons) { 28 | while (iter.hasNext()) { 29 | T t = iter.next(); 30 | cons.accept(t); 31 | } 32 | } 33 | 34 | public T reduce(T identity, BinaryOperator f) { 35 | T result = identity; 36 | while (iter.hasNext()) { 37 | T t = iter.next(); 38 | result = f.apply(result, t); 39 | } 40 | return result; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/collectionstream/SimpleStreamTest.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.collectionstream; 2 | 3 | import java.util.*; 4 | 5 | public class SimpleStreamTest { 6 | public static void main(String[] args) { 7 | Collection c = Arrays.asList("a", "bb", "ccc", "dddd"); 8 | 9 | // Select the strings having length between 2 and 3, 10 | // convert them to uppercase, and print them. 11 | 12 | new SimpleStream(c.iterator()) 13 | .filter(s->s.length() > 1) 14 | .filter(s->s.length() < 4) 15 | .map(s->s.toUpperCase()) 16 | .forEach(s->System.out.println(s)); 17 | 18 | // Select the strings having length between 2 and 3, 19 | // convert them to their length, find the max, and print it. 20 | 21 | Integer max = 22 | new SimpleStream(c.iterator()) 23 | .filter(s->s.length() > 1) 24 | .filter(s->s.length() < 4) 25 | .map(s->s.length()) 26 | .reduce(0, (i1,i2)->Math.max(i1, i2)); 27 | System.out.println("The max length is " + max); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/iostream/DecoratedEncryptDecrypt.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.iostream; 2 | 3 | import java.io.*; 4 | import javax.crypto.*; 5 | 6 | public class DecoratedEncryptDecrypt { 7 | public static void main(String[] args) throws Exception { 8 | KeyGenerator kg = KeyGenerator.getInstance("DES"); 9 | kg.init(56); // DES uses 56-bit keys 10 | SecretKey key = kg.generateKey(); 11 | 12 | encrypt("mobydick.txt", "encrypted.txt", key, Cipher.ENCRYPT_MODE); 13 | encrypt("encrypted.txt", "decrypted.txt", key, Cipher.DECRYPT_MODE); 14 | } 15 | 16 | private static void encrypt(String source, String output, SecretKey key, int mode) throws Exception { 17 | Cipher c = Cipher.getInstance("DES"); 18 | c.init(mode, key); 19 | try (InputStream is = new FileInputStream(source); 20 | InputStream cis = new CipherInputStream(is, c); 21 | OutputStream os = new FileOutputStream(output)) { 22 | 23 | int x = cis.read(); 24 | while (x >= 0) { 25 | byte b = (byte) x; 26 | os.write(b); 27 | x = cis.read(); 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/iostream/DoubledChars.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.iostream; 2 | 3 | import java.io.*; 4 | 5 | public class DoubledChars { 6 | public static final int N = 1; 7 | 8 | public static void main(String[] args) throws IOException { 9 | try (Reader r = new FileReader("mobydick.txt"); 10 | Reader br = new BufferedReader(r)) { 11 | int x = br.read(); // For each char, 12 | while (x >= 0) { 13 | char c = (char) x; 14 | check(br, c); // check the N+1st char after it. 15 | x = br.read(); 16 | } 17 | } 18 | } 19 | 20 | private static void check(Reader r, char c) throws IOException { 21 | char[] a = new char[N+1]; 22 | r.mark(N+1); 23 | int howmany = r.read(a); 24 | if (howmany == N+1 && a[N] == c) { 25 | System.out.print(c); System.out.println(a); 26 | } 27 | r.reset(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/iostream/ProgressMonitorFileRead.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter08.iostream; 2 | 3 | import java.io.*; 4 | import javax.swing.ProgressMonitorInputStream; 5 | 6 | public class ProgressMonitorFileRead { 7 | public static void main(String[] args) throws IOException { 8 | String src = "mobydick.txt"; 9 | try (InputStream is = new FileInputStream("mobydick.txt"); 10 | InputStream pmis = new ProgressMonitorInputStream(null, "reading " + src, is)) { 11 | int x = pmis.read(); 12 | while (x >= 0) { 13 | byte b = (byte) x; 14 | // process b ... 15 | x = pmis.read(); 16 | } 17 | } 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter08/iostream/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 8 I/O Stream Examples 2 | 3 | InputStreamEfficiency demonstrates the value of the 4 | BufferedInputStream decorator class. 5 | 6 | ProgressMonitorFileRead shows how to use ProgressMonitorInputStream. 7 | 8 | DecoratedEncryptDecrypt shows how to use CipherInputStream. 9 | 10 | DoubleChars shows how to use the mark and reset methods. 11 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter09/cookbook/BasicFood.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter09.cookbook; 2 | 3 | import java.util.*; 4 | 5 | public class BasicFood implements FoodItem { 6 | private String name; 7 | private boolean isvegan; 8 | 9 | public BasicFood(String name, boolean isvegan) { 10 | this.name = name; 11 | this.isvegan = isvegan; 12 | } 13 | 14 | public String name() { 15 | return name; 16 | } 17 | 18 | public boolean isVegan() { 19 | return isvegan; 20 | } 21 | 22 | public Iterator childIterator() { 23 | return Collections.emptyIterator(); 24 | } 25 | 26 | public String toString() { 27 | String veg = isvegan ? " (vegan)" : ""; 28 | return name + veg; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter09/cookbook/FoodItem.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter09.cookbook; 2 | 3 | import java.util.*; 4 | 5 | public interface FoodItem extends Iterable { 6 | String name(); 7 | boolean isVegan(); 8 | Iterator childIterator(); 9 | 10 | default Iterator iterator() { 11 | return new FoodIterator(this); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter09/cookbook/FoodIterator.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter09.cookbook; 2 | 3 | import java.util.*; 4 | 5 | public class FoodIterator implements Iterator { 6 | private Stack> s = new Stack<>(); 7 | 8 | public FoodIterator(FoodItem f) { 9 | Collection c = Collections.singleton(f); 10 | s.push(c.iterator()); 11 | } 12 | 13 | public boolean hasNext() { 14 | return !s.isEmpty(); 15 | } 16 | 17 | public FoodItem next() { 18 | FoodItem food = s.peek().next(); 19 | if (!s.peek().hasNext()) 20 | s.pop(); 21 | Iterator iter = food.childIterator(); 22 | if (iter.hasNext()) 23 | s.push(iter); 24 | return food; 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter09/cookbook/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 9 Cookbook Example 2 | 3 | The classes here use the composite pattern to implement a 4 | sample online cookbook application. 5 | The class Cookbook contains the main method. 6 | 7 | FoodItem is the composite interface. Recipe is the recursive 8 | class and BasicFood is the base class. 9 | 10 | FoodItem implements Iterable. It has a default iterator method 11 | that creates a FoodIterator object. FoodIterator uses a 12 | stack of child iterators to implement a non-recursive 13 | traversal of the stack. 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter09/fx/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 9 JavaFX Examples 2 | 3 | The AccountCreationWindow class illustrates a typical 4 | JavaFX program, focusing on the creation of a node hierarchy. 5 | 6 | PrintNodeInformation demonstrates the methods getId, setId, 7 | and getLayoutBounds of classNode. 8 | 9 | NodeTraversal shows how you can use the getChildren method of 10 | Pane to traverse a node hierarchy. 11 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter09/predicate/AndPredicate.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter09.predicate; 2 | 3 | import java.util.function.Predicate; 4 | 5 | public class AndPredicate extends CompositePredicate { 6 | public AndPredicate(Predicate p1, Predicate p2) { 7 | super(p1, p2); 8 | } 9 | 10 | public boolean test(T t) { 11 | return p1.test(t) && p2.test(t); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter09/predicate/CompositePredicate.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter09.predicate; 2 | 3 | import java.util.function.Predicate; 4 | 5 | public abstract class CompositePredicate implements Predicate { 6 | protected Predicate p1, p2; 7 | 8 | protected CompositePredicate(Predicate p1, Predicate p2) { 9 | this.p1 = p1; 10 | this.p2 = p2; 11 | } 12 | 13 | public abstract boolean test(T t); 14 | } 15 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter09/predicate/CompositePredicateTest.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter09.predicate; 2 | 3 | import java.util.function.Predicate; 4 | 5 | // Three ways to create a composite predicate 6 | public class CompositePredicateTest { 7 | public static void main(String[] args) { 8 | Predicate p1 = n -> n < 20; 9 | Predicate p2 = n -> n%2 == 0; 10 | Predicate p3 = n -> n%3 == 0; 11 | 12 | // First: use AndPredicate and OrPredicate objects 13 | Predicate p4 = new OrPredicate(p2, p3); 14 | Predicate p5 = new AndPredicate(p1, p4); 15 | printUsing(p5); 16 | 17 | // Second: use the 'or' and 'and' methods individually 18 | Predicate p6 = p2.or(p3); 19 | Predicate p7 = p1.and(p6); 20 | printUsing(p7); 21 | 22 | // Third: compose the 'or' and 'and' methods 23 | Predicate p8 = p1.and(p2.or(p3)); 24 | printUsing(p8); 25 | } 26 | 27 | private static void printUsing(Predicate p) { 28 | for (int i=1; i<100; i++) 29 | if (p.test(i)) 30 | System.out.print(i + " "); 31 | System.out.println(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter09/predicate/OrPredicate.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter09.predicate; 2 | 3 | import java.util.function.Predicate; 4 | 5 | public class OrPredicate extends CompositePredicate { 6 | public OrPredicate(Predicate p1, Predicate p2) { 7 | super(p1, p2); 8 | } 9 | 10 | public boolean test(T t) { 11 | return p1.test(t) || p2.test(t); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter09/predicate/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 9 Predicate Examples 2 | 3 | These classes implement compound predicates using the composite 4 | pattern. 5 | 6 | The class CompositePredicate is the wrapper class. 7 | 8 | AndPredicate and OrPredicate are the recursive classes. 9 | 10 | CompositePredicateTest tests these classes. It also shows how 11 | to use the "and" and "or" methods of Predicate to do the same thing. 12 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/AccountFactories.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public enum AccountFactories implements AccountFactory { 4 | SAVINGS("Savings", 5 | acctnum -> new SavingsAccount(acctnum)), 6 | REGULAR_CHECKING("Regular checking", 7 | acctnum -> new RegularChecking(acctnum)), 8 | INTEREST_CHECKING("Interest checking", 9 | acctnum -> new InterestChecking(acctnum)), 10 | NEW_CUSTOMER("New Customer Savings", 11 | acctnum -> { BankAccount result = new SavingsAccount(acctnum); 12 | result.deposit(1000); // $10 for free! 13 | return result; }); 14 | 15 | private String name; 16 | private AccountFactory af; 17 | 18 | private AccountFactories(String name, AccountFactory af) { 19 | this.name = name; 20 | this.af = af; 21 | } 22 | 23 | public BankAccount create(int acctnum) { 24 | return af.create(acctnum); 25 | } 26 | 27 | public String toString() { 28 | return name; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/AccountFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public interface AccountFactory { 4 | BankAccount create(int acctnum); 5 | 6 | static AccountFactory[] factories = AccountFactories.values(); 7 | 8 | static BankAccount createAccount(int type, int acctnum) { 9 | AccountFactory af = factories[type-1]; //account types begin at 1 10 | return af.create(acctnum); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/Auditor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public class Auditor implements BankObserver { 4 | public Auditor(Bank bank) { 5 | bank.addObserver(BankEvent.NEW, this); 6 | bank.addObserver(BankEvent.SETFOREIGN, this); 7 | } 8 | 9 | public void update(BankEvent e, BankAccount ba, int depositamt) { 10 | if (ba.isForeign()) { 11 | if (e == BankEvent.NEW) 12 | System.out.println("New foreign account: " 13 | + ba.getAcctNum()); 14 | else 15 | System.out.println("Modified foreign account: " 16 | + ba.getAcctNum()); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/AuthorizerWrapper.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public abstract class AuthorizerWrapper implements LoanAuthorizer { 4 | protected LoanAuthorizer auth; 5 | 6 | protected AuthorizerWrapper(LoanAuthorizer auth) { 7 | this.auth = auth; 8 | } 9 | 10 | public boolean authorizeLoan(int amt) { 11 | return auth.authorizeLoan(amt); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.io.Serializable; 4 | 5 | public interface BankAccount extends Comparable, Serializable { 6 | int getAcctNum(); 7 | int getBalance(); 8 | void deposit(int amt); 9 | boolean isForeign(); 10 | void setForeign(boolean isforeign); 11 | boolean hasEnoughCollateral(int loanamt); 12 | String toString(); 13 | void addInterest(); 14 | int fee(); 15 | 16 | default boolean isEmpty() { 17 | return getBalance() == 0; 18 | } 19 | 20 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 21 | BankAccount ba = AccountFactories.SAVINGS.create(acctnum); 22 | ba.deposit(n); 23 | return ba; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/BankAccountAdapter.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public class BankAccountAdapter implements FBIAcctInfo { 4 | private BankAccount ba; 5 | 6 | public BankAccountAdapter(BankAccount ba) { 7 | this.ba = ba; 8 | } 9 | 10 | public int balance() { 11 | return ba.getBalance() / 100; 12 | } 13 | 14 | public boolean isForeign() { 15 | return ba.isForeign(); 16 | } 17 | 18 | public String acctType() { 19 | return "deposit"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/BankEvent.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public enum BankEvent { 4 | NEW, DEPOSIT, SETFOREIGN, INTEREST; 5 | } 6 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/BankObserver.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public interface BankObserver { 4 | void update(BankEvent event, BankAccount ba, int depositamt); 5 | } 6 | 7 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/BankProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.util.*; 4 | 5 | public class BankProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank18.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Auditor aud = new Auditor(bank); 12 | bank.addObserver(BankEvent.DEPOSIT, 13 | (event,ba,amt) -> { 14 | if (amt > 10000000) 15 | bank.makeSuspicious(ba.getAcctNum()); 16 | }); 17 | Scanner scanner = new Scanner(System.in); 18 | BankClient client = new BankClient(scanner, bank); 19 | client.run(); 20 | info.saveMap(accounts, bank.nextAcctNum()); 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected double collateralRatio() { 10 | return 2.0 / 3.0; 11 | } 12 | 13 | protected abstract String accountType(); 14 | protected abstract double interestRate(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/CollateralAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public class CollateralAuthorizer implements LoanAuthorizer { 4 | private BankAccount ba; 5 | 6 | public CollateralAuthorizer(BankAccount ba) { 7 | this.ba = ba; 8 | } 9 | 10 | public boolean authorizeLoan(int amt) { 11 | return ba.hasEnoughCollateral(amt); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/CreditScoreAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.util.Random; 4 | 5 | public class CreditScoreAuthorizer extends AuthorizerWrapper { 6 | private int score; 7 | 8 | public CreditScoreAuthorizer(LoanAuthorizer auth) { 9 | super(auth); 10 | // For simplicity, mock up the credit score 11 | // associated with the owner of the bank account. 12 | Random rnd = new Random(); 13 | this.score = 300 + rnd.nextInt(500); 14 | } 15 | 16 | public boolean authorizeLoan(int amt) { 17 | if (score > 700 && amt < 1000000) 18 | return true; 19 | else if (score < 500) 20 | return false; 21 | else 22 | return auth.authorizeLoan(amt); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/FBIAcctInfo.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | interface FBIAcctInfo { 4 | int balance(); 5 | boolean isForeign(); 6 | String acctType(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/FBIClient.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.util.*; 4 | 5 | public class FBIClient { 6 | public static void main(String[] args) { 7 | Bank b = getBank(); 8 | 9 | // put account info into a single list 10 | List L = new ArrayList<>(); 11 | for (BankAccount ba : b) 12 | L.add(new BankAccountAdapter(ba)); 13 | for (Loan ln : b.loans()) 14 | L.add(new LoanAdapter(ln)); 15 | 16 | // then process the list 17 | int count = 0; 18 | for (FBIAcctInfo a : L) 19 | if (a.isForeign() && a.balance() > 1000.0) 20 | count++; 21 | System.out.println("The count is " + count); 22 | } 23 | 24 | private static Bank getBank() { 25 | SavedBankInfo info = new SavedBankInfo("bank18.info"); 26 | Map accounts = info.getAccounts(); 27 | int nextacct = info.nextAcctNum(); 28 | return new Bank(accounts, nextacct); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/GoodCustomerAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.util.Random; 4 | 5 | public class GoodCustomerAuthorizer extends AuthorizerWrapper { 6 | private boolean isgood; 7 | 8 | public GoodCustomerAuthorizer(LoanAuthorizer auth) { 9 | super(auth); 10 | // For simplicity, mock up the customer status 11 | // associated with the owner of the bank account. 12 | Random rnd = new Random(); 13 | isgood = rnd.nextBoolean(); 14 | } 15 | 16 | public boolean authorizeLoan(int amt) { 17 | if (isgood && amt < 200000) 18 | return true; 19 | else 20 | return auth.authorizeLoan(amt); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/InputCommand.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.util.Scanner; 4 | 5 | public interface InputCommand { 6 | int execute(Scanner sc, Bank bank, int current); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | public InterestChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | protected double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/LoanAdapter.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public class LoanAdapter implements FBIAcctInfo { 4 | private Loan ln; 5 | 6 | public LoanAdapter(Loan ln) { 7 | this.ln = ln; 8 | } 9 | 10 | public int balance() { 11 | return (int) (ln.remainingPrincipal() / 100); 12 | } 13 | 14 | public boolean isForeign() { 15 | return !ln.isDomestic(); 16 | } 17 | 18 | public String acctType() { 19 | return "loan"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/LoanAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public interface LoanAuthorizer { 4 | boolean authorizeLoan(int amt); 5 | 6 | static LoanAuthorizer getAuthorizer(BankAccount ba) { 7 | LoanAuthorizer auth = new CollateralAuthorizer(ba); 8 | auth = new CreditScoreAuthorizer(auth); 9 | return new GoodCustomerAuthorizer(auth); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/MaxBalanceVisitor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public class MaxBalanceVisitor implements Visitor { 4 | private int max = 0; 5 | 6 | public void accept(BankAccount ba) { 7 | int bal = ba.getBalance(); 8 | if (bal > max) 9 | max = bal; 10 | } 11 | 12 | public Integer result() { 13 | return max; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/OwnerStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.io.Serializable; 4 | 5 | public interface OwnerStrategy extends Serializable { 6 | boolean isForeign(); 7 | String location(); 8 | int fee(); 9 | } 10 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/Owners.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public enum Owners implements OwnerStrategy { 4 | DOMESTIC(false,0,"domestic"), FOREIGN(true,500,"foreign"); 5 | 6 | private boolean isforeign; 7 | private int fee; 8 | private String location; 9 | 10 | private Owners(boolean isforeign, int fee, String location) { 11 | this.isforeign = isforeign; 12 | this.fee = fee; 13 | this.location = location; 14 | } 15 | 16 | public boolean isForeign() { 17 | return isforeign; 18 | } 19 | 20 | public int fee() { 21 | return fee; 22 | } 23 | 24 | public String location() { 25 | return location; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 18. 2 | 3 | The demo is modified to allow Bank to be an observable. 4 | The observer interface is BankObserver. 5 | 6 | The bank supports four different event types, which are 7 | defined in the enum BankEvent. 8 | 9 | The Bank class is modified to implement a map of BankObserver 10 | observer lists, one for each event type. 11 | Bank has the methods addObserver and notifyObservers. The Bank 12 | methods that update accounts are modified to call notifyObservers. 13 | 14 | The Bank class gets a new method isForeign, which could be needed by 15 | observers that use pull. That method should probably have 16 | existed since version 3, but had been omitted in the interest 17 | of simplicity. 18 | 19 | There is a new class Auditor that implements BankObserver. 20 | 21 | BankProgram is modified to add two observers to the bank. 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | 5 | public RegularChecking(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected String accountType() { 10 | return "Regular checking"; 11 | } 12 | 13 | protected double interestRate() { 14 | return 0.0; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | public SavingsAccount(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected double collateralRatio() { 9 | return 1.0 / 2.0; 10 | } 11 | 12 | protected String accountType() { 13 | return "Savings"; 14 | } 15 | 16 | protected double interestRate() { 17 | return 0.01; 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/StreamAccountStats.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.util.function.*; 4 | import java.util.stream.Stream; 5 | 6 | public class StreamAccountStats { 7 | private Bank bank; 8 | 9 | public StreamAccountStats(Bank b) { 10 | bank = b; 11 | } 12 | 13 | public void printAccounts6(Predicate pred) { 14 | Stream s = bank.stream(); 15 | s = s.filter(pred); 16 | s.forEach(ba->System.out.println(ba)); 17 | } 18 | 19 | public void printAccounts7(Predicate pred) { 20 | bank.stream() 21 | .filter(pred) 22 | .forEach(ba->System.out.println(ba)); 23 | } 24 | 25 | public int maxBalance6(Predicate pred) { 26 | return bank.stream() 27 | .filter(pred) 28 | .map(ba->ba.getBalance()) 29 | .reduce(0, (x,y)->Math.max(x,y)); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/StreamStatProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.util.Map; 4 | import java.util.function.Predicate; 5 | 6 | public class StreamStatProgram { 7 | public static void main(String[] args) { 8 | SavedBankInfo info = new SavedBankInfo("bank.info"); 9 | Map accounts = info.getAccounts(); 10 | int nextacct = info.nextAcctNum(); 11 | Bank bank = new Bank(accounts, nextacct); 12 | StreamAccountStats stats = new StreamAccountStats(bank); 13 | Predicate pred = ba -> ba.fee() == 0; 14 | System.out.println("The max balance of the domestic accounts is " + stats.maxBalance6(pred)); 15 | 16 | System.out.println("Here are the domestic accounts."); 17 | stats.printAccounts6(pred); 18 | System.out.println("Here are the domestic accounts again."); 19 | stats.printAccounts7(pred); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/SuspiciousAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.util.Date; 4 | 5 | public class SuspiciousAccount extends BankAccountWrapper { 6 | public SuspiciousAccount(BankAccount ba) { 7 | super(ba); 8 | } 9 | 10 | public void deposit(int amt) { 11 | Date d = new Date(); 12 | String msg = "On " + d + " account #" + ba.getAcctNum() + " deposited " + amt; 13 | System.out.println(msg); 14 | ba.deposit(amt); 15 | } 16 | 17 | public String toString() { 18 | return "## " + ba.toString(); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/UnmodifiableAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | public class UnmodifiableAccount extends BankAccountWrapper { 4 | 5 | public UnmodifiableAccount(BankAccount ba) { 6 | super(ba); 7 | } 8 | 9 | public void deposit(int amt) { 10 | throw new UnsupportedOperationException(); 11 | } 12 | 13 | public void addInterest() { 14 | throw new UnsupportedOperationException(); 15 | } 16 | 17 | public void setForeign(boolean isforeign) { 18 | throw new UnsupportedOperationException(); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/UnmodifiableBankIterator.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.util.Iterator; 4 | 5 | public class UnmodifiableBankIterator implements Iterator { 6 | private Iterator iter; 7 | 8 | public UnmodifiableBankIterator(Iterator iter) { 9 | this.iter = iter; 10 | } 11 | 12 | public boolean hasNext() { 13 | return iter.hasNext(); 14 | } 15 | 16 | public BankAccount next() { 17 | return new UnmodifiableAccount(iter.next()); 18 | } 19 | 20 | public void remove() { 21 | throw new UnsupportedOperationException(); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/bank18/Visitor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter10.bank18; 2 | 3 | import java.util.function.Consumer; 4 | 5 | public interface Visitor extends Consumer { 6 | R result(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter10/fx/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Chapter 10 JavaFX Example 2 | 3 | The AccountCreationWindow class from chapter 9 is modified to 4 | observe change events (via change listeners) 5 | and input events (via event handlers). 6 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/AccountFactories.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public enum AccountFactories implements AccountFactory { 4 | SAVINGS("Savings", 5 | acctnum -> new SavingsAccount(acctnum)), 6 | REGULAR_CHECKING("Regular checking", 7 | acctnum -> new RegularChecking(acctnum)), 8 | INTEREST_CHECKING("Interest checking", 9 | acctnum -> new InterestChecking(acctnum)), 10 | NEW_CUSTOMER("New Customer Savings", 11 | acctnum -> { BankAccount result = new SavingsAccount(acctnum); 12 | result.deposit(1000); // $10 for free! 13 | return result; }); 14 | 15 | private String name; 16 | private AccountFactory af; 17 | 18 | private AccountFactories(String name, AccountFactory af) { 19 | this.name = name; 20 | this.af = af; 21 | } 22 | 23 | public BankAccount create(int acctnum) { 24 | return af.create(acctnum); 25 | } 26 | 27 | public String toString() { 28 | return name; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/AccountFactory.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public interface AccountFactory { 4 | BankAccount create(int acctnum); 5 | 6 | static AccountFactory[] factories = AccountFactories.values(); 7 | 8 | static BankAccount createAccount(int type, int acctnum) { 9 | AccountFactory af = factories[type-1]; //account types begin at 1 10 | return af.create(acctnum); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/AllController.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class AllController implements BankObserver { 4 | private Bank bank; 5 | private AllView view; 6 | 7 | public AllController(Bank bank) { 8 | this.bank = bank; 9 | bank.addObserver(BankEvent.NEW, this); 10 | bank.addObserver(BankEvent.DEPOSIT, this); 11 | bank.addObserver(BankEvent.SETFOREIGN, this); 12 | bank.addObserver(BankEvent.INTEREST, this); 13 | } 14 | 15 | // methods called by the view 16 | public void setView(AllView view) { 17 | this.view = view; 18 | refreshAccounts(); 19 | } 20 | public void interestButton() { 21 | bank.addInterest(); 22 | } 23 | 24 | // method called by the model 25 | public void update(BankEvent e, BankAccount ba, int amt) { 26 | refreshAccounts(); 27 | } 28 | 29 | private void refreshAccounts() { 30 | StringBuffer result = new StringBuffer(); 31 | for (BankAccount ba : bank) 32 | result.append(ba + "\n"); 33 | view.setAccounts(result.toString()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/AllView2.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import javafx.geometry.*; 4 | import javafx.scene.control.*; 5 | import javafx.scene.layout.*; 6 | import javafx.scene.paint.Color; 7 | import javafx.scene.text.Font; 8 | 9 | public class AllView2 { 10 | private Pane root; 11 | ListView accts = new ListView<>(); 12 | 13 | public AllView2(AllController2 controller) { 14 | root = createNodeHierarchy(controller); 15 | accts.setItems(controller.getAccountList()); 16 | accts.setPrefSize(300, 200); 17 | } 18 | 19 | public Pane root() { 20 | return root; 21 | } 22 | 23 | private Pane createNodeHierarchy(AllController2 cont) { 24 | Button intbtn = new Button("Add Interest"); 25 | intbtn.setOnAction(e -> cont.interestButton()); 26 | 27 | VBox p1 = new VBox(8); 28 | p1.setAlignment(Pos.TOP_CENTER); 29 | p1.setPadding(new Insets(10)); 30 | Label title = new Label("Manage All Accounts"); 31 | double size = title.getFont().getSize(); 32 | title.setFont(new Font(size*2)); 33 | title.setTextFill(Color.GREEN); 34 | p1.getChildren().addAll(title, accts, intbtn); 35 | return p1; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/Auditor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class Auditor implements BankObserver { 4 | public Auditor(Bank bank) { 5 | bank.addObserver(BankEvent.NEW, this); 6 | bank.addObserver(BankEvent.SETFOREIGN, this); 7 | } 8 | 9 | public void update(BankEvent e, BankAccount ba, int depositamt) { 10 | if (ba.isForeign()) { 11 | if (e == BankEvent.NEW) 12 | System.out.println("New foreign account: " 13 | + ba.getAcctNum()); 14 | else 15 | System.out.println("Modified foreign account: " 16 | + ba.getAcctNum()); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/AuthorizerWrapper.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public abstract class AuthorizerWrapper implements LoanAuthorizer { 4 | protected LoanAuthorizer auth; 5 | 6 | protected AuthorizerWrapper(LoanAuthorizer auth) { 7 | this.auth = auth; 8 | } 9 | 10 | public boolean authorizeLoan(int amt) { 11 | return auth.authorizeLoan(amt); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/BankAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.io.Serializable; 4 | 5 | public interface BankAccount extends Comparable, Serializable { 6 | int getAcctNum(); 7 | int getBalance(); 8 | void deposit(int amt); 9 | boolean isForeign(); 10 | void setForeign(boolean isforeign); 11 | boolean hasEnoughCollateral(int loanamt); 12 | String toString(); 13 | void addInterest(); 14 | int fee(); 15 | 16 | default boolean isEmpty() { 17 | return getBalance() == 0; 18 | } 19 | 20 | static BankAccount createSavingsWithDeposit(int acctnum, int n) { 21 | BankAccount ba = AccountFactories.SAVINGS.create(acctnum); 22 | ba.deposit(n); 23 | return ba; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/BankAccountAdapter.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class BankAccountAdapter implements FBIAcctInfo { 4 | private BankAccount ba; 5 | 6 | public BankAccountAdapter(BankAccount ba) { 7 | this.ba = ba; 8 | } 9 | 10 | public int balance() { 11 | return ba.getBalance() / 100; 12 | } 13 | 14 | public boolean isForeign() { 15 | return ba.isForeign(); 16 | } 17 | 18 | public String acctType() { 19 | return "deposit"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/BankClient.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.*; 4 | 5 | public class BankClient { 6 | private Scanner scanner; 7 | private InputController controller; 8 | private InputCommand[] commands = InputCommands.values(); 9 | 10 | public BankClient(Scanner scanner, InputController controller) { 11 | this.scanner = scanner; 12 | this.controller = controller; 13 | } 14 | 15 | public void run() { 16 | String usermessage = constructMessage(); 17 | String response = ""; 18 | while (!response.equals("Goodbye!")) { 19 | System.out.print(usermessage); 20 | int cnum = scanner.nextInt(); 21 | InputCommand cmd = commands[cnum]; 22 | response = cmd.execute(scanner, controller); 23 | System.out.println(response); 24 | } 25 | } 26 | 27 | private String constructMessage() { 28 | int last = commands.length-1; 29 | String result = "Enter Account Type ("; 30 | for (int i=0; i accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | Auditor aud = new Auditor(bank); 12 | bank.addObserver(BankEvent.DEPOSIT, 13 | (event,ba,amt) -> { 14 | if (amt > 10000000) 15 | bank.makeSuspicious(ba.getAcctNum()); 16 | }); 17 | InputController controller = new InputController(bank); 18 | Scanner scanner = new Scanner(System.in); 19 | BankClient client = new BankClient(scanner, controller); 20 | client.run(); 21 | info.saveMap(accounts, bank.nextAcctNum()); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public abstract class CheckingAccount extends AbstractBankAccount { 4 | 5 | protected CheckingAccount(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected double collateralRatio() { 10 | return 2.0 / 3.0; 11 | } 12 | 13 | protected abstract String accountType(); 14 | protected abstract double interestRate(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/CollateralAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class CollateralAuthorizer implements LoanAuthorizer { 4 | private BankAccount ba; 5 | 6 | public CollateralAuthorizer(BankAccount ba) { 7 | this.ba = ba; 8 | } 9 | 10 | public boolean authorizeLoan(int amt) { 11 | return ba.hasEnoughCollateral(amt); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/CreationController.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class CreationController implements BankObserver { 4 | private Bank bank; 5 | private CreationView view; 6 | 7 | public CreationController(Bank bank) { 8 | this.bank = bank; 9 | bank.addObserver(BankEvent.NEW, this); 10 | } 11 | 12 | // methods called by the view 13 | void setView(CreationView view) { 14 | this.view = view; 15 | } 16 | public void buttonPressed(int type, boolean isforeign) { 17 | bank.newAccount(type+1, isforeign); 18 | } 19 | 20 | // method called by the model 21 | public void update(BankEvent e, BankAccount ba, int amt) { 22 | view.setTitle("Account " + ba.getAcctNum() + " created"); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/CreditScoreAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.Random; 4 | 5 | public class CreditScoreAuthorizer extends AuthorizerWrapper { 6 | private int score; 7 | 8 | public CreditScoreAuthorizer(LoanAuthorizer auth) { 9 | super(auth); 10 | // For simplicity, mock up the credit score 11 | // associated with the owner of the bank account. 12 | Random rnd = new Random(); 13 | this.score = 300 + rnd.nextInt(500); 14 | } 15 | 16 | public boolean authorizeLoan(int amt) { 17 | if (score > 700 && amt < 1000000) 18 | return true; 19 | else if (score < 500) 20 | return false; 21 | else 22 | return auth.authorizeLoan(amt); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/FBIAcctInfo.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | interface FBIAcctInfo { 4 | int balance(); 5 | boolean isForeign(); 6 | String acctType(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/FBIClient.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.*; 4 | 5 | public class FBIClient { 6 | public static void main(String[] args) { 7 | Bank b = getBank(); 8 | 9 | // put account info into a single list 10 | List L = new ArrayList<>(); 11 | for (BankAccount ba : b) 12 | L.add(new BankAccountAdapter(ba)); 13 | for (Loan ln : b.loans()) 14 | L.add(new LoanAdapter(ln)); 15 | 16 | // then process the list 17 | int count = 0; 18 | for (FBIAcctInfo a : L) 19 | if (a.isForeign() && a.balance() > 1000.0) 20 | count++; 21 | System.out.println("The count is " + count); 22 | } 23 | 24 | private static Bank getBank() { 25 | SavedBankInfo info = new SavedBankInfo("bank16.info"); 26 | Map accounts = info.getAccounts(); 27 | int nextacct = info.nextAcctNum(); 28 | return new Bank(accounts, nextacct); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/GoodCustomerAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.Random; 4 | 5 | public class GoodCustomerAuthorizer extends AuthorizerWrapper { 6 | private boolean isgood; 7 | 8 | public GoodCustomerAuthorizer(LoanAuthorizer auth) { 9 | super(auth); 10 | // For simplicity, mock up the customer status 11 | // associated with the owner of the bank account. 12 | Random rnd = new Random(); 13 | isgood = rnd.nextBoolean(); 14 | } 15 | 16 | public boolean authorizeLoan(int amt) { 17 | if (isgood && amt < 200000) 18 | return true; 19 | else 20 | return auth.authorizeLoan(amt); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/InputCommand.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.Scanner; 4 | 5 | public interface InputCommand { 6 | String execute(Scanner sc, InputController controller); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/InterestChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class InterestChecking extends CheckingAccount { 4 | public InterestChecking(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected String accountType() { 9 | return "Interest checking"; 10 | } 11 | 12 | protected double interestRate() { 13 | return 0.01; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/LoanAdapter.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class LoanAdapter implements FBIAcctInfo { 4 | private Loan ln; 5 | 6 | public LoanAdapter(Loan ln) { 7 | this.ln = ln; 8 | } 9 | 10 | public int balance() { 11 | return (int) (ln.remainingPrincipal() / 100); 12 | } 13 | 14 | public boolean isForeign() { 15 | return !ln.isDomestic(); 16 | } 17 | 18 | public String acctType() { 19 | return "loan"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/LoanAuthorizer.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public interface LoanAuthorizer { 4 | boolean authorizeLoan(int amt); 5 | 6 | static LoanAuthorizer getAuthorizer(BankAccount ba) { 7 | LoanAuthorizer auth = new CollateralAuthorizer(ba); 8 | auth = new CreditScoreAuthorizer(auth); 9 | return new GoodCustomerAuthorizer(auth); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/MaxBalanceVisitor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class MaxBalanceVisitor implements Visitor { 4 | private int max = 0; 5 | 6 | public void accept(BankAccount ba) { 7 | int bal = ba.getBalance(); 8 | if (bal > max) 9 | max = bal; 10 | } 11 | 12 | public Integer result() { 13 | return max; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/ObservableMapAdapter.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.*; 4 | import javafx.collections.*; 5 | 6 | public class ObservableMapAdapter extends ModifiableObservableListBase { 7 | 8 | private final Map delegate; 9 | 10 | public ObservableMapAdapter(Map m) { 11 | delegate = m; 12 | } 13 | 14 | public BankAccount get(int index) { 15 | return delegate.get(index); 16 | } 17 | 18 | public int size() { 19 | return delegate.size(); 20 | } 21 | 22 | protected void doAdd(int index, BankAccount element) { 23 | delegate.put(index, element); 24 | } 25 | 26 | protected BankAccount doSet(int index, BankAccount element) { 27 | return delegate.put(index, element); 28 | } 29 | 30 | protected BankAccount doRemove(int index) { 31 | return delegate.remove(index); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/OwnerStrategy.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.io.Serializable; 4 | 5 | public interface OwnerStrategy extends Serializable { 6 | boolean isForeign(); 7 | String location(); 8 | int fee(); 9 | } 10 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/Owners.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public enum Owners implements OwnerStrategy { 4 | DOMESTIC(false,0,"domestic"), FOREIGN(true,500,"foreign"); 5 | 6 | private boolean isforeign; 7 | private int fee; 8 | private String location; 9 | 10 | private Owners(boolean isforeign, int fee, String location) { 11 | this.isforeign = isforeign; 12 | this.fee = fee; 13 | this.location = location; 14 | } 15 | 16 | public boolean isForeign() { 17 | return isforeign; 18 | } 19 | 20 | public int fee() { 21 | return fee; 22 | } 23 | 24 | public String location() { 25 | return location; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Banking Demo, Version 19. 2 | 3 | The bank-related code from BankClient and InputCommands 4 | is moved to the new class InputController. BankProgram 5 | is modified to create the InputController object and 6 | connect it to the bank client. 7 | 8 | The classes StreamStatProgram and StreamAccountStats are modified 9 | to use MVC. The new classes are StreamStatProgram, 10 | StreamStatModel, StreamStatController, and StreamStatView. 11 | 12 | A JavaFX interface to the banking demo is created. There are 13 | JavaFX classes for three views and their associated controllers: 14 | AllView, AllController, InfoView, InfoController, CreationView, 15 | and CreationController. When one view causes the model to change, 16 | that change is reflected in all relevant views. 17 | 18 | There is also an alternative view AllView2, which uses a 19 | ListView control to display the account information. This 20 | control is connected to an observable list which is managed 21 | by the new class AllController2. 22 | 23 | There is also an alternative view AllView3, which uses a 24 | TableView control to display the account information. This 25 | class can use AllController2 as its controller. 26 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/RegularChecking.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class RegularChecking extends CheckingAccount { 4 | 5 | public RegularChecking(int acctnum) { 6 | super(acctnum); 7 | } 8 | 9 | protected String accountType() { 10 | return "Regular checking"; 11 | } 12 | 13 | protected double interestRate() { 14 | return 0.0; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/SavingsAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class SavingsAccount extends AbstractBankAccount { 4 | public SavingsAccount(int acctnum) { 5 | super(acctnum); 6 | } 7 | 8 | protected double collateralRatio() { 9 | return 1.0 / 2.0; 10 | } 11 | 12 | protected String accountType() { 13 | return "Savings"; 14 | } 15 | 16 | protected double interestRate() { 17 | return 0.01; 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/StreamStatController.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.function.*; 4 | 5 | public class StreamStatController { 6 | private StreamStatModel model; 7 | Predicate pred = ba -> ba.fee() == 0; 8 | 9 | public StreamStatController (StreamStatModel model) { 10 | this.model = model; 11 | } 12 | 13 | public String getAccounts6() { 14 | return model.getAccounts6(pred); 15 | } 16 | 17 | public String getAccounts7() { 18 | return model.getAccounts7(pred); 19 | } 20 | 21 | public String maxBalance6() { 22 | return Integer.toString(model.maxBalance6(pred)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/StreamStatModel.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.function.*; 4 | import java.util.stream.Stream; 5 | 6 | public class StreamStatModel { 7 | private Bank bank; 8 | 9 | public StreamStatModel(Bank b) { 10 | bank = b; 11 | } 12 | 13 | public String getAccounts6(Predicate pred) { 14 | Stream s = bank.stream(); 15 | s = s.filter(pred); 16 | Stream t = s.map(ba->ba.toString()); 17 | return t.reduce("", (s1,s2)-> s1+s2+"\n"); 18 | } 19 | 20 | public String getAccounts7(Predicate pred) { 21 | return bank.stream() 22 | .filter(pred) 23 | .map(ba->ba.toString()) 24 | .reduce("", (s1,s2)-> s1+s2+"\n"); 25 | } 26 | 27 | public int maxBalance6(Predicate pred) { 28 | return bank.stream() 29 | .filter(pred) 30 | .map(ba->ba.getBalance()) 31 | .reduce(0, (x,y)->Math.max(x,y)); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/StreamStatProgram.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.Map; 4 | 5 | public class StreamStatProgram { 6 | public static void main(String[] args) { 7 | SavedBankInfo info = new SavedBankInfo("bank19.info"); 8 | Map accounts = info.getAccounts(); 9 | int nextacct = info.nextAcctNum(); 10 | Bank bank = new Bank(accounts, nextacct); 11 | StreamStatModel m = new StreamStatModel(bank); 12 | StreamStatController c = new StreamStatController(m); 13 | StreamStatView v = new StreamStatView(c); 14 | v.run(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/StreamStatView.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class StreamStatView { 4 | StreamStatController c; 5 | 6 | public StreamStatView(StreamStatController c) { 7 | this.c = c; 8 | } 9 | 10 | public void run() { 11 | System.out.println("The max balance of the domestic accounts is " + c.maxBalance6()); 12 | System.out.println("Here are the domestic accounts."); 13 | System.out.println(c.getAccounts6()); 14 | System.out.println("Here are the domestic accounts again."); 15 | System.out.println(c.getAccounts7()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/SuspiciousAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.Date; 4 | 5 | public class SuspiciousAccount extends BankAccountWrapper { 6 | public SuspiciousAccount(BankAccount ba) { 7 | super(ba); 8 | } 9 | 10 | public void deposit(int amt) { 11 | Date d = new Date(); 12 | String msg = "On " + d + " account #" + ba.getAcctNum() + " deposited " + amt; 13 | System.out.println(msg); 14 | ba.deposit(amt); 15 | } 16 | 17 | public String toString() { 18 | return "## " + ba.toString(); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/UnmodifiableAccount.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | public class UnmodifiableAccount extends BankAccountWrapper { 4 | 5 | public UnmodifiableAccount(BankAccount ba) { 6 | super(ba); 7 | } 8 | 9 | public void deposit(int amt) { 10 | throw new UnsupportedOperationException(); 11 | } 12 | 13 | // public void addInterest() { 14 | // throw new UnsupportedOperationException(); 15 | // } 16 | 17 | public void setForeign(boolean isforeign) { 18 | throw new UnsupportedOperationException(); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/UnmodifiableBankIterator.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.Iterator; 4 | 5 | public class UnmodifiableBankIterator implements Iterator { 6 | private Iterator iter; 7 | 8 | public UnmodifiableBankIterator(Iterator iter) { 9 | this.iter = iter; 10 | } 11 | 12 | public boolean hasNext() { 13 | return iter.hasNext(); 14 | } 15 | 16 | public BankAccount next() { 17 | return new UnmodifiableAccount(iter.next()); 18 | } 19 | 20 | public void remove() { 21 | throw new UnsupportedOperationException(); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/bank19/Visitor.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.bank19; 2 | 3 | import java.util.function.Consumer; 4 | 5 | public interface Visitor extends Consumer { 6 | R result(); 7 | } 8 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count1/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-prog-design/b8028b786f81ed3b2369105ccc94b7171b5be599/javaprogramdesign/chapter11/count1/.DS_Store -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count1/Count1.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count1; 2 | 3 | import javafx.application.Application; 4 | import javafx.geometry.*; 5 | import javafx.stage.*; 6 | import javafx.scene.Scene; 7 | import javafx.scene.control.*; 8 | import javafx.scene.layout.*; 9 | 10 | public class Count1 extends Application { 11 | private static int count = 0; 12 | private static Label lbl = new Label("Count is 0"); 13 | 14 | public void start(Stage stage) { 15 | Button inc = new Button("Increment"); 16 | Button dec = new Button("Decrement"); 17 | VBox p = new VBox(8); 18 | p.setAlignment(Pos.CENTER); 19 | p.setPadding(new Insets(10)); 20 | p.getChildren().addAll(lbl, inc, dec); 21 | 22 | inc.setOnAction(e -> updateBy(1)); 23 | dec.setOnAction(e -> updateBy(-1)); 24 | 25 | stage.setScene(new Scene(p)); 26 | stage.show(); 27 | } 28 | 29 | private static void updateBy(int n) { 30 | count += n; 31 | lbl.setText("Count is " + count); 32 | } 33 | 34 | public static void main(String[] args) { 35 | Application.launch(args); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count1/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Count Demo, Version 1. 2 | 3 | The JavaFX window has two buttons and a label. 4 | The label displays the current count. 5 | The buttons increment and decrement the count. 6 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count2/Count2.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count2; 2 | 3 | import javafx.application.Application; 4 | import javafx.stage.*; 5 | import javafx.scene.Scene; 6 | 7 | public class Count2 extends Application { 8 | public void start(Stage stage) { 9 | CountModel model = new CountModel(); 10 | CountController controller = new CountController(model); 11 | CountView view = new CountView(controller); 12 | 13 | Scene scene = new Scene(view.getRoot()); 14 | stage.setScene(scene); 15 | stage.show(); 16 | } 17 | 18 | public static void main(String[] args) { 19 | Application.launch(args); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count2/CountController.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count2; 2 | 3 | class CountController { 4 | private CountModel model; 5 | 6 | public CountController(CountModel model) { 7 | this.model = model; 8 | } 9 | 10 | public String incrementButtonPressed() { 11 | model.updateBy(1); 12 | return "Count is " + model.getCount(); 13 | } 14 | public String decrementButtonPressed() { 15 | model.updateBy(-1); 16 | return "Count is " + model.getCount(); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count2/CountModel.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count2; 2 | 3 | public class CountModel { 4 | private int count = 0; 5 | 6 | public void updateBy(int n) { 7 | count += n; 8 | } 9 | 10 | public int getCount() { 11 | return count; 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count2/CountView.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count2; 2 | 3 | import javafx.geometry.*; 4 | import javafx.scene.control.*; 5 | import javafx.scene.layout.*; 6 | 7 | class CountView { 8 | private Pane root; 9 | 10 | public CountView(CountController cont) { 11 | root = createNodeHierarchy(cont); 12 | } 13 | 14 | public Pane getRoot() { 15 | return root; 16 | } 17 | 18 | private Pane createNodeHierarchy(CountController cont) { 19 | Button inc = new Button("Increment"); 20 | Button dec = new Button("Decrement"); 21 | Label lbl = new Label("Count is 0"); 22 | 23 | VBox p = new VBox(8); 24 | p.setAlignment(Pos.CENTER); 25 | p.setPadding(new Insets(10)); 26 | p.getChildren().addAll(lbl, inc, dec); 27 | 28 | inc.setOnAction(e -> { 29 | String s = cont.incrementButtonPressed(); 30 | lbl.setText(s); 31 | }); 32 | dec.setOnAction(e -> lbl.setText(cont.decrementButtonPressed())); 33 | 34 | return p; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count2/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Count Demo, Version 2. 2 | 3 | Modify version 1 so that there are model, view, and 4 | controller classes. 5 | 6 | The model contains the current count. 7 | The view displays the two buttons and label. 8 | The controller coordinates between the model and view. 9 | 10 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count3/CountController.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count3; 2 | 3 | class CountController implements CountObserver { 4 | private CountModel model; 5 | private CountView view; 6 | 7 | public CountController(CountModel model) { 8 | this.model = model; 9 | model.addObserver(this); 10 | } 11 | 12 | // Methods called by the view 13 | public void setView(CountView view) { 14 | this.view = view; 15 | } 16 | public void incrementButtonPressed() { 17 | model.updateBy(1); 18 | } 19 | public void decrementButtonPressed() { 20 | model.updateBy(-1); 21 | } 22 | 23 | // Method called by the model 24 | public void countUpdated(int count) { 25 | view.setLabel("Count is " + count); 26 | } 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count3/CountModel.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count3; 2 | 3 | import java.util.*; 4 | 5 | public class CountModel { 6 | private int count = 0; 7 | private Collection observers = new ArrayList<>(); 8 | 9 | public void addObserver(CountObserver obs) { 10 | observers.add(obs); 11 | } 12 | 13 | public void updateBy(int n) { 14 | count += n; 15 | notifyObservers(count); 16 | } 17 | 18 | public int getCount() { 19 | return count; 20 | } 21 | 22 | private void notifyObservers(int count) { 23 | for (CountObserver obs : observers) 24 | obs.countUpdated(count); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count3/CountObserver.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count3; 2 | 3 | public interface CountObserver { 4 | public void countUpdated(int count); 5 | } 6 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count3/CountView.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count3; 2 | 3 | import javafx.geometry.*; 4 | import javafx.scene.control.*; 5 | import javafx.scene.layout.*; 6 | 7 | class CountView { 8 | private Label lbl = new Label("Count is 0"); 9 | private Pane root; 10 | 11 | public CountView(CountController controller) { 12 | root = createNodeHierarchy(controller); 13 | controller.setView(this); 14 | } 15 | 16 | public Pane root() { 17 | return root; 18 | } 19 | 20 | public void setLabel(String s) { 21 | lbl.setText(s); 22 | } 23 | 24 | private Pane createNodeHierarchy(CountController cont) { 25 | Button inc = new Button("Increment"); 26 | Button dec = new Button("Decrement"); 27 | 28 | VBox p = new VBox(8); 29 | p.setAlignment(Pos.CENTER); 30 | p.setPadding(new Insets(10)); 31 | p.getChildren().addAll(lbl, inc, dec); 32 | 33 | inc.setOnAction(e -> cont.incrementButtonPressed()); 34 | dec.setOnAction(e -> cont.decrementButtonPressed()); 35 | 36 | return p; 37 | } 38 | } -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count3/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Count Demo, Version 3. 2 | 3 | Add a new view/controller to version 2, 4 | which watches the current count. 5 | 6 | The count view and its controller are rewritten 7 | to conform to the MVC pattern. In particular, the 8 | controller is now a model observer. 9 | 10 | Both views are displayed in a single window. 11 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count3/WatcherController.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count3; 2 | 3 | public class WatcherController implements CountObserver { 4 | private WatcherView view; 5 | private int howmany = 0; 6 | 7 | public WatcherController(CountModel model) { 8 | model.addObserver(this); 9 | } 10 | 11 | public void setView(WatcherView view) { 12 | this.view = view; 13 | } 14 | 15 | public void countUpdated(int count) { 16 | howmany++; 17 | boolean iseven = (count%2 == 0); 18 | boolean isodd = !iseven; 19 | String msg = "The count has changed " + howmany + " times"; 20 | view.update(msg, iseven, isodd); 21 | } 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /javaprogramdesign/chapter11/count3/WatcherView.java: -------------------------------------------------------------------------------- 1 | package javaprogramdesign.chapter11.count3; 2 | 3 | import javafx.geometry.*; 4 | import javafx.scene.control.*; 5 | import javafx.scene.layout.*; 6 | 7 | class WatcherView { 8 | private Label lbl = new Label("The count has not yet changed"); 9 | private CheckBox iseven = new CheckBox("Value is now even"); 10 | private CheckBox isodd = new CheckBox("Value is now odd"); 11 | private Pane root; 12 | 13 | public WatcherView(WatcherController controller) { 14 | root = createNodeHierarchy(); 15 | controller.setView(this); 16 | } 17 | 18 | public Pane root() { 19 | return root; 20 | } 21 | 22 | public void update(String s, boolean even, boolean odd) { 23 | lbl.setText(s); 24 | iseven.setSelected(even); 25 | isodd.setSelected(odd); 26 | } 27 | 28 | private Pane createNodeHierarchy() { 29 | iseven.setSelected(true); 30 | isodd.setSelected(false); 31 | 32 | VBox p = new VBox(8); 33 | p.setAlignment(Pos.CENTER); 34 | p.setPadding(new Insets(10)); 35 | p.getChildren().addAll(lbl, iseven, isodd); 36 | 37 | return p; 38 | } 39 | } 40 | --------------------------------------------------------------------------------