Skip to content
Snippets Groups Projects
Commit adbc2560 authored by jonas.stirnema's avatar jonas.stirnema
Browse files

Added class, basic getter and setters

parent e47d5291
No related branches found
No related tags found
No related merge requests found
public class Contact {
// Attributes
private String fname;
private DynArray names;
private String address;
private DynArray phoneNumbers;
private DynArray emails;
private DynArray socials;
private String job;
public String getFname() {
return fname;
}
public void setFname(String fname) {
if (!fname.isBlank()) {
this.fname = fname;
} else {
this.fname = "Anon";
}
}
public DynArray getNames() {
return names;
}
public void setNames(DynArray names) {
if (!names.isEmpty()) {
this.names = names;
} else {
this.names = new DynArray(new String[] { "Anon" });
}
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
if (!address.isBlank()) {
this.address = address;
} else {
this.address = "Nowhere";
}
}
public DynArray getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(DynArray phoneNumbers) {
if (!phoneNumbers.isEmpty()) {
this.phoneNumbers = new DynArray(new String[] { "0102030405" });
}
}
public DynArray getEmails() {
return emails;
}
public void setEmails(DynArray emails) {
if (!emails.isEmpty()) {
this.emails = new DynArray(new String[] { "none@none.com" });
}
}
public DynArray getSocials() {
return socials;
}
public void setSocials(DynArray socials) {
if (!socials.isEmpty()) {
this.socials = socials;
} else {
this.socials = new DynArray(new String[] { "@anon" });
}
}
public String getJob() {
return job;
}
public void setJob(String job) {
if (!job.isBlank()) {
this.job = job;
} else {
this.job = "none";
}
}
}
public class DynArray {
private final int basev = 10;
private int size = 0;
private String[] array;
private int capacity;
public DynArray(String[] a) {
this.size = a.length;
this.capacity = this.basev;
this.array = new String[this.capacity];
copy_content(a, this.array);
}
public DynArray() {
this.array = new String[this.capacity];
this.size = 0;
}
public void append(String[] a) {
make_sure_size(a); // REALLOC IF NEEDED
this.size += a.length;
int s = this.size;
// Add the new values
for (int i = s; i < s + a.length; i++) {
// System.out.println("arr[" + (i - a.length) + "] = " + a[i - s]);
this.array[i - a.length] = a[i - s];
}
}
public void pop() {
if (this.size < 1) {
System.err.println("Cannot remove from an epmty list");
return;
}
this.size--;
}
public void make_sure_size(String[] a) {
if ((this.size + a.length) > this.capacity) // Overflow?
{
// Realloc new array
this.capacity *= 2;
String[] new_arr = new String[this.capacity];
copy_content(this.array, new_arr);
this.array = new_arr; // Copy pointer
}
}
public void show_it() {
System.out.println(this.intoString());
}
public String intoString() {
String s = "[ ";
for (int i = 0; i < this.size; i++) {
s = s.concat(array[i]);
s = s.concat(", ");
}
s = s.concat(" ]");
return s;
}
public boolean isEmpty() {
return size == 0;
}
private static void copy_content(String[] source, String[] dest) {
// System.out.println("Source lenght" + source.length);
// System.out.println("Dest lenght" + dest.length);
if (source.length > dest.length) {
throw new Error("Cannot copy : second array smaller than first");
}
for (int i = 0; i < source.length; i++) {
dest[i] = source[i];
}
}
}
\ No newline at end of file
package src;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hey"); DynArray ar1 = new DynArray(new String[] { "Test", "Test2", "Test3" });
ar1.show_it();
ar1.append(new String[] { "Test4", "Test5", "Test6" });
ar1.show_it();
ar1.pop();
ar1.show_it();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment