Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mockup for SCJD

 
Makeshkumar Subramaniam
Greenhorn
Posts: 21
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi peeps,
Its always nice to start something with a mock up (my opinion). I've cleared SCJP and planned to do SCJD as soon as possible. I feel like i need to test that i am kinda guy who would make good design decisions. For that very reason i've created a simple kinda component. This is sorta application that shares a room-rent among the room-shares...
For instance:

there are two persons sharing a house..each will spent on by their own when there are some general expenses towards water, grocery etc. what happens in the end of month is they have to share the total-rent as well as the general expenses they made individually...
E.g,
Total Rent : 3000
General expenses by John : 200
General expenses by David : 100

At the end of calculations, John have to pay 950 as his share and David have to pay 1050 as his share...

hope you understand the problem . ;-)




I AM OPEN FOR COMMENTS... ;-)

[ Moderator : in any case, if you think this thread violates the law.. feel free to do whatever that i deserves ]


///coding/////
///////////////////////////////////////////////////////////////////////////
Calculator.java
//////////////////////////////////////////////////////////////////////////


/**
* Interface that plays a role of supertype/prototype for all calculations
* ( remember the Design rule - Program to interface not implementation ;-))
* @author Makeshkumar.s
* @Date Dec 17, 2008
* @version 1.01
*
*/

public interface Calculator {

/**
* abstract method to calculte ( anything)
*
*/
void calculate();
}

////////////////////////////////////////////////////////////////////////
RoomMate.java
////////////////////////////////////////////////////////////////////////
/**
* Interface to abstract common activities of a RoomMate
* @author Makeshkumar.s
* @date Dec 17, 2008
* @version 1.01
*/
public interface RoomMate {

/**
* abstract method that get the calculated share for a room-mate
* @return calculated share
*/
double getShare();

/**
* abstract method that get the total amount spent by a room-mate for
* general things like grocery, water, Electric Bill..etc
* @return amount spent by a roomMate
*/
double getSpent();

/**
* abstract method that set the calculated shares for each and every room-mates
* @return calculated share
*/
void setShare(double share);

}

//////////////////////////////////////////////////////////////////////////
Rent.java
//////////////////////////////////////////////////////////////////////////
/**
* This class abstracts the activites of Rent
* @author Makeshkumar.s
* @date Dec 17, 2008
* @version 1.01
*
*/
public interface Rent {


/**
* this method sets the total rent to be paid for sometime (E.g, a month)
* @param totalAmount
*/
void setTotalRent(double totalAmount);

/**
* adds the room-sharer
* @param roomSharer
* @throws DuplicateRoomMateException
* @throws NullPointerException
*/
void addRoomMate(RoomMate roomSharer) throws NullPointerException, DuplicateRoomMateException;

/**
* gets the room-sharer's due of share
* @param roomSharer
* @return
*/
double getShare(RoomMate roomSharer);
}
/////////////////////////////////////////////////////////////////////////
RentCalculator.java
////////////////////////////////////////////////////////////////////////
import java.util.ArrayList;
import java.util.List;

/**
* class that calculates the rent-share for the given room-shares.
*
* @author Makeshkumar.s
* @date Dec 17, 2008
* @version 1.01
*
*/
public class RentCalculator implements Calculator, Rent {

/**
* list that holds all the room-sharers
*/
private List<RoomMate> roomSharers = new ArrayList<RoomMate>();

/**
* total rent
*/
private double totalRent = 0;

/**
* calculate the individual share
*/
public void calculate() {

if (totalRent > 0) {
double normShare = totalRent / roomSharers.size();
for (RoomMate rm : roomSharers) {
updateSpentShare(rm, normShare);
}
}

}

/**
* adds room-sharers to the rent-calculation
* @throws DuplicateRoomMateException
*/
public void addRoomMate(RoomMate roomSharer)throws NullPointerException, DuplicateRoomMateException {



if ( !(roomSharer == null) ) {
if( !( roomSharers.contains(roomSharer)) ){
roomSharers.add(roomSharer);
}else {
throw new DuplicateRoomMateException();
}
} else {
throw new NullPointerException();
}

}

/**
* gets individul share of a room-sharer
*/
public double getShare(RoomMate roomSharer) {

double share = 0;
for (RoomMate rm : roomSharers) {
if (rm.equals(roomSharer)) {
share = rm.getShare();
}
}

return share;
}

/**
* setst the total rent
*/
public void setTotalRent(double totalAmount) {

this.totalRent = totalAmount;
}

/*
* method to calculate other room-mate's share by the amount spent by
* the given(argumented) roomSharer
*/
private void updateSpentShare(RoomMate roomSharer,double initalShare) {
double totalSubDiv = 0;
int size = roomSharers.size();

// soon i wiil find time to comment this part ;-))

if (roomSharer.getSpent() > 0) {

totalSubDiv = roomSharer.getSpent() / size;
for (RoomMate rm : roomSharers) {

if (!rm.equals(roomSharer)) {
rm.setShare((rm.getShare() + totalSubDiv));
} else {
rm.setShare( initalShare + ((rm.getShare() - (totalSubDiv * (size - 1)))) );
}

}

}else
{
roomSharer.setShare(initalShare + roomSharer.getShare() );
}

}

}
//////////////////////////////////////////////////////////////////////
RoomSharer.java


/**
* class represents the room-sharer
* @author Makeshkumar.s
* @date Dec 17, 2008
* @version 1.01
*/
public class RoomSharer implements RoomMate {

private String name ;
private String dob ;

private double spent = 0;
private double share = 0;

public RoomSharer(String name, String dob, double spent) {
this.name = name;
this.dob = dob;
this.spent = spent;
}
public RoomSharer() {}

public double getShare() {

return this.share;
}

public double getSpent() {

return this.spent;
}

public void setShare(double share) {
this.share = share;

}
public void setSpent(double spent) {
this.spent = spent ;
}


public String getDob() {
return dob;
}

public void setDob(String dob) {
this.dob = dob;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public boolean equals(Object obj) {
boolean result = false;
if(obj != null && obj instanceof RoomSharer) {
RoomSharer rs =(RoomSharer)obj;
result = this.name.equals(rs.getName()) && this.dob.equals(rs.getDob());
}
return result;
}

public String toString() {
return this.name +" : " + this.dob +" : " +this.spent + " : "+share;
}
public int hashCode() {
int hash = 0;
if(this.name!=null && this.dob != null) {
hash = this.name.hashCode() + this.dob.hashCode();
}
return hash;
}


public static void main(String[] args) {
RoomSharer rs1 =new RoomSharer();
rs1.setName("Maki");
rs1.setDob("Dec 17, 2008");
rs1.setSpent(100);

RoomSharer rs2 =new RoomSharer();
rs2.setName("John");
rs2.setDob("Dec 13, 2008");
rs2.setSpent(0);

RoomSharer rs3 =new RoomSharer();
rs3.setName("Joe");
rs3.setDob("Dec 17, 2008");
rs3.setSpent(100);


RoomSharer rs4 =new RoomSharer();
rs4.setName("David");
rs4.setDob("Dec 17, 2008");
rs4.setSpent(0);


RentCalculator rc =new RentCalculator();
rc.setTotalRent(2720);
try {
rc.addRoomMate(rs1);
rc.addRoomMate(rs2);
rc.addRoomMate(rs3);
rc.addRoomMate(rs4);

} catch (NullPointerException e) {
e.printStackTrace();
} catch (DuplicateRoomMateException e) {
e.printStackTrace();
}

rc.calculate();

System.out.println( rs1.getName() +" : "+ rc.getShare(rs1));
System.out.println( rs2.getName() +" : "+ rc.getShare(rs2));
System.out.println( rs3.getName() +" : "+ rc.getShare(rs3));
System.out.println( rs4.getName() +" : "+ rc.getShare(rs4));



}

}
 
mohamed sulibi
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi;

i don't understand anythings may be i am in the wrong time and location

regards.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So... basically, you wrote all this code for practice before jumping into SCJD ?

Did you need any help or information on SCJD ?

Regards,
Alex
 
Makeshkumar Subramaniam
Greenhorn
Posts: 21
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex ,
o... basically, you wrote all this code for practice before jumping into SCJD ?


Yes, you are right friend!. i feel that i want to test myself that i can make use of good design principles; if you feel like you can show me some other way to do this, you are most welcome.

Thank you very much for you all, who replied ;-)
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

you could always get Andrew Monkhouse's book on SCJD. The book will guide you through writing a *similar* application.

SCJD Exam with J2SE 5, Second Edition

I feel you'll be right on target with his book. Andrew is also on this forum every once in a while.

For a good pure design book, I really really love : Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development

The title might be confusing, it is not a book on UML syntax. Read the comments from other readers on amazon..

Regards,
Alex
[ December 22, 2008: Message edited by: Alex Belisle Turcot ]
 
Makeshkumar Subramaniam
Greenhorn
Posts: 21
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mate !!!
currently i am learning design patterns from "Head First DesignPatterns" book; anyway, i will try buying those books you mentioned. i will make sure myself that i will have read those books before getting into SCJD Examination.

;-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic