添加到ArrayList然后删除元素的问题

时间:2014-10-29 03:54:43

标签: java arraylist

对不起,如果在某个地方之前有人问过这个问题,但我真的没有看到任何有助于解决我所遇到的问题的事情。这是为了学校的工作,但是我试图弄清楚我做错了什么,我已经打了两天自己,所以如果有人可以提供帮助,我会非常感激。

我有一个程序,基本上应该输入两个销售人员的名字,显示他们的潜在收入表,最后比较他们的销售差异。我在NetBeans IDE中这样做,并且我在这里有各种各样的东西试图找出它。

一切正常,直到我尝试从ArrayLists中提取任何内容。我当时认为应该可以在那里存放一些东西,然后把它拉出去做我需要做的数学但是我一定做错了。以下是我遇到问题的特定类代码:

class EarningDifference {

  SalesPerson persons = new SalesPerson(); //Access to SalesPerson class
  AnnualSales aSaleC = new AnnualSales(); //Access to AnnualSales class
  ArrayList<SalesPerson> list = new ArrayList<>(); //Access to SalesPerson ArrayList
  ArrayList<AnnualSales> aSales = new ArrayList<>(); //Access to AnnualSales ArrayList

  AnnualSales person1EarningStr; //Person one earnings from AnnualSales ArrayList
  AnnualSales person2EarningStr; //Person two earnings from AnnualSales ArrayList
  Double person1Earning; //Person one earnings as double
  Double person2Earning; //Person two earnings as double
  String person1; //String for person one full name
  String person2; //String for person two full name
  Double difference; //Variable to store the difference between Person1 and Person2 sales
  double totalSales; //variable for Total sales

public void people() {
    person1 = list.get(0).getFirstName() + " " + list.get(0).getLastName();
    person2 = list.get(1).getFirstName() + " " + list.get(1).getLastName();
    System.out.println("Comparing " + person1 + " with " + person2 + ".");
}

void settotalSales(Double totalSales) {
    this.totalSales = totalSales;

        AnnualSales person1EarningStr = aSales.get(0);
        AnnualSales person2EarningStr = aSales.get(1);

    double person1Earning = person1EarningStr.doubleValue();
    double person2Earning = person2EarningStr.doubleValue();

    if (aSales.size() < 2) {
        System.out.println("Need another person to compare");
    } else if (person1Earning > person2Earning) {
        difference = person1Earning - person2Earning;
        System.out.println(person1 + " has earned " + difference + " more in sales than " + person2 + ".");
    } else {
        difference = person2Earning - person1Earning;
        System.out.println(person2 + " has earned " + difference + " more in sales than " + person1 + ".");
    }
}

}

以下是将项添加到AnnualSales ArrayList的类:

public class  SalaryTotal {

        double fixedSalary; //Variable for the fixed salary amount
        double commission; //Variable for the comission
        double minimumSales; //Variable to define the minimum amount of annual sales sales persn must make to receive compensation
        double maximumSales; //Variable to define the number after which commission increases
        double advanceRate; //Variable used to define the rate at which compensation increases after minimum sales are met
        double compensation; //Variable for the amount of compensation based on sales
        double totalSalary; //Variable for the total salary
        double totalSales;
        Double compensationD;

        SalesPerson persons = new SalesPerson();
        ArrayList<SalesPerson> list = new ArrayList<>();
        ArrayList<Double> aSales = new ArrayList<>();

    public void getCompensation(double totalSales) {

            commission = .21; //The constant commission rate
            minimumSales = 120000; //The constant minimum sales needed to receive compensation
            maximumSales = 150000; //The constant maximum amount above which commission increases by the advanceRate
            advanceRate = 1.67; //The constant at which compensation increasea after minimum sales

                this.totalSales = totalSales;

                if (totalSales < minimumSales) {
                    compensation = 0; //sets compensation to 0 if minimum sales are not met  
                } else if (totalSales <= 150000){
                    compensation = totalSales * commission + fixedSalary; //calculates total of compensation if total sales meet minimum sales
                } else if (totalSales > maximumSales) {
                    compensation = totalSales * commission * advanceRate + fixedSalary; //calculates total compensation if total sales exceed maximum sales
                }

                Double compensationD = new Double(compensation);

                aSales.add(compensationD);

                System.out.println("Current total compensation:" + compensation);

                    for (int i = 0; i < aSales.size(); i++) {
                    System.out.println(aSales.get(i));
                    System.out.println("Show " + persons.makePerson1() + ".");
                    }
        }
            }

    }

我确定这是一个完整的错误编码,但任何帮助都会受到赞赏,只需忽略我输入的奇数编码的随机位,试图缩小我的问题范围。非常感谢你。

程序的其余部分看起来像这样,我还删除了以前部分中用于问题测试的位

主:

import java.util.Scanner; //Needed for scanner input
import java.text.DecimalFormat; //Needed for correct output of decimals
import java.util.ArrayList;

public class AnnualCompensation {
   private static String Y;

public static void main(String[] args) {

    DecimalFormat df = new DecimalFormat("$###,###.00"); //This will correctly format the money amounts

    Double totalSales;
    int startY;


    ArrayList<SalesPerson> list = new ArrayList<>();
    ArrayList<AnnualSales> aSales = new ArrayList<AnnualSales>();

    Scanner start = new Scanner(System.in); //Creates scanner input
    System.out.println("Are you ready to compare sales? Press 1 if yes, 2 if no."); //Prints out entry command
    startY = start.nextInt();

    switch (startY){
    case 1:
        InputClass inputs = new InputClass();
        inputs.getInputs();
        break;
    case 2:
        System.out.println("Thank you anyway!");
        System.exit(0);
        break;
    default:
        System.out.println("You have selected neither Y or N, please try again.");
        System.exit(0);
}


    SalaryTotal calculator = new SalaryTotal(); //Calls class SalaryTotal

    PossibleCompensation table = new PossibleCompensation(); //Calls class PossibleCompensation

}
}

可能的补偿:

public class PossibleCompensation {

   SalesPerson persons = new SalesPerson();
   ArrayList<SalesPerson> list = new ArrayList<>();
   ArrayList<AnnualSales> aSales = new ArrayList<AnnualSales>();

DecimalFormat df = new DecimalFormat("$###,###.00"); //This will correctly format the money amounts

double commission; //Variable for the comission
double minimumSales; //Variable to define the minimum amount of annual sales sales persn must make to receive compensation
double maximumSales; //Variable to define the number after which commission increases
double advanceRate; //Variable used to define the rate at which compensation increases after minimum sales are met
double compensation; //Variable for the amount of compensation based on sales
double totalSales; //Variable for sales person annual sale

public void settotalSales(double totalSales) {

    for (int i = 0; i < list.size(); i++)
        this.totalSales = totalSales; //Sets the input from AnnualCompensation to totalSales in this class

        commission = .21; //The constant commission rate
        minimumSales = 120000; //The constant minimum sales needed to receive compensation
        maximumSales = 150000; //The constant maximum amount above which commission increases by the advanceRate
        advanceRate = 1.67; //The constant at which compensation increasea after minimum sales
        String heading1 = "Total Sales"; //variable to print a header for the table
        String heading2 = "Total Compensation"; //variable to print a header for the table

        System.out.print("\nBelow is a table based on possible commissions if sales increase:\n"); //Prints instructions
        System.out.printf("\n%20s %20s", heading1, heading2); //prints header and formats spacing

        for (double i=totalSales; i <= (totalSales * 1.5); i+= 5000) { //FOR loop for calculating and constructing table
                if (i < minimumSales) {
                    compensation = 0; //sets compensation to 0 if minimum sales are not met  
                } else if (i <= 150000){
                    compensation = i * commission; //calculates total of compensation if total sales meet minimum sales
                } else if (i > maximumSales) {
                    compensation = i * commission * advanceRate; //calculates total compensation if total sales exceed maximum sales
                }

            System.out.printf("\n%20s %15s", df.format(i), df.format(compensation) + "\n"); //Prints out the table
    }
}
}

class SalesPerson {

String firstName;
String lastName;
String totalSalesStr;

ArrayList<SalesPerson> list = new List<>();

SalesPerson类:

public SalesPerson() {
    this.firstName = firstName;
    this.lastName = lastName;
    this.totalSalesStr = totalSalesStr;
}

SalesPerson(String firstName, String lastName, String totalSalesStr) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.totalSalesStr = totalSalesStr;
}

public String getFirstName() {
    return this.firstName;
}

public String getLastName() {
    return this.lastName;
}

public String getTotalSalesStr() {
    return this.totalSalesStr;
}

public Double getTotalSales() {
    Double totalSales = Double.parseDouble(totalSalesStr);
    return totalSales;
}

public String makePerson1() {
    String person1 = list.get(0).getFirstName() + " " + list.get(0).getLastName();
    return person1;
}

public String makePerson2() {
    String person2 = list.get(1).getFirstName() + " " + list.get(1).getLastName();
    return person2;
}

@Override
public String toString() {
    return ("Sales person " + this.getFirstName() + " " + this.getLastName() + "  has annual sales of " + this.getTotalSalesStr() + ".");
}

}

class AnnualSales {

Double compensationD;
AnnualSales person1EarningStr;
AnnualSales person2EarningStr;

ArrayList<AnnualSales> aSales = new ArrayList<AnnualSales>();

public AnnualSales() {
    this.compensationD = compensationD;
}

public AnnualSales(Double compensationD) {
    this.compensationD = compensationD;
}

public Double getCompensationD() {
    return this.compensationD;
}

public AnnualSales getperson1EarningStr() {
    person1EarningStr = aSales.get(0);
    return person1EarningStr;
}

public AnnualSales getperson2EarningStr() {
    person2EarningStr = aSales.get(1);
    return person2EarningStr;
}

@Override
public String toString() {
    return ("please show us" + compensationD);
}
}

输入类:

class InputClass {

String firstName;
String lastName;
Double totalSales;
String totalSalesStr;

CopyOnWriteArrayList<SalesPerson> list = new CopyOnWriteArrayList<>();

public void getInputs() {
    for(double i=0; i < 2; i++){

        Scanner persFN = new Scanner(System.in); //Creates scanner input
        System.out.println("Enter sales person's first name:"); //Prints out entry command
        String firstName = persFN.next();

        Scanner persLN = new Scanner(System.in); //Creates scanner input
        System.out.println("Enter sales person's last name:"); //Prints out entry command
        String lastName = persLN.next();

        Scanner input = new Scanner(System.in); //Creates scanner input
        System.out.println("Enter Total Sales:"); //Prints out entry command
        totalSales = input.nextDouble();

        String totalSalesStr = totalSales.toString();

        list.add(new SalesPerson(firstName, lastName, totalSalesStr));

        SalaryTotal calculator = new SalaryTotal(); //Calls class SalaryTotal
        calculator.getCompensation(totalSales);

        PossibleCompensation table = new PossibleCompensation(); //Calls class PossibleCompensation
        table.settotalSales(totalSales);

        EarningDifference earning = new EarningDifference();
        earning.settotalSales(totalSales);
    }
}
}

我认为这应该是一切相关的!

1 个答案:

答案 0 :(得分:0)

确保在类中使用覆盖equals()方法。作为一种良好做法,请务必确保同时覆盖equals()hashCode()

为什么?

来自Java doc

  

从此列表中删除第一次出现的指定元素,   如果它存在。如果列表不包含该元素,则为   不变。更正式地,删除具有最低索引i的元素   这样(o == null?get(i)== null:o.equals(get(i)))(如果是这样的话)   元素存在)。如果此列表包含指定的,则返回true   element(或等效地,如果此列表由于更改而更改)   呼叫)。

如果您的ArrayList元素不是String(更一般地说,如果它不是内置Java类),则需要覆盖equals()方法。