Solved

need help quick

Posted on 2004-10-10
20
211 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now