简体   繁体   中英

Structuring My Java Application

I am planning out a Java application that will store information and pictures about items in my coin collection. For example, each coin will have its own record page. On that page there will be text fields where user-entered information will be shown, along with pictures of that particular coin. The user will have the options to add, remove, and update the records in this application.

My question is, how do I go about storing all of this data? Right now, I have most of the GUI designed and working properly. But I am unsure of the "correct" way to store and organize all the data and images for each record. Should I be pushing this data into an SQL database when the record is created and then pull from it when the user is browsing that record? Or should I create some sort of directory structure where each record has its own directory which contains the images and a text file of all the data fields?

Also, I plan to distribute this application to some of my friends so I want to make sure that everything will work correctly for them without having to install any other software (besides Java).

I am pretty new to writing these kinds of applications as I usually just write command-line scripts. Any help that anybody can provide will be greatly appreciated!

Thank you.

you can have a relational database like MySql with below basic schema.

Tables : Coins, user_entered_infos, Users

Coins - Id(PK), name, image_link

Users - Id(PK), first_name, second_name

user_entered_infos - Id(PK), coin_id(FK), user_id(FK), user_entered_info

In this way you can select for each coin you can select all the user entered information from user_entered_infos table and view it. And when user add or remove from the coin page remove it from the table.

A database may be overkill for your needs and is a whole learning-curve unto itself. You might get what you are after with some Java's built-in object serialization. Serialzation is an easy way to save your objects to a file or pass them to other applications.

Consider the following code a very simple sketch for how you might approach this problem. I'm sure you have much more detailed objects than what I am about to show you, this code's purpose is to give you an general idea for how to serialize an object and save it.

import java.io.*;
import java.util.ArrayList;

public class CoinCollectionApp {

    static File collectionFile = new File("path/to/file/coin-collection.obj");

    public static void main(String[] args) {
        ArrayList<Coin> coins = new ArrayList<>();
        Coin lincolnPenny = new Coin("Lincoln penny", 1955, "fine");
        coins.add(lincolnPenny);
        saveCollection(coins);
    }

    static void saveCollection(ArrayList<Coin> coinCollection) {
        try {
            FileOutputStream fout = new FileOutputStream(collectionFile);
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(coinCollection);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    static class Coin implements Serializable {
        private static final long serialVersionUID = 1L;
        String name;
        int year;
        String quality;

        public Coin(String name, int year, String quality) {
            this.name = name;
            this.year = year;
            this.quality = quality;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM