Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

need help quick

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
DreamWeaver777
Asked:
DreamWeaver777
  • 10
  • 10
1 Solution
 
DreamWeaver777Author Commented:
 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
 
DreamWeaver777Author Commented:
the GUI is above
0
 
objectsCommented:
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!

 
DreamWeaver777Author Commented:
when I insert my model into the GUI i get errors and dosen't compile
0
 
objectsCommented:
>      theModel.setCheckAmount(inValue);
>      outputArea.setText(theModel.getMoneyWords());

Instead of that you should be using something like:

      outputArea.setText(theModel.getMoneyWords((int)inValue));
0
 
DreamWeaver777Author Commented:
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
 
objectsCommented:
so what problem exactly are you having with adding the methods?
0
 
objectsCommented:
and why can't u change your gui?
0
 
DreamWeaver777Author Commented:
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
 
objectsCommented:
> i guess i need to change something in my model to the method getCheckAmount

no you need to add new methods
0
 
DreamWeaver777Author Commented:
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
 
objectsCommented:
what getCheckAmount() are you talking about, I don't see anywhere that your gui calls that.
0
 
DreamWeaver777Author Commented:
oops i mean setCheckAmount()
0
 
objectsCommented:
it just needs to store the amount by the looks of it.
0
 
DreamWeaver777Author Commented:
what amount does it need to store i little confused by it
0
 
objectsCommented:
the value that is passed to it, so that when the new method getMoneyWords() is called it knows what value to convert.
0
 
DreamWeaver777Author Commented:
so how would u store the amount into the method
0
 
objectsCommented:
add a member variable to the class.
0
 
DreamWeaver777Author Commented:
ok i appreciate it man
0
 
objectsCommented:
no worries :)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 10
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now