-4

我有一个名为faculty 的类,它带有添加faculty 属性的方法。我已经实现了方法askModel() askSerial() 并将它们作为参数传递给我的box.java 的构造函数...。问题是我该怎么做我的属性被附加到我在我的 Main 中的列表中。

编辑: 对于上面的问题,我试图归档下面的代码。它现在按预期工作。

   package first_;

import java.util.InputMismatchException;
import java.util.Scanner;

public class faculty extends person {

    double salary;
    String title;
    double yearsINservice;
    public faculty(){
     super();
     this.salary = salary;
     this.title = title;
     this.yearsINservice = yearsINservice;
     }



     /*Name Age is an abstract Method from superClass Person*/
        public void nameAge(){
        setFirstName(scanfirstName());
        setLastName(scanlastName());
        setAge(scanage());
        setSalary(scansalary());
        setTitle(scantitle());
        setYearsINservice(scanyearsINservice());
    }

     /*Address is a SuperClass extended by Abstract class Person*/
        public void addressparamaters(){
              setHouseNum(scnhouseNum());
              setStreetName(scnstreeetName());
              setCity(scnCity());
              setState(scnState());
              setZip(scnZip());
          }  
     /*SETTERS AND GETTERS */
    public double getSalary() {return salary;}
    public void setSalary(double salary) {this.salary = salary;}
    public String getTitle() {return title;}
    public void setTitle(String title) {this.title = title;}
    public double getYearsINservice() { return yearsINservice;}
    public void setYearsINservice(double yearsINservice) {this.yearsINservice = yearsINservice;}






    public int scansalary(){
        int salary;

        System.out.println("Please Enter The Monthly Salary");
        salary = scan.nextInt();

              return salary;
    }


    public String scantitle(){
        String title = "";

        System.out.println("Please Enter The Title");
        title = scan.next();

              return title;
    }

    public double scanyearsINservice(){
        double yearsINservice = 0;

       System.out.println("Please Enter The Number of Years in Service");
       salary = scan.nextDouble();

                return yearsINservice;
    }

    public String scanfirstName(){

        String firstname = "";

        System.out.println("Please Enter First Name");

    boolean notDone = true;
    while(true){
    try{
       firstname = scan.next();
    break;
    }catch(InputMismatchException e){
        scan.nextLine();
        System.out.println(e.getMessage());
        System.out.println("Please a valid Name format");
    continue;
         }
         }
           return firstname;
    }

    public String scanlastName(){
        String lastname = "";

        System.out.println("Please Enter Last Name");
        lastname = scan.next();
          return lastname;
    }

    public double scanage(){

        double age= 0 ;

    System.out.println("Please Enter Age");
    boolean notDone = true;
    while(notDone) {
    try{
        age = scan.nextDouble();
        break;

    } catch (InputMismatchException e) {
      scan.nextLine();
      System.out.println(e.getMessage());
      System.out.println("Please a valid age format");
      continue;
    }

    }
    return age;
    }
    @Override
    public String toSTring() {
         return  "\n First Name:" + " " + firstName +
                 "\n Last Name : " +"  "+ lastName +
                 "\n Age : " +"  "+ age + 
                 "\n Salary : " + "   " + salary +
                 "\n Title : " + "  "+ title + 
                 "\n yearsINservice : " +"  "+ yearsINservice +
                 "\n HOUSE NUMBER:" + " " + houseNum +
                 "\nStreet Name : " +"  "+ streetName +
                 "\nCity : " +"  "+ City +  
                 "\n State: " +"  "+ State + 
                 "\n Zip: " +"  "+ zip; 

                 }

}
4

1 回答 1

1

关于您的程序的一些观察:

您所要做的就是将box对象添加到列表中:

boxList.add(box);

在那里,您可以访问数组列表条目以获取其中每个框的所有属性。请检查Java API 以获取ArrayList.

现在,我必须告诉你:你在你的Box类中混入了一切:对象属性和用户界面。我建议你拿出Box类的扫描仪,用你的main方法来处理用户输入。您可以在您的类中编写setXXX() 方法Box来设置来自外部代码的属性值:

public class Box {
    private String type;
    private String model;
    private String serialNum;

    public Box() {
        // A simple constructor that does nothing
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setModel(String model) {
        this.model = model;
    }

    // And so on
}

在你的main方法中:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    ArrayList<Box> boxList = new  ArrayList<Box>();
    Box box = new Box(); // Uses the `dummy` constructor
    System.out.println("Box type?\t");
    box.setType(input.next());
    System.out.println("Box model?\t");
    box.setModel(input.next());
    // And so on
    boxList.add(box);
    // Add as many boxes as you need
}
于 2013-08-06T19:16:59.093 回答