3 Replies - 1899 Views - Last Post: 06 March 2011 - 02:20 PM Rate Topic: -----

#1 Amaramb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-March 11

If Statement Inside Set Method Not Working?

Posted 06 March 2011 - 02:59 AM

I'm working on an assignment for my entry level java class and having trouble with returning a value. I have written out my code and I am not getting any errors but for some reason, no matter what I enter when running the program, I get 0 for the invoice and date. The instructions for the assignment indicate that I should force the invoice and date to 0 if they don't meet the minimum number (specified in the code), but even if the number is within the range that I set it for, it's still giving me 0. I assume that this is some kind of an issue with my If statements because the balance set method seems to work just fine (which is not using an If). I have tried moving things around a few different ways with no luck. Can anyone see what my problem is?

Invoice class:
import javax.swing.*;
public class Invoice {

    //data members
    private double balance;
    private int invoice, month, day, year;

    //constructor
    public Invoice(int i, double b, int m, int d, int y){
        setInvoice(i);
        setBalance(B)/>;
        setMonth(m);
        setDay(d);
        setYear(y);
    }
     public void setInvoice(int i){
        if (i >= 1000){
            i = invoice;
        }else{
            invoice = 0;
        }
    }
    public void setBalance(double B)/>{
        balance = b;
    }
    public void setMonth(int m){
        if (m >= 1 && m <= 12){
                m = month;
            }
        else{
                m = 0;
        }
    }

    public void setDay(int d){
        if (d >= 1 && d <= 31){
            d = day;
        }
        else{
            d = 0;
        }
    }

    public void setYear(int y){
        if (y >= 2005 && y <= 2012){
            y = year;
        }
        else{
            y = 0;
        }
    }
    public int getInvoice(){
        return invoice;
    }
    public double getBalance(){
        return balance;
    }
    public int getMonth(){
        return month;
    }
    public int getDay(){
        return day;
    }
    public int getYear(){
        return year;
    }
    public void displayResults(){

    String results;
        results = new String();

        results = JOptionPane.showInputDialog(null, "\n\t Your invoice number is: " + invoice +
                "\n\t The balance owed is: $" + balance +
                "\n\t The balance is due on: " + month + "/" + day + "/" + year +
                "\n" );
    }
}


Main class:

package quiz2;
import javax.swing.*;
/**
 *
 * @author Amara
 */
public class Main {
    private JFrame mainWindow;

    public static void main(String[] args) {
        Main main = new Main();
        main.start();
    }
    //constructor
    public Main(){
        mainWindow = new JFrame ("Invoice");
        mainwindow.setSize(500, 400);
        mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainwindow.setVisible(false);
    }

    public void start(){
        int inv = Integer.parseInt(JOptionPane.showInputDialog(mainWindow, "Please enter the invoice number: "));
        double bal = Double.parseDouble(JOptionPane.showInputDialog(mainWindow, "Please enter the balance: " ));
        int mon = Integer.parseInt(JOptionPane.showInputDialog(mainWindow, "Please enter month due: "));
        int day = Integer.parseInt(JOptionPane.showInputDialog(mainWindow, "Please enter day due: "));
        int yr = Integer.parseInt(JOptionPane.showInputDialog(mainWindow, "Please enter year due: "));

        Invoice invoice = new Invoice(inv, bal, mon, day, yr);

        invoice.displayResults();
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: If Statement Inside Set Method Not Working?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,296
  • Joined: 20-September 08

Re: If Statement Inside Set Method Not Working?

Posted 06 March 2011 - 05:44 AM

In setInvoice

Quote

i = invoice;


should be

invoice = i;

Was This Post Helpful? 1
  • +
  • -

#3 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: If Statement Inside Set Method Not Working?

Posted 06 March 2011 - 11:14 AM

And
d = day;
should be
day = d;
.
Was This Post Helpful? 1
  • +
  • -

#4 Amaramb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-March 11

Re: If Statement Inside Set Method Not Working?

Posted 06 March 2011 - 02:20 PM

Wow, I feel silly. I've done that more than once. Thank you so much!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1