Sunday, February 27, 2011

Java classes

Here is the code for the java classes that I have been working on. These classes show how to implement an interface called IActivity. The classes are meant to represent a very simple bank account application.

Here is a picture from Eclipse of the files involved:



Here is the UML for the class diagrams that I did before writing the program There have been some modifications to the classes in the process of writing the code that are not reflected in the diagram. Specifically I modified the interface and changed the transactions method into GetDebitTransactions and GetCreditTransactions. Also there were some modifications of the Display class.



Finally, before I give the code for the actual classes. Here is the console transcript that shows how the program looks when running:



Choose an account:
1. Checking
2. Savings
3. HomeEquity/n0 to quit

1
Enter the beginning balance

1000
Enter Your choice, 0 to exit
********************************
*1. Credit *
*2. Debit *
*3. Balance *
*4. Get Transactions *
*5. Get Interest *
********************************
1
Enter the credit amount
250
Enter Your choice, 0 to exit
********************************
*1. Credit *
*2. Debit *
*3. Balance *
*4. Get Transactions *
*5. Get Interest *
********************************
1
Enter the credit amount
1200
Enter Your choice, 0 to exit
********************************
*1. Credit *
*2. Debit *
*3. Balance *
*4. Get Transactions *
*5. Get Interest *
********************************
2
Enter the debit amount
35.96
Enter Your choice, 0 to exit
********************************
*1. Credit *
*2. Debit *
*3. Balance *
*4. Get Transactions *
*5. Get Interest *
********************************
2
Enter the debit amount
813.77
Enter Your choice, 0 to exit
********************************
*1. Credit *
*2. Debit *
*3. Balance *
*4. Get Transactions *
*5. Get Interest *
********************************
2
Enter the debit amount
99.50
Enter Your choice, 0 to exit
********************************
*1. Credit *
*2. Debit *
*3. Balance *
*4. Get Transactions *
*5. Get Interest *
********************************
4
Credits
250.0
1200.0
Debits
35.96
813.77
99.5
Balance
1500.77
Enter Your choice, 0 to exit
********************************
*1. Credit *
*2. Debit *
*3. Balance *
*4. Get Transactions *
*5. Get Interest *
********************************
0


So here are the classes:
IActivity

import java.util.*;
/*******************************************
* this is an interface that contains
* five method definitions one for credits
* one for debits one for getting the balance
* one for getting all debits in a list
* and one for getting all the credits in a list
* An interface forces any class that
* implements it to implement all its methods
* @author Steve
*
********************************************/
public interface IActivity {

public void Credit(double amt);
public void Debit (double amt);
public double GetBalance();
public ArrayList GetDebits();
public ArrayList GetCredits();
}

Program.Java

