I’m working on a java swing application which handles Customer management for a shop.
I’m trying to build it based on the mvc pattern, but actually i’m somewhat unexperienced in it.
Basically, theres a View with some Textfields for Customer Creation and of course the class called Customer. If the form is submitted an object of Customer is created and saved to the database.
Now, my problem was setting up a view to show all Customers in a JTable. Actually, not the view itself was the problem, but refreshing the view, when a new customer was added or a customer was changed.
Therefore i created a new class called “Customers” which had an arraylist with all customers in it and everytime a customer was created, it was added to this arraylist.
When the mainframe of my application started, an object of “Customers” was created, fetching all customers from my database, putting it in the arraylist just for the jtable.
the jtable was added to the object of customers as a listener and had an interface called CustomerListener implemented, which set a new model everytime the arraylist of customers changed.
mh okay, now i really have problems explaining what my problem is but, basically i thought this class “customers” was redundant, so i just added the arraylist and stuff to my “normal” “Customer” class:
package v1a;
import java.sql.ResultSet;
import java.util.ArrayList;
public class Customer {
public static final String KUNDENNUMMER = "Kundennummer";
public static final String ANREDE = "Anrede";
public static final String NACHNAME = "Nachname";
public static final String VORNAME = "Vorname";
public static final String PLZ = "PLZ";
public static final String ORT = "Ort";
public static final String STRASSE = "Strasse";
public static final String LAND = "Land";
public static final String TELEFON = "Telefon";
public static final String MOBIL = "Mobil";
public static final String EMAIL = "Email";
public static final String[] CUSTOMER_FIELDS = { KUNDENNUMMER, ANREDE, NACHNAME, VORNAME, PLZ, ORT, STRASSE, LAND, TELEFON, MOBIL, EMAIL };
private String kn, anrede, nachname, vorname, plz, ort, strasse, land, telefon, mobil = "", email = "";
private ArrayList<Customer> customers = new ArrayList<Customer>();
private ArrayList<ICustomerModelListener> listeners = new ArrayList<ICustomerModelListener>();
public Customer() {
getAllFromDatabase();
}
public Customer(String[] str) {
this.kn = str[0];
this.anrede = str[1];
this.nachname = str[2];
this.vorname = str[3];
this.plz = str[4];
this.ort = str[5];
this.strasse = str[6];
this.land = str[7];
this.telefon = str[8];
this.mobil = str[9];
this.email = str[10];
}
public void getAllFromDatabase(){
SQL.getInstance();
ArrayList<Customer> arrlist = new ArrayList<Customer>();
ResultSet rs = SQL.select("SELECT kundennummer, anrede, name, vorname, strasse, plz, ort, land, telefon, mobil, email FROM kunden");
try {
while(rs.next()){
String[] values = new String[Customer.CUSTOMER_FIELDS.length];
values[0] = String.valueOf(rs.getInt("kundennummer"));
values[1] = rs.getString("anrede");
values[2] = rs.getString("name");
values[3] = rs.getString("vorname");
values[4] = rs.getString("strasse");
values[5] = String.valueOf(rs.getInt("plz"));
values[6] = rs.getString("ort");
values[7] = rs.getString("land");
values[8] = rs.getString("telefon");
values[9] = rs.getString("mobil");
values[10] = rs.getString("email");
Customer c = new Customer(values);
arrlist.add(c);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
SQL.cleanup();
this.customers = arrlist;
for(ICustomerModelListener l : listeners){
l.CustomerChanged();
}
}
public ArrayList<Customer> getAll(){
return customers;
}
public void addCustomer(Customer customer){
customers.add(customer);
for(ICustomerModelListener l : listeners){
l.CustomerChanged();
}
}
public void addListener(ICustomerModelListener listener){
listeners.add(listener);
}
public static boolean knExists(int kn){
boolean bool = false;
SQL.getInstance();
ResultSet rs = SQL.select("SELECT kundennummer FROM kunden WHERE kundennummer = "+kn);
try {
while(rs.next()){
bool = true;
}
} catch (Exception e){
System.out.println(e.getMessage());
}
SQL.cleanup();
return bool;
}
public static int getFreeKn(){
SQL.getInstance();
ResultSet rs = SQL.select("SELECT kundennummer FROM kunden ORDER BY kundennummer DESC LIMIT 1");
int kn = 0;
try {
while(rs.next()){
kn = rs.getInt("kundennummer");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
kn++;
SQL.cleanup();
return kn;
}
public static Customer getByKn(int kn){
if(knExists(kn)){
SQL.getInstance();
ResultSet rs = SQL.select("SELECT * FROM kunden WHERE kundennummer = "+kn);
String[] values = new String[CUSTOMER_FIELDS.length];
try {
while(rs.next()){
values[0] = String.valueOf(rs.getInt("kundennummer"));
values[1] = rs.getString("anrede");
values[2] = rs.getString("name");
values[3] = rs.getString("vorname");
values[4] = String.valueOf(rs.getInt("plz"));
values[5] = rs.getString("ort");
values[6] = rs.getString("strasse");
values[7] = rs.getString("land");
values[8] = rs.getString("telefon");
values[9] = rs.getString("mobil");
values[10] = rs.getString("email");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
Customer customer = new Customer(values);
SQL.cleanup();
return customer;
} else {
return null;
}
}
public boolean save() {
SQL.getInstance();
boolean bool = SQL.saveUser(this.kn, this.anrede, this.nachname, this.vorname, this.plz, this.ort, this.strasse, this.land, this.telefon, this.mobil, this.email);
SQL.cleanup();
return bool;
}
public String getValue(String s){
switch (s){
case KUNDENNUMMER:
return this.kn;
case ANREDE:
return this.anrede;
case NACHNAME:
return this.nachname;
case VORNAME:
return this.vorname;
case PLZ:
return this.plz;
case ORT:
return this.ort;
case STRASSE:
return this.strasse;
case LAND:
return this.land;
case TELEFON:
return this.telefon;
case MOBIL:
return this.mobil;
case EMAIL:
return this.email;
default :
return "";
}
}
}
NOW to my question:
is this the right way of modelling such a class regarding mvc pattern?
maybe my first approach was “cleaner” because i had something like a “container” which had all customers in it and the “real” customer class.
The current approach would drive me mad. You have a
Customerclass with 2 completely distinct behaviors:Customerinstance actually represents the whole list of customersCustomerobject now represents oneCustomerIn short, whenever you encounter a
Customerinstance in your code, you know absolutely nothing.Further, storing the whole database in memory might be a bit overkill (although probably doable for a limited amount of customers). However, if you will be using this approach for more tables, you will quickly run out-of-memory. Consider only retrieving the data you actually need (for example in a
JTableonly a certain numbers of customers are visible at the same time, so no need to fetch them all).And then their is the problem of mixing the business logic with the database logic. I would suggest to clearly separate your
Customerclass from the actual database access. You never know you will switch databases in the future (or opt for something different then a database). You do not want to rewrite your whole application at that point, just the data-access layer