Best Projects

Bug Trackingsystem,Books online shopping,college management system,HR management system,Internet banking,Online chat room ,Online exam,Telephone billing system,Banking application,ATM database,airways reservation system,virtual network computing ,calculator,SMTP(simple mail transfer protocol)

A CLASS SHOWS HOW

Description: To show how interface, abstract classes works

//Account
public class Account{

private float balance = 0;


public void deposit(float amount) throws IllegalAmountException{
if (amount < 0)
throw new IllegalAmountException();
else
balance +=amount;
}
public float getBalance(){ return balance;}

}




//Campany
public class Company extends LegalEntity{
private String taxNumber;


}



//Hasaddress
interface HasAddress{

public void addAddress(String address);
public String getAddress();

}



//IllegalAmountException

public class IllegalAmountException extends Exception{

private final String strMessage = "Amount is less than the minimum";
public String getMessage(){
return this.strMessage;
}
}



//Legalentity
import java.util.*;

public abstract class LegalEntity implements HasAddress{

private String address;
private String area;
private String registrationDate;
private ArrayList accounts;

public LegalEntity()
{
accounts = new ArrayList();
}

public void addAddress(String address){this.address = address;}

public String getAddress(){return address;}

public void addAccount(Account account)
{
accounts.add(account);
}

public ArrayList returnAccounts()
{
return accounts;
}
}



//person
public class Person extends LegalEntity{



}




//test.java
import java.util.*;

class Test
{
public static void main(String[] args)
{
Test aTest = new Test();
aTest.test();
}

private void test()
{
Person person = new Person();
person.addAddress("123 Surrey Avenue");
System.out.println(person.getAddress());
Account acc = new Account();

try{
acc.deposit(-33);
}
catch(IllegalAmountException e){
System.out.println(e.getMessage());
}

person.addAccount(acc);
Account acc2 = new Account();

try{
acc2.deposit(200);
}
catch(IllegalAmountException e){
System.out.println(e.getMessage());
}

person.addAccount(acc2);

ArrayList it = person.returnAccounts();
Iterator t = it.iterator();

Account anAccount;

float balance;

while(t.hasNext()){
anAccount = (Account) t.next();
balance = anAccount.getBalance();
System.out.println("Balance = " + balance);
}
}
}

No comments: