Link to home
Start Free TrialLog in
Avatar of psi3000
psi3000

asked on

Bank Account Java program using inheritance and polymorphism

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.


ASKER CERTIFIED SOLUTION
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of psi3000
psi3000

ASKER

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.
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..
Avatar of psi3000

ASKER

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?
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.
Avatar of psi3000

ASKER

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
Avatar of psi3000

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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].