Current location: Hot Scripts Forums » Programming Languages » Everything Java » problem returning interest in bankaccount program


problem returning interest in bankaccount program

Reply
  #1 (permalink)  
Old 03-16-06, 02:39 AM
psi3000 psi3000 is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
problem returning interest in bankaccount program

Ok it keeps desplaying 0 for interest when it shouldnt be. Can anyone find any mistakes. Yes there might be some extra variables that arnt used, sorry.
import javax.swing.JOptionPane;
import javax.swing.Spring;

/*
* Created on Mar 13, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author Tricky
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class customer {

public static void main(String[] args)
{
String namei;
String soci;
String typei;
String input;
double inputi;
String addressi;
String daysi;
int interesti;
String monthi;
double inter;


Person cus = new Person();

namei =
JOptionPane.showInputDialog("What is" +
"the customers name?");
cus.setN(namei);

soci =
JOptionPane.showInputDialog("What is the customers"+
"social security number?");
cus.setSoc(soci);
typei =
JOptionPane.showInputDialog("What type of account is it?");
input =
JOptionPane.showInputDialog("What is the balance of the customer?");
double balance = Double.parseDouble(input);

addressi =
JOptionPane.showInputDialog("What is the address of the customer?");
cus.setAdd(addressi);
monthi =
JOptionPane.showInputDialog("What is the Month?");
int month = Integer.parseInt(monthi);
switch (month)
{

case 1:
cus.setRat(.6);
break;

case 2:
month = 31;
cus.setRat(.6);
break;

case 3:
month = 28;
cus.setRat(.6);
break;

case 4:
month = 31;
cus.setRat(.5);
break;

case 5:
month = 30;
cus.setRat(.5);
break;

case 6:
month = 31;
cus.setRat(.5);
break;

case 7:
month = 30;
cus.setRat(.4);
break;

case 8:
month = 31;
cus.setRat(.4);
break;

case 9:
month = 31;
cus.setRat(.4);
break;

case 10:
month = 30;
cus.setRat(.3);
break;

case 11:
month = 31;
cus.setRat(.3);
break;

case 12:
month = 30;
cus.setRat(.3);
break;
}
daysi =
JOptionPane.showInputDialog("What is the day of the month?");
double day = Double.parseDouble(daysi);
interesti = (int) ((cus.getB() * cus.getDa() * cus.getRat())/365);

JOptionPane.showMessageDialog(null, "Name: " + namei +
"\nsocial security #:" + soci + "\ntype:" + typei + "\nbalance:" +
input + "\naddress:" + addressi + "\ndate:" + daysi + "\ninteterst:" + interesti);


}
}

Here is the original class:




/*
* Created on Mar 11, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author Tricky
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Person {
// data fields
public String name;
public String socSecNum;
public String type;
public double balance;
public String address;
public String date;
public double interest;
private double total;
public double days;
public double rate;
public double month;




public Person(String n, String soc, String t,
double b, String add, String d, double in, double da, double mo,double rat){

name = n;
socSecNum = soc;
type = t;
balance = b;
address = add;
date = d;
interest = in;
days = da;
month = mo;
rate = rat;
}

/**
*
*/
public Person() {

// TODO Auto-generated constructor stub
;
}


//modifiers
public void setN(String n) {
name = n;
}
public void setSoc(String soc) {
socSecNum = soc;
}
public void setT(String t) {
type = t;
}
public void setB(double b) {
balance = b;
}
public void setAdd(String add) {
address = add;
}
public void setD(String d) {
date = d;
}
public void setDa (double da){
days = da;
}
public void setMo (double mo){
month = mo;
}
public void setRat (double rat){
rate = rat;
}
public void setIntr (double intr){
interest = intr;
}
//accessors
public String getN(){
return name;
}
public String getSoc(){
return socSecNum;
}
public String getT(){
return type;
}
public double getB(){
return balance;
}
public String getAdd(){
return address;
}
public String getD(){
return date;
}
public double getDa(){
return days;
}
public double getMo(){
return month;
}
public double getInterest()
{
return (balance * days * rate)/365;
}
public double getRat(){
return rate;
}
//postcondition:Returns the object's state as a string
public String ToString(){
return"name: "+name+", social security " + socSecNum +
", type: " + type + "\naddress: " + address +
", balance: " + balance + ", da1te: " + date + "interest" + interest;
}}
Reply With Quote
  #2 (permalink)  
Old 03-16-06, 10:18 AM
stdunbar stdunbar is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Superior, CO, USA
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
It appears from your code that you're using an IDE to help generate this code. Why not learn how to use the debugging features of the program?

Additionally, it's no wonder you're confused. Do you have to pay extra for every key stroke? I'd flunk you in a heart beat for having the "setRate" method called "setRat" - I know that there is an "e" on your keyboard. And having "setD" for the date and "setDa" for the day! Is it really that hard to type "setRate", "setDate" and "setDay"? That is a total of 7 more characters.

You're going to get much better help and improve your own efficiency if you work on your code style some. As it is most people would have a tough time digging through the code.

Lastly, do everyone a favor and wrap your code in [ code ] and [ /code ] (but take out the spaces). That way your code will look like:

Code:
    namei = JOptionPane.showInputDialog("What is the customers name?");
    cus.setN(namei);    // type the three characters!
__________________
Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums.
Sure they're new - come get them started!
Reply With Quote
  #3 (permalink)  
Old 03-16-06, 12:38 PM
stdunbar stdunbar is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Superior, CO, USA
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, so I've been challanged to find the issue. I've pulled the code into Eclipse and redone it a little. But first the problem.

In the code:

Code:
interesti = (int) ((cus.getB() * cus.getDa() * cus.getRat())/365);
you are getting the interest. However, if you look further up the code:

Code:
JOptionPane.showInputDialog("What is the balance of the customer?");
double balance = Double.parseDouble(input);
you'll notice that the balance is never set with a "setB". Therefore, the balance is always zero.

You are trying to calculate the interest in two different ways. The first is in the customer class and the second is in the Person class. I'd just use the Person class one and change the first code set to:

Code:
interesti = (int)cus.getInterest();
Another question you asked was related to variables vs. methods. Your current code:

Code:
public void setN(String n) {
    name = n;
}
public String getN(){
    return name;
}
can be one of two ways:

Code:
public void setName(String n) {
    name = n;
}
public String getName(){
    return name;
}
or
Code:
public void setName(String name) {
    this.name = name;
}
// getter is the same
In this way you have used the built in variable "this" to eliminate any confusion as to which "name" you mean in the setter. Some people swear by the second method but I feel that it is a matter of preference.
__________________
Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums.
Sure they're new - come get them started!
Reply With Quote
  #4 (permalink)  
Old 03-17-06, 09:53 PM
psi3000 psi3000 is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
How would I setB since B is a double it says to me that it can not be used with a arg string. Im sure you know how to do this. Your responce and solution make total sence.
thanks
Reply With Quote
  #5 (permalink)  
Old 03-20-06, 10:32 AM
stdunbar stdunbar is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Superior, CO, USA
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry, I'm not following you. In your current code:

Code:
JOptionPane.showInputDialog("What is the balance of the customer?");
double balance = Double.parseDouble(input);
// add the setB here
cus.setB( balance );
setB takes a double which is what you have. Where are you getting an error?
__________________
Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums.
Sure they're new - come get them started!
Reply With Quote
  #6 (permalink)  
Old 03-27-06, 02:03 AM
psi3000 psi3000 is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Ok I have figured out what I had to do to set the balance. Now my switch is having problems. It says "type mismached: can not convert to string to char" Here is my code agian for the main:
public class Person {
// data fields
public String name;
public String socSecNum;
public String type;
public double balance;
public String address;
public double date;
public double interest;
private double total;
public double days;
public double rate;
public double month;




public Person(String n, String soc, String t,
double b, String add, double d, double in, double da, double mo,double rat){

name = n;
socSecNum = soc;
type = t;
balance = b;
address = add;
date = d;
interest = in;
days = da;
month = mo;
rate = rat;
}

public Person() {


;
}


public void setN(String n) {
name = n;
}
public void setSoc(String soc) {
socSecNum = soc;
}
public void setT(String t) {
type = t;
}
public void setB(double b) {
balance = b;
}
public void setAdd(String add) {
address = add;
}
public void setD(double d) {
date = d;
}
public void setDa (double da){
days = da;
}
public void setMo (double mo){
month = mo;
}
public void setRat (double rat){
rate = rat;
}
public void setIntr (double intr){
interest = intr;
}

public String getN(){
return name;
}
public String getSoc(){
return socSecNum;
}
public String getT(){
return type;
}
public double getB(){
return balance;
}
public String getAdd(){
return address;
}
public double getD(){
return days + month;
}
public double getDa(){
return days;
}
public double getMo(){
return month;
}
public double getInterest()
{
return (balance * rate *date )/365;
}
public double getRat(){
return rate;
}

public String ToString(){
return"name: "+name+", social security " + socSecNum +
", type: " + type + "\naddress: " + address +
", balance: " + balance + ", da1te: " + date + "interest" + interest;
}}



Here is the test class:


import javax.swing.JOptionPane;
import javax.swing.Spring;


public class customer {

public static void main(String[] args)
{
String namei;
String soci;
String typei;
String input;
double inputi;
String addressi;
String daysi;
int interesti;
String monthi;
double inter;
char test;


Person cus = new Person();
{

namei =
JOptionPane.showInputDialog("What is" +
"the customers name?");
cus.setN(namei);

soci =
JOptionPane.showInputDialog("What is the customers"+
"social security number?");
cus.setSoc(soci);
typei =
JOptionPane.showInputDialog("What type of account is it?");
input =
JOptionPane.showInputDialog("What is the balance of the customer?");
cus.setB(Double.parseDouble(input));






addressi =
JOptionPane.showInputDialog("What is the address of the customer?");
cus.setAdd(addressi);
monthi =
JOptionPane.showInputDialog("What is the Month?");
cus.setD (Integer.parseInt(monthi));
test = monthi;
switch (test)
{

case 1:
cus.setRat(.6);
break;

case 2:
cus.month = 31;
cus.setRat(.6);
break;

case 3:
cus.month = 59;
cus.setRat(.6);
break;

case 4:
cus.month = 90;
cus.setRat(.5);
break;

case 5:
cus.month = 120;
cus.setRat(.5);
break;

case 6:
cus.month = 151;
cus.setRat(.5);
break;

case 7:
cus.month = 181;
cus.setRat(.4);
break;

case 8:
cus.month = 212;
cus.setRat(.4);
break;

case 9:
cus.month = 243;
cus.setRat(.4);
break;

case 10:
cus.month = 273;
cus.setRat(.3);
break;

case 11:
cus.month = 304;
cus.setRat(.3);
break;

case 12:
cus.month = 334;
cus.setRat(.3);
break;
}
daysi =
JOptionPane.showInputDialog("What is the day of the month?");
double da = Double.parseDouble(daysi);
interesti = (int)cus.getInterest();

JOptionPane.showMessageDialog(null, "Name: " + namei +
"\nsocial security #:" + soci + "\ntype:" + typei + "\nbalance:" +
input + "\naddress:" + addressi + "\ndate:" + daysi + "\ninteterst:" + interesti);

}
}
}


the "monthi" part is underlined red in eclips after the "test =" part. If anyone could assist me in this matter I would be very greatful.
Reply With Quote
  #7 (permalink)  
Old 03-27-06, 01:14 PM
stdunbar stdunbar is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Superior, CO, USA
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
What are you trying to do with this chunk of code?

Code:
monthi = JOptionPane.showInputDialog("What is the Month?");
cus.setD (Integer.parseInt(monthi));
test = monthi;
monthi is a String and test is a character. If you want the first character in the String then you'll want something like:

Code:
monthi =JOptionPane.showInputDialog("What is the Month?");
cus.setD (Integer.parseInt(monthi));
test = monthi.charAt(0);
That should allow the case statement to work - sorta. You'll want to update the case statement to switch on characters not an integer. So you'll want to change it to:

Code:
case '1':
    cus.setRat(.6);
    break;

case '2':
    cus.month = 31;
    cus.setRat(.6);
    break;
The other option is to convert the String into an integer. You can do that with:

Code:
int test;    // changed from char
...
monthi = JOptionPane.showInputDialog("What is the Month?");
cus.setD (Integer.parseInt(monthi));
test = Integer.parseInt(monthi);
Then your switch statement will be as you have it now.
__________________
Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums.
Sure they're new - come get them started!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BankAccount object oreinted help psi3000 Everything Java 6 03-16-06 10:07 AM
Count problem kasic ASP.NET 1 10-20-04 12:23 AM
Upload Script Problem!!! seanknighton Perl 0 03-21-04 09:54 PM


All times are GMT -5. The time now is 05:36 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.