【问题标题】:Trying to print out comission and table in console试图在控制台中打印出佣金和表格
【发布时间】:2016-02-09 00:39:45
【问题描述】:

所以我有一个家庭作业要求我做以下事情:

  • 公司最近更改了其年度总薪酬政策以提高销售额
  • 销售人员将继续获得 55,000.00 美元的固定工资。当前每位销售人员的销售目标是 100,000.00 美元。
  • 销售激励仅在达到 80% 的销售目标时才开始。目前的佣金是总销售额的 14%。
  • 如果销售人员超过销售目标,佣金将根据加速因子增加。加速因子为 1.40。
  • 应用程序应要求用户输入年度销售额,并应显示年度总薪酬。
  • 应用程序还应显示销售人员本可以赚取的潜在年度薪酬表,以高于销售人员年销售额 5,000.00 美元的增量计算,单位为高于销售人员年销售额的 50%。

因此,这是我必须创建的表的示例,它假设年总销售额为 100,000.00 美元。所以它看起来像这样:

Total Sales Total Compensation
------------------------------------------------------------------
100,000 <<Program Calculated Value>>
105,000 <<Program Calculated Value>>
110,000 <<Program Calculated Value>>
115,000 <<Program Calculated Value>>
120,000 <<Program Calculated Value>>
125,000 <<Program Calculated Value>>
130,000 <<Program Calculated Value>>
135,000 <<Program Calculated Value>>
140,000 <<Program Calculated Value>>
145,000 <<Program Calculated Value>>
150,000 <<Program Calculated Value>>

上表是一个例子!它会根据输入的年销售额而有所不同。

应用程序还必须满足这些技术要求:

  • 除了应用程序控制类之外,应用程序应该至少有一个类
  • 您的代码必须使用一种方法来计算补偿
  • 源代码必须演示条件和循环结构的使用
  • 应该有适当的文档

基本上,我是在前几周的任务基础上构建的,该任务是构建一个计算销售人员年度总薪酬的应用程序。

  • 销售人员将获得 55,000.00 美元的固定工资
  • 销售人员还将获得佣金作为销售奖励。佣金是销售人员年销售额的百分比。目前的佣金是总销售额的 14%。
  • 年薪总额为固定工资加上赚取的佣金。

所以,我遇到的问题是,我对此感到非常困惑。我收到一个错误,我将在下面显示,当我运行程序时,我得到了这个结果:

run:
This is a console based application that calculates the total annual compensation of a salesperson.
Just follow along with the prompts, and insert the correct data.
---------------------------------------------------------------------------
Enter salesperson's annual sales
/* NOTE: I INSERTED THE BELOW VALUE OF 100000 */
100000
Total annual compensation: $69000.00
0.0
$55000.00
Total Sales      Total Compensation
ERROR: Please enter a valid sales amount.
Press enter to exit.

所以在while循环中我得到了错误:

'void' type not allowed here
'void' type not allowed here
not a statement
----
(Alt-Enter shows hint)

这里显示:

while (annualSales <= annualSalesTable) {
    System.out.println(annualSales) + "\t" + calculate.printAnnualCompensation();
    annualSales = annualSales + 5000;
}

我不知道如何解决这个问题。代码如下,主要有两个类SalesPerson.java,然后是Calculate.java:

SalesPerson.java:

package salesperson;

import java.io.IOException
import java.text.DecimalFormat;
import java.util.Scanner;

public class SalesPerson {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println("This is a console based application that calculates the total annual compensation");
        System.out.println("Just follow along with the prompts, and insert the correct data.");
        System.out.println("----------------------------------------------\n");

        getInput(in);

        try {
            System.out.println("Press Enter to exit");
            System.in.read();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }

    public static void getInput(Scanner in) {
        Calculate calculate = new Calculate();
        double annualSales;

        try {
            System.out.println("Enter salesperson's annual sales");
            if(in.hasNextDouble()) {
                annualSales = in.nextDouble();

                if (annualSales == 0)
                {
                    System.out.println("Please enter a valid annual sales amount.");
                }
                else 
                {
                     double tot = calculate.calcCompensation(annualSales);
                     DecimalFormat decFormat = new DecimalFormat(".00");
                     System.out.println("Total annual compensation: " + "$" + decFormat(tot));
                     /* NOTE: I DO NOT KNOW HOW TO GET annualSales to populate this! */
                     calculate.printAnnualCompensation();

                     System.out.println();
                     System.out.println("Total Sales\tTotal Compensation");

                     double annualSaleTable = annualSales * 1.5;

                     while(annualSales <= annualSaleTable) {
                         System.out.println(annualSales) + "\t" + calculate.printAnnualCompensation(); /* ERROR HERE */
                         annualSales = annualSales + 5000;
                     }
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println("ERROR: Please enter a valid sales amount.");
        }
    }
}

这是Calculate.java 类:

package salesperson;

import java.text.DecimalFormat;

public class Calculate {
    private double annualSales;
    private double comissionRate = 0.14;
    private double fixedSalary = 55000.00;
    private double salesTarget = 100000.00;
    private double accelerationFactor = 1.40;

    public double calcCompensation(double annualSales) {
        // Sets the annual salary to multiply by commission rate and then assign the total to currentComm
        double currentComm = annualSales * comissionRate;
        // Adds currentComm variable to the fixSalary variable and assigns that total to the "total" variable
        double total = fixedSalary + currentComm;

        return total;
    }

    public double getCommission() {
        return this.annualSales * this.comissionRate;
    }

    public double annualSales(double anSales) {
        annualSales = anSales;
        return annualSales;
    }

    public void printAnnualCompensation() { /* THIS IS THROWING ERROR ON MAIN CLASS */
        DecimalFormat decFormat = new DecimalFormat(".00");

        /** 
          * Total sales that is less than 80% of the target = no commission
          * Total sales that is between 80% of the target and the less than the target - 14%
          * Total sales that is equal or exceeds the target sales is 14% * 1.4 or 19.6%
         */

        double total = this.fixedSalary;

        // Getting the percentage of the target achieved
        double percentage = (this.annualSales / this.salesTarget) * 100;
        System.out.println(percentage);

        if (percentage < 80) {
            // Do nothing, don't get commission if the percentage is less than 80% of target
        } else if(percentage > 80 && percentage < 100) {
            // Total = annualSalary + commission
            total += getCommission();
        } else if (percentage >= 100) {
            // total = annualSalary + (commission * accelerationFactor);
            total += (getCommission() * this.accelerationFactor);
        }

        System.out.println("$" + decFormat + format(total));
    }
}

所以,这对我来说根本不起作用,我仍然需要我无法得到的桌子才能工作。我也需要它来满足要求,但对我来说,我似乎有很多代码只是无所事事,或者我正在复制所有内容,但我不知道如何清理它。

我不希望你们为我做这件事,我只是需要帮助,因为我越来越迷茫和沮丧!

感谢您能给我的任何帮助!

【问题讨论】:

    标签: java


    【解决方案1】:

    这一行的错误

    System.out.println(annualSales) + "\t" + calculate.printAnnualCompensation();
    

    是您尝试将+ 运算符与void 类型的左侧一起使用。你想做这样的事情:

    System.out.println(annualSales + "\t" + calculate.printAnnualCompensation());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 2023-03-08
      • 2021-06-05
      • 2022-11-20
      相关资源
      最近更新 更多