⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 retire.java

📁 corejava的源程序内有好多的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
class RetireInfo
{
   /**
      Gets the available balance for a given year.
      @param year the year for which to compute the balance
      @return the amount of money available (or required) in
      that year
   */
   public double getBalance(int year)
   {
      if (year < currentAge) return 0;
      else if (year == currentAge)
      {
         age = year;
         balance = savings;
         return balance;
      }
      else if (year == age)
         return balance;
      if (year != age + 1)
         getBalance(year - 1);
      age = year;
      if (age < retireAge)
         balance += contrib;
      else
         balance -= income;
      balance = balance
         * (1 + (investPercent - inflationPercent));
      return balance;
   }

   /**
      Gets the amount of prior savings.
      @return the savings amount
   */
   public double getSavings()
   {
      return savings;
   }

   /**
      Sets the amount of prior savings.
      @param x the savings amount
   */
   public void setSavings(double x)
   {
      savings = x;
   }

   /**
      Gets the annual contribution to the retirement account.
      @return the contribution amount
   */
   public double getContrib()
   {
      return contrib;
   }

   /**
      Sets the annual contribution to the retirement account.
      @param x the contribution amount
   */
   public void setContrib(double x)
   {
      contrib = x;
   }

   /**
      Gets the annual income.
      @return the income amount
   */
   public double getIncome()
   {
      return income;
   }

   /**
      Sets the annual income.
      @param x the income amount
   */
   public void setIncome(double x)
   {
      income = x;
   }

   /**
      Gets the current age.
      @return the age
   */
   public int getCurrentAge()
   {
      return currentAge;
   }

   /**
      Sets the current age.
      @param x the age
   */
   public void setCurrentAge(int x)
   {
      currentAge = x;
   }

   /**
      Gets the desired retirement age.
      @return the age
   */
   public int getRetireAge()
   {
      return retireAge;
   }

   /**
      Sets the desired retirement age.
      @param x the age
   */
   public void setRetireAge(int x)
   {
      retireAge = x;
   }

   /**
      Gets the expected age of death.
      @return the age
   */
   public int getDeathAge()
   {
      return deathAge;
   }

   /**
      Sets the expected age of death.
      @param x the age
   */
   public void setDeathAge(int x)
   {
      deathAge = x;
   }

   /**
      Gets the estimated percentage of inflation.
      @return the percentage
   */
   public double getInflationPercent()
   {
      return inflationPercent;
   }

   /**
      Sets the estimated percentage of inflation.
      @param x the percentage
   */
   public void setInflationPercent(double x)
   {
      inflationPercent = x;
   }

   /**
      Gets the estimated yield of the investment.
      @return the percentage
   */
   public double getInvestPercent()
   {
      return investPercent;
   }

   /**
      Sets the estimated yield of the investment.
      @param x the percentage
   */
   public void setInvestPercent(double x)
   {
      investPercent = x;
   }

   private double savings;
   private double contrib;
   private double income;
   private int currentAge;
   private int retireAge;
   private int deathAge;
   private double inflationPercent;
   private double investPercent;

   private int age;
   private double balance;
}

/**
   This panel draws a graph of the investment result.
*/
class RetireCanvas extends JPanel
{
   public RetireCanvas()
   {
      setSize(WIDTH, HEIGHT);
   }

   /**
      Sets the retirement information to be plotted.
      @param newInfo the new retirement info.
   */
   public void setInfo(RetireInfo newInfo)
   {
      info = newInfo;
      repaint();
   }

   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D)g;
      if (info == null) return;

      double minValue = 0;
      double maxValue = 0;
      int i;
      for (i = info.getCurrentAge();
           i <= info.getDeathAge(); i++)
      {
         double v = info.getBalance(i);
         if (minValue > v) minValue = v;
         if (maxValue < v) maxValue = v;
      }
      if (maxValue == minValue) return;

      int barWidth = getWidth() / (info.getDeathAge()
         - info.getCurrentAge() + 1);
      double scale = getHeight() / (maxValue - minValue);

      for (i = info.getCurrentAge();
           i <= info.getDeathAge(); i++)
      {
         int x1 = (i - info.getCurrentAge()) * barWidth + 1;
         int y1;
         double v = info.getBalance(i);
         int height;
         int yOrigin = (int)(maxValue * scale);

         if (v >= 0)
         {
            y1 = (int)((maxValue - v) * scale);
            height = yOrigin - y1;
         }
         else
         {
            y1 = yOrigin;
            height = (int)(-v * scale);
         }

         if (i < info.getRetireAge())
            g2.setColor(colorPre);
         else if (v >= 0)
            g2.setColor(colorGain);
         else
            g2.setColor(colorLoss);
         Rectangle2D bar = new Rectangle2D.Double(x1, y1,
            barWidth - 2, height);
         g2.fill(bar);
         g2.setColor(Color.black);
         g2.draw(bar);
      }
   }

   /**
      Sets the color to be used before retirement.
      @param color the desired color
   */
   public void setColorPre(Color color)
   {
      colorPre = color;
      repaint();
   }

   /**
      Sets the color to be used after retirement while
      the account balance is positive.
      @param color the desired color
   */
   public void setColorGain(Color color)
   {
      colorGain = color;
      repaint();
   }

   /**
      Sets the color to be used after retirement when
      the account balance is negative.
      @param color the desired color
   */
   public void setColorLoss(Color color)
   {
      colorLoss = color;
      repaint();
   }

   private RetireInfo info = null;

   private Color colorPre;
   private Color colorGain;
   private Color colorLoss;
   private static final int WIDTH = 400;
   private static final int HEIGHT = 200;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -