You are on page 1of 8

Coll

ecti
onAgency .j
ava
**
******
****
* *
***********
** **
****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
*
**
***
i
mpor tj
ava.i
o.BufferedReader ;
i
mpor tj
ava.i
o.Fil
eReader ;
i
mpor tj
ava.i
o.I
OExcept i
on;
i
mpor tj
ava.sql
.Connect i
on;
i
mpor tj
ava.sql
.PreparedSt atement
;
i
mpor tj
ava.sql
.ResultSet;
i
mpor tj
ava.sql
.SQLExcept i
on;
i
mpor tj
ava.uti
l.
ArrayList;
i
mpor tj
ava.uti
l.
List;
i
mpor tj
ava.uti
l.
Scanner ;
i
mpor tj
ava.uti
l.
regex.Mat cher;
i
mpor tj
ava.uti
l.
regex.Pattern;

publ
iccl
assCol lectionAgency{
/**
*Thismet hodshoul dtaket hef i
lepat hasargument
*andi tshoul dpar sethedat astoredi nthefi
leand
*itshoul dv alidatethepol icyI dbyinv oki
ngthev ali
dat
e(Str
ingpol icy
Id)met
hod,
*ifvali
d, const ructaPay mentobj ectforeachrecordinthef i
le,
*andt hencal culatet hepay mentamountbyi nvokingthecalcul at
ePayment
Amountmethod
ofPaymentcl ass.
*Aftercal culat i
ngt hepay mentamount ,
*eachPay mentshoul dbeaddedt othelistandt hismet hodshouldretur
nthel
istof
Payment.
*@par am f i
lePathPat hincludet henamewher et hefi
l
ei sl
ocat ed
*@r eturnLi stofPay mentaf terreadingdatafrom thefil
e
*@seePay ment
*/
publi
cLi st<Pay ment >gener atePay ment Amount(Stri
ngfi
lePath){
List<Pay ment >pay ment List=newAr ray
List<>()
;

t
ry{
//Creat
ingscannerobj
ectf
orreadi
ngdat
afrom thef
il
e
Scannerscanner=newScanner(
newBuff
eredReader
(newFi
l
eReader
(fi
l
ePat
h))
);

whi
l
e( scanner.
hasNext(
)){
Stri
ng[]val
ues=scanner.next
Line(
).spli
t(
",
")
;
Stri
ngpol i
cyI
d=v al
ues[
0];
doublemont hl
yPremium =Double.parseDoubl
e(v
alues[
1])
;
i
ntnoOf Month=Integer.
parseI
nt(v
alues[2])
;

t
ry{
//Vali
datingpoli
cyI
d
i
f(vali
date(pol
icy
Id)
){
Paymentpay ment=newPay ment()
;
payment .
set
Poli
cyI
d(poli
cyI
d);
payment .
set
MonthlyPr
emium(monthly
Premi
um)
;
payment .
set
NoOfMonths(noOfMonth)
;
payment .
cal
cul
atePaymentAmount(
);

/
/Addi
ngnewPay
mentt
othepay
ment
List
paymentList
.add(payment);
}
}cat
ch(I
nvali
dPol
icyI
dExceptione){
//Pr
int
ingerr
ormessagei ft
hepolicyi
disi
nval
i
d
Syst
em.out.
pri
ntl
n(e.getMessage()
);
}
}

scanner.
close()
;
}cat
ch(IOExcepti
one){
e.pr
int
StackTrace(
);
}

r
etur
npay
ment
List
;
}

/**
*Thismet hodshoul dv ali
dat ethepol i
cyId,
*ifval
idr etur ntr
ueel set hismet hodshoul dt hrowanuser -def
inedexcepti
on
*andaddi ngi ttot helist.
*Thepol icy Idshoul dbei nt hefoll
owi ngformat :
*1.Thepol i
cy I
dshoul dcont ainexactly10char acter s
*2.Thef i
fthchar actermustbeanal phabet“ H”i nupper -
caseonl y.
*Ifthepol icyIdisv ali
dt henpar sethedat aandcal culatethepay mentamount
*elset hrow auserdef inedExcept i
on“ Inv ali
dPol i
cyIdExcepti
on”wi t
hamessage" I
nval
i
d
Pol
i
cyId".
*@par am pol i
cyIdPol i
cyI dofacust omer
*@r et
ur nt rueifthepol icyIdqualif
yt hespeci fi
cationgiven
*@t hr
owsI nvali
dPol icyIdExcept i
onwhenpol i
cy I
ddoesnotmat chthespeci
fi
cat
ion
*/
publi
cbool eanv alidate(St r
ingpol i
cyId)throwsI nvalidPoli
cyIdExcepti
on{
Patternpat t
ern=Pat tern.compile("^
\\w{ 4}H\ \w{5}$");
Matchermat cher=pat tern.matcher(policy I
d);

i
f(matcher.
matches(
)){
retur
ntrue;
}el
se{
thrownewInvali
dPol
icy
IdExcept
ion(
"I
nval
i
dPol
i
cyI
d")
;
}
}

/**
*Thismet hodshouldupdat ethebalance_ pr
emium byr educi
ngtheexi
sti
ngvaluewi
ththe
cal
cul
atedpaymentamounti nt hePoli
cy_Detai
lstable.
*Assumet hatthebalance_premium wil
lbegr eaterthanorequaltocal
cul
atedpayment
amount.
*@par am paymentLi
stListofPayment
*@seePay ment
*/
publi
cvoidupdatePoli
cyDet ai
l
s(Li
st<Payment >payment Li
st){
Connecti
onconnection=newDBHandl er(
).
establi
shConnecti
on(
);

f
or(
Paymentpay
ment:
pay
ment
List
){
try{
//Gettingcurrentbalancepr emium
PreparedStatement preparedStatement1 =
connect
ion.
prepar
eStatement (
"selectbalance_ pr
emi um f
rom Pol i
cy_Detai
lswher
epol
i
cy_
id=?
;")
;
preparedStatement 1.
set St
ri
ng(1, payment.getPol
icyId(
));
ResultSetresult
Set=pr eparedStatement1.executeQuery(
);

resul
tSet
.next(
);
doublecurr
entBalance=resul
tSet.
getDoubl
e(1);
doubleupdatedBalance=curr
entBalance-payment
.get
Pay
ment
Amount
();

//Updat i
ngthebalancepremium withthenewv al
ue
PreparedStatement preparedStatement2 =
connect
ion.
prepar
eStatement (
"updat
ePolicy_
Detail
ssetbal ance_premium =?wher
epol
i
cy_
id=
?;
")
;
preparedStatement2.
set
Double(1,updatedBalance);
preparedStatement2.
set
Stri
ng(2,payment .
getPoli
cyId(
));

prepar
edStatement2.
execut
eUpdat
e()
;
}cat
ch(SQLExceptione){
e.pr
int
StackTrace(
);
}
}
}
}

-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--

DBHandl
er.
java
**
***
****
***
* *
****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
**

i
mpor
tjav
a.i
o.Fil
eInputStream;
i
mpor
tjav
a.i
o.I
OExcept ion;
i
mpor
tjav
a.sql
.Connect i
on;
i
mpor
tjav
a.sql
.Dri
v er
Manager ;
i
mpor
tjav
a.sql
.SQLExcept i
on;
i
mpor
tjav
a.uti
l.
Properti
es;

publi
ccl assDBHandler{
/**
*Thi smet hodshouldconnecttot hedatabasebyr eadi
ngt
hedatabasedetai
l
sfrom the
db.properti
esfil
eanditshoul
dr et
urntheconnecti
onobj ect
*@r etur
n Connecti
on tot he MySQL dat abase ornul
lwhen t
hereis some probl
em
connectingtothedatabase
*@seeConnect ion
*/
publicConnecti
onestabl
ishConnecti
on(){
Pr
operti
esproper
ti
es=newPr opert
ies(
);

t
ry{
//Creati
nginputstr
eam from db.
proper
ti
esf i
l
e
Fil
eInputSt
ream f
il
eInput
Stream =newFileI
nputSt
ream(
"db.
proper
ti
es"
);
properti
es.
load(
fi
leI
nputStream);

/
/Get
ti
ngv
alueoft
hepr
oper
ti
esf
il
e
St
ri
ngdri
ver=propert
ies.
getProperty
("db.
classname");
St
ri
ngurl=proper
ti
es.get
Property(
"db.url
")
;
St
ri
ngusername=properti
es.getPropert
y("db.
username");
St
ri
ngpassword=properti
es.getPr
oper t
y("
db.password")
;

//Makingsur
edri
vejari
sav
ail
abl
e
Class.
for
Name(dr
iver
);

//Returninganewdat abaseconnection
retur
nDr iverManager .
get
Connection(
ur l
,
user name,
passwor d
);
}cat
ch( I
OExcept i
on| Cl
assNotFoundExcept
ion|
SQLExcept
ione){
e.pri
ntStackTr ace()
;
}

r
etur
nnul
l
;
}
}

-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

I
nval
i
dPol
i
cyI
dExcept
ion.
jav
a

***
********
****
******
****
** *
*****
* *
* *
* *
*******
***
******
***
***
***
*****
***
****
***
****
**
publi
cclassInvali
dPoli
cyIdExceptionext endsExcepti
on{
/**
*Custom excepti
onf orinval
idpol i
cyid
*@par am messageMessagepassedt obet hr
ownwhent hei
nval
idpol
icyi
disdet
ect
ed
*/
publi
cInvali
dPoli
cyI
dExcept i
on( Str
ingmessage){
super(
message);
}
}

-
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
Main.j
ava

i
mpor
tjav
a.sql
.Connecti
on;
i
mpor
tjav
a.sql
.Resul
tSet;
i
mpor
tjav
a.sql
.SQLExcepti
on;
i
mpor
tjav
a.uti
l
.Li
st;

publ
i
cclassMai n{
pri
vatestat
icvoi
dprint
Database(
){
Connecti
onconnecti
on=newDBHandl
er(
).
est
abl
i
shConnect
ion(
);

t
ry{
Resul
tSetr
esul
tSet= connect
ion.
creat
eSt
atement
().
execut
eQuer
y("
sel
ect*f
rom
Pol
i
cy_
Detai
l
s;"
);
whi
l
e( r
esult
Set .
next()
){
Str
ingpol i
cyId=result
Set.getStr
ing(1)
;
doubletotalCover
age=r esultSet.
getDouble(2);
doublebalancePremium =r esult
Set.getDouble(
3);
i
ntpremi umDurationYears=r esult
Set.get
Int
(4);

System. out .
pri
ntl
n(Str
ing.
format(
"%-20s%-20s%-20s%-20s"
, "
pol
i
cy_
id"
,
"
total
_cov er
age","balance_ premium","pr
emium_ durat
ion_year
_int
"))
;
System. out .
pri
ntl
n(Str
ing.
format(
"%-20s%-20.
2f%-20.2f%-
20d"
, pol
i
cyI
d,
t
otalCoverage,balancePr emi um,premiumDur ati
onYears)
);
}
}catch(SQLExcept ione){
e.
pri
nt StackTr ace()
;
}
}

publ
i
cstat
icvoidmain(St
ri
ng[]ar
gs){
Col
l
ectionAgencycol
lect
ionAgency=newCol
l
ect
ionAgency
();

Sy stem.
out.pr
intl
n("
PaymentsRetri
evedfr
om t
hetextfi
l
e..
.")
;
List<Pay
ment > paymentLi
st =
col
l
ect
ionAgency.generat
ePaymentAmount("Pol
icy
Payment
Detail
s.t
xt"
);
pay mentLi
st.f
orEach(Sy
stem.out
::
pri
ntl
n);

Syst
em.
out.
pri
ntl
n("
Dat
abasebef
oreupdat
ing.
..
")
;
pr
int
Dat
abase(
);

System.out.
pri
ntl
n("Dat
abaseaft
erupdati
ng.
..
")
;
coll
ecti
onAgency.updat
ePoli
cyDet
ail
s(pay
mentList
);
pri
ntDatabase(
);
}
}

-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

Pay
ment
.j
ava

publ
i
cclassPay ment{
pri
vateStr
ingpoli
cyI
d;
pri
vatedoublemonthl
yPremi
um;
pri
vatei
ntnoOfMonths;
pri
vatedoublepayment
Amount;

publ
i
cStri
nggetPol
icy
Id(
){
ret
urnpol
i
cyId;
}

publ
i
cv oidset
Pol
icy
Id(
Stri
ngpol
i
cyI
d){
thi
s.pol
icy
Id=poli
cyI
d;
}

publ
i
cdoublegetMonthl
yPremi
um(
){
ret
urnmonthl
yPremium;
}

publ
i
cv oi
dsetMont
hly
Premium(doubl
emonthl
yPr
emi
um){
thi
s.mont
hly
Premium =monthl
yPremi
um;
}

publ
i
cintget
NoOfMonths(
){
ret
urnnoOf
Months;
}

publ
i
cv oidset
NoOfMont
hs(
intnoOfMont
hs){
thi
s.noOfMont
hs=noOfMonths;
}

publ
i
cdoubleget
Payment
Amount
(){
ret
urnpay
mentAmount;
}

publ
i
cv oidset
Payment
Amount
(doublepay
mentAmount
){
thi
s.payment
Amount=pay
ment Amount;
}

/**
*Thi smet hod shoul d calcul
ateand sett hepay mentamountbased on t hemont hl
y
Premium and
*noofMont hsforeachpay ment.
*
*NoOfMont hs Penalt
yPer centageont hepay mentAmount
*1 0%( Nopenal ty)
*>1and<=5 3%
*>5and<=12 5%
*>12 7%
*
*Forexampl e:Ifapay menthasamont hlypremium ofRs.5000andt henumberofmont hs
as4,t henthepay mentamountwi l
lbe( 5000*4)whichis20000.00.Sincet
henumberofmont hs
i
s4, thepenalt
yper centagewillbe3%.
*Ther ef
ore,t hepenal t
ywi l
lbe( 20000.0*
(3/100))whichisRs.600. 00.Theref
ore,the
paymentamountf orthispaymentwi l
lbe((5000*4)-
600.0)whi
chi sRs.
19400.00.
*Aftercalculati
ngthepay mentamountf oreachpay ment,st
orethepay mentobj
ectint
oa
l
ist.
*/

publ
i
cvoidcalculat
ePay
mentAmount
(){
paymentAmount=monthly
Premi
um *(
doubl
e)noOf
Mont
hs;
doubl
eper cent
age=0.
0;

i
f(noOfMont hs>1&&noOfMont
hs<=5){
percentage=3;
}el
seif(noOf Mont
hs>5&&noOfMont
hs<=12){
percentage=5;
}el
seif(noOf Mont
hs>12){
percentage=7;
}

doubl
epenal
ty=payment
Amount*per
cent
age/100.
0;
payment
Amount-=penal
ty;
}

@Overr
ide
publ
i
cSt r
ingt
oSt r
ing(){
ret
urn"Payment {"+
"poli
cyId='"+poli
cyI
d+'\'
'+
",mont hly
Premium="+mont hl
yPr
emium +
",noOfMont hs="+noOfMonths+
",payment Amount="+paymentAmount+
'}
';
}
}

--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
dbpropert
ies

db.
classname=com.my sql
.j
dbc.Dr
iver
db.
url=j
dbc:
my sql
:/
/local
host:
3306/test
base
db.
username=rit
am
db.
password=passwor d

-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--

scr
iptf
il
e

dr
opdat
abasei
fexi
stsI
nsur
ance;

cr
eat
edat
abaseI
nsur
ance;

useI
nsur
ance;

creat
et ablePolicy_
Detai
ls
(
policy_i
d varchar
(25)pri
marykey
,
total_
coverage double(10,2)
,
balance_premium double(10,
2),
premi um_durati
on_y
earsint
);

i
nserti
ntoPol
icy
_Detai
ls
v
alues('
2005H37012'
,100000,
100000,15);
i
nserti
ntoPol
icy
_Detai
ls
v
alues('
2006H37013'
,100000,
85000,20);
i
nserti
ntoPol
icy
_Detai
ls
v
alues('
2007H37014'
,150000,
150000,25);
i
nserti
ntoPol
icy
_Detai
ls
v
alues('
2008H37015'
,250000,
150000,10);
i
nserti
ntoPol
icy
_Detai
ls
v
alues('
2009H37016'
,800000,
75000,30);

sel
ect*
f
rom Pol
i
cy_
Det
ail
s;

t
runcat
ePol
i
cy_
Det
ail
s;

COMMI
T;

You might also like