开发者

Simple Object Oriented Design Example with Java

I'm studing on a project. It's a bank simulation and just for practicing OOP metodologies. Here is my code, could you help me about OOD. How can I use inheritance and interfaces on this project开发者_高级运维?

public class Main {

    public static void main(String[] args) {

        User[] User = new User[10];
        for(int i = 0; i < 10; i++) 
            User[i] = new User(i);

        System.out.print("User Number:");
        Scanner s = new Scanner(System.in);
        int UserNo = 0;
        if(s.hasNextInt())
            UserNo = Integer.parseInt(s.next());

   public void withdraw()

   public void payIn(){

   public void MoneyOrder(){


}


You could put all your withdraw() etc other methods in an interface and create a concrete implementation of these methods..

And for inheritance you an categorize the users as privileged user or general user .You can further do categorization based on Account type as Current or Saving Account etc.

interface Bank
{
   public void withdraw();
   public void deposit();
   //Other methods    

}
class ABC_Bank implements Bank
{
 //Implementation of methods defined by Bank Interface ,
   User Username;                    //can also include its Own methods
}

class User
{
 String name;
 //Other fields and getter setter methods

}
class PrivilegedUSer extends User
{
    //Methods specific to privileged User. 
} 

Banking Simulation is a Big Project and You must follow all the OOSE concepts like drawing Use Case Diagrams ,Class Diagrams Which would help you in identifying relationship between classes. For e.g Users have Account.Thus there is an Association Relationship between the two classes.And depending upon your application you can select multiplicity(A user can have many Accounts).This means each of these classes would contain object references of each other.


I think you need to start thinking in terms of Objects. So decide what your Objects are e.g. you have User, but what about Account. Maybe you can look at an Account interface which defines the common types of methods an Account has (pay in, withdraw) then that could be implemented by concrete Account types e.g. Savings, Current.


It seems you're half getting the idea of OOP. In this case you'd probably need to create a class thats an account(or an interface and a class that implements it) some info here.The idea is to model classes as objects as they appear in the world.(eg: an account is an account object).As for inheritance, i don't know the requirements for your homework but perhaps some other classes that inherit your account class ( info here),maybe current and savings as outlined in another answer here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