Results 1 to 9 of 9
  1. #1
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default Need some understanding and help!

    Is anyone familiar with using DataSource? My instructor threw this at us for the first time a few days ago, and I need to know to implement it in a code before my finals next week.

    To summarize, I have multiple classes...one being a DataSource class that needs to be linked to an ActionEvent in my GUI class. I was told that it makes handling objects easier.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Is DataSource a general programming term or is it simply the name of the class that your instructor wants you to use that has certain properties?

    edit: does he mean use of DataSource in this context?: http://download.oracle.com/javase/6/...ataSource.html

  3. #3
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    That's just it, I don't know. I've looked at the link hours ago, and have no idea what it is trying to say. My best guess is that a "connection" must be made between my ActionEvent method and this DataSource class. I'm sorry if what I'm saying isn't making sense.

    edit: I guess I have to explain what the program is supposed to do. I have a GUI that contains two buttons (Open and Save) and a jTextArea. I'm suposed to open a text file that contains names and bank account balances and display it in the jTextArea. (I have that part down just fine.) When the save button is pressed, the information saved in a new file. (Fundamentally had this work, but many other things need to happen in between). I have a Person class, a BankAccount class, a Checking class (extends BankAccount), a Savings class (extends BankAccount), and a Bank class (I couldn't tell you what it's supposed to do...I was copying what he was typing, but it looks like it creates a new list<Person>).

    I'm supposed to be able to make deposits or withdrawals and the info is to be saved in a new file and displayed in the jTextArea(I know how to display it). This code deals with a few ArrayLists, which I'm also not too comfortable working with.
    Last edited by Kevinius; 05-14-2011 at 05:16 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Perhaps you want to simply post the assignment verbatim or the link and someone can help translate it for you.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Oh, and there's a tutorial on connecting using DataSource objects here: Connecting with DataSource Objects

  6. #6
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    I edited my previous response with the assignment details. If more detail is needed, let me know. As far as the latest link you sent. It sounds like what I need to do, because the instructor mentioned that this would be useful for databases. We obviously haven't dealt with databases in this class. As I said, he sprung this on us on Tuesday. While he was writing his code, I was struggling trying to catch it all.

  7. #7
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    The ActionEvent section of the BankGUI class isn't complete. I need to be able to tie the other classes together. I have to break the opened file into tokens...hence the StringTokenizer that isn't currently being used.

    Java Code:
     
    public class Person {
    
        List<BankAccount> accounts = new ArrayList<BankAccount>();
        String name;
    
        public Person(List<BankAccount> accounts, String name) {
            this.accounts = accounts;
            this.name = name;
        }
    
        public List<BankAccount> getAccounts() {
            return accounts;
        }
    
        public void setAccounts(List<BankAccount> accounts) {
            this.accounts = accounts;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    //BankAccount class
    public class BankAccount {
    
        private double balance;
        public static int accountNumber = 1000;
        private int myAccount;
      
    
        public BankAccount(double balance) {
            this.balance = balance;
            myAccount = accountNumber;
            accountNumber++;
        }
    
        public int getAccountNumber() {
            return myAccount;
        }
    
        public void setAccountNumber(int aNumber) {
            accountNumber = aNumber;
        }
    
        public double getBalance() {
            return balance;
        }
    
        public void setBalance(double balance) {
            this.balance = balance;
        }
    
    public class Checking extends BankAccount {
    
        private double balance;
        private double withdrawal;
        private double deposit;
    
        public Checking(double balance, double withdrawal, double deposit) {
            super(balance);
            this.balance = balance;
            this.withdrawal = withdrawal;
            this.deposit = deposit;
        }
    
        @Override
        public double getBalance(){
            if (deposit == 0){
                return balance;
            }else if (deposit > 0){
                balance = balance + deposit;
                return balance;
            }else if (withdrawal == 0){
                return balance;
            }else{
                balance = balance - withdrawal;
                return balance;
            }
        }
    }
    
    public class Savings extends BankAccount {
    
        private double balance;
        private double deposit;
        private double withdrawal;
    
        public Savings(double balance, double deposit, double withdrawal) {
            super(balance);
            this.balance = balance;
            this.deposit = deposit;
            this.withdrawal = withdrawal;
        }
        @Override
        public double getBalance(){
            if (deposit == 0){
                return balance;
            }else if (deposit > 0){
                balance = balance + deposit;
                return balance;
            }else if (withdrawal == 0){
                return balance;
            }else{
                balance = balance - withdrawal;
                return balance;
            }
        }
    }
    
    public class Bank {
    
        public static List<Person> person;
    
        static {
            person = new ArrayList<Person>();
        }
    
        public static List<Person> getPerson() {
            return person;
        }
    
        public static void setPerson(List<Person> person) {
            Bank.person = person;
        }
    }
    
    // BankGUI...it's not complete because I don't know anything about this DataSource stuff.
    public class BankGUI extends JPanel implements ActionListener {
    
        static JFrame frame;
        JFileChooser jFileChooser;
        JButton openFileBtn;
        JButton saveFileBtn;
        JTextArea jTextArea;
        JScrollPane jScrollPane;
        List<BankAccount> accounts;
        List<Person> person = new ArrayList<Person>();
    
        public BankGUI() {
            super(new BorderLayout());
            JPanel top = new JPanel();
            JPanel p = new JPanel();
            JPanel bottom = new JPanel(new BorderLayout());
            jFileChooser = new JFileChooser();
            jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            openFileBtn = new JButton("Open");
            openFileBtn.addActionListener((ActionListener) this);
            saveFileBtn = new JButton("Save");
            saveFileBtn.addActionListener(this);
            jTextArea = new JTextArea();
            jScrollPane = new JScrollPane(jTextArea);
    
    
            bottom.setPreferredSize(new Dimension(300, 300));
            p.add(openFileBtn);
            p.add(saveFileBtn);
            top.add(p);
            bottom.add(jScrollPane);
            add(top, BorderLayout.NORTH);
            add(bottom, BorderLayout.CENTER);
    
    
    
        }
    
        private static void createAndShowGUI() {
            frame = new JFrame("Bank GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BankGUI customGUI = new BankGUI();
            frame.setPreferredSize(new Dimension(500, 500));
            frame.add(customGUI);
            frame.pack();
            frame.setVisible(true);
    
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(
                    new Runnable() {
    
                        public void run() {
                            createAndShowGUI();
                        }
                    });
        }
    
        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() instanceof JButton) {
                JButton b = (JButton) ae.getSource();
                if (b == this.openFileBtn) {
                    int returnVal = jFileChooser.showOpenDialog(BankGUI.this);
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
    
    
    
    
                        File file = jFileChooser.getSelectedFile();
                        BufferedReader infile = null;
                        try {
                            infile = new BufferedReader(new FileReader(file));
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(BankGUI.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        String line = null;
                        StringTokenizer st;
                        try {
    
                                while ((line = infile.readLine()) != null) {
                                    st = new StringTokenizer(line);
    
    
    
                                    
                                    jTextArea.append(line + "\n");
    //                                jTextArea.append(p.getName() + "\n");
                                }
    
                        } catch (IOException ex) {
                            Logger.getLogger(BankGUI.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        try {
                            infile.close();
                        } catch (IOException ex) {
                            Logger.getLogger(BankGUI.class.getName()).log(Level.SEVERE, null, ex);
                        }
    
    
    
                    }
                } else {
                    try {
                        String line2 = null;
                        BufferedWriter output = new BufferedWriter(new FileWriter("out-data.txt"));
                        StringTokenizer st = new StringTokenizer(line2);
    
                    } catch (IOException ex) {
                        Logger.getLogger(BankGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }
    
    }
    Last edited by Kevinius; 05-14-2011 at 05:28 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Since you don't appear to be using a database, perhaps my link was wrong. Perhaps you're using this DataSource: javax/activation/DataSource

    And as for myself, yeah, I'd love to see more information about the assignment, in particular the actual assignment requirements.

  9. #9
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    Unfortunately, there is nothing written about the assignment. This code will be on the test (with various things left out that we must fix). When spoken it sounds simple. Open a file and have the contents of the file displayed in the text area. When I click the "Save" button, certain changes (deposits or withdrawals) must be made to some account balances that were in the file and saved into a new file(out-data.txt) and displayed in the text area. I didn't post the the DataSource class, because all it has in it is "public static void read(String filename){ }".

Similar Threads

  1. Help with understanding Threads
    By samp in forum Java Applets
    Replies: 2
    Last Post: 03-09-2011, 06:46 PM
  2. Understanding Iterators
    By Domo230 in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 12:03 AM
  3. Am I understanding this correctly?
    By phixion in forum New To Java
    Replies: 9
    Last Post: 01-27-2011, 05:13 AM
  4. Understanding this recursion
    By Yakg in forum New To Java
    Replies: 6
    Last Post: 01-05-2011, 09:39 PM
  5. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •