如何实现具有属性的中间类的类之间的关联?

时间:2019-05-16 23:05:08

标签: java associations

我正在创建两个相互关联的类之间的关联实现,但它们之间也具有属性。

通常在类图中它看起来像这样: enter image description here

但是我必须像这样实现它: enter image description here

我想要实现的是我希望能够列出分配给特定博览会的所有展览。

重要的是我不想使用任何设置器/获取器。

现在我已经像下面这样连接了这三个类,但是我知道我错过了一些东西,但无法弄清楚……

import java.text.SimpleDateFormat;
import java.util.Date;

public class Reservation {


    protected String reservationId;
    protected Date from;
    protected Date till;

    private Exhibit exhibit;
    private Exposition exposition;


    public Reservation(String reservationId, String from, String till, Exhibit exhibit, Exposition exposition) throws Exception{
        this.reservationId = reservationId;

        this.from = new SimpleDateFormat("dd-MM-yyyy").parse(from);
        this.till = new SimpleDateFormat("dd-MM-yyyy").parse(till);

        this.exhibit = exhibit;
        this.exposition = exposition;

        exhibit.addReservation(this, from, till);
        exposition.addReservation(this, from, till);

        }


    public void addExhibit(Exhibit exhibit, String from, String till) throws Exception{
        this.exhibit = exhibit;
        this.from = new SimpleDateFormat("dd-MM-yyyy").parse(from);
        this.till = new SimpleDateFormat("dd-MM-yyyy").parse(till);
    }

    public void addExposition(Exposition exposition, String from, String till) throws Exception{
        this.exposition = exposition;
        this.from = new SimpleDateFormat("dd-MM-yyyy").parse(from);
        this.till = new SimpleDateFormat("dd-MM-yyyy").parse(till);
    }


    public String toString(){
        String info = "Reservation: " + reservationId + " pertains to:" + "\n";

        info += "   exhibit: " + this.exhibit.exhibitId + " " + this.exhibit.exhibitName + "\n"
                + "   exposition: " + this.exposition.expositionId + " " + this.exposition.expositionName + "\n"
                + "   od: " + this.from + "\n" + "   do: " + this.till;

        return info;
    }

}

import java.util.ArrayList;
import java.util.Date;

public class Exposition {

    protected String expositionId;
    protected String expositionName;
    protected Date startDate;
    protected Date finishDate;
    protected String description;

    private Hall hall;

    private ArrayList<Reservation> Reservation = new ArrayList<>();

    private Exposition(Hall hall, String name, String expositionId){
        this.expositionName = name;
        this.expositionId = expositionId;
        this.hall = hall;
    }

    public static Exposition stworzEkspozycje(Hall hall, String expositionName, String expositionId) throws Exception{

        if (hall == null){
            throw new Exception("Hall nie istnieje!");
        }

        Exposition newExposition = new Exposition(hall, expositionName, expositionId);

        hall.addExposition(newExposition);

        return newExposition;
    }



    public Exposition(String expositionId, String expositionName, Date startDate, Date finishDate){
        this.expositionId = expositionId;
        this.expositionName = expositionName;
        this.startDate = startDate;
        this.finishDate = finishDate;
    }



    public void addReservation(Reservation newReservation, String from, String till) throws Exception{
        if(!Reservation.contains(newReservation)){
            Reservation.add(newReservation);
            newReservation.addExposition(this, from, till);
        }
    }

}
import java.util.ArrayList;

public class exhibit {

    protected String exhibitId;
    protected String exhibitName;

    private ArrayList<Reservation> Reservation = new ArrayList<>();

    public exhibit(String exhibitId, String exhibitName){
        this.exhibitId = exhibitId;
        this.exhibitName = exhibitName;
    }

    public void addReservation(Reservation newReservation, String from, String till) throws Exception{
        if(!Reservation.contains(newReservation)){
            Reservation.add(newReservation);
            newReservation.addExhibit(this, from, till);
        }
    }


    public String toString(){
        String info = "exhibit: " + exhibitId + " reserved for " + "\n";
        for (Reservation r : Reservation)
            info += "   " + "reservation: " + r.reservationId + "\n" + "   from: " + r.from + "\n" + "   till: " + r.till + "\n";

        return info;

    }
}

0 个答案:

没有答案