- package aa.demo.classes.aa;
- /**
- * 一个百万富翁遇到一个程序员,程序员找他谈一个换钱的计划,该计划如下
- :程序员每天给该富翁10万元,而富翁第一天给程序员一分钱,第二天给程序员二分钱
- 第三天给程序员四分钱,依次类推,每一天给的钱都是前一天的两倍,直到满一个月(按30天),富翁欣然接受。
- 编写一个程序计算一个月后两人各的多少钱。
- *
- */
-
- /////////////////////异常类//////////////////////////////////
- class NoMenoyException extends RuntimeException{
- public NoMenoyException(String message){
- super(message);
- }
- }
- ////////////////////////抽象类人///////////////////////////////
- abstract class PerSon{
- private double menoy;
- public static final double SHIWANMENOY=100000.0;//1o万
- public abstract void payMenoy();
- public abstract void shouYi(double menoy);
- public void setMenoy(double menoy) {
- this.menoy = menoy;
- }
- public double getMenoy() {
- return menoy;
- }
- }
- ////////////////////程序员/////////////////////////////////////////
- class Cxy extends PerSon{
- private Fw instance=null;
- private int count;
- public String str;
- public boolean flag;
- public void setInstance(Fw instance){
- this.instance=instance;
- }
- @Override
- public void payMenoy(){
- if(this.getMenoy()<SHIWANMENOY){
- throw new NoMenoyException("程序员没有这么多钱支付给富翁");
- }
-
- count++;
- Tools.sop("\r\n");
- Tools.sop(count+"号支付情况:");
- Tools.sop("程序员付给富翁"+Tools.format(SHIWANMENOY));
-
- this.setMenoy(this.getMenoy()-SHIWANMENOY);
- instance.shouYi(SHIWANMENOY);
-
- }
- @Override
- public void shouYi(double menoy) {
- this.setMenoy(this.getMenoy()+menoy);
-
- Tools.sop("程序员收到富翁"+Tools.format(menoy));
- Tools.sop("\t\r程序员资金为:"+Tools.format(getMenoy()));
- Tools.sop("富翁的资金为:"+Tools.format(instance.getMenoy()));
-
- if(menoy>=this.SHIWANMENOY && !flag){
- str="\r\t地"+count+"天,富翁支付的金额超过程序员支付的金额.\r\n富翁支付给程序员的金额为:"+
- Tools.format(menoy).toString()+"\r\n超出"+Tools.format(this.SHIWANMENOY-menoy)+"\r\t" +
- "程序员资金为:"+Tools.format(getMenoy())+"\r\t"+
- "富翁的资金为:"+Tools.format(instance.getMenoy())+"\r\t";
- flag=true;
- }
- }
-
- }
- //////////////////////////////富人////////////////////////////////////////
- class Fw extends PerSon{
- private double payMenoy=0.01;
- private Cxy instance=null;
- public void setInstance(Cxy instance){
- this.instance=instance;
- }
- @Override
- public void payMenoy() {
- if(getMenoy()<payMenoy){
- throw new NoMenoyException("富翁没这么多钱支付给程序员");
-
- }
-
- Tools.sop("富翁付给程序员"+Tools.format(payMenoy));
-
- setMenoy(getMenoy()-payMenoy);
- this.instance.shouYi(payMenoy);
- payMenoy*=2;
-
- }
- @Override
- public void shouYi(double payMenoy) {
- this.setMenoy(this.getMenoy()+payMenoy);
- Tools.sop("富翁收到程序员"+Tools.format(SHIWANMENOY));
- }
-
- }
- ///////////////////////DEMO///////////////////////////////////
- public class GeveMeMenoy{
- public static void main(String[] args) {
- init();
- }
- static void init(){
- Cxy cxy=new Cxy();
- Fw fw=new Fw();
-
- cxy.setInstance(fw);
- fw.setInstance(cxy);
-
- //给程序员和富翁每人一千万启动资金
- cxy.setMenoy(PerSon.SHIWANMENOY*100);
- fw.setMenoy(PerSon.SHIWANMENOY*100);
-
- Tools.sop("程序员和富翁每人本金一千万。");
-
- for(int day=0;day<30;day++){
- cxy.payMenoy();
- fw.payMenoy();
-
- }
-
-
- Tools.sop(cxy.str);
- Tools.sop("双方资金总共为:"+Tools.format(cxy.getMenoy()+fw.getMenoy()));
- Tools.sop("除去成本双方资金分别为:\r\n程序员的资金即利润为:"+Tools.format((cxy.getMenoy()-PerSon.SHIWANMENOY*100)));
- Tools.sop("富翁的资金即利润为:"+Tools.format(fw.getMenoy()-PerSon.SHIWANMENOY*100));
- }
- }
- //////////////////////////工具类///////////////////////////////////////////
- class Tools{
- //打印
- public static void sop(Object obj){
- System.out.println(obj);
- }
-
- //资金转换成大写
- public static StringBuilder format(double d){
- String tmp="";
- if(d<0){
- tmp=" 负的 ";
- d=-d;
- }
- long menoy=(long)d;
- StringBuilder str=new StringBuilder();
- return str.append(tmp).append(format(menoy,true)).append(format(getXiaoShu(d),false));
- }
-
- /**
- * @param menoy 要转换的人民币
- * @param isZhengShu true为整数 false为小数
- * @return
- */
- private static StringBuilder format(long menoy,Boolean isZhengShu){
- char[] arrs={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
- char[] dw={'分','角','元','拾','佰','仟','万','拾','佰','仟','亿','拾','佰','仟','万'};
- StringBuilder menoyStr=new StringBuilder();
- for(int i=0,temp,index=isZhengShu?2:0;menoy!=0;i++,menoy/=10){
- temp=(int)(menoy%10);
- if(temp==0){
- if(isZhengShu){
- switch(i){
- case 0:
- case 4:
- //如果资金上亿 万哪里是0的话不加万
- if(menoy/10000>0){
- break;
- }
- case 8:
- menoyStr.insert(0, dw[i+index]);
- break;
- }
- }
- continue;
- }
- menoyStr.insert(0, arrs[temp]).insert(1,(dw[i+index]));
-
- }
-
- return menoyStr;
- }
- //返回小数部分2位
- public static int getXiaoShu(double d){
- int x=(int)((d*100)%100);
- return x;
-
- }
- }
复制代码 |