Analysis of a interview problem

xiaoxiao2021-04-01  227

This is a java language of my Java language I design ... um ... can also be said to be two: write a Money class, including two attributes of the amount and currency, and plus two methods. Additional questions: Implement the Money class in the topic as a valueObject.

Let's see this question first.

The first aspect to be examined is to select the correct data type for two attributes. (I don't know what is attribute? ... go back to change your professional career more promising.)

For the amount, the most natural choice is float or double. But this is not the best choice. In the computer, Float and Double are erroneous. For example, 1 may be 1.00000001. So in this important and sensitive thing for money, it is better to BigDecimal. Of course, it is more troublesome for later addition and subtraction operations. At this time, it is still a more memorandum of C operators. How is it with int? If you use int to delay the amount divided into unit, it is not possible. Such accuracy is guaranteed, and the operation is also convenient. But if you want to express 5% ... There is no way.

For currency, many people have naturally selected String. But I thought String is not as int, or short. Although in Java, String can almost use it as a basic data type, but String is an object, or often considering the issue of Null NULL, and string is always convenient and fast as int (or short).

In the era of JDK 1.5, we have a choice: Enum. However, if it is in an information system, the currency species can be maintained by the user, then the enum cannot be used.

In addition, is there a better design from an OO perspective? Have. However, I left this question to the next discussion.

Then it is the design of the method. Because there are currencies, therefore, add, it is natural to consider the amount of currency to add (temporarily regardless of the exchange rate conversion). What should I do if the currency is not the same? This is the second focus of the exam: error handling.

Many people use the return value to indicate the success of the calculation. It is quite weird that most of them have the experience of C, C programmers, I don't know if it is the result of school education. Some people use system.out.println output error messages, this is more outrageous.

Positive solution should be using Exception. If you are lazy, you can use IllegalarGumentexception. If you know yourself, you can define an Exception, such as INCOMPALCURRENCYEXCEPTION, if it is an interview, this person has basically cleared it.

So the prototype of two methods should be:

Public Void Add (Money Operand) Throws IncompatibleCurrenException Public Void Subtract (Money Operand) Throws IncOPaultCurrencyExcepiton

If you use BigDecimal to indicate the amount, there is a problem with the name of the BigDecimal's decrease. But this is not important. There is always an API when writing the program.

There are two options for NULL for parameters. One is the parameter null, nothing. The other is to throw an exception, such as NullPointerException. I think the second choice is good. In most cases, use NULL to do parameters to call Add and Subtract methods, I am afraid it is not the original meaning of programmers, but the results of other programs. Direct throwing an exception helps to find out the mistakes in other parts.

In addition to attributes and plus, reduced methods, constructor, and getter / setter functions, although not mentioned, as a complete class, it should still be. From the perspective of robustness, the input parameters should be determined in the constructor. For example, the amount cannot be NULL. Next is an additional question. What is ValueObject. Simply put, the value of valueObject (status) will not change once it is created. So it can be used as the basic data type. For example, String in Java, BigDecimal, Biginteger is ValueObject. StringBuffer is not.

To turn Money into valueObject, you can't have a setter. The constructor is the only way to set attribute values ​​from the outside of the class. Second, the add and subtract methods cannot modify the properties of this, and the results should be returned with the new Money object, so their prototype is to become:

Public Money Add (Money Operand) Throws IncompatibleCurrencyException Public Money Subtract (Money Operand) Throws IncOzaCurrencyExcepiton

Third, it is best to redefine two methods for Equals and HashCode. How to redefine, see the "Effective Java Programming Language Guide" 7, 8 of Pearson Education.

Unfortunately, it has not been able to reach the above-mentioned candidates so far. Never say that I am very unrealistic in my heart, I saw a hiring first wrote this line:

Public class mothertest extends testcase {

......

转载请注明原文地址:https://www.9cbs.com/read-131062.html

New Post(0)