Java 如何比较多个类实例的属性?

Java 如何比较多个类实例的属性?,java,Java,好的,这是泡菜。我正在学习这门用Java教授逻辑编程的课程。我只懂一点JavaScript,所以Java对我来说是非常陌生的技术 我正在做这个任务,我需要创建一个会议管理器应用程序(仅基于控制台)。每次会议都会举办讲座(你想要多少就多少)。每个会议都有一些属性,如会议经理的姓名、电话号码、小时费率等;讲座也一样。我希望能够用扫描仪方法输入这些数据。这就是我到目前为止所做的: 开始创建两个类: 1) 会议创造者 import java.util.*; public class Conferenc

好的,这是泡菜。我正在学习这门用Java教授逻辑编程的课程。我只懂一点JavaScript,所以Java对我来说是非常陌生的技术

我正在做这个任务,我需要创建一个会议管理器应用程序(仅基于控制台)。每次会议都会举办讲座(你想要多少就多少)。每个会议都有一些属性,如会议经理的姓名、电话号码、小时费率等;讲座也一样。我希望能够用扫描仪方法输入这些数据。这就是我到目前为止所做的:

开始创建两个类:

1) 会议创造者

import java.util.*;

public class Conference {
    String nameConference;
    String nameManagerConference;
    String telManagerConference;
    String dateStartConference;
    String dateEndConference;
    float hourlyRateManager;
    float hoursAmountConference;

    public void setConferenceData() {
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Conference name: ");
        this.nameConference = keyboard.nextLine();

        System.out.print("Conference manager name: ");
        this.nameManagerConference = keyboard.nextLine();

        System.out.print("Conference manager telephone number: ");
        this.telManagerConference = keyboard.nextLine();

        System.out.print("Conference start date: ");
        this.dateStartConference = keyboard.nextLine();

        System.out.print("Conference end date: ");
        this.dateEndConference = keyboard.nextLine();

        System.out.print("Manager hourly rate: ");
        this.hourlyRateManager = keyboard.nextFloat();

        System.out.print("Conference amount of hours: ");
        this.hoursAmountConference = keyboard.nextFloat();

        System.out.println(this.nameManagerConference + ", manager of the conference " + "\"" + this.nameConference +"\"" + ", cost R$ " + (this.hoursAmountConference * this.hourlyRateManager));
    }
}
2) 演讲创作者

import java.util.*;

public class Lectures {
    float totalCost = 0;
    String lecturesList = "Lectures list: ";
    ArrayList<Float> arrLecturesCostTotal = new ArrayList<>();
    ArrayList<String> listLectures = new ArrayList<>();

    public void getLecturesTotalCost() {
        for (int i = 0; i < arrLecturesCostTotal.size(); i++) {
             totalCost += arrLecturesCostTotal.get(i);
        }
        System.out.println("The total lectures cost is $ " + totalCost); 
    }

    public void getLecturesList() {
        for (int i = 0; i < listLectures.size(); i++) {
             lecturesList += "\n" + "- " + listLectures.get(i);
        }
        System.out.println(lecturesList);
    }

    public class Lecture{
        String lectureTitle;
        String lectureStartHour;
        String lecturerName;
        String lecturerTelephone;
        String lectureDescription;
        float lecturerHourlyRate;
        float lectureHoursAmount;
        float lectureCost = 0;

        public void setDataLecture() {
            Scanner keyboard = new Scanner(System.in);

            System.out.print("Lecture title: ");
            this.lectureTitle = keyboard.nextLine();

            System.out.print("Lecture start time: ");
            this.lectureStartHour = keyboard.nextLine();

            System.out.print("Lecturer name: ");
            this.lecturerName = keyboard.nextLine();

            System.out.print("Lecturer telephone number: ");
            this.lecturerTelephone = keyboard.nextLine();

            System.out.print("Lecture description: ");
            this.lectureDescription = keyboard.nextLine();

            System.out.print("Lecturer hourly rate ");
            this.lecturerHourlyRate = keyboard.nextFloat();

            System.out.print("Lecture hours amount: ");
            this.lectureHoursAmount = keyboard.nextFloat();

                this.lectureCost = this.lecturerHourlyRate * this.lectureHoursAmount;
                System.out.println("The cost of the lecture " + this.lecturerName + " is $ " + this.lectureCost);

                arrLecturesCostTotal.add(this.lecturerHourlyRate * this.lectureHoursAmount);
                listLectures.add(this.lectureTitle + " by " + this.lecturerName);
        }
    }
}
因此,其中一个可交付成果是比较讲座的成本。我需要返回最昂贵和最便宜的讲座(费用和名称)。但是,我不知道如何做,因为我不知道如何比较实例的属性值。特别是因为它们是通过在控制台中输入数据创建的

我的逻辑可能是错误的,因为我几乎是在做实验,交叉手指,所以我在控制台中没有看到错误,但这是我能想到的全部

有人能帮我吗?

这里有几点提示

  • 您可能不应该在数据类中调用Scanner。相反,在主方法中调用scanner,并通过构造函数或setter将结果提供给类。通过这种方式,您可以分离关注点和类,如
    会议
    讲座
    不需要了解任何有关输入法的信息(在本例中为扫描器)
  • 会议应该包含讲座,这意味着
    列表
    可能应该是类内的字段
    会议
    以及其他字段
  • 讲座应该有两个字段(除其他内容外)
    double length hours
    double hourlyCost
    。然后你可以在
    讲座中找到一个方法
  • 然后你可以在
    会议
    中找到一个方法:

    public double totalCost() {
      double lecturesTotal = 0.0;
      for (Lecture lecture : lectures {
        lecturesTotal += lecture.totalCost();
      }
      return lecturesTotal + //other stuff like conference managers pay;
    }
    

    希望这能让你走上正确的方向。

    @user,是的,会的。但我一直在寻找一种方法来获取课堂上的所有实例,并自动比较它们的成本,这样我就不必手动进行了,你知道吗?@user,这听起来很有希望!但是什么样的名单呢?阵列?我怎么能做到呢?此外,我将在何处插入此“比较器”?很抱歉问了这么多问题。我在这里有点迷路了。你可以用一个循环来检查成本。@NomadMaker,我来试试。我刚刚发现我可以创建一个对象数组。我已经创建了一个,并试图找到一种方法来比较这些属性。谢谢数组提供了哪些ArrayList没有的功能?谢谢您的输入。我的第一个版本使用嵌套类(讲座在会议内部),但我很难处理这两个类之间的数据。我再试试看。你知道有什么方法可以让我得到最昂贵和最便宜的讲座(连同他们的名字)?再次感谢!您可以像我在totalCost方法中一样使用相同的for循环,但不需要计算将每个讲座的总成本与您找到的最低(或最高)成本进行比较的总和。只要在循环之前初始化的临时变量中保留最低(或最高)值,如果当前迭代值低于之前的最低值(或更高,取决于您查找的是最小值还是最大值),请使用新值设置它。谢谢,我会尝试!顺便说一句,如果你有一个要求,你需要改变讲座或讲座的细节后,你最初创建了他们,那么你可以使用地图,而不是列表。您可以执行类似于
    conference.gettouctions().get(“some touction”).setHourlyCost(newHourlyCost)
    的操作。但是如果你不需要,那就别担心。
    public double totalCost() {
      return hourlyCost * lengthHours;
    }
    
    public double totalCost() {
      double lecturesTotal = 0.0;
      for (Lecture lecture : lectures {
        lecturesTotal += lecture.totalCost();
      }
      return lecturesTotal + //other stuff like conference managers pay;
    }