所以我的作业如下。
挑战...... 该计划应该询问销售的单位数量并计算购买的总成本。 我也应该包括输入验证,以便单位数大于0。 套餐价格为99美元,数量折扣为: 10-19 20% 20-49 30% 50-99 40% 100或更多50%
我有我的课程演示,我必须用它来测试我的代码,
package Chapter4;
import java.text.DecimalFormat;
import java.util.Scanner;
public class SoftwareSalesDemo {
public static void main(String[] args) {
int units; // To hold units sold
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the units sold.
System.out.print("Enter the units sold: ");
units = keyboard.nextInt();
// Create a SoftwareSales object.
SoftwareSales sales = new SoftwareSales(units);
// Display purchase info.
DecimalFormat dollar = new DecimalFormat("#,##0.00");
System.out.println("Units sold: " + sales.getUnitsSold());
System.out.println("Discount: $" + dollar.format(sales.getDiscount()));
System.out.println("Cost: $" + dollar.format(sales.getCost()));
}
}
以下是我写的这个课程,这是我需要帮助的。
package Chapter4;
/*
* Software sales class
*
* Eric Goldberg
*
* Calculate the total cost of units sold */
public class SoftwareSales {
//fields
private int UnitsSold;
private double Discount;
private double Cost;
private double UnitCost=99.00;
public SoftwareSales (int uni)
{
UnitsSold = uni;
}
public Discount(double dis)
{
Discount = dis;
if(UnitsSold >=10 && UnitsSold<=19) {
Discount = .20;
}
if(UnitsSold >= 20 && UnitsSold<=49) {
Discount = .30;
}
if (UnitsSold >= 50 && UnitsSold <= 99) {
Discount = .40;
}
if (UnitsSold >= 100) {
Discount = .50;
}
} //end of discount
public double UnitCost() {
return UnitCost=99.00;
}
public void setCost(double cost) {
Cost = Discount*UnitsSold;
}
//return the discount amount.
public double getDiscount()
{
return Discount;
}
//return unitssold.
public int getUnitsSold() {
return UnitsSold;
}
//return cost
public double getCost() {
return Cost;
}
}
答案 0 :(得分:0)
您需要更正成本和折扣计算。我假设您需要总成本和总折扣。您可以根据需要将输入(没有单位)验证视为幻想。 (比如读取字符串,然后是parseInt)。现在,我假设输入将是一个整数,直到用户输入整数&gt; 0,我继续问。
SoftwareSalesDemo.java
Test.Parameters
SoftwareSales.java
package Chapter4;
import java.text.DecimalFormat;
import java.util.Scanner;
public class SoftwareSalesDemo {
public static void main(String[] args) {
// To hold units sold
int units;
// Display purchase info.
DecimalFormat dollar = new DecimalFormat("#,##0.00");
// Get the units sold.
System.out.print("Enter the units sold: ");
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
units = keyboard.nextInt();
while (units <= 0) {
System.out.println("Please enter valid no. of units (>0)");
units = keyboard.nextInt();
}
// Create a SoftwareSales object.
SoftwareSales sales = new SoftwareSales(units);
System.out.println("Units sold : " + sales.getUnitsSold());
System.out.println("Total Discount : $" + dollar.format(sales.getDiscount()));
System.out.println("Total Cost : $" + dollar.format(sales.getCost()));
}
}
示例运行
package Chapter4;
/*
* Software sales class
*
* Eric Goldberg
*
* Calculate the total cost of units sold */
public class SoftwareSales {
// fields
private int unitsSold;
private double discount;
private double cost;
private double unitCost = 99.00;
public SoftwareSales(int units) {
this.unitsSold = units;
setDiscount();
setCost();
}
// set discount based on no. of units sold
private void setDiscount() {
if (unitsSold >= 10 && unitsSold <= 19) {
discount = .20;
}
if (unitsSold >= 20 && unitsSold <= 49) {
discount = .30;
}
if (unitsSold >= 50 && unitsSold <= 99) {
discount = .40;
}
if (unitsSold >= 100) {
discount = .50;
}
} // end of discount
// return per unit cost
private double getUnitCost() {
return unitCost;
}
// calculate total cost
private void setCost() {
cost = unitsSold * (getUnitCost() * (1 - discount));
}
// return the discount amount.
public double getDiscount() {
return ((discount * getUnitCost()) * unitsSold);
}
// return unitsSold.
public int getUnitsSold() {
return unitsSold;
}
// return total cost
public double getCost() {
return cost;
}
}