2

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).

Create a class named Customer that will determine the monthly repayment amount due by a customer for a product bought on credit. The class has five fields: customer name, contact number, product price, number of months, and the monthly repayment amount.

Write get and set methods for each field, except for the monthly repayment amount field. 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.

This class also needs a method to calculate the monthly repayment amount (product price divided by the number of months).

Add a subclass named Finance_Period that will determine if a customer will pay interest or not.

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.

The maximum number of months to pay for the product is 12.

Override the calculate_repayment() method by determining if the customer will pay interest or not and calculate the monthly repayment amount.

Create a class called Customer_Finance that contains the logic to test the two classes.

Prompt the user for data for the first object where no interest applies and display the results; 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.

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.

This is my main class, where I want to display 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.

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.

Konrad Botor
  • 4,765
  • 1
  • 16
  • 26
  • what is your output from your main class? – jaSnom Sep 01 '20 at 11:00
  • 1
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: main.Customer at main.Main.main(Main.java:25) It just gives me this error when it comes to outputting amtRepay @jaSnom – Henno Grové Sep 01 '20 at 11:03
  • First off, i think that you need to define another constructor, since your default constructor is expecting AmtRepay, which you are not passing. therefore you could define a new Constructor hat only cointains the Paramters you are passing: ```public Customer (String name, int cNumber, int months, double price) { this.name = name; this.cNumber = cNumber; this.months = months; this.price = price; }``` – jaSnom Sep 01 '20 at 11:08
  • Also private Variables cannot be directly accessed. You need to make them either public or define a getter. In this Case a getter would be enough since you do not want to change it from the outsied, if im correct. – jaSnom Sep 01 '20 at 11:12
  • Okay, but I can't use getters and setters? I have removed the amtRepay from my constructer. – Henno Grové Sep 01 '20 at 11:15
  • Solutions provided here: https://stackoverflow.com/questions/1196192/how-to-read-the-value-of-a-private-field-from-a-different-class-in-java – jaSnom Sep 01 '20 at 11:17
  • https://stackoverflow.com/questions/36203915/java-get-private-variable-from-another-class-without-setting-a-getter – jaSnom Sep 01 '20 at 11:19
  • I looked at these, but i have no idea how to apply this to my code. – Henno Grové Sep 01 '20 at 11:21
  • If both your classes are in the same package main, make sure the first line in Customer.java is `package main;`and not `package Main;` Java is very sensible about lowercase/uppercase. – Conffusion Sep 01 '20 at 13:30

2 Answers2

1

Change your constructor: leave the amtRepay argument.

//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. 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.

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

JOptionPane.showMessageDialog(null, c.calcAmtRepay());
Conffusion
  • 4,335
  • 2
  • 16
  • 28
  • what do you mean by "doesnt't work" ? What is the error you got ? Compilation error, runtime error ? – Conffusion Sep 01 '20 at 11:27
  • Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: main.Customer at main.Main.main(Main.java:25) This is what i get – Henno Grové Sep 01 '20 at 11:29
0

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.

Change package Main; to package main; in your Customer.java and it should work.

Edit:

I don't have Netbeans so I used Visual Studio Code with Java extensions installed.

I created an empty directory. Inside that directory I created file build.xml and subdirectory src. Inside src I created package main and copied your code. I also created empty 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. I fixed that and then added the following to my 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:

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.

Please let me know if any of this helps.

Edit 2:

I put the working code on GitLab. You can ignore the Dockerfile and references to Docker, the rest I already explained.

Konrad Botor
  • 4,765
  • 1
  • 16
  • 26