-
-
Save megascus/3010699 to your computer and use it in GitHub Desktop.
消費税を日付によって変更できるようにする。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package megascus.tax; | |
import java.math.BigDecimal; | |
import java.util.Calendar; | |
import java.util.Date; | |
/** | |
* 消費税を保持します。 日付によって消費税が変更できるように実装するサンプルです。 実際に施行される日付とは違う可能性があるので注意して下さい。 | |
* | |
* @author megascus | |
*/ | |
public class ConsumptionTax { | |
private static Entry[] entries; | |
static { | |
entries = new Entry[]{new Entry(getTimeInMillis(1989, 4, 1), "0.03"), | |
new Entry(getTimeInMillis(1997, 4, 1), "0.05"), | |
new Entry(getTimeInMillis(2014, 4, 1), "0.08"), | |
new Entry(getTimeInMillis(2015, 10, 1), "0.1")}; | |
} | |
private ConsumptionTax() { | |
} | |
private static long getTimeInMillis(int year, int month, int date) { | |
Calendar cal = Calendar.getInstance(); | |
cal.clear(); | |
cal.set(year, month - 1, date); | |
return cal.getTimeInMillis(); | |
} | |
/** | |
* 日付を指定して消費税を取得します。 | |
* | |
* @param date | |
* @return 消費税 | |
*/ | |
public static BigDecimal getRate(Date date) { | |
long ldate = date.getTime(); | |
for (int i = entries.length - 1; i > 0; i--) { | |
if (entries[i].date <= ldate) { | |
return entries[i].rate; | |
} | |
} | |
return BigDecimal.ZERO; | |
} | |
/** | |
* 日付を指定して消費税を取得します。 | |
* | |
* @param date | |
* @return 消費税 | |
*/ | |
public static BigDecimal getRate(Calendar date) { | |
return getRate(date.getTime()); | |
} | |
/** | |
* 今日の消費税を取得します。 | |
* | |
* @return 消費税 | |
*/ | |
public static BigDecimal getRate() { | |
return getRate(new Date()); | |
} | |
} | |
class Entry { | |
Entry(long date, String rate) { | |
this.date = date; | |
this.rate = new BigDecimal(rate); | |
} | |
long date; | |
BigDecimal rate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment