简体   繁体   English

在Java中创建一个数组列表并存储与员工姓名相关的值

[英]Create an array list in java and store values tied to an employee name

I have a program that calculates an employees annual salary based upon there base salary and commission sales. 我有一个程序可以根据那里的基本工资和佣金销售来计算员工的年薪。 I want to take the program a step further and create an arrayList that will ask for the employee name and then create an array, store the values from the program and print out those values for each employee name inputted. 我想使程序更进一步,并创建一个要询问雇员姓名的arrayList,然后创建一个数组,存储程序中的值,并为输入的每个雇员姓名打印出这些值。

I imagine that I need to create a for or a else loop to collect the data. 我想我需要创建一个for或else循环来收集数据。 Problem is I'm not sure where to do that and how to create the array. 问题是我不确定该在何处以及如何创建阵列。 I have two classes: 我有两节课:

1 class for my commission variable's and calculations: 1类用于我的佣金变量和计算:

package Commissions;

public class Calculations {

    double totalSales;
    private double comRate = 0.025;
    private double annualSal = 80000;
    private double salesTarget = 120000;
    private double acceleration = 0.015;
    private double compensation;

    public Calculations (double TotalSales) {
        this.totalSales = totalSales;

    }

    public double getCommissionCalc () {

        if (totalSales >= salesTarget) {

            compensation = (annualSal + (totalSales * (comRate + acceleration)));
                return compensation;

        } else if (totalSales >= salesTarget * .8) {

            compensation = (annualSal + (totalSales * comRate));
                return compensation;

        } else {

            compensation = annualSal;
                return compensation;
        }
    }

}

1 class for my main and user input 我的主要输入和用户输入有1个类别

package Commissions;

import java.text.NumberFormat;
import java.util.*;

public class paycheck {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner input = new Scanner (System.in);

        NumberFormat nf = NumberFormat.getCurrencyInstance();

        System.out.println("Enter the employee name: ");
        long empName = input.nextLong();

        mediaArray[empName];

        System.out.println("Enter your total sales for the year:  ");
        double totalSales = input.nextDouble();

        System.out.println("\r");

        Calculations c = new Calculations (totalSales);

        System.out.println("Your Total compensation with your annual sales is:  " + nf.format(c.getCommissionCalc()));

        System.out.println("\r");

        System.out.println("If you were to increase your sales you could earn even more money!");

        System.out.println("\r");

        double i = totalSales + 5000;
        double finish = totalSales * 1.5;

        while (i <= finish) {

            c.totalSales = i;

            System.out.println("If you were to increase your sales commission to " + nf.format(i) + " you could earn: " + nf.format(c.getCommissionCalc()));

            i = i + 5000;
        }

    }

}

Here is the code as per my understanding of your problem, had to correct few things as you are saying enter name but taking long as input: 根据我对您的问题的理解,这是代码,您必须纠正一些错误,因为您说的是输入名称,但需要长时间输入:

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

public class Paycheck {

    public static void main(String[] args) {

        List<Employee> empList = new ArrayList<Employee>();

        while (true) {
            Scanner input = new Scanner(System.in);
            NumberFormat nf = NumberFormat.getCurrencyInstance();
            System.out.println("Enter the employee name (enter exit to exit): ");
            String empName = input.nextLine();
            // mediaArray[empName];

            if(empName.equals("exit")) {
                break;
            }

            System.out.println("Enter your total sales for the year:  ");
            double totalSales = input.nextDouble();

            Calculations c = new Calculations(totalSales);

            System.out
                    .println("Your Total compensation with your annual sales is:  "
                            + nf.format(c.getCommissionCalc()));

            System.out.println("\r");

            System.out
                    .println("If you were to increase your sales you could earn even more money!");

            System.out.println("\r");
            double i = totalSales + 5000;
            double finish = totalSales * 1.5;

            while (i <= finish) {

                c.totalSales = i;

                System.out
                        .println("If you were to increase your sales commission to "
                                + nf.format(i)
                                + " you could earn: "
                                + nf.format(c.getCommissionCalc()));

                i = i + 5000;
            }
            System.out.println("\r");

            //store employee data into arraylist
            empList.add(new Employee(empName, i));
        }


        for(Employee emp : empList) {
            System.out.println("Employee Name: " + emp.getName() + " Total Sales: " + emp.getSales());
        }
    }
}

class Employee {

    private String name;
    private Double sales;

    public Employee(String empName, double totalSales) {
        this.name = empName;
        this.sales = totalSales;
    }

    public Double getSales() {
        return sales;
    }

    public void setSales(Double sales) {
        this.sales = sales;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

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

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