SHARE
TWEET

Untitled

a guest Aug 20th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. * Underscores in numeric literals
  2. * Strings in switch
  3. * Multi-catch
  4. * Type inference for typed object creation
  5. * try with resources and suppressed exceptions
  6.  
  7.  
  8. public class ProjectCoinBanker {
  9.  
  10.   private static final Integer ONE_MILLION = 1_000_000;
  11.   private static final String RICH_MSG = "You need more than $%,d to be considered rich.";
  12.  
  13.   public static void main(String[] args) throws Exception {
  14.     System.out.println(String.format(RICH_MSG, ONE_MILLION));
  15.  
  16.     String requestType = args[0];
  17.     String accountId = args[1];
  18.     switch (requestType) {
  19.         case "displayBalance":
  20.             printBalance(accountId);
  21.             break;
  22.         case "lastActivityDate" :
  23.             printLastActivityDate(accountId);
  24.             break;
  25.         case "amIRich" :
  26.             amIRich(accountId);
  27.             break;
  28.         case "lastTransactions" :
  29.             printLastTransactions(accountId, Integer.parseInt(args[2]));
  30.             break;
  31.         case "averageDailyBalance" :
  32.             printAverageDailyBalance(accountId);
  33.             break;
  34.         default: break;
  35.     }
  36.   }
  37.  
  38.   private static void printAverageDailyBalance(String accountId) {
  39.         String sql = String.format(AVERAGE_DAILY_BALANCE_QUERY, accountId);
  40.     try (
  41.           PreparedStatement s = _conn.prepareStatement(sql);
  42.               ResultSet rs = s.executeQuery();
  43.              ) {
  44.             while (rs.next()) {
  45.           //print the average daily balance results
  46.                 }
  47.          } catch (SQLException e) {
  48.         // handle exception, but no need for finally to close resources
  49.                 for (Throwable t : e.getSuppressed()) {
  50.            System.out.println("Suppressed exception message is " + t.getMessage());
  51.         }
  52.          }
  53.   }
  54.  
  55.   private static void printLastTransactions(String accountId, int numberOfTransactions) {
  56.     List<Transaction> transactions = new ArrayList<>();
  57.     ... handle fetching/printing transactions
  58.   }
  59.  
  60.   private static void printBalance(String accountId) {
  61.     try {
  62.         BigDecimal balance = getBalance(accountId);
  63.         //print balance
  64.     } catch (AccountFrozenException | ComplianceViolationException | AccountClosedException e) {
  65.         System.err.println("Please see your local branch for help with your account.");
  66.     }
  67.   }
  68.  
  69.   private static void amIRich(String accountId) {
  70.     try {
  71.         BigDecimal balance = getBalance(accountId);
  72.         //find out if the account holder is rich
  73.     } catch (AccountFrozenException | ComplianceViolationException | AccountClosedException e) {
  74.         System.out.println("Please see your local branch for help with your account.");
  75.     }
  76.   }
  77.  
  78.   private static BigDecimal getBalance(String acccountId)
  79.       throws AccountFrozenException, AccountClosedException, ComplianceViolationException {
  80.       ... getBalance functionality
  81.   }
  82.  
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top