8 Replies - 344 Views - Last Post: 01 November 2017 - 01:43 PM Rate Topic: -----

#1 Jubie3018  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 17

Help displaying the right number

Posted 29 October 2017 - 03:14 PM

Hi, I need to display a set of significant check numbers (if multiple checks were used),
but when I print, it displays the check number of the very last check that was used.

Thanks in advance

    public static void listChecks() {
        String message;
        frame.setVisible(false);

        message = "List of All Checks for " + c.getName() + "\n" + "\n";
        message += String.format("%-20s%-20s%15s\n", "ID", "Check #", "Amount");

        for (int i = 0; i < c.getTransCount(); i++) {

            if (c.getTrans(i).getTransId() == 1) {
                String amount = fmt.format(c.getTrans(i).getTransAmount());
               
                message += String.format("%-20d%-20d%15s\n", c.getTrans(i).getTransNumber(),chk.getCheckNumber(), amount);
            }
        }

        JTextArea text = new JTextArea(message);
        text.setBorder(null);
        text.setOpaque(false);
        text.setFont(new Font("Monospaced", Font.PLAIN, 14));
        JOptionPane.showMessageDialog(null, text);

        frame.setVisible(true);
    }



Check Class:

public class Check extends Transaction {

    private int checkNumber; // check number for each check transaction

    public Check(int tId, double tAmt, int tCount, int checkNumber) {
        super(tCount, tId, tAmt);
        this.checkNumber = checkNumber;
    }

    public int getCheckNumber() {

        return checkNumber;
    }

