?
Solved

need help quick

Posted on 2004-10-10
20
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1500 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 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