public class Program {

/**
* This is the starting point of the program
* It contains the main method.
* The only thing the main method will do
* is call the display class.
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Display dsplay=new Display();
}

}

Display.Java
import java.util.*;
/***********************************
* The purpose of the display class is
* to gather the input needed and
* to display the output. This display
* class is console, but it could be replaced
* by a form or web page without any changes
* being reguired in the other classes.
* @author Steve
*
*/

public class Display {

//we are declaring the interface as a class
//level variable. At runtime we can assign
//one of the classes that implements this
//interface to the variable.
//this is a core principle of Object oriented
//design and actually one of the elements
//of polymorphism: a child can always be substituted
//for the parent
IActivity actv;
int bal=0;
Scanner scan = new Scanner(System.in);

public Display()
{
Menu();
}

public void Menu()
{
int choice=1;

System.out.println("Choose an account:\n1. Checking\n2. Savings\n3. HomeEquity/n0 to quit");
choice = scan.nextInt();
if (choice==1)
{
SetBalance();
actv = new Checking(bal);
}
else if(choice==2)
{
SetBalance();
actv = new Savings(bal);
}
else if(choice==3)
{
//not defined at this time
return;
}
else if(choice==0)
{
return;
}
else
{
System.out.println("not a valid choice");
Menu();
}

while(choice != 0)
{
System.out.println("Enter Your choice, 0 to exit");


System.out.println("********************************");
System.out.println("*1. Credit *");
System.out.println("*2. Debit *");
System.out.println("*3. Balance *");
System.out.println("*4. Get Transactions *");
System.out.println("*5. Get Interest *");
System.out.println("********************************");

choice = scan.nextInt();

//at this point only the checking options are
//implemented
switch (choice)
{
case 0:
break;
case 1:
AddCredit();
break;
case 2:
AddDebit();
break;
case 3:
GetBalance();
break;
case 4:
GetTransactions();
break;
case 5:
GetInterest();
break;
default:
System.out.println("Not a valid choice");
return;


}


}
}



private void SetBalance()
{
System.out.println("Enter the beginning balance");
bal=scan.nextInt();
}

private void AddCredit(){

System.out.println("Enter the credit amount");
double cr = scan.nextDouble();
actv.Credit(cr);
}

private void AddDebit(){

System.out.println("Enter the debit amount");
double db = scan.nextDouble();
actv.Debit(db);
}

private void GetBalance() {
double balance = actv.GetBalance();
System.out.println("the balance is " + balance);
}

private void GetTransactions(){
ArrayList creditList =actv.GetCredits();
ArrayList debitList=actv.GetDebits();

System.out.println("Credits");
for (int i=0;i<creditList.size();i++)
{
System.out.println(creditList.get(i));
}

System.out.println("Debits");
for (int i=0;i<debitList.size();i++)
{
System.out.println(debitList.get(i));
}
System.out.println("Balance");
System.out.println(actv.GetBalance());

}

private void GetInterest()
{
//not defined
}

}

Checking.Java
import java.util.List;
/***************************************
* this class handles checking deposits
* and withdrawels. It implements the interface
* IActivity and so much implement each or
* its methods
*/

import java.util.*;
public class Checking implements IActivity {

double balance;
//the arraylists below are to keep track
//of the activitires
ArrayList Debits = new ArrayList();
ArrayList Credits= new ArrayList();

//a constructor that takes in the beginning
//balance
public Checking(double balance)
{
//the this keyword differentiates
//the balance that belongs to this class
//from the one that comes through
//the contructors arguments
this.balance=balance;
}

@Override
public void Credit(double amt) {
//add the amount to the balance
//and add it to the list of credit
//transactions
balance += amt;
Credits.add(amt);
}

@Override
public void Debit(double amt) {
//Make sure the result won't
//be negative and then subtract
//the amount from the balance
//and add it to the debit list
if(balance >= (balance-amt))
{
balance -= amt;
Debits.add(amt);
}
}

@Override
public double GetBalance() {

return balance;
}

@Override
public ArrayList GetDebits() {
//we are just returning the list
//the display can figure out what to
//do with it
return Debits;
}

@Override
public ArrayList GetCredits() {
// TODO Auto-generated method stub
return Credits;
}



}

Savings.Java

import java.util.*;

/***************************************
* this class handles savings accounts. It is really
* a mirror of the checking, but adds an interest
* calculation. One could argue that there should
* be another level of abstraction, because there
* is so much that is similar between this and checking.
* Either this class should inherit from checking or
* both should inherit from an abstract class called
* Account
* @author Steve
*
*/
public class Savings implements IActivity {

double balance;
ArrayList credits = new ArrayList();
ArrayList debits=new ArrayList();
final double INTERESTRATE=.04;

public Savings(double balance)
{
this.balance=balance;

}


@Override
public void Credit(double amt) {
balance += amt;
credits.add(amt);
}

@Override
public void Debit(double amt) {
balance -= amt;
debits.add(amt);

}

@Override
public double GetBalance() {
// TODO Auto-generated method stub
return balance;
}

@Override
public ArrayList GetDebits() {
// TODO Auto-generated method stub
return debits;
}

@Override
public ArrayList GetCredits() {
// TODO Auto-generated method stub
return credits;
}

public double CalculateInterest()
{
return 0;
}

}

HomeEquity.Java (not implemented yet)

//public class HomeEquity extends Savings{

//public HomeEquity(){}

/********************************
* this class is not implemented at this time.
* It would inherit from savings and add
* only an extra field that accounts for
* the total equity avaialable
*/
//}

No comments:

Post a Comment