简体   繁体   中英

OO programming about state

This is my professor's assignment for oo programming. But I found that the marital status can be an variable in the Person class, how can I achieve this with two classes?

Assume people can have marital status: single, married, widow, divorced. Create a state OODP (ie, Java, JavaScript, C++, Python, or Ruby) that deal with people's marital status. You will have at least two classes: Person, and Marital State. Make sure the following rules are followed: single->married married-> divorced | widow divorced -> married widow -> married create a Client class to test your program. Make sure you test valid and invalid change of marital status.
The assignment page


These are my codes:

public class AssignmentOOP {
    public static void main(String[] args) {
        Person p1 = new Person("p1");
        Person p2 = new Person("p2");
        Person p3 = new Person("p3");
        p1.PrintMaritalStatus();
        p2.PrintMaritalStatus();
        p3.PrintMaritalStatus();
        p1.GetMarried(p2);
        p1.GetMarried(p3);
        p2.Died();
        p1.GetMarried(p3);
    }
}

class Person {
    String maritalstatus;
    boolean mateIsAlive;
    Person mate;
    String name;

    Person(String name1) {
        maritalstatus = "single";
        mate = null;
        name = name1;
    }

    void GetMarried(Person mate) {
        if(this.maritalstatus.equals("married")|| mate.maritalstatus.equals("married"))
        {
            System.out.println("Marital status error! At least one of you are married");
            return;
        } else {
            this.maritalstatus = "married";
            this.mate = mate;
            mate.maritalstatus = "married";
            mate.mate = this;
            System.out.println("Congratulations!!! " + this.name + " and " + mate.name + " are married!");
        }
    }

    void GetDivorced(Person mate) {
        if(this.maritalstatus.equals("married") && this.mate == mate) {
            maritalstatus = "divorced";
            System.out.println(this.name+" and "+mate.name+" are getting divorced.");
        }else if(this.maritalstatus.equals("single")) {
            System.out.println("You are not married and you cannot get divorced before getting married");
        }else if(maritalstatus.equals("widow")) {
            System.out.println("Your marital status is widow, you cannot get divorced.");
        }
    }

    void Died() {
        this.maritalstatus = "dead";
        this.mate.maritalstatus = "widow";
        System.out.println("Sorry for your loss, " + this.mate.name + " marital status is widow.");
    }

    void PrintMaritalStatus() {
        System.out.println(this.name + " marital status is " + this.maritalstatus);
    }
}

Here:

String maritalstatus;

That is probably not what your professor wants. The point of that marital status is: there is only a fixed set of values.

The proper way to express that is to use an enum in java, like:

public enum MaritalStatus { SINGLE, MARRIED, ... } 

You could then further enhance this by creating a state machine , that implements the rules, such as: when you are SINGLE, and doMarry() , your new status should be MARRIED afterwards.

Before copy and past..

This solution is maybe not what your professor want. There are multiple other solutions that could be used in this scenario.

In the following solution I assume that you can only use 2 classes!

Possible Solution

Your Person should have a field of type MaritalState . This MaritalState tracks the state of a Person so it don't have to do it by its own.

The following is just quick and dirty and is to show you the idea behind an aggregation.

Person

The Person now delegates all the logic that depends on the MaritalState to MatrialState

public class Person {

  MaritalState maritalState;
  Person mate;
  String name;

  Person(String name1) {
    maritalState = new MaritalState();
    mate = null;
    name = name1;
  }

  void GetMarried(Person mate) {
    maritalState.marry(this, mate);
  }

  void GetDivorced(Person mate) {
    maritalState.divorce(this, mate);
  }

  void Died() {
    maritalState.die(this);
  }

  void PrintMaritalStatus() {
    System.out.println(this.name + " maritalState status is " + this.maritalState.get());
  }

  public boolean isMarried() {
    return maritalState.isMarried(maritalState);
  }

  public boolean isSingle() {
    return maritalState.isSingle(maritalState);
  }

  public boolean isWidow() {
    return maritalState.isWidow(maritalState);
  }
}

MaritalState

The logic which was in Person is now in Martiral state. I extend some functionality like isSingle() to make the code more readable.

public class MaritalState {

  private static final String MARRIED = "married";
  private static final String SINGLE = "single";
  private static final String WIDOW = "widow";

  private String state;

  private MaritalState(String state) {
    this.state = state;
  }

  public MaritalState() {
    this.state = "single";
  }

  void marry(Person person, Person mate) {
    if (person.isMarried() || mate.isMarried()) {
      System.out.println("MaritalState status error! At least one of you are married");
    } else {
      person.maritalState = new MaritalState(MARRIED);
      person.mate = mate;
      mate.maritalState = new MaritalState(MARRIED);
      mate.mate = person;
      System.out.println("Congratulations!!! " + person.name + " and " + mate.name + " are married!");
    }
  }

  void divorce(Person person, Person mate) {
    if (person.isMarried() && person.mate == mate) {
      person.maritalState = new MaritalState("divorced");
      System.out.println(person.name + " and " + mate.name + " are getting divorced.");
    } else if (person.isSingle()) {
      System.out.println("You are not married and you cannot get divorced before getting married");
    } else if (person.isWidow()) {
      System.out.println("Your maritalState status is widow, you cannot get divorced.");
    }
  }

  void die(Person person) {
    person.maritalState = new MaritalState("dead");
    person.mate.maritalState = new MaritalState("widow");
    System.out.println("Sorry for your loss, " + person.mate.name + " maritalState status is widow.");
  }

  public boolean isMarried(MaritalState maritalState) {
    return maritalState.state.equals(MARRIED);
  }

  public boolean isSingle(MaritalState maritalState) {
    return maritalState.state.equals(SINGLE);
  }

  public boolean isWidow(MaritalState maritalState) {
    return maritalState.state.equals(WIDOW);
  }

  public String get() {
    return state;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM