In: Computer Science
Specifications:
This project will have two data classes and a tester class.
Design:
Create a solution for this programming task. You will need to have the following parts:
Here are the interfaces for your data classes:
package project1;
import java.util.ArrayList;
public interface Person {
public void setId (String id);
public String getId ();
public void setName (String name);
public String getName ();
public void addTHING (String THING);
public void removeTHING (String THING);
public ArrayList<String> getTHINGs ();
public String toString();
}
package project1;
import java.util.ArrayList;
public interface PersonDataManager {
public void addPerson (Person p);
public
Person getPerson (String id);
public
ArrayList<String> getTHINGSDESCRIPTION
(ArrayList<String> THINGs); //for this, its just a set of
descriptions for THINGs (just use THINGdescription1, 2, 3 for each
one and ill do the rest
public void writeToFile ();
public void writeToFile (String fn);
public String toString();
}
For ace's, they are either going to be a 1 or 0 for 10 different attributes. I only need a basic skeleton for the code and can do the rest but I am struggling with the starting point
Try to edit code, its simple code to understand how interface will be implemented..
/*****************************Person1.java************************/
package project1;
import java.util.ArrayList;
import java.util.Iterator;
/**
* The Class Person1.
*/
public class Person1 implements Person {
/** The id. */
private String id;
/** The name. */
private String name;
/** The things. */
private ArrayList<String> things;
/**
* Instantiates a new person 1.
*
* @param id the id
* @param name the name
*/
public Person1(String id, String name) {
super();
this.id = id;
this.name = name;
things = new
ArrayList<>();
}
/*
* (non-Javadoc)
*
* @see project1.Person#setId(java.lang.String)
*/
@Override
public void setId(String id) {
this.id = id;
}
/*
* (non-Javadoc)
*
* @see project1.Person#getId()
*/
@Override
public String getId() {
return id;
}
/*
* (non-Javadoc)
*
* @see project1.Person#setName(java.lang.String)
*/
@Override
public void setName(String name) {
this.name = name;
}
/*
* (non-Javadoc)
*
* @see project1.Person#getName()
*/
@Override
public String getName() {
return name;
}
/*
* (non-Javadoc)
*
* @see
project1.Person#addTHING(java.lang.String)
*/
@Override
public void addTHING(String THING) {
this.things.add(THING);
}
/*
* (non-Javadoc)
*
* @see
project1.Person#removeTHING(java.lang.String)
*/
@Override
public void removeTHING(String THING) {
Iterator<String> itr =
things.iterator();
while (itr.hasNext()) {
String thing = itr.next();
if (thing.equalsIgnoreCase(THING)) {
itr.remove();
}
}
}
/*
* (non-Javadoc)
*
* @see project1.Person#getTHINGs()
*/
@Override
public ArrayList<String> getTHINGs() {
return things;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Person1 [id=" + id + ",
name=" + name + ", things=" + things + "]";
}
}
/**************************************PersonData.java**********************/
package project1;
import java.io.File;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
/**
* The Class PersonData.
*/
public class PersonData implements PersonDataManager {
/** The persons. */
private ArrayList<Person> persons;
/**
* Instantiates a new person data.
*/
public PersonData() {
persons = new
ArrayList<>();
}
/* (non-Javadoc)
* @see
project1.PersonDataManager#addPerson(project1.Person)
*/
@Override
public void addPerson(Person p) {
persons.add(p);
}
/* (non-Javadoc)
* @see
project1.PersonDataManager#getPerson(java.lang.String)
*/
@Override
public Person getPerson(String id) {
Person actPerson = null;
for (Person person : persons) {
if (person.getId().equalsIgnoreCase(id)) {
actPerson = person;
}
}
return actPerson;
}
/* (non-Javadoc)
* @see
project1.PersonDataManager#getTHINGSDESCRIPTION(java.util.ArrayList)
*/
@Override
public ArrayList<String>
getTHINGSDESCRIPTION(ArrayList<String> THINGs) {
return null;
}
/* (non-Javadoc)
* @see project1.PersonDataManager#writeToFile()
*/
@Override
public void writeToFile() {
}
/* (non-Javadoc)
* @see
project1.PersonDataManager#writeToFile(java.lang.String)
*/
@Override
public void writeToFile(String fn) throws Exception
{
Writer rw = new PrintWriter(new
File(fn));
for (Person person : persons) {
rw.write(person.toString());
}
}
}
/*************************************TestPerson.java*********************/
package project1;
public class TestPerson {
public static void main(String[] args) {
Person1 person1 = new
Person1("A101", "Virat");
person1.addTHING("Thing1");
person1.addTHING("Thing2");
person1.addTHING("Thing3");
PersonData personData = new
PersonData();
personData.addPerson(person1);
personData.writeToFile("out.txt");
System.out.println(personData.getPerson("A101"));
}
}
/***************output***************/
Person1 [id=A101, name=Virat, things=[Thing1, Thing2,
Thing3]]
/****************out.txt********************/
Person1 [id=A101, name=Virat, things=[Thing1, Thing2, Thing3]]
Please let me know if you have any doubt or modify the answer, Thanks :)