[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Bank Account Java program using inheritance and polymorphism

Posted on 2006-04-18
11
Medium Priority
?
4,624 Views
Last Modified: 2013-11-25
I have a second part to a java program for a assignment that I dont quite get.:
her is the first part of it that I did (there are extra variables and stuff I know):
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 daysa;
      public double rate;
      public double month;
      public double montha;
      public double datea;
      public double checking;
      
      
      
      
      public Person(String n, String soc, String t,
                  double b, String add, double d, double in, double da, double mo,double rat, double moa,
                  double daa, double daya, double check){
            
            name = n;
            socSecNum = soc;
            type = t;
            balance = b;
            address = add;
            date = d;
            interest = in;
            days = da;
            daysa = daya;
            month = mo;
            rate = rat;
            montha = moa;
            datea = daa;
            checking = check;
      }


      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 setDaa(double daa){
            datea = daa;
      }
      public void setDa (double da){
            days = da;
      }
      public void setDaya (double daya){
            daysa = daya;
      }
      public void setMo (double mo){
            month = mo;
      }
      public void setRat (double rat){
            rate = rat;
      }
      public void setIntr (double intr){
            interest = intr;
      }
      public void setMoa (double moa){
            montha = moa;
      }
      public void setCheck (double check){
            checking = check;
      }
      public String getN(){
            return name;
      }
      public String getSoc(){
            return socSecNum;
      }
      public String getT(){
            return type;
      }
      public double getB(){
            return balance - checking;
      }
      public String getAdd(){
            return address;
      }
      public double getD(){
            return days + month;
      }
      public double getDaa(){
            return (montha + daysa)- (month + days) ;
      }
      public double getDa(){
            return days;
      }
      public double getDaya(){
            return daysa;
      }

      public double getMo(){
            return month;
      }
      public double getMoa(){
            return montha;
      }
      public double getCheck(){
            return checking;
      }
      public double getInterest()
      {
                  return (balance * rate * datea )/365;
      }
      public double getRat(){
            return rate;
      }

      


      }




TEST CLASS:


import javax.swing.JOptionPane;
import java.swing.Scanner;




public class customer {

      public static void main(String[] args)
      {
                  String namei;
                  String soci;
                  String typei;
                  String input;
                  double inputi;
                  String inputii;
                  String addressi;
                  String daysi;
                  String daysa;
                  double interesti;
                  String monthi;
                  double inter;
                  int test;
                  int testi;
                  String monthb;
                  char repeat;
                  
                        
                  
                  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);
            double typeii;
            typei =
                  JOptionPane.showInputDialog("What type of account is it, 1 for checking 2 for savings?");
            typeii = Double.parseDouble(typei);
            
            if (typeii == 1)
                  cus.setCheck (-6);
            inputii =
                  JOptionPane.showInputDialog("How old is the customer?");
            cus.nowBday(Integer.parseInt(inputii));
            input =
                  JOptionPane.showInputDialog("What is the balance of the customer?");
            cus.setB(Integer.parseInt(input));
      if (input > 4999 && inputi > 54)
            cus.setCheck (0);
            cus.setRat(++.01);
            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 = Integer.parseInt(monthi);
            switch (test)
            {

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

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

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

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

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

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

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

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

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

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

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

            case 12:
            cus.month = 334;
            cus.setRat(.03);
            break;
            }
            daysi =
                  JOptionPane.showInputDialog("What is the day of the month?");
            double da = Double.parseDouble(daysi);
            
            monthb = JOptionPane.showInputDialog("What is the Month now?");
            cus.setDaa (Integer.parseInt(monthb));
            testi = Integer.parseInt(monthb);
            switch (testi)
            {

            case 1:
            cus.montha = 0;
            break;

            case 2:
            cus.montha = 31;
            
            break;

            case 3:
            cus.montha = 59;
      
            break;

            case 4:
            cus.montha = 90;
      
            break;

            case 5:
            cus.montha = 120;
      
            break;

            case 6:
            cus.montha = 151;
      
            break;

            case 7:
            cus.montha = 181;
            
            break;

            case 8:
            cus.montha = 212;
            
            break;

            case 9:
            cus.montha = 243;
      
            break;

            case 10:
            cus.montha = 273;
            
            break;

            case 11:
            cus.montha = 304;
            
            break;

            case 12:
            cus.montha = 334;
            break;
                  
            default:
            JOptionPane.showInputDialog("You did not enter a valid number");
            
            break;
            }
            daysa =
                  JOptionPane.showInputDialog("What is the day of the month now?");
            double daya = Double.parseDouble(daysa);
            interesti = (int)cus.getInterest();
            
            JOptionPane.showMessageDialog(null, "Name: " + namei +
                        "\nsocial security #:" + soci + "\ntype:" + typei + "\nbalance:" +
                        input + "\naddress:" + addressi + "\ndate:" + daysi + "\ninteterst:" + interesti);
            System.out.println("Would you like to calculate another customers interest?");
            System.out.println("Enter Y for yes or N for no:");
            input = keyboard.nextLine();
            repeat = input.charAt(0);
      
            }while (repeat == 'y' || repeat == 'Y');
            
                  }
      }

Now I have to have a bank account for senior citizens.  To qualify for this senior citizen free checking account you must be 55 years or older and have a balance of 5000 or over.  As you can see I have started to impliment my third class in the test class.  Balically I need a third class that extends the other class for the senior citizens acconts who get no charge for thier checkbook and a increase of 1% in interest.  can somone help me implement this using inheritance and polymorphism?  I really need help with this have been fooling with it all weekend.


0
Comment
Question by:psi3000
  • 4
  • 3
  • 2
9 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 500 total points
ID: 16476308
Something along the lines of this perhaps:

public class Senior extends Person
{
   
    //  I N S I S T   T H A T   P A R A M E T E R S   A R E   P A S S E D   A T   I N S T A N T I A T I O N
   
    public Senior(String n, String soc, String t,
                       double b, String add, double d, double in, double da, double mo,double rat, double moa,
                       double daa, double daya, double check){
       
        // do your checks here;
        // if b < 5000 then spit up an exception
        // and if the date info you're given is to do with date of birth, then check that they're of age here also.
       
        name = n;
        socSecNum = soc;
        type = t;
        balance = b;
        address = add;
        date = d;
        interest = in;
        days = da;
        daysa = daya;
        month = mo;
        rate = rat;
        montha = moa;
        datea = daa;
        checking = check;
    }
   
}



Now, this instance demonstrate Polymorphism through inheritance.

An alternative method (which could be considered 'better'), would be to use Polymorphis through Abstraction, instead.

To do this, you would define either an abstract class, or an interface - with which you define the common behaviour with which a 'Person' account should have.

You can then create other classes based on this interface (or abstract class) - which would perhaps be a 'neater' approach to things..


Just a thought.
0
 

Author Comment

by:psi3000
ID: 16478846
ok I see how you are checking to see if they qualify for senior class but how do I set the intrest rate plus 1% or did I do it right?  And how do I make it a free checking book instead of subtracting 6 or did I do this right also?  The class i am writing this program for is a level ahead of what I should have taken, so I am very confused.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16479169
Yer, there's nothing wrong with your approach; but it doesn't seem to use any form of Polymorphism, which I suspect is what you're expected to demonstrate.


You should really be extending or implementing something here..
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:psi3000
ID: 16479965
ok, now that is the part I am confused on.  How would I set the interest rate and make the checkbook free using the polymorphism approch?
0
 
LVL 1

Expert Comment

by:abalonia
ID: 16482774
The Senior class can automatically set check to zero inside the constructor. To set the interest rate, Senior can override the setRate method to automatically add 1%.

public class Senior extends Person {
public class Senior (String n, String soc, String t,
                       double b, String add, double d, double in, double da, double mo,double rat, double moa,
                       double daa, double daya) {
  // This calls Person's constructor. check is set to 0. Add 1% to interest rate.
  super(n, soc, t, b, add, d, in, da, mo, rat + 0.01, moa, daa, daya, 0);
}

public void setRat (double rat){
          rate = rat;
     }

// Check is always 0 for seniors, so don't let the user change it.
public void setCheck(double c) {
   check = 0;
}
}

In the test class you should be able to use polymorphism when you initialize the Person object.
  Person cus;
  cus = new Senior(n, soc, t, b, add, d, in, da, mo, rat, moa, daa, daya);
You probably shouldn't initialize cus until after you know whether you need to create a regular Person or a Senior.
0
 

Author Comment

by:psi3000
ID: 16495207
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 daysa;
      public double rate;
      public double month;
      public double montha;
      public double datea;
      public double checking;
      public double birthday;
      
      
      
      
      public Person(String n, String soc, String t,
                  double b, String add, double d, double in, double da, double mo,double rat, double moa,
                  double daa, double daya, double check, double bday){
            
            name = n;
            socSecNum = soc;
            type = t;
            balance = b;
            address = add;
            date = d;
            interest = in;
            days = da;
            daysa = daya;
            month = mo;
            rate = rat;
            montha = moa;
            datea = daa;
            checking = check;
            birthday = bday;
      }


      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 setDaa(double daa){
            datea = daa;
      }
      public void setDa (double da){
            days = da;
      }
      public void setDaya (double daya){
            daysa = daya;
      }
      public void setMo (double mo){
            month = mo;
      }
      public void setRat (double rat){
            rate = rat;
      }
      public void setIntr (double intr){
            interest = intr;
      }
      public void setMoa (double moa){
            montha = moa;
      }
      public void setCheck (double check){
            checking = check;
      }
      public void setBday (double bday){
            birthday = bday;
      }
      public String getN(){
            return name;
      }
      public String getSoc(){
            return socSecNum;
      }
      public String getT(){
            return type;
      }
      public double getB(){
            return balance - checking;
      }
      public String getAdd(){
            return address;
      }
      public double getD(){
            return days + month;
      }
      public double getDaa(){
            return (montha + daysa)- (month + days) ;
      }
      public double getDa(){
            return days;
      }
      public double getDaya(){
            return daysa;
      }

      public double getMo(){
            return month;
      }
      public double getMoa(){
            return montha;
      }
      public double getCheck(){
            return checking;
      }
      public double getInterest()
      {
                  return (balance * rate * datea )/365;
      }
      public double getRat(){
            return rate;
      }

      


      }


public class now extends Person {
      public double birthday;
      public now (String n, String soc, String t,
                             double b, String add, double d, double in, double da, double mo,double rat, double moa,
                             double daa, double daya, double bday) {
                  
        // This calls Person's constructor. check is set to 0. Add 1% to interest rate.
        super(n, soc, t, b, add, d, in, da, mo, rat + 0.01, moa, daa, daya, 0, bday);

      }

      public void setRat (double rat){
                rate = rat;
           }


      public void setCheck(double c) {
       c = 0;           //sets checkbook charge to 0
      }
      }
import javax.swing.JOptionPane;
import java.swing.Scanner;




public class customer {

      public static void main(String[] args)
      {
                  String namei;
                  String soci;
                  String typei;
                  String input;
                  double inputi;
                  String inputii;
                  String addressi;
                  String daysi;
                  String daysa;
                  double interesti;
                  String monthi;
                  double inter;
                  int test;
                  int testi;
                  String monthb;
                  char repeat;
                  
                  
                  
                  
                  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);
            double typeii;
            typei =
                  JOptionPane.showInputDialog("What type of account is it, 1 for checking 2 for savings?");
            typeii = Double.parseDouble(typei);
            
            if (typeii == 1)
                  cus.setCheck (-6);
            inputii =
                  JOptionPane.showInputDialog("How old is the customer?");
            cus.setBday(Integer.parseInt(inputii));
            input =
                  JOptionPane.showInputDialog("What is the balance of the customer?");
            cus.setB(Integer.parseInt(input));
      if (input >= 5000 && inputii >= 55)
            


            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 = Integer.parseInt(monthi);
            switch (test)
            {

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

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

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

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

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

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

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

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

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

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

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

            case 12:
            cus.month = 334;
            cus.setRat(.03);
            break;
            }
            daysi =
                  JOptionPane.showInputDialog("What is the day of the month?");
            double da = Double.parseDouble(daysi);
            
