简体   繁体   English

如何在不使用 getter 和 setter 的情况下显示另一个 class 的变量?

[英]How can I display a variable from another class without using getters and setters?

I'm a first-year student and I'm really struggling here.我是一名一年级学生,我在这里真的很挣扎。 This is one of the questions that I have to do for my Assignment (I am allowed to use StackOverflow for guidance).这是我必须为作业做的问题之一(我可以使用 StackOverflow 进行指导)。

Create a class named Customer that will determine the monthly repayment amount due by a customer for a product bought on credit.创建一个名为Customer的 class,它将确定客户对赊购产品的每月还款额。 The class has five fields: customer name , contact number , product price , number of months , and the monthly repayment amount . class有五个字段: customer namecontact numberproduct pricenumber of monthsthe monthly repayment amount

Write get and set methods for each field, except for the monthly repayment amount field.每个字段都写get和set方法,每月还款金额字段除外。 The set methods must prompt the user to enter the values for the following fields: customer name , contact number , product price , and a number of months .设置方法必须提示用户输入以下字段的值: customer namecontact numberproduct pricenumber of months

This class also needs a method to calculate the monthly repayment amount (product price divided by the number of months).这个class还需要一个计算每月还款额的方法(产品价格除以月数)。

Add a subclass named Finance_Period that will determine if a customer will pay interest or not.添加一个名为Finance_Period的子类,它将确定客户是否支付利息。

If the number of months to pay for the product is greater than three, the customer will pay 25% interest, or else no interest applies.如果支付产品的月数大于三个月,客户将支付 25% 的利息,否则不收取利息。

The maximum number of months to pay for the product is 12.为产品支付的最大月数为 12。

Override the calculate_repayment() method by determining if the customer will pay interest or not and calculate the monthly repayment amount.通过确定客户是否支付利息并计算每月还款额来覆盖calculate_repayment()方法。

Create a class called Customer_Finance that contains the logic to test the two classes.创建一个名为Customer_Finance的 class,其中包含测试这两个类的逻辑。

Prompt the user for data for the first object where no interest applies and display the results;提示用户输入没有兴趣的第一个 object 的数据并显示结果; then prompt the user for data where interest is applicable and displays the results.然后提示用户输入感兴趣的数据并显示结果。

I'm struggling to call amtRepay into my main without using getters and setters .我正在努力在不使用getterssetters的情况下将amtRepay调用到我的 main 中。

I'm not even sure if I understand the question correctly, any guidance or advice would be greatly appreciated.我什至不确定我是否正确理解了这个问题,任何指导或建议将不胜感激。

Also, I have another class called Finace_Period , there is nothing there yet, I'm not 100% sure of what I'm doing yet.另外,我还有另一个 class 叫做Finace_Period ,那里什么都没有,我还不能 100% 确定我在做什么。

This is my main class, where I want to display amtRepay .这是我的主要 class,我想在其中显示amtRepay

package main;

import javax.swing.JOptionPane;


public class Main {

    
    public static void main(String[] args) {
        //Variables
        String name;
        int cNumber, months;
        double price;
        
        //Input
        name = JOptionPane.showInputDialog(null, "Please enter the customer's name:");
        cNumber = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the customer's contact number:"));
        price = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the price of the product:"));
        months = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the number of repayment months:"));
        
        Customer c = new Customer(name, cNumber, months, price);
        
        JOptionPane.showMessageDialog(null, c.calcaAmtRepay());
                  
        }
    
    }

and this is my secondary class, where amtRepay is calculated.这是我的辅助 class,其中计算了amtRepay

package Main;

public class Customer extends Finance_Period {
    
    //Atributes
    private String name;
    private int cNumber, months;
    private double price, amtRepay;
    
    //Constructors
    public Customer (String name, int cNumber, int months, double price) {
        this.name = name;
        this.cNumber = cNumber;
        this.months = months;
        this.price = price;
       
        
    }

    //Getters
    public String getName() {
        return name;
    }

    public int getcNumber() {
        return cNumber;
    }
    
    public int getMonths() {
        return months;
    }
    
     public double getPrice() {
        return price;
    }
     
    //Setter
    public void setName(String name) {
        this.name = name;
    }

    public void setcNumber(int cNumber) {
        this.cNumber = cNumber;
    }

    public void setMonths(int months) {
        this.months = months;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    //Calculation of monthly repayments
    public double calcAmtRepay () {
       
        amtRepay = price / months;
        
        return price / months;
    }
}

Thanks.谢谢。

Change your constructor: leave the amtRepay argument.更改您的构造函数:保留 amtRepay 参数。

//Constructors
public Customer (String name, int cNumber, int months, double price) {
    this.name = name;
    this.cNumber = cNumber;
    this.months = months;
    this.price = price;
}

Be aware that after the call to this constructor, the amtRepay field is still not calculated.请注意,调用此构造函数后,amtRepay 字段仍未计算。 Better: you don't even need the amtRepay field in Customer as its value is calculated each time you call calcAmtRepay() which is a good idea because otherwise you have to recalculate it also in setMonths and setPrice .更好:您甚至不需要 Customer 中的 amtRepay 字段,因为每次调用calcAmtRepay()时都会计算它的值,这是一个好主意,否则您还必须在setMonthssetPrice中重新计算它。

public double calcAmtRepay () {
    return price / months;    
}

JOptionPane.showMessageDialog(null, c.calcAmtRepay());

In your class main.Main you are referring to class main.Customer , but according to the code you posted you only have class Main.Customer .在您的 class main.Main中,您指的是 class main.Customer ,但根据您发布的代码,您只有 class Main.Customer

Change package Main;package Main; to package main;package main; in your Customer.java and it should work.在您的Customer.java中,它应该可以工作。

Edit:编辑:

I don't have Netbeans so I used Visual Studio Code with Java extensions installed.我没有 Netbeans,所以我使用安装了 Java 扩展的 Visual Studio Code。

I created an empty directory.我创建了一个空目录。 Inside that directory I created file build.xml and subdirectory src .在该目录中,我创建了文件build.xml和子目录src Inside src I created package main and copied your code.src中,我创建了 package main并复制了您的代码。 I also created empty class main.Finance_Period .我还创建了空的 class main.Finance_Period

My IDE notified me that class main.Customer has method calcAmtRepay() , but in class main.Main you call calcaAmtRepay() - notice the additional, lowercase a between calc and Amt .我的 IDE 通知我 class main.Customer有方法calcAmtRepay() ,但是在 class main.Main你调用calcaAmtRepay() - 注意calcAmt之间的额外的小写a I fixed that and then added the following to my build.xml :我修复了它,然后将以下内容添加到我的build.xml

<project name="MyProject" default="dist" basedir=".">
  <description>
    Java assignment build file
  </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist" location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source">
    <!-- Compile the Java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution">
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the java-assignment-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/java-assignment-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up">
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

Then I ran ant dist in the shell and finally executed:然后我在 shell 中运行ant dist最后执行:

java -cp dist/lib/java-assignment-20200901.jar main.Main

to run the application.运行应用程序。

Both the compilation and the execution runs fine.编译和执行都运行良好。

I used OpenJDK 11.0.8 2020-07-14.我使用的是 OpenJDK 11.0.8 2020-07-14。

Please let me know if any of this helps.如果有任何帮助,请告诉我。

Edit 2:编辑 2:

I put the working code on GitLab .我把工作代码放在GitLab上。 You can ignore the Dockerfile and references to Docker, the rest I already explained.您可以忽略Dockerfile和对 Docker 的引用,即我已经解释过的 rest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在aspectJ中排除getter和setter? - How can I exclude getters and setters in aspectJ? 使用getter和setter来显示颜色 - Using getters and setters to display the color 我可以自定义 lombok 为 class 中的每个变量编写代码,类似于 getter 和 setter 吗? - Can I customize lombok to writing code for each variable in class similar to getters and setters? 我可以在Service类中使用setters / getters类吗? - Can i use setters/getters class in Service class? 如何从main设置Setter并从另一个类Java 7获取Getters - How to set Setters from main and get the Getters from Another Class Java 7 用Java中的setter和getter从另一个类调用方法 - Calling a method from another class with setters and getters in Java 我可以使用getters和setters来初始化变量吗? - can i use getters and setters to already initialized variable? 如何使用SET类从Type Setters&Getters类的“ Arraylist字符串对象”的Arraylist中获取唯一行 - How to get unique rows using SET class from an Arraylist of “Arraylist string objects” of Type Setters & Getters class Java-如何使用setter和getter基于另一个参数设置参数? - Java - How do I set a parameter based on another parameter, using setters and getters? 从ProductionWorker1类创建对象并使用setter和getter - Creating object from ProductionWorker1 class and using setters and getters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM