Money.java
上传用户:ljt780218
上传日期:2022-07-30
资源大小:110k
文件大小:2k
源码类别:

金融证券系统

开发平台:

Java

  1. //<html><head><title>ATM Simulation - Implementation of a Representation for Money</title></head><body><h2>ATM Simulation - Implementation of a Representation for Money</h2><pre>
  2. /*
  3.  * Example ATM simulation - file Money.java
  4.  *
  5.  * This file implements the class used to represent money,
  6.  *
  7.  * Copyright (c) 1997 - Russell C. Bjork
  8.  *
  9.  */
  10. package atm.util;
  11. public class Money
  12.   {
  13.     public Money()
  14.       { _cents = 0;
  15.       }
  16.     
  17.     public Money(int dollars)
  18.       { _cents = 100L * dollars;
  19.       }
  20.     
  21.     public Money(int dollars, long cents)
  22.       { _cents = 100L * dollars + cents;
  23.       }
  24.     
  25.     public void set(Money value)
  26.       { _cents = value._cents;
  27.       }
  28.       
  29.     static public Money add(Money first, Money second)
  30.       { return new Money(0, first._cents + second._cents); 
  31.       }
  32.     
  33.     static public Money subtract(Money minuend, Money subtrahend)
  34.       { return new Money(0, minuend._cents - subtrahend._cents); }
  35.     
  36.     public Money add(Money other)
  37.       { _cents += other._cents; 
  38.         return this;
  39.       }
  40.     
  41.     public Money subtract(Money other)
  42.       { _cents -= other._cents; 
  43.         return this;
  44.       }
  45.     
  46.     public int dollars()
  47.       { return (int) _cents / 100; 
  48.       }
  49.     
  50.     public int cents()
  51.       { return (int) _cents % 100;
  52.       }
  53.     
  54.     public boolean equals(Money other)
  55.       { return _cents == other._cents;
  56.       }
  57.     
  58.     public boolean less(Money other)
  59.       { return _cents < other._cents;
  60.       }
  61.     // Instance variable
  62.     
  63.     private long _cents;
  64.   }
  65. //</pre></body></html>