Chitika

Monday, January 09, 2006

Reading a character from the Console

Reading a character from the Console should not be a problem. I hadn't had that problem using C++ back in the college days, but somehow it's not that easy in Java. I have never found the need for it myself, but someone at the local JUG mailing list apparently needs it.

Java has always have the problem of reading a character from the Console, without buffering and echoing back to the Console. This has been an outstanding bug for so long, and it's said to be fixed on Mustang. Well, the bug is really about masking password input, not about un-buffering the character inputs. So, my friend there is not really getting his needs fulfilled.

Someone from the mailing list replied with a link to JCurses. So I tried it. I tried Enigma as well, but it doesn't seem to get the job done.

Here's the code:
import jcurses.system.InputChar;
import jcurses.system.Toolkit;

public class Console {
  public static void main (String[] args) {
    while (true) {
      InputChar c = Toolkit.readCharacter ();
    System.out.println (
        "you typed '" + c.getCharacter() + "' (" + c.getCode() + ")");

      // break on Ctrl-C (3)
      if (c.getCode() == 3) break;
    }
  }
}

It's quick & simple. Part of another surprise, there are some other bugs that still remained till Tiger release. The list includes not being able to change working directory.

What's the most irritating bug of JDK that has not been fixed, according to you?
Does the voting mechanism for bug fixes really work?

Monday, January 02, 2006

Understanding Fluent Interface

After reading Martin Fowler's post regarding fluent interface, I try to write small trivial implementations to understand the concept. Here are the common interfaces of Customer, Order & OrderLine classes, as required by Martin's "makeNormal" method:

Customer
  +addOrder(Order): void

Order
  +addLine(OrderLine): void
  +setRush(boolean): void

OrderLine
  +OrderLine(int, Product)
  +setSkippable(boolean): void

It's quite standard for OO design. Looking at Martin's "makeFluent" method, we need the following interfaces, which I've added some trivial implementations to help understanding the concept:

Customer
+newOrder(): Order
{
  Order o = new Order ();
  this.addOrder (o);
  return o;
}

Order
+with(qty, productId): OrderLine
{
  Product p = Product.find (productId);
  OrderLine line = new OrderLine (qty, p);
  this.addLine (line);
  line.setOrder (this);
  return line;
}
+priorityRush(): Order
{
  this.setRush (true);
  return this;
}

OrderLine
+with(qty, productId): OrderLine
  //note: return the last order added, instead of this
{
  Product p = Product.find (productId);
  OrderLine line = new OrderLine (qty, p);
  this.order.addLine (line);
  line.setOrder (this.order);
  return line;
}
+setOrder(order): void
{
  this.order = order;
}
+skippable(): OrderLine
{
  this.setSkippable (true);
  return this;
}
+priorityRush(): Order
{
  this.order.setRush (true);
  return this.order;
}

Looking at the above sample implementation codes, we need to design additional interfaces to help the client usage be more fluent. Things I've noted:
1. Fluent API can only be applicable for highly used API, otherwise the investment of designing the API may not be as effective as intended. Thus, not all API can be made fluent.
2. Fluent API may be good for "feature-envy" code smells. Feature-envy is when a "client" method calls methods on another class three or more times.
3. Fluent API is suitable for frameworks, e.g. Hibernate, libraries, e.g. Jakarta Commons, or related OO classes (as in Martin's example). But, the timing for designing Fluent API for OO classes is still a big question. If we design it too early, there may be useless API (or the API is not fluent enough). If we design it too late, the ROI may not be as expected.

The pendulum is still swinging.. :D
I hope I can think this through the week, and let you guys know what I think..

Thursday, December 15, 2005

Getting a credit card

I've always been a no-credit guy. I don't like to ask for any loan for any purpose. That's my principle since I was a kid, perhaps parts of what my dad has taught me. Now, I've created and/or implemented more than 2 automotive loan application systems (one of them is a product of my employer), and since I know exactly the formula they use for the loan installment payments, it further discourages me from taking any loan.

I have neither a car nor a house. I plan to have one of both in the next 2-3 years, and I know I may need to take a loan for them. One way is to apply for a loan in the local bank using my current employment records & employer's recommendation as part of the loan application attachments. The loan itself may be able to cover only 30% of a car's price though.

From my experience in deploying the loan application system on multiple funding companies, I know also that a good credit history is required for a large amount of loan. Even though credit card is new & highly unpopular as part of Indonesian life style, it's getting its popularity among the urbans in the recent couple of years.

Now, after looking at this very nice post about how to increase your credit score, I know that I need to get at least a credit card, and use it! :P. I did get myself a credit card 4 years ago, which I planned on using them to buy books from Amazon. Yet, it's never been the case. I paid the annual fee, but never use the credit limit even for once.

For an American whose credit card history is quite long, since the 30's I believe (a few years back I've read the credit card history in the US through some book, I forget the book's title though), it's very important to get a good credit card history for applying a larger amount of loan. But, for an Asian countryman, is it already accustomed to have it?

Is it true that a decent hard-working employee can never pay himself a good house/car in cash? I find it's rather hard to accept, yet easier to believe.