抛出异常的数据验证

时间:2013-12-20 02:29:32

标签: java validation double illegalargumentexception

我试图在一个数字中读取一年中第一季度的经纪人收入。我想确保不能输入0或更少但是当我输入0时它只是接受它并且不会抛出例外? 我做错了什么?非常感谢任何帮助。

 public void setQuarter1(double newQuarter1)
 {
   if ( newQuarter1 > 0)
     quarter1 = newQuarter1;
   else
     throw new IllegalArgumentException("new quarter must be > 0.0");
 }

好的,这是我的整个作业代码

import java.util.Scanner;



public class Broker {

//(a) declare instance variables

private String department, firstName, lastName;
private double quarter1, quarter2, quarter3, quarter4;




//(b) Access methods for instance variables

public void setDepartmentName(String newName)
{
    department=newName;
}


public String getDepartment ()
{
    return department;
}

//set and get methods for first name

public void setFirstName (String newFirstName)
{
    firstName=newFirstName;
}


public String getFirstName ()
{
    return firstName;
}

//set and get methods for last name

public void setLastName(String newLastName)
{
    lastName=newLastName;
}

public String getLastName ()
{
    return lastName;
}

//set and get methods for Quarter 1

public void setQuarter1(double newQuarter1)
{

    if ( newQuarter1 > 0)
          quarter1 = newQuarter1;
        else
          throw new IllegalArgumentException(
      "new quarter  must be > 0.0");


}


public double getQuarter1()
{
    return quarter1;
}

//set and get methods for Quarter 2

public void setQuarter2(double newQuarter2)
{
    quarter2 = newQuarter2;
}

public double getQuarter2 ()
{
    return quarter2;
}

//set and get methods for Quarter 3

public void setQuarter3(double newQuarter3)
{
    quarter2 = newQuarter3;
}

public double getQuarter3 ()
{
    return quarter3;
}



//set and get methods for Quarter 4

public void setQuarter4(double newQuarter4)
{
    quarter4 = newQuarter4;
}

public double getQuarter4 ()
{
    return quarter4;
}

//(c) class variable annualbrokerage total and two access methods

private static double brokerageTotal;


public void setbrokerageTotal(double newBrokerageTotal)
{
    newBrokerageTotal=brokerageTotal;
}


//(c) constructor to initialise instance variables department,firstname and lastname

public Broker (String dept, String first, String last )
{
    department = dept;
    firstName = first;
    lastName = last;

}



// (d) constructor to initialise all instance variables from (a)

public Broker (String dept, String first, String last,double q1,double q2,double q3,double q4 )
{
    department = dept;
    firstName = first;
    lastName = last;
    quarter1 = q1;
    quarter2 = q2;
    quarter3 = q3;
    quarter4 = q4;


}


// (e) no-argument constructor to initialise default broker instance

public Broker ()
{
    department = null;
    firstName = null;
    lastName = null;
    quarter1 = 0;
    quarter2 = 0;
    quarter3 = 0;
    quarter4 = 0;
}


//(f)   Method to read in quarters from user

public void readInQuarters ()
{
    Scanner input = new Scanner(System.in);


     System.out.println("Please enter Q1,Q2,Q3 and Q4 figures for broker:");






        quarter1 = input.nextInt();
        quarter2 = input.nextInt();
        quarter3 = input.nextInt();
        quarter4 = input.nextInt();



 }  //end of read in quarters method



 // (g) getBrokerTotal Method to return total trades for 4 quarters


public double getBrokerTotal()

 {

     //code to calculate broker quarterly totals

     double brokerTotal = quarter1 + quarter2 + quarter3 + quarter4;

     return brokerTotal;
 }   //end of getBrokerTotal method


//(e)  getBonus method to calculate brokers bonus

public double getBonus()
 {

   double bonusRate=0;
   double bonus=0;


    //bonus rate depending on department rate

   if("Dublin"==department)
          bonusRate=.12;
   else if("London"==department)
          bonusRate=.15;
   else
      bonusRate=.10;

     bonus = (quarter1 + quarter2 + quarter3 + quarter4)*(bonusRate);
        return bonus;
  }


   //(i) to string method for broker class

   public String toString()
     {
      return String.format("  Name: "+ getFirstName()+"\n  Surname: "+getLastName()+"\n  Department: "+getDepartment()+"\n  Total: "+getBrokerTotal()+"\n  Bonus: "+getBonus()+"\n\n");
     }//end of toString method





    //(i) Static methods to read in broker array and output quarterly totals

     //Quarter1 totals method
    public static double getQuarter1Total (Broker[]brokerQuarter1Array)

         {
            double quarter1Total = brokerQuarter1Array[0].getQuarter1()+ brokerQuarter1Array[1].getQuarter1()+ brokerQuarter1Array[2].getQuarter1()+ brokerQuarter1Array[3].getQuarter1()
                                   + brokerQuarter1Array[4].getQuarter1() + brokerQuarter1Array[5].getQuarter1();

                                   return quarter1Total;
         }


     //Quarter2 totals method
    public static double getQuarter2Total (Broker[]brokerQuarter2Array)

        {
            double quarter2Total = brokerQuarter2Array[0].getQuarter2()+ brokerQuarter2Array[1].getQuarter2()+ brokerQuarter2Array[2].getQuarter2()+ brokerQuarter2Array[3].getQuarter2()
                                   + brokerQuarter2Array[4].getQuarter2() + brokerQuarter2Array[5].getQuarter2();

                                   return quarter2Total;
        }


     //Quarter3 totals method
    public static double getQuarter3Total (Broker[]brokerQuarter3Array)

    {
        double quarter3Total = brokerQuarter3Array[0].getQuarter3()+ brokerQuarter3Array[1].getQuarter3()+ brokerQuarter3Array[2].getQuarter3()+ brokerQuarter3Array[3].getQuarter3()
                               + brokerQuarter3Array[4].getQuarter3() + brokerQuarter3Array[5].getQuarter3();

                               return quarter3Total;
    }


     //Quarter4 totals method
    public static double getQuarter4Total (Broker[]brokerQuarter4Array)

        {
            double quarter4Total = brokerQuarter4Array[0].getQuarter4()+ brokerQuarter4Array[1].getQuarter4()+ brokerQuarter4Array[2].getQuarter4()+ brokerQuarter4Array[3].getQuarter4()
                                   + brokerQuarter4Array[4].getQuarter4() + brokerQuarter4Array[5].getQuarter4();

                                   return quarter4Total;
    }




   // Static method to calculate total brokerage totals for all brokers

public static void setBrokerageTotal (Broker[] brokerTotalsArray)

            {
             double annualBrokerageTotal= brokerTotalsArray[0].getBrokerTotal() + brokerTotalsArray[1].getBrokerTotal()

             + brokerTotalsArray[2].getBrokerTotal() + brokerTotalsArray[3].getBrokerTotal() + brokerTotalsArray[4].getBrokerTotal() + brokerTotalsArray[5].getBrokerTotal();

             }


 // Static method to get the total bonuses for all brokers cobined

  public static double getBrokerageBonus (Broker [] brokerageBonusTotalArray)

            {
             double totalBrokerageBonus  = brokerageBonusTotalArray[0].getBonus()+ brokerageBonusTotalArray[1].getBonus()+ brokerageBonusTotalArray[2].getBonus()+ brokerageBonusTotalArray[3].getBonus()
             + brokerageBonusTotalArray[4].getBonus() + brokerageBonusTotalArray[5].getBonus();

                return totalBrokerageBonus;
    }




public static void main(String[]args)

{

    //Part-B


    ///(a) create broker1 with the no argument constructor

    Broker broker1=new Broker();

    broker1.setDepartmentName("Dublin");
    broker1.setFirstName("John");
    broker1.setLastName("Wall");
    broker1.setQuarter1(12);
    broker1.setQuarter2(24);
    broker1.setQuarter3(26);
    broker1.setQuarter4(17);

    System.out.print(broker1);



//(b) create broker2

    Broker broker2 = new Broker("London","Sarah","May");
    broker2.setQuarter1(8);
    broker2.setQuarter2(11);
    broker2.setQuarter3(7);
    broker2.setQuarter4(9);

    System.out.print(broker2);


//(c) create broker3

     Broker broker3 = new Broker("London","Ruth","Lavin");
     //call read in quarters method
     broker3.readInQuarters();

   System.out.print(broker3);




//(d) create broker4,broker5,broker6
     Broker broker4=new Broker("Dublin","Conor","Smith",21,23,26,31);
     Broker broker5=new Broker("Paris","Jerome","Duignan",14,14,17,18);
     Broker broker6=new Broker("Paris","Patick","Bateman",23,24,26,35);


//(e)  Create broker array

     Broker[] brokers;
     brokers=new Broker [6];

     brokers[0]=broker1;brokers[1]=broker2;brokers[2]=broker3;brokers[3]=broker4;brokers[4]=broker5;brokers[5]=broker6;



//(f)  Output second table
      String[] headings ={"Dept","Firstname","Surname","Q1","Q2","Q3","Q4","Total","Bonus"};


        //loop to print the headings
     for (int i = 0; i < headings.length; i++)

       {
         System.out.print(headings[i]+"     ");
       }


       //print a space under the headings
          System.out.println(" \n");


       //loop to print the main table plus format specifiers to align the text


     for (int i = 0; i < 5; i++)
       {

          System.out.printf("%-7s  %-13s %-11s %-6s %-6s %-6s %-6s %-10s %.1f \n\n",brokers[i].getDepartment(), brokers[i].getFirstName(),brokers[i].getLastName(),brokers[i].getQuarter1(),brokers[i].getQuarter2(),brokers[i].getQuarter3(),brokers[i].getQuarter4(),brokers[i].getBrokerTotal(),brokers[i].getBonus());
       }
    //  console printout for quarterly totals

 System.out.printf("%33s \n","Quarterly ");
 System.out.printf("%29  %9s  %6s %6s %6s %6s  \n","Total ",getQuarter1Total(brokers),getQuarter2Total(brokers),getQuarter3Total(brokers),getQuarter4Total(brokers),getBrokerageBonus(brokers));

}    //end of method main


}    //end of class broker

ER

2 个答案:

答案 0 :(得分:0)

您没有使用您的二传手。问题出在这里

public void readInQuarters () {
  Scanner input = new Scanner(System.in);
  System.out.println("Please enter Q1,Q2,Q3 and Q4 figures for broker:");
  quarter1 = input.nextInt(); // <-- Use your setters!
  quarter2 = input.nextInt();
  quarter3 = input.nextInt();
  quarter4 = input.nextInt();

  // should be,
  setQuarter1(input.nextInt()); // and so on... although I will point out, you should
                                // be reading double(s) apparently.
}

答案 1 :(得分:0)

Hello Friend我已经提出了一个建议,这个建议也在我们的项目中使用 在提交值之前调用此方法。如果返回true则更新数据另外显示mgs在验证方法中从调用更新

boolean validate() {

int c = Integer.parseInt(txtFieldSetupTopElevation.getText().toString().trim());

if (c <= 0) {
// Here use code for show msg error or information
// return true if value is greater than 0 other wise return else
return false;
     }
}

和Sandeep