-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b3e8e37
Showing
63 changed files
with
2,061 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.mycompany.app</groupId> | ||
<artifactId>my-app</artifactId> | ||
<packaging>jar</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>my-app</name> | ||
<url>http://maven.apache.org</url> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.swinglabs</groupId> | ||
<artifactId>swing-layout</artifactId> | ||
<version>1.0.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.xerial</groupId> | ||
<artifactId>sqlite-jdbc</artifactId> | ||
<version>3.16.1</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
119 changes: 119 additions & 0 deletions
119
src/main/java/com/mycompany/app/applications/PetManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package applications; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.List; | ||
import java.util.Observable; | ||
|
||
import javax.swing.JFrame; | ||
import javax.swing.SwingUtilities; | ||
|
||
import domain.Person; | ||
import persistence.ActiveRecordManager; | ||
import ui.ListUpdate; | ||
import ui.PetManagerFrame; | ||
import ui.StatisticWindow; | ||
|
||
public class PetManager extends Observable { | ||
|
||
private List<Person> peopleList; | ||
|
||
public PetManager() { | ||
peopleList = Person.findAll(); | ||
} | ||
|
||
public List<Person> getPeopleList() { | ||
return peopleList; | ||
} | ||
|
||
public Person getPersonAt(int position) { | ||
return peopleList.get(position); | ||
} | ||
|
||
public void addPerson(Person person) { | ||
peopleList.add(0,person); | ||
person.save(); | ||
setChanged(); | ||
ListUpdate up = new ListUpdate(); | ||
up.addInserts(new int[] {0}); | ||
notifyObservers(up); | ||
} | ||
|
||
public void removePeople(List<Person> pepoleToRemove) { | ||
int[] removeIndexes = new int[pepoleToRemove.size()]; | ||
for(int i = 0; i < pepoleToRemove.size(); i++) { | ||
Person p = pepoleToRemove.get(i); | ||
removeIndexes[i] = peopleList.indexOf(p); | ||
p.delete(); | ||
} | ||
peopleList.removeAll(pepoleToRemove); | ||
setChanged(); | ||
ListUpdate up = new ListUpdate(); | ||
up.addDeletes(removeIndexes); | ||
notifyObservers(up); | ||
} | ||
|
||
public void setPerson(int position, Person personToReplace) { | ||
personToReplace.save(); | ||
peopleList.set(position, personToReplace); | ||
setChanged(); | ||
ListUpdate up = new ListUpdate(); | ||
up.addUpdates(new int[] {position}); | ||
notifyObservers(up); | ||
} | ||
|
||
public double getAveragePetPerPerson() { | ||
double sum = 0; | ||
for(Person p : peopleList) { | ||
sum += p.getPets().size(); | ||
} | ||
return sum / peopleList.size(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
createTablesIfMissing(); | ||
SwingUtilities.invokeLater(new Runnable() { | ||
public void run() { | ||
PetManager application = new PetManager(); | ||
PetManagerFrame editView = new PetManagerFrame(application); | ||
editView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
editView.setVisible(true); | ||
StatisticWindow toolbar = new StatisticWindow(editView, application); | ||
toolbar.setVisible(true); | ||
editView.setLocation(100, 100); | ||
|
||
// // Second view to test observer pattern | ||
// PetManagerFrame editView2 = new PetManagerFrame(application); | ||
// editView2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
// editView2.setVisible(true); | ||
// StatisticWindow toolbar2 = new StatisticWindow(editView2, application); | ||
// toolbar2.setVisible(true); | ||
// editView2.setLocation(editView.getX() + editView.getWidth() + toolbar.getWidth() + 20, editView.getY()); | ||
} | ||
}); | ||
} | ||
|
||
private static void createTablesIfMissing() { | ||
String sql = "select * from people"; | ||
try { | ||
ActiveRecordManager.execute(sql); | ||
} catch (SQLException e) { | ||
try { | ||
createTables(); | ||
} catch (SQLException e1) { | ||
e1.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private static void createTables() throws SQLException{ | ||
Connection connection = ActiveRecordManager.getConnection(); | ||
Statement stat = connection.createStatement(); | ||
stat.executeUpdate("drop table if exists people;"); | ||
stat.executeUpdate("drop table if exists pet;"); | ||
stat.executeUpdate("create table people (id INTEGER PRIMARY KEY AUTOINCREMENT, name, job);"); | ||
stat.executeUpdate("create table pet (id INTEGER PRIMARY KEY AUTOINCREMENT,owner INTEGER REFERENCES people(id), name, breed);"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package domain; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Observable; | ||
|
||
import persistence.ActiveRecord; | ||
import persistence.ActiveRecordManager; | ||
|
||
public class Person extends Observable implements ActiveRecord { | ||
|
||
public String name, job; | ||
private int id = NOTINDB; | ||
private List<Pet> pets; | ||
|
||
public Person(String name, String job) { | ||
this.name = name; | ||
this.job = job; | ||
pets = new ArrayList<Pet>(); | ||
} | ||
|
||
public Person(ResultSet row) throws SQLException { | ||
this(row.getString("name"), row.getString("job")); | ||
this.id = row.getInt("id"); | ||
} | ||
|
||
public void addPet(Pet p) { | ||
pets.add(p); | ||
p.setOwner(this); | ||
setChanged(); | ||
notifyObservers(p); | ||
} | ||
|
||
public boolean removePet(Pet p) { | ||
if (pets.remove(p)) { | ||
p.delete(); | ||
setChanged(); | ||
notifyObservers(p); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public List<Pet> getPets() { | ||
return pets; | ||
} | ||
|
||
public int getID() { | ||
return id; | ||
} | ||
|
||
/** | ||
* returns false if saving the {@link Person} was not successful. | ||
*/ | ||
public boolean save() { | ||
try { | ||
if (!isInDB()) | ||
id = ActiveRecordManager.executeInsert("insert into people (name,job) values (?,?)", name, job); | ||
else { | ||
ActiveRecordManager.execute("UPDATE people SET name = ?, job = ? WHERE id = ?", name, job, Integer.toString(id)); | ||
} | ||
for (Pet p : pets) { | ||
p.save(); | ||
} | ||
} catch (SQLException e) { | ||
System.err.println(e); | ||
return false; | ||
} | ||
setChanged(); | ||
notifyObservers(this); | ||
return true; | ||
} | ||
|
||
/** | ||
* returns false if deleting the {@link Person} was not successful. | ||
*/ | ||
public boolean delete() { | ||
try { | ||
if (isInDB()) { | ||
ActiveRecordManager.execute("DELETE FROM pet WHERE owner=?;", Integer.toString(id)); | ||
List<Integer> lastIdListPet = ActiveRecordManager.getIntegerList("SELECT MAX(id) FROM pet;"); | ||
ActiveRecordManager.execute("UPDATE sqlite_sequence SET seq=? WHERE name='pet';", lastIdListPet.get(0).toString()); | ||
ActiveRecordManager.execute("DELETE FROM people WHERE id=?;", Integer.toString(id)); | ||
List<Integer> lastIdList = ActiveRecordManager.getIntegerList("SELECT MAX(id) FROM people;"); | ||
ActiveRecordManager.execute("UPDATE sqlite_sequence SET seq=? WHERE name='people';", lastIdList.get(0).toString()); | ||
} | ||
} catch (SQLException e) { | ||
System.err.println(e); | ||
return false; | ||
} | ||
setChanged(); | ||
notifyObservers(this); | ||
return true; | ||
} | ||
|
||
public boolean isInDB() { | ||
return id > NOTINDB; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof Person) { | ||
Person pers = (Person) obj; | ||
if(isInDB()){ | ||
return id == pers.getID(); | ||
} | ||
else{ | ||
return name.equals(pers.name) && pets.equals(pers.pets) && job.equals(pers.job); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ID: " + id + " Name: " + name + " Job: " + job; | ||
} | ||
|
||
public static List<Person> findAll() { | ||
String sql = "select * from people;"; | ||
List<Person> lp = ActiveRecordManager.getObjectList(sql, Person.class); | ||
loadPets(lp); | ||
return lp; | ||
} | ||
|
||
public static Person findByID(int id) { | ||
String sql = "select * from people WHERE id = " + id + ";"; | ||
List<Person> res = ActiveRecordManager.getObjectList(sql, Person.class); | ||
if (res.isEmpty()) | ||
return null; | ||
else { | ||
loadPets(res); | ||
return res.get(0); | ||
} | ||
} | ||
|
||
private static void loadPets(List<Person> people) { | ||
for (Person person : people) { | ||
person.pets = Pet.findByOwner(person); | ||
for (Pet pet : person.pets) { | ||
pet.setOwner(person); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.