简体   繁体   中英

Read user input to list, sort based on element value, and return list elements within array in order - Java

My code is comprised of the following aspects and goals: 1) Take user input of three equipment pieces 2) Store those inputs in a list 3) Store lists in an array 4) Sort lists within array based on larger value of ROI (Index 3 of each list in array) 5) Return lists in array in order of largest ROI

To do this, I have created three class files: Main, Equipment, and Sort

My issues: 1) My set() method is returning an error of undefined for type 2) My comparison tools returns values from ascending order, but I need descending order (Largest to smallest)

 import java.util.ArrayList; import java.util.List; import java.util.Collections; import java.util.*; public class Main { public static void main(String[] args) { Equipment equ1 = new Equipment("Name", 1.00, 2.00, 3.00); Equipment equ2 = new Equipment("Name", 1.00, 2.00, 3.00); Equipment equ3 = new Equipment("Name", 1.00, 2.00, 3.00); //Equipment Set 1 from User Input System.out.println("Enter Equipment Set 1 Name: "); Scanner input = new Scanner (System.in); String equName1 = input.nextLine(); equ1.set(0, equName1); System.out.println("Enter Equipment Set 1 Gain: "); Double equGain1 = input.nextDouble(); equ1.set(1, equGain1); System.out.println("Enter Equipment Set 1 Cost: "); Double equCost1 = input.nextDouble(); equ1.set(2, equCost1); double roi1 = (equGain1 - equCost1) / equCost1; equ1.set(3, roi1); //Place ROI at index 3 //Equipment Set 2 from User Input System.out.println("Enter Equipment Set 2 Name: "); String equName2 = input.nextLine(); equ2.set(0, equName2); System.out.println("Enter Equipment Set 2 Gain: "); Double equGain2 = input.nextDouble(); equ2.set(1, equGain2); System.out.println("Enter Equipment Set 2 Cost: "); Double equCost2 = input.nextDouble(); equ2.set(2, equCost2); double roi2 = (equGain1 - equCost1) / equCost1; equ2.set(3, roi2); //Equipment Set 3 from User Input System.out.println("Enter Equipment Set 3 Name: "); String equName3 = input.nextLine(); equ3.set(0, equName3); System.out.println("Enter Equipment Set 3 Gain: "); Double equGain3 = input.nextDouble(); equ3.set(1, equGain3); System.out.println("Enter Equipment Set 3 Cost: "); Double equCost3 = input.nextDouble(); equ3.set(2, equCost3); double roi3 = (equGain1 - equCost1) / equCost1; equ3.set(3, roi3); List<Equipment> equipment = new ArrayList<Equipment>(); equipment.add(equ1); //Add each list to the array list equipment.add(equ2); equipment.add(equ3); Collections.sort(equipment, new Sort()); System.out.println(equipment); } } 

Equipment.java

 public class Equipment { //This class seeks to define the elements and the structure of Equipment String equName; double equGain; double equLoss; double roi; public Equipment(String equName, double equGain, double equLoss, double roi) { this.equName = equName; this.equGain = equGain; this.equLoss = equLoss; this.roi = roi; } public String toString() { return "Equipment: " + equName + " Gain: " + equGain + "Loss: " + equLoss + "ROI: " + roi; } } 

Sort.java

 import java.util.Comparator; public class Sort implements Comparator<Equipment> { //Implementing a comparison tool for lists @Override public int compare(Equipment o1, Equipment o2) { //I want to compare one list to another - Update from Java 8 return Double.compare(o1.getroi() - o2.getroi()); //I want to compare the ROI value of each list } } 

Your main problem here is the lack of knowledge of Object-Oriented Programming. I recommend to understand how this paradigm works. You have multiple problems in your code, so let is take it step by step:

For the first problem it appears that you didn't define setter and getter methods for your model. Define them and make the attributes of class private.

public class Equipment {
    private String equName;
    private double equGain;
    private double equLoss;
    private double roi;

    public Equipment() {
    }

    public Equipment(String equName, double equGain, double equLoss, double roi) {
        this.equName = equName;
        this.equGain = equGain;
        this.equLoss = equLoss;
        this.roi = roi;

    }

    public String getEquName() {
        return equName;
    }

    public void setEquName(String equName) {
        this.equName = equName;
    }

    public double getEquGain() {
        return equGain;
    }

    public void setEquGain(double equGain) {
        this.equGain = equGain;
    }

    public double getEquLoss() {
        return equLoss;
    }

    public void setEquLoss(double equLoss) {
        this.equLoss = equLoss;
    }

    public double getRoi() {
        return roi;
    }

    public void setRoi(double roi) {
        this.roi = roi;
    }

    public String toString() {
        return "Equipment: " + equName + " Gain:  " + equGain + "Loss: " + equLoss + "ROI: " + roi;
    }
}

Your comparator method uses Double.compare(double obj1,double obj2) for comparison. You use the method wrong. You don't have to subtract the two doubles from each other. You give the two double value as parameters of method. You should swap the method parameters to get ascending or descending order: Ex:

Double.compare(obj1.getRoi(),obj2.getRoit()) --> ascending order
Double.compare(obj2.getRoi(),obj1.getRoit()) --> descending order

Sort.class

import java.util.Comparator;

public class Sort implements Comparator<Equipment> { //Implementing a comparison tool for lists
    @Override
    public int compare(Equipment o1, Equipment o2) { //I want to compare one list to another - Update from Java 8
        return Double.compare(o2.getRoi(), o1.getRoi());
    }
}

In your main you confusing the variables. For every user input you calculate the first object is ROI not the others. Apart from them set method should be replaced with each Equipment attribute is setter method.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ArrayTest {

    public static void main(String[] args) {

        Equipment equ1 = new Equipment();
        Equipment equ2 = new Equipment();
        Equipment equ3 = new Equipment();

        //Equipment Set 1 from User Input
        System.out.println("Enter Equipment Set 1 Name: ");
        Scanner input = new Scanner(System.in);
        String equName1 = input.nextLine();
        equ1.setEquName(equName1);

        System.out.println("Enter Equipment Set 1 Gain: ");
        Double equGain1 = input.nextDouble();
        equ1.setEquGain(equGain1);

        System.out.println("Enter Equipment Set 1 Cost: ");
        Double equCost1 = input.nextDouble();
        equ1.setEquLoss(equCost1);

        double roi1 = (equGain1 - equCost1) / equCost1;
        equ1.setRoi(roi1); //Place ROI at index 3

        //Equipment Set 2 from User Input
        System.out.println("Enter Equipment Set 2 Name: ");
        String equName2 = input.nextLine();
        equ2.setEquName(equName2);

        System.out.println("Enter Equipment Set 2 Gain: ");
        Double equGain2 = input.nextDouble();
        equ2.setEquGain(equGain2);

        System.out.println("Enter Equipment Set 2 Cost: ");
        Double equCost2 = input.nextDouble();
        equ2.setEquLoss(equCost2);

        double roi2 = (equGain2 - equCost2) / equCost2;
        equ2.setRoi(roi2);

        //Equipment Set 3 from User Input
        System.out.println("Enter Equipment Set 3 Name: ");
        String equName3 = input.nextLine();
        equ3.setEquName(equName3);

        System.out.println("Enter Equipment Set 3 Gain: ");
        Double equGain3 = input.nextDouble();
        equ3.setEquGain(equGain3);

        System.out.println("Enter Equipment Set 3 Cost: ");
        Double equCost3 = input.nextDouble();
        equ3.setEquLoss(equCost3);

        double roi3 = (equGain3 - equCost3) / equCost3;
        equ3.setRoi(roi3);

        List<Equipment> equipment = new ArrayList<>();
        equipment.add(equ1); //Add each list to the array list
        equipment.add(equ2);
        equipment.add(equ3);

        equipment.sort(new Sort());
        System.out.println(equipment);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM