DPOO PAC3 2020-2021 2 PDF

Title DPOO PAC3 2020-2021 2
Course Diseño y programación orientada a objetos
Institution Universitat Oberta de Catalunya
Pages 12
File Size 70.2 KB
File Type PDF
Total Downloads 42
Total Views 635

Summary

EJERCICIO 1package edu.uoc;public class Farmer {private String name;private String phoneNumber;public Farmer() throws Exception{ this("Foo", "000000000"); }public Farmer(String name, String phoneNumber) throws Exception{ setName(name); setPhoneNumber(phoneNumber); }public String getName() { return n...


Description

EJERCICIO 1 package edu.uoc.pac3; public class Farmer { private String name; private String phoneNumber; public Farmer() throws Exception{ this("Foo", "000000000"); } public Farmer(String name, String phoneNumber) throws Exception{ setName(name); setPhoneNumber(phoneNumber); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) throws Exception{ String regex = "[0-9]+"; if(phoneNumber.length() != 9) { throw new Exception("[ERROR] Farmer's phone number does not have the correct length"); }else if(!phoneNumber.matches(regex)) { throw new Exception("[ERROR] Farmer's phone number is not a numeric value"); }else { this.phoneNumber = phoneNumber; } } } package edu.uoc.pac3; public class Farm { private int id;

private static int nextId

= 0;

private String name; private String street; private Farmer farmer; private int capacity; public Farm() throws Exception{ this("Default Farm", "Default Street", 30, null); }

public Farm(String name, String street, int capacity, Farmer farmer) throws Exception{ setName(name); setStreet(street); setCapacity(capacity); setFarmer(farmer); setId(); } public int getId() { return id; }

private void setId() { this.id = getNextId(); incNextId(); }

public static int getNextId() { return nextId; }

private void incNextId(){ nextId++; } public String getName() { return name; }

public void setName(String name) throws Exception{ if(name.length()>50) { throw new Exception("[ERROR] Farm's name cannot be longer than 50 characters"); }else { this.name = name; } } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; }

public Farmer getFarmer() { return farmer; } public void setFarmer(Farmer farmer){ this.farmer = farmer; }

public int getCapacity() { return capacity; } public void setCapacity(int capacity) throws Exception{ if(capacity50) { throw new Exception("[ERROR] Farm's name cannot be longer than 50 characters"); }else {

this.name = name; } } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public Farmer getFarmer() { return farmer; }

public void setFarmer(Farmer farmer){ this.farmer = farmer; }

public int getCapacity() { return capacity; } public void setCapacity(int capacity) throws Exception{ if(capacity...


Similar Free PDFs