    public void setCheckNumber(int checkNumber) {
        this.checkNumber = checkNumber;
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Help displaying the right number

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: Help displaying the right number

Posted 29 October 2017 - 03:41 PM

Is chk an instance variable? That could be your problem since it doesn't change.
Was This Post Helpful? 0
  • +
  • -

#3 Jubie3018  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 17

Re: Help displaying the right number

Posted 29 October 2017 - 03:47 PM

View PostCasiOo, on 29 October 2017 - 03:41 PM, said:

Is chk an instance variable? That could be your problem since it doesn't change.


 public static CheckingAccount c;
    public static DecimalFormat fmt;

    public static boolean firstTime = true;
    public static double initialBalance, transAmount;
    public static JFrame frame;
    public static JTextArea ta;
    public static Check chk;
    public static Deposit dpst;



It's static. It should be static since I want that to change, right?
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: Help displaying the right number

Posted 29 October 2017 - 04:35 PM

None of those fields should be static or public.

Look, you call chk.getCheckNumber() inside your loop, but your chk reference never change inside the loop, so it will keep printing the same value
Also i'm not sure why you have an if condition for trans id equals 1 if (c.getTrans(i).getTransId() == 1) {
Was This Post Helpful? 0
  • +
  • -

#5 Jubie3018  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 17

Re: Help displaying the right number

Posted 29 October 2017 - 09:32 PM

View PostCasiOo, on 29 October 2017 - 04:35 PM, said:

None of those fields should be static or public.

Look, you call chk.getCheckNumber() inside your loop, but your chk reference never change inside the loop, so it will keep printing the same value
Also i'm not sure why you have an if condition for trans id equals 1 if (c.getTrans(i).getTransId() == 1) {


Yeah.. That's the part that I don't know how to fix. I don't know how to change the reference inside the loop.

Also that part is for some other things to be done that I did not include above. I had tomake sure that the input is equal to 1 to list the check.
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,312
  • Joined: 20-March 10

Re: Help displaying the right number

Posted 29 October 2017 - 11:44 PM

As CasiOo was saying chk in listChecks should be private, public is only used when you want to use a method from somewhere outside of the class like in another class.

your class Check needs a redesign as its a design issue that is the point of contention here.....


your constructor as it is ....
public Check(int tId, double tAmt, int tCount, int checkNumber) {
        super(tCount, tId, tAmt);
        this.checkNumber = checkNumber;
    }




probably needs to be something like this where checkNumber is an int array..... ie int[]...

public Check(int tId, double tAmt, int tCount, int[] checkNumber) {
        super(tCount, tId, tAmt);
        this.checkNumber[tCount] = checkNumber;
    }




in your setter it needs to updated as something like this......

public void setCheckNumber(int[] checkNumber) {
        this.checkNumber[tCount] = checkNumber;
        tCount++;
    }

presuming tCount does keep track of the amount checks added....

having two variables called checkNumber is also very confusing and you might want to change the local one to something else....

your getter obviously needs to be changed too to incorporate the change from int to int[] but you get the gist of it......

This post has been edited by snoopy11: 29 October 2017 - 11:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3444
  • View blog
  • Posts: 15,606
  • Joined: 20-September 08

Re: Help displaying the right number

Posted 30 October 2017 - 03:33 AM

Quote

It's static. It should be static since I want that to change, right?

This is how you should deal with static (for now)

http://technojeeves....is-a-swear-word
Was This Post Helpful? 0
  • +
  • -

#8 Jubie3018  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 17

Re: Help displaying the right number

Posted 01 November 2017 - 12:12 PM

Hi, I've been advised to add the lines " dpst = (Deposit)c.getTrans(i)" and "chk = (Check)c.getTrans(i)" when displaying the checks and deposits. // line 14 and 11 respectively.

But as soon as I added that, it displayed an error of :
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: main.Transaction cannot be cast to main.Check
at main.Main.listChecks(Main.java:280)
at main.OptionsPanel$OptionListener.actionPerformed(OptionsPanel.java:68)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.window.dispatchEventImpl(window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

I did not understand what that error meant, can anyone help please?

public static void listChecks() {
        String message;
        int checknumb;

        frame.setVisible(false);

        message = "List of All Checks for " + c.getName() + "\n" + "\n";
        message += String.format("%-20s%-20s%15s\n", "ID", "Check #", "Amount");

        for (int i = 0; i < c.getTransCount(); i++) {

            if (c.getTrans(i).getTransId() == 1) {
                String amount = fmt.format(c.getTrans(i).getTransAmount());
                chk = (Check)c.getTrans(i);
                message += String.format("%-20d%-20d%15s\n", c.getTrans(i).getTransNumber(), chk.getCheckNumber(), amount);
            }
        }
        JTextArea text = new JTextArea(message);

        text.setBorder(
                null);
        text.setOpaque(
                false);
        text.setFont(
                new Font("Monospaced", Font.PLAIN, 14));
        JOptionPane.showMessageDialog(
                null, text);

        frame.setVisible(
                true);
    }



    public static void listDeposits() {
        String message;
        frame.setVisible(false);

        message = "List of All Deposits Made for " + c.getName() + "\n" + "\n";
        message += String.format("%-7s%10s%15s%20s\n", "ID", "Check", "Cash", "Amount");

        for (int i = 0; i < c.getTransCount(); i++) {
            if (c.getTrans(i).getTransId() == 2) {
                String amount = fmt.format(c.getTrans(i).getTransAmount());
                dpst = (Deposit)c.getTrans(i);
                message += String.format("%-7s%10s%15s%20s\n", c.getTrans(i).getTransNumber(), dpst.getCheckAmt(), dpst.getCashAmt(), amount);
            }
        }
        JTextArea text = new JTextArea(message);
        text.setBorder(null);
        text.setOpaque(false);
        text.setFont(new Font("Monospaced", Font.PLAIN, 14));
        JOptionPane.showMessageDialog(null, text);

        frame.setVisible(true);
    }

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3444
  • View blog
  • Posts: 15,606
  • Joined: 20-September 08

Re: Help displaying the right number

Posted 01 November 2017 - 01:43 PM

Transaction can't necessarily be cast to Check. You'd probably need to type-check it first. That shows a weakness of holding mixed types in a collection.
Actually a check is not a transaction anyway
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1