            monthb = JOptionPane.showInputDialog("What is the Month now?");
            cus.setDaa (Integer.parseInt(monthb));
            testi = Integer.parseInt(monthb);
            switch (testi)
            {

            case 1:
            cus.montha = 0;
            break;

            case 2:
            cus.montha = 31;
            
            break;

            case 3:
            cus.montha = 59;
      
            break;

            case 4:
            cus.montha = 90;
      
            break;

            case 5:
            cus.montha = 120;
      
            break;

            case 6:
            cus.montha = 151;
      
            break;

            case 7:
            cus.montha = 181;
            
            break;

            case 8:
            cus.montha = 212;
            
            break;

            case 9:
            cus.montha = 243;
      
            break;

            case 10:
            cus.montha = 273;
            
            break;

            case 11:
            cus.montha = 304;
            
            break;

            case 12:
            cus.montha = 334;
            break;
                  
            default:
            JOptionPane.showInputDialog("You did not enter a valid number");
            
            break;
            }
            daysa =
                  JOptionPane.showInputDialog("What is the day of the month now?");
            double daya = Double.parseDouble(daysa);
            interesti = (int)cus.getInterest();
            
            JOptionPane.showMessageDialog(null, "Name: " + namei +
                        "\nsocial security #:" + soci + "\ntype:" + typei + "\nbalance:" +
                        input + "\naddress:" + addressi + "\ndate:" + daysi + "\ninteterst:" + interesti);
            System.out.println("Would you like to calculate another customers interest?");
            System.out.println("Enter Y for yes or N for no:");
            input = keyboard.nextLine();
            repeat = input.charAt(0);
      
            }while (repeat == 'y' || repeat == 'Y');
            
                  }
      }

Ok I changed some things and feel I am very close to having a solution.  I have a problem with the current program and a question.  The problem is that the if statement to determin whether or not they are a senior I dont know what to do to make this if statement qualify them for the now/senior account.  And where should I initialize Person cus;
  cus = new Senior(n, soc, t, b, add, d, in, da, mo, rat, moa, daa, daya);
? I am confused on this part as well.
thanks
rich
0
 

Author Comment

by:psi3000
ID: 16495217
Oh yeh almost frogot. Is there a way to change the month and setting the interest in the test class to a if statement to shorten it up?  If so how?
thanks
0
 
LVL 1

Assisted Solution

by:abalonia
abalonia earned 500 total points
ID: 16500376
Instead of defining the Person object first and then setting the values, save the answers to the questions first without declaring a Person. After you have all the answers, you know whether the person qualifies for a senior account. Then you define the person, either as a Person or a Senior. For example:

boolean senior = false;
namei = JOptionPane.showInputDialog("What is" +
                                                       "the customers name?");
          soci =
               JOptionPane.showInputDialog("What is the customers"+
                                                                      "social security number?");
// When you declare the Person, pass in namei and soci as arguments to the constructor.

// ...
if (input >= 5000 && inputii >= 55) {
  senior = true;
  // ...
}

// At the end:
if (senior) {
  cus = new Senior(namei, soci, t, b, add, d, in, da, mo, rat, moa, daa, daya);  // Put he correct variable names
                                                                                                           // in the constructor.
}
else {
  cus = new Person(n, soc, t,
               b, add, d, in, da, mo, rat, moa,
               daa, daya, check);
// Put the correct variable names in the constructor.
}
0
 
LVL 1

Expert Comment

by:abalonia
ID: 16501110
To cut down on switch-case statements for the interest rate, make an array mapping months to interest rate.

float interest[] = {0.06, 0.06, 0.06, 0.05, 0.05, 0.05, 0.04, 0.04, 0.04, 0.03, 0.03, 0.03};

Then each month's interest rate is:
 interest[test - 1]

I suppose that the month values stored in Person are the number of days between the beginning of the year and the beginning of the month? You could make an array mapping months to the number of days in the month and write a formula to compute the number of days. Alternatively, you could pre-calculate the number of days, since you already have them in your existing code, and put them in an array.
int num_days_since_year[] = {0, 31, ... }
and access the number of days as num_days_since_year[test - 1].
0

Featured Post

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.

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Progress

872 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