编辑(严厉判断):我的 display() 方法不会显示,我不确定为什么

EDIT (judge harshly): My display() method won't display and I'm not sure why

我最近发布了一个关于我的 display() 方法只显示某些对象的问题,并且能够通过我之前收到的关于我的 toString() 方法的一些反馈来纠正这个问题。但是,我不得不将我的 idNum 更改为一个 int,现在我的 displayMethod() 根本不会显示。我试着原路返回,但不确定发生了什么。

应该包含标识号、销售额和人员姓名的对象数组。但是,当我显示数组时,没有显示任何内容。我已经尝试了 for 循环,增强的 for 循环并尝试了 system.out.print 调用 get() 方法。

我不知道这是否与我的 displayDatabase() 方法、我使用 Scanner 变量 (USER_INPUT) 设置输入数据的方式有关,或者与我的有关构造函数。

我的构造函数如下所示:

============================================= =====

 public class Salesperson 
   {
   private String salesName;
   private int salesID;
   private double annualSales; 

   public Salesperson(String salesName, int salesIDNum, double yearlySales)
   {
       this.salesName = salesName;
       salesID = salesIDNum;
       annualSales = yearlySales;
   }

   public String getSalesName()
   {
       return salesName;
   } 
   public void setSalesName(String salesName)
   {
       this.salesName = salesName;
   }
   public double getSalesID()
   {
       return salesID;
   }      
   public void setSalesID(int salesIDNum)
   {
       salesID = salesIDNum;
   }
   public double getAnnualSales()
   {
       return annualSales;
   }
   public void setAnnualSales(double yearlySales)
   {
       annualSales = yearlySales;
   }
   @Override
   public String toString()
   {
       return String.format("%s-%-10s%-10.2f", salesName,
                salesID, annualSales);
    } 

}

And my code for application looks like this:

    import java.util.Arrays;  
    import java.util.Scanner;
     public class CreateSalesperson 
     {
       private static final Scanner USER_INPUT = new Scanner(System.in);
       private static final int UPPER_SIZE_LIMIT = 20;
       private static final int LOWER_SIZE_LIMIT = 0;
       private static Salesperson[] salesStaffInDatabase = new 
      Salesperson[20];
       private static int numOfSalesPpl = 0;
       private static boolean loop = true;    
       public static void main(String[] args)
      {
       String selection;
       selection = programMenu();
       String response;

            while(loop)  
              switch(selection)
              {
                  case "A":
                      if(numOfSalesPpl == UPPER_SIZE_LIMIT)
                      {
                        System.out.print("Database has reached capacity.");
                        System.out.print(" Please delete a record before ");
                        System.out.println("adding to the database.");
                      }
                      else
                      {
                        addRecord();      
                      }
                      break;
                  case "a":
                      if(numOfSalesPpl == UPPER_SIZE_LIMIT)
                      {
                        System.out.print("Database has reached capacity.");
                        System.out.print(" Please delete a record before ");
                        System.out.println("adding to the database.");
                      }
                      else
                      {
                        addRecord();      
                      }
                      break;
                    case "C":
                      if(numOfSalesPpl == LOWER_SIZE_LIMIT)
                      {
                        System.out.print("Database is empty. ");
                        System.out.print("Please add a record."); 
                      }
                      else
                      {
                        changeRecord();      
                      }
                      break;
                    case "c":
                      if(numOfSalesPpl == LOWER_SIZE_LIMIT)
                      {
                        System.out.print("Database is empty. ");
                        System.out.print("Please add a record."); 
                      }
                      else
                      {
                        changeRecord();      
                      }
                      break;
                    case "E":
                    System.out.print("You Are Leaving Database");
                    loop = false;
                    break;
                    case "e":
                    System.out.print("You Are Leaving Database");
                    loop = false;
                    break;
                }
                }

      public static void changeRecord()
        {
            String idNum;
            String salesName;
            double salesAmount;
            String response;

            System.out.print("Enter Sales ID: ");
            idNum = USER_INPUT.nextLine();        
            if(isValidID(idNum))

            {   
            int searchResult = Arrays.binarySearch(salesStaffInDatabase, idNum);
            System.out.println(salesStaffInDatabase[searchResult]);
            }
            else
            {
                System.out.println("Invalid Sales ID");
            }
        }
     public static boolean isValidID(String idNum)
     {
     boolean isValid= false;
     for(int val = 0;val < numOfSalesPpl && !isValid; ++val)
     {
         if(salesStaffInDatabase[val].equals(idNum))
                 {
                   isValid = true;  
                 }
     }
     return isValid;
     }     
     public static void addRecord()
     {
        int idNum;
        String salesName;
        double salesAmount;
        String idNo;
        String response;

       do
       {
        System.out.print("Please enter sales ID: ");
        idNum = USER_INPUT.nextInt();
        idNo = Integer.toString(idNum);
        if(idNo.length() != 8)

           System.out.println("Sales ID must be 8 digits long: ");

       }
       while(idNo.length() < 8 || idNo.length() > 8);

        System.out.print("Name: ");
        salesName = USER_INPUT.nextLine();
        USER_INPUT.nextLine();
        System.out.print("Sales Amount: ");
        salesAmount = Double.parseDouble(USER_INPUT.nextLine());
        salesStaffInDatabase[numOfSalesPpl] = new 
     Salesperson(salesName,idNum,salesAmount);

        salesStaffInDatabase[numOfSalesPpl].setSalesName(salesName);
        salesStaffInDatabase[numOfSalesPpl].setSalesID(idNum);
        salesStaffInDatabase[numOfSalesPpl].setAnnualSales(salesAmount);



        System.out.print("Do you want to display database Y/N?: ");
        response = USER_INPUT.nextLine();
        while(response.equalsIgnoreCase("Y")||response.equalsIgnoreCase("yes"))
        {
          displayDatabase();
        }
    } 

    public static void displayDatabase()
    {

        for(int val=0;val < numOfSalesPpl; val++)
        {
              System.out.println(salesStaffInDatabase[val]);
        }

    }    
    public static String programMenu()
    {
    String selection;
     do
        {

         System.out.println("(A)dd a Record");
         System.out.println("(C)hange a Record");
         System.out.println("(E)xit Database"); 
         System.out.print("Enter selection: ");
         selection = USER_INPUT.nextLine();
        }
        while(!selection.equalsIgnoreCase("a") && 
    !selection.equalsIgnoreCase("c")
        && !selection.equalsIgnoreCase("e"));
        return selection;
    }
    }

============================================= ====================

编辑 Salesperson 中的 toString() 方法 class :

      @Override
public String toString() {
    return "Salesperson{" +
            "salesFirstName='" + salesFirstName + '\'' +
            ", salesLastName='" + salesLastName + '\'' +
            ", salesID='" + salesID + '\'' +
            ", annualSales=" + String.format("%-10.2f",  annualSales)+
            '}';
}

在Java中,每当你想将一个对象显示为字符串时,你必须覆盖toString()方法。 您发布的代码,销售人员的 toString() 方法 returns 只有 salesIDanualSales。如果你想显示另一个属性,你必须把它放在toString()方法中。

如果你想在输出的开头显示名字,你可以这样做:

@Override public String toString() {
    return String.format("%s - %-10s%-10.2f", salesFirstName, salesID, annualSales);
}