From: Erik V. <ev...@us...> - 2010-05-07 20:03:57
|
Update of /cvsroot/rails/18xx/rails/game In directory sfp-cvsdas-4.v30.ch3.sourceforge.com:/tmp/cvs-serv20269/rails/game Modified Files: PublicCompanyI.java OperatingRound.java PublicCompany.java Log Message: 1835: Implemented denial of dividend for PR shares received in exchange for precursors that have operated before in the same OR. This required some refactoring of the dividend payout code. Index: OperatingRound.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/OperatingRound.java,v retrieving revision 1.127 retrieving revision 1.128 diff -C2 -d -r1.127 -r1.128 *** OperatingRound.java 5 May 2010 21:44:51 -0000 1.127 --- OperatingRound.java 7 May 2010 20:03:49 -0000 1.128 *************** *** 13,16 **** --- 13,17 ---- import rails.game.state.IntegerState; import rails.util.LocalText; + import rails.util.SequenceUtil; /** *************** *** 919,923 **** ReportBuffer.add(LocalText.getText("CompanyDoesNotPayDividend", operatingCompany.getName())); ! operatingCompany.withhold(amount); } else if (revenueAllocation == SetDividend.PAYOUT) { --- 920,924 ---- ReportBuffer.add(LocalText.getText("CompanyDoesNotPayDividend", operatingCompany.getName())); ! withhold(amount); } else if (revenueAllocation == SetDividend.PAYOUT) { *************** *** 926,930 **** operatingCompany.getName(), Bank.format(amount) )); ! operatingCompany.payout(amount); } else if (revenueAllocation == SetDividend.SPLIT) { --- 927,931 ---- operatingCompany.getName(), Bank.format(amount) )); ! payout(amount); } else if (revenueAllocation == SetDividend.SPLIT) { *************** *** 933,937 **** operatingCompany.getName(), Bank.format(amount) )); ! operatingCompany.splitRevenue(amount); } else if (revenueAllocation == SetDividend.WITHHOLD) { --- 934,938 ---- operatingCompany.getName(), Bank.format(amount) )); ! splitRevenue(amount); } else if (revenueAllocation == SetDividend.WITHHOLD) { *************** *** 941,945 **** Bank.format(amount) )); ! operatingCompany.withhold(amount); } --- 942,946 ---- Bank.format(amount) )); ! withhold(amount); } *************** *** 952,955 **** --- 953,1058 ---- } + /** + * Distribute the dividend amongst the shareholders. + * + * @param amount + */ + public void payout(int amount) { + + if (amount == 0) return; + + int part; + int shares; + + Map<CashHolder, Integer> sharesPerRecipient = countSharesPerRecipient(); + + // Calculate, round up, report and add the cash + + // Define a precise sequence for the reporting + Set<CashHolder> recipientSet = sharesPerRecipient.keySet(); + for (CashHolder recipient : SequenceUtil.sortCashHolders(recipientSet)) { + if (recipient instanceof Bank) continue; + shares = (sharesPerRecipient.get(recipient)); + if (shares == 0) continue; + part = (int) Math.ceil(amount * shares * operatingCompany.getShareUnit() / 100.0); + ReportBuffer.add(LocalText.getText("Payout", + recipient.getName(), + Bank.format(part), + shares, + operatingCompany.getShareUnit())); + new CashMove(bank, recipient, part); + } + + // Move the token + operatingCompany.payout(amount); + + } + + protected Map<CashHolder, Integer> countSharesPerRecipient () { + + Map<CashHolder, Integer> sharesPerRecipient = new HashMap<CashHolder, Integer>(); + + // Changed to accomodate the CGR 5% share roundup rule. + // For now it is assumed, that actual payouts are always rounded up + // (the withheld half of split revenues is not handled here, see splitRevenue()). + + // First count the shares per recipient + for (PublicCertificateI cert : operatingCompany.getCertificates()) { + CashHolder recipient = getBeneficiary(cert); + if (!sharesPerRecipient.containsKey(recipient)) { + sharesPerRecipient.put(recipient, cert.getShares()); + } else { + sharesPerRecipient.put(recipient, + sharesPerRecipient.get(recipient) + cert.getShares()); + } + } + return sharesPerRecipient; + } + + /** Who gets the per-share revenue? */ + protected CashHolder getBeneficiary(PublicCertificateI cert) { + + Portfolio holder = cert.getPortfolio(); + CashHolder beneficiary = holder.getOwner(); + // Special cases apply if the holder is the IPO or the Pool + if (operatingCompany.paysOutToTreasury(cert)) { + beneficiary = operatingCompany; + } + return beneficiary; + } + + /** + * Withhold a given amount of revenue (and store it). + * + * @param The revenue amount. + */ + public void withhold(int amount) { + if (amount > 0) new CashMove(bank, operatingCompany, amount); + // Move the token + operatingCompany.withhold(amount); + } + + /** Split a dividend. TODO Optional rounding down the payout + * + * @param amount + */ + public void splitRevenue(int amount) { + + if (amount > 0) { + // Withhold half of it + // For now, hardcode the rule that payout is rounded up. + int numberOfShares = operatingCompany.getNumberOfShares(); + int withheld = + (amount / (2 * numberOfShares)) * numberOfShares; + new CashMove(bank, operatingCompany, withheld); + ReportBuffer.add(operatingCompany.getName() + " receives " + Bank.format(withheld)); + + // Payout the remainder + int payed = amount - withheld; + payout(payed); + } + + } + /** Default version, to be overridden if need be */ protected int checkForDeductions (SetDividend action) { *************** *** 1155,1159 **** operatingCompany.initTurn(); trainsBoughtThisTurn.clear(); - // setStep (GameDef.OrStep.LAY_TRACK); duplication } --- 1258,1261 ---- Index: PublicCompany.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PublicCompany.java,v retrieving revision 1.95 retrieving revision 1.96 diff -C2 -d -r1.95 -r1.96 *** PublicCompany.java 22 Apr 2010 19:09:58 -0000 1.95 --- PublicCompany.java 7 May 2010 20:03:49 -0000 1.96 *************** *** 1245,1271 **** } - /** Split a dividend. TODO Optional rounding down the payout - * - * @param amount - */ - public void splitRevenue(int amount) { - - if (amount > 0) { - // Withhold half of it - // For now, hardcode the rule that payout is rounded up. - int withheld = - (amount / (2 * getNumberOfShares())) * getNumberOfShares(); - new CashMove(bank, this, withheld); - ReportBuffer.add(name + " receives " + Bank.format(withheld)); - - // Payout the remainder - int payed = amount - withheld; - payout(payed); - } - - } - /** ! * Distribute the dividend amongst the shareholders. * * @param amount --- 1245,1250 ---- } /** ! * Determine if the price token must be moved after a dividend payout. * * @param amount *************** *** 1275,1313 **** if (amount == 0) return; - int part; - int shares; - Map<CashHolder, Integer> sharesPerRecipient = new HashMap<CashHolder, Integer>(); - - // Changed to accomodate the CGR 5% share roundup rule. - // For now it is assumed, that actual payouts are always rounded up - // (the withheld half of split revenues is not handled here, see splitRevenue()). - - // First count the shares per recipient - for (PublicCertificateI cert : certificates) { - CashHolder recipient = getBeneficiary(cert); - if (!sharesPerRecipient.containsKey(recipient)) { - sharesPerRecipient.put(recipient, cert.getShares()); - } else { - sharesPerRecipient.put(recipient, - sharesPerRecipient.get(recipient) + cert.getShares()); - } - } - - // Calculate, round up, report and add the cash - - // Define a precise sequence for the reporting - Set<CashHolder> recipientSet = sharesPerRecipient.keySet(); - for (CashHolder recipient : SequenceUtil.sortCashHolders(recipientSet)) { - if (recipient instanceof Bank) continue; - shares = (sharesPerRecipient.get(recipient)); - part = (int) Math.ceil(amount * shares * shareUnit.intValue() / 100.0); - ReportBuffer.add(LocalText.getText("Payout", - recipient.getName(), - Bank.format(part), - shares, - shareUnit.intValue())); - new CashMove(bank, recipient, part); - } - // Move the token if (hasStockPrice --- 1254,1257 ---- *************** *** 1319,1343 **** } ! /** Who gets the per-share revenue? */ ! protected CashHolder getBeneficiary(PublicCertificateI cert) { Portfolio holder = cert.getPortfolio(); - CashHolder beneficiary = holder.getOwner(); - // Special cases apply if the holder is the IPO or the Pool if (holder == bank.getIpo() && ipoPaysOut || holder == bank.getPool() && poolPaysOut) { ! beneficiary = this; } ! return beneficiary; } /** ! * Withhold a given amount of revenue (and store it). * * @param The revenue amount. */ public void withhold(int amount) { - if (amount > 0) new CashMove(bank, this, amount); - // Move the token if (hasStockPrice) stockMarket.withhold(this); } --- 1263,1282 ---- } ! public boolean paysOutToTreasury (PublicCertificateI cert) { Portfolio holder = cert.getPortfolio(); if (holder == bank.getIpo() && ipoPaysOut || holder == bank.getPool() && poolPaysOut) { ! return true; } ! return false; } /** ! * Determine if the price token must be moved after a withheld dividend. * * @param The revenue amount. */ public void withhold(int amount) { if (hasStockPrice) stockMarket.withhold(this); } Index: PublicCompanyI.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PublicCompanyI.java,v retrieving revision 1.53 retrieving revision 1.54 diff -C2 -d -r1.53 -r1.54 *** PublicCompanyI.java 22 Apr 2010 19:09:58 -0000 1.53 --- PublicCompanyI.java 7 May 2010 20:03:49 -0000 1.54 *************** *** 214,225 **** public int getFloatPercentage(); - //public Portfolio getPortfolio(); - public void payout(int amount); - public void splitRevenue(int amount); - public void withhold(int amount); public boolean isSoldOut(); --- 214,223 ---- public int getFloatPercentage(); public void payout(int amount); public void withhold(int amount); + public boolean paysOutToTreasury (PublicCertificateI cert); + public boolean isSoldOut(); *************** *** 231,234 **** --- 229,233 ---- public int getShareUnit(); public int getShareUnitsForSharePrice(); + public int getNumberOfShares(); /** |