从另一个类+排序调用方法

时间:2014-03-24 00:50:25

标签: java sorting methods

我正在开发一个程序,我必须调用一个方法来提示用户从另一个类输入数据。该程序应打印客户的姓名,年龄,地址和性别。但是,我在调用输入每个客户信息的方法时遇到问题。

另外,我必须创建一种方法,按升序对客户的年龄进行排序。因此,程序根据从最年轻的客户到(最老的客户)的年龄顺序打印出所有信息。我不知道如何创建一种方法,只对客户的年龄进行排序,而无需对名称,地址和性别进行排序。我真的很感激任何反馈或意见!

这是我到目前为止所做的。

    import java.util.Scanner;
  public class Customer1 {
        public  static void main(String [] args){

        Scanner input = new Scanner(System.in);
        int x;

        System.out.print("Total number of customers: ");
        x = input.nextInt();

        Customer [] person = new Customer[x];


        System.out.println("Name" + "     " + "Age"+ "       " + "Address" +  "     " + "Gender");

        for(int i = 0; i < person.length; i++){
            System.out.println(person.toString());
        }


        }
}



class Customer{

        String name;
        int age;
        String address;
        String gender;

        public Customer(String newName, int newAge, String newAddress, String newGender){
            name = newName;
            age = newAge;
            address = newAddress;
            gender = newGender;

        }

        public void data(Customer [] person){

            Scanner input = new Scanner(System.in);

            for(int i = 0; i < person.length; i++){

                System.out.print("Name: ");
                name= input.toString();

                System.out.print("Age: ");
                age = input.nextInt();

                System.out.print("Address: ");
                address= input.toString();

                System.out.print("Gender: ");
                gender = input.toString();


            }
        }

        /*This is the "uncompleted" method that I tried to create in order to sort  the ages of customers.
         But I don't know how to use it in  order to sort only the ages*/
        public void sort(Customer [] person){

          double temp;
          for(int a = 0; a < (person.length - 1); a++){

                    for( int b = (a + 1); b < person.length; b++){
                            if(person[a] > person[b]){

                                    temp = person[a];
                                    person[a] = person[b];
                                    person[b] = temp;

                            }


                    }


            }


        }

        public String toString(){

            String result;
            result = name + "       " + age + "           " +  address +  "       " + gender;
            return result;
        }
}

3 个答案:

答案 0 :(得分:1)

我建议您重新考虑一下代码并查看以下提示

使用Comparator或Comparable接口

这些界面可以帮助您对集合,列表等进行排序,即Comparator interface允许您使用Collections.sort和Arrays.sort操作对您的集合进行排序。

您必须根据目标类(Person)定义Comparator的实现,然后按您想要的任何字段定义排序:

class PersonSort implements Comparator<Person>{

@Override
public int compare(Person p1, Person p2) {
    return p1.getAge() - p2.getAge();
}}

然后你可以通过Arrays.sort(T [],Comparator)强制它的排序:

Arrays.sort(yourArray, new PersonSort());

我还建议你看看Oracle's Collection Framework Tutorial。您将找到有关订购,实施等的信息。

答案 1 :(得分:0)

<强> 1。获取所需数据

目前,在您的Customer1课程中,您接受了来自用户输入的x个客户。之后,您将为x Customer个对象创建一个数组。您当前没有使用任何数据填充数组。

Customer[] person = new Customer[x];

在此行之后,您可以使用以下内容执行for循环:

        String name;
        int age;
        String address;
        String gender;
        for( int i = 0; i < person.length; i++ )
        {
            System.out.print("Name: ");
            name = input.next();

            System.out.print("Age: ");
            age = input.nextInt();

            System.out.print("Address: ");
            address= input.next();

            System.out.print("Gender: ");
            gender = input.next();

            person[i] = new Customer( name, age, address, gender );
        }

您的代码中必须遵守cavaet,您已放置input.toString()。这将为您提供扫描仪的字符串表示,而不是输入。 input.next()会以字符串形式为您提供下一个输入。

<强> 2.Sorting

我建议查看comparator文档。有一个比较器对象,它使用Customer作为类型参数来实现比较器。覆盖比较以检查每个Customer对象的年龄。

示例是:

class CustomerComparator implements Comparator<Customer>
{
    @Override
    public int compare(Customer a, Customer b)
    {
        return a.age < b.age ? -1 : a.age == b.age ? 0 : 1;
    }
}

您应该考虑将变量nameageaddress gender设为私有且使用getX()方法(getters / setters)。

答案 2 :(得分:0)

尝试以下可能解决此问题的代码。我已经包含了之前回复中建议的方法并创建了此程序..

import java.util.Scanner;

public class ReadSortCustomerData {

    public static void main(String [] args) {
    int numberOfCustomers;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the total number of customers: ");
    numberOfCustomers = input.nextInt();

    CustomerData [] customer = new CustomerData[numberOfCustomers];

    for(int countCustomer=0 ; countCustomer < numberOfCustomers; countCustomer++) {

        System.out.println("Enter the name of the"+(countCustomer+1)+"customer");
        customer[countCustomer].setName(input.next());
        System.out.println("Enter the age of the"+(countCustomer+1)+"customer");
        customer[countCustomer].setAge(input.nextInt());
        System.out.println("Enter the gender of the"+(countCustomer+1)+"customer");
        customer[countCustomer].setGender(input.next());
        System.out.println("Enter the address of the"+(countCustomer+1)+"customer");
        customer[countCustomer].setGender(input.next());
    }


  }

    public CustomerData[] sortCustomerData(CustomerData[] customers) {

        for (int i=0;i<customers.length;i++) {

            for(int j=i+1;j<customers.length;j++) {

                if(ageCompare(customers[i], customers[j])==1) {
                    CustomerData tempCustomer = new CustomerData();
                    tempCustomer = customers[i];
                    customers[i] = customers[j];
                    customers[j] = tempCustomer;
                }

                }
            }

        return customers;
    }

    public int ageCompare(CustomerData a, CustomerData b)
    {
        return a.getAge() < b.getAge() ? -1 : a.getAge() == b.getAge() ? 0 : 1;
    }
}




import java.util.Comparator;
import java.util.Scanner;


public class CustomerData {

    private String name;
    private int age;
    private String address;
    private String gender;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }

}

这可能需要在运行时进行一些调整,但它应该会给你一个良好的开端。