Skip to main content

Marina class diagram implementation code

Short answer

Map class diagram to code using private attributes, public methods, ‘extends’ for inheritance, and reference collections for associations. Define abstract Boat with SailBoat and PowerBoat subclasses. Lease computes daily/yearly charges plus length surcharge, then creates a Bill.

For the above class diagram created (XYZ Marina), apply object oriented techniques to convert such class diagram into implementation code in any of your favorite object oriented programming language. Your implementation code should clearly show class definition, Attributes with their proper visibility and method signatures with required parameter.

The class diagram has Customer, Boat (abstract) with SailBoat and PowerBoat, Slip, Lease and Bill. Mapping rules: attributes are private, methods are public, generalization is extends, associations are reference fields or collections.

import java.time.LocalDate;
import java.util.*;

// ---------- Boat hierarchy ----------
abstract class Boat {
    private String registrationNo;
    private int manufactureYear;
    private double length;

    public Boat(String regNo, int year, double length) {
        this.registrationNo = regNo;
        this.manufactureYear = year;
        this.length = length;
    }
    public String getRegistrationNo() { return registrationNo; }
    public double getLength() { return length; }
    public abstract String getType();
}

class SailBoat extends Boat {
    private double sailArea;
    public SailBoat(String r, int y, double l, double sailArea) {
        super(r, y, l); this.sailArea = sailArea;
    }
    public String getType() { return "Sail boat"; }
}

class PowerBoat extends Boat {
    private int enginePower;
    public PowerBoat(String r, int y, double l, int power) {
        super(r, y, l); this.enginePower = power;
    }
    public String getType() { return "Power boat"; }
}

// ---------- Slip ----------
class Slip {
    private int slipNo;
    private double size;
    private boolean vacant = true;

    public Slip(int slipNo, double size) { this.slipNo = slipNo; this.size = size; }
    public boolean isVacant() { return vacant; }
    public void assign()  { vacant = false; }
    public void release() { vacant = true; }
    public int getSlipNo() { return slipNo; }
}

// ---------- Customer ----------
class Customer {
    private int customerId;
    private String name, address;
    private List<Boat> boats = new ArrayList<>();      // 1 - *
    private List<Lease> leases = new ArrayList<>();    // 1 - *

    public Customer(int id, String name, String address) {
        this.customerId = id; this.name = name; this.address = address;
    }
    public void addBoat(Boat b) { boats.add(b); }
    public void addLease(Lease l) { leases.add(l); }
    public List<Lease> getLeases() { return leases; }
}

// ---------- Lease ----------
class Lease {
    public enum Type { DAILY, YEARLY }
    private int leaseId;
    private Type type;
    private LocalDate startDate, endDate;
    private Customer customer;
    private Slip slip;
    private Boat boat;
    private Bill bill;

    public Lease(int id, Type type, LocalDate start, LocalDate end, Customer c) {
        this.leaseId = id; this.type = type;
        this.startDate = start; this.endDate = end; this.customer = c;
        c.addLease(this);
    }
    public void assignBoat(Boat b, Slip s) {       // assign boat to slip
        if (!s.isVacant()) throw new IllegalStateException("Slip not vacant");
        this.boat = b; this.slip = s; s.assign();
    }
    public double computeAmount() {                // assumed rates
        long days = java.time.temporal.ChronoUnit.DAYS.between(startDate, endDate) + 1;
        double rate = (type == Type.DAILY) ? 500.0 * days : 100000.0;
        return rate + boat.getLength() * 10;       // length-based surcharge
    }
    public Bill generateBill() {
        bill = new Bill(leaseId, computeAmount());
        return bill;
    }
    public Customer getCustomer() { return customer; }
    public Slip getSlip() { return slip; }
}

// ---------- Bill ----------
class Bill {
    private int billNo;
    private double amount;
    private boolean paid;
    public Bill(int no, double amt) { billNo = no; amount = amt; }
    public void pay() { paid = true; }
    public double getAmount() { return amount; }
    public boolean isPaid() { return paid; }
}

// ---------- Marina (facade) ----------
class Marina {
    private List<Slip> slips = new ArrayList<>();
    private List<Customer> customers = new ArrayList<>();

    public void addSlip(Slip s) { slips.add(s); }
    public void addCustomer(Customer c) { customers.add(c); }

    public List<Slip> searchVacantSlips() {
        List<Slip> result = new ArrayList<>();
        for (Slip s : slips) if (s.isVacant()) result.add(s);
        return result;
    }
    public List<Slip> slipsLeasedTo(Customer c) {
        List<Slip> result = new ArrayList<>();
        for (Lease l : c.getLeases()) result.add(l.getSlip());
        return result;
    }
}

Notes: attributes carry private visibility; the abstract Boat and its subclasses use inheritance; one-to-many associations use List; Lease implements the tasks "creating lease", "computing lease amount" and "assigning boats", and Marina supports "search for vacant slips" and "slips leased to specific customer"; Bill supports the billing system. Rates are assumed.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Analysis and Design chapter 4 questions →