Solved

need help quick

Posted on 2004-10-10
20
216 Views
Last Modified: 2012-05-05
Hey guys I need some serious help on a program.  I was wondering if anybody could help me convert some code for converting numbers to english text in to some methods that will work in a GUI.  Any help will be greatly appreciated!  Here is my code I have:

class MoneyModel {
 
  private static final String[] mNames = {
    "",
    " Thousand",
    };

  private static final String[] tNames = {
    "",
    " Ten",
    " Twenty",
    " Thirty",
    " Fourty",
    " Fifty",
    " Sixty",
    " Seventy",
    " Eighty",
    " Ninety"
    };

  private static final String[] nNames = {
    "",
    " One",
    " Two",
    " Three",
    " Four",
    " Five",
    " Six",
    " Seven",
    " Eight",
    " Nine",
    " Ten",
    " Eleven",
    " Twelve",
    " Thirteen",
    " Fourteen",
    " Fifteen",
    " Sixteen",
    " Seventeen",
    " Eighteen",
    " Nineteen"
    };

 private String convertLessThanOneThousand(int number) {
    String soFar;

    if (number % 100 < 20){
        soFar = nNames[number % 100];
        number /= 100;
       }
    else {
        soFar = nNames[number % 10];
        number /= 10;

        soFar = tNames[number % 10] + soFar;
        number /= 10;
       }
    if (number == 0) return soFar;
    return nNames[number] + " Hundred" + soFar;
}

public String getMoneyWords(int number) {
    /* special case */
    if (number == 0) { return "zero"; }

    String prefix = "";

    if (number < 0) {
        number = -number;
        prefix = "";
      }

    String soFar = "";
    int place = 0;

    do {
      int n = number % 1000;
      if (n != 0){
         String s = convertLessThanOneThousand(n);
         soFar = s + mNames[place] + soFar;
        }
      place++;
      number /= 1000;
      } while (number > 0);

    return (prefix + soFar).trim();
}

public static void main(String[] args) {
    MoneyModel f = new MoneyModel();
    System.out.println("*** " + f.getCheckWords(0));
    System.out.println("*** " + f.getCheckWords(1));
    System.out.println("*** " + f.getCheckWords(16));
    System.out.println("*** " + f.getCheckWords(100));
    System.out.println("*** " + f.getCheckWords(118));
    System.out.println("*** " + f.getCheckWords(200));
    System.out.println("*** " + f.getCheckWords(219));
    System.out.println("*** " + f.getCheckWords(800));
    System.out.println("*** " + f.getCheckWords(801));
    System.out.println("*** " + f.getCheckWords(999999));
    System.out.println("*** " + f.getCheckWords(-45));
    /*
    *** Zero
    *** One
    *** Sixteen
    *** One hundred
    *** One hundred eighteen
    *** Two hundred
    *** Two hundred nineteen
    *** Eight hundred
    *** Eight hundred one
    *** Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
    *** Fourty Five
    */
}
}


Here is my methods that I need to store information into:

public MoneyModel()  for null constructor, not sure how to do this

public double getMoneyAmount()

public void setMoneyAmount(double)

public String getMoneyWords()   (return value into english text)
0
Comment
Question by:DreamWeaver777
  • 10
  • 10
20 Comments
 

Author Comment

by:DreamWeaver777
ID: 12273493
 private MoneyModel theModel;

  /**
   * JButton to let the stuff go
   */
   private JButton goButton;

  /**
   * JTextArea to get the input from the user
   */
  private JTextArea inputArea;

  /**
   * JTextArea to show the output to the user
   */
  private JTextArea outputArea;

  /**
   * Default constructor to setup the window
   * @since 1.0
   */
  public MoneyGUI() {
    //For the application setup the window
    if (this instanceof JFrame) {
      setSize(500,200);
      setLocation(40,40);
      setTitle("Check Writer");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    //Initialize instance variables
    theModel = new MoneyModel();
    goButton = new JButton("Go");
    goButton.addActionListener(this);
    inputArea = new JTextArea(1,10);
    outputArea = new JTextArea(3,40);
    outputArea.setEditable(false);
    outputArea.setLineWrap(true);
    outputArea.setWrapStyleWord(true);

    //Setup top panel
    JPanel topPanel = new JPanel();
    JLabel numLabel = new JLabel("Number to translate: ");
    topPanel.add(numLabel);
    topPanel.add(inputArea);    

    //Setup center panel
    JPanel centerPanel = new JPanel();
    JLabel outLabel = new JLabel("Number as text: ");
    centerPanel.add(outLabel);
    centerPanel.add(outputArea);
   
    this.getContentPane().add(topPanel,BorderLayout.NORTH);
    this.getContentPane().add(centerPanel,BorderLayout.CENTER);
    this.getContentPane().add(goButton,BorderLayout.SOUTH);
  }

  public void actionPerformed (ActionEvent e) {
    try {
      String inText = inputArea.getText();
      double inValue = Double.parseDouble(inText);
      theModel.setCheckAmount(inValue);
      outputArea.setText(theModel.getMoneyWords());
    }
    catch (NumberFormatException except) {
      outputArea.setText("You have input a non-numeric value");
      inputArea.setText("");
    }
  }

  /**
   * Main method to get stuff started
   * @since 1.0
   */
  public static void main (String args[]) {
    MoneyGUI myGUI = new MoneyGUI();
    myGUI.show();
  }
}
0
 

Author Comment

by:DreamWeaver777
ID: 12273496
the GUI is above
0
 
LVL 92

Expert Comment

by:objects
ID: 12273499
Doesn't look like any conversion is necessary, why can't you just use the class as is in your gui?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DreamWeaver777
ID: 12273522
when I insert my model into the GUI i get errors and dosen't compile
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12273541
>      theModel.setCheckAmount(inValue);
>      outputArea.setText(theModel.getMoneyWords());

Instead of that you should be using something like:

      outputArea.setText(theModel.getMoneyWords((int)inValue));
0
 

Author Comment

by:DreamWeaver777
ID: 12273556
well i have to leave the GUI as it is, what can I change in my code for the ((int))inValue)) in the GUI
0
 
LVL 92

Expert Comment

by:objects
ID: 12273565
so what problem exactly are you having with adding the methods?
0
 
LVL 92

Expert Comment

by:objects
ID: 12273569
and why can't u change your gui?
0
 

Author Comment

by:DreamWeaver777
ID: 12273595
i guess i need to change something in my model to the method getCheckAmount but i not sure what to change in the model, well the GUI was provided for me and can't be changed
0
 
LVL 92

Expert Comment

by:objects
ID: 12273604
> i guess i need to change something in my model to the method getCheckAmount

no you need to add new methods
0
 

Author Comment

by:DreamWeaver777
ID: 12273629
what would I need to put in the getCheckAmount method so I get the same results that I am getting in my model now, everything is coming out fine in the terminal window for my print line in my model now, by the way thanks for consulting with me i appreciate it.
0
 
LVL 92

Expert Comment

by:objects
ID: 12273637
what getCheckAmount() are you talking about, I don't see anywhere that your gui calls that.
0
 

Author Comment

by:DreamWeaver777
ID: 12273640
oops i mean setCheckAmount()
0
 
LVL 92

Expert Comment

by:objects
ID: 12273647
it just needs to store the amount by the looks of it.
0
 

Author Comment

by:DreamWeaver777
ID: 12273673
what amount does it need to store i little confused by it
0
 
LVL 92

Expert Comment

by:objects
ID: 12273689
the value that is passed to it, so that when the new method getMoneyWords() is called it knows what value to convert.
0
 

Author Comment

by:DreamWeaver777
ID: 12273712
so how would u store the amount into the method
0
 
LVL 92

Expert Comment

by:objects
ID: 12273725
add a member variable to the class.
0
 

Author Comment

by:DreamWeaver777
ID: 12273741
ok i appreciate it man
0
 
LVL 92

Expert Comment

by:objects
ID: 12273782
no worries :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tomcat: Unable to run tomcat service. 2 30
Bot application - advice 3 64
web project error add remove 1 55
Github api to fetch the number of collaborators in a git repo 1 31
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question