Money.java
资源名称:ATM.zip [点击查看]
上传用户:ljt780218
上传日期:2022-07-30
资源大小:110k
文件大小:2k
源码类别:
金融证券系统
开发平台:
Java
- //<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>
- /*
- * Example ATM simulation - file Money.java
- *
- * This file implements the class used to represent money,
- *
- * Copyright (c) 1997 - Russell C. Bjork
- *
- */
- package atm.util;
- public class Money
- {
- public Money()
- { _cents = 0;
- }
- public Money(int dollars)
- { _cents = 100L * dollars;
- }
- public Money(int dollars, long cents)
- { _cents = 100L * dollars + cents;
- }
- public void set(Money value)
- { _cents = value._cents;
- }
- static public Money add(Money first, Money second)
- { return new Money(0, first._cents + second._cents);
- }
- static public Money subtract(Money minuend, Money subtrahend)
- { return new Money(0, minuend._cents - subtrahend._cents); }
- public Money add(Money other)
- { _cents += other._cents;
- return this;
- }
- public Money subtract(Money other)
- { _cents -= other._cents;
- return this;
- }
- public int dollars()
- { return (int) _cents / 100;
- }
- public int cents()
- { return (int) _cents % 100;
- }
- public boolean equals(Money other)
- { return _cents == other._cents;
- }
- public boolean less(Money other)
- { return _cents < other._cents;
- }
- // Instance variable
- private long _cents;
- }
- //</pre></body></html>