Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8444193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:20:36+00:00 2026-06-10T09:20:36+00:00

I’m working on a java swing application which handles Customer management for a shop.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:20:38+00:00Added an answer on June 10, 2026 at 9:20 am

    The current approach would drive me mad. You have a Customer class with 2 completely distinct behaviors:

    • When using the default constructor, you are actually storing the contents of the whole database in memory (well, at least of the customer table). So in this case a Customer instance actually represents the whole list of customers
    • When using the constructor with parameters, a Customer object now represents one Customer

    In short, whenever you encounter a Customer instance 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 JTable only 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 Customer class 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.