一、类图
二、代码
package application;
public class Commission {
/*
* hp:耳机 80元 mpc:手机壳 10元 cpsp:手机贴膜 8元
*/
public float calculate(String line) {
int hp = 0, mpc = 0, cpsp = 0;
String[] input = null;
float money = 0;
while (true) {
// 【去掉字符串前后的空格】
line = line.trim();
// 【去掉字符串中多余的空格】
line = line.replaceAll("\s{1,}", " ");
input = line.split(" ");
if (Judge(input)) {
// 判断是否不小于0
if ((hp = Integer.parseInt(input[0])) < 0) {
System.out.println("输入数量不满足要求");
return -1;
}
if ((mpc = Integer.parseInt(input[1])) < 0) {
System.out.println("输入数量不满足要求");
return -1;
}
if ((cpsp = Integer.parseInt(input[2])) < 0) {
System.out.println("输入数量不满足要求");
return -1;
}
} else {
System.out.println("输入数量不满足要求");
return -1;
}
money = commission(hp, mpc, cpsp);
return money;
}
}
// 计算佣金
private static float commission(int hp, int mpc, int cpsp) {
float commission = 0;
int total = hp * 80 + mpc * 10 + cpsp * 8;
if (total < 1000) {
commission = (float) (total * 0.1);
} else if (total <= 1800) {
commission = (float) (1000 * 0.1 + (total - 1000) * 0.15);
} else {
commission = (float) (1000 * 0.1 + 800 * 0.15 + (total - 1800) * 0.2);
}
return commission;
}
// 判断用户输入的是不是三个整数
private static boolean Judge(String[] input) {
String number = "0123456789";
// 判断输入的是不是三个字符串
if (input.length != 3) {
return false;
}
// 判断三个字符串是不是纯数字且不含小数点
for (int i = 0; i < 3; i++) {
for (int j = 0; j < input[i].length(); j++) {
if ("+".equals(String.valueOf(input[i].charAt(0))) || "-".equals(String.valueOf(input[i].charAt(0)))) {
if ("+".equals(String.valueOf(input[i].charAt(0)))) {
input[i].substring(1);
}
continue;
}
if (number.indexOf(input[i].charAt(j)) == -1) {
return false;
}
// 【判断输入的字符串是否大于整型的最大数值】
input[i] = input[i].replaceFirst("^0*", "");
if (Long.parseLong(input[i]) > Integer.MAX_VALUE || input[i].length() > 10) {
return false;
}
}
}
return true;
}
}
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
primaryStage.setTitle("Calculate Commission");
primaryStage.setScene(new Scene(root));
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="278.0" prefWidth="399.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.MainController">
<children>
<Text layoutX="14.0" layoutY="32.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="请输入销售数量" wrappingWidth="165.240234375">
<font>
<Font size="18.0" />
</font>
</Text>
<Text layoutX="14.0" layoutY="73.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="耳机:">
<font>
<Font size="16.0" />
</font>
</Text>
<Text layoutX="147.0" layoutY="73.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="手机壳:">
<font>
<Font size="16.0" />
</font>
</Text>
<Text layoutX="289.0" layoutY="72.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="贴膜:">
<font>
<Font size="16.0" />
</font>
</Text>
<TextField fx:id="TF_hp" layoutX="62.0" layoutY="53.0"
prefHeight="23.0" prefWidth="48.0">
<font>
<Font size="13.0" />
</font>
</TextField>
<TextField fx:id="TF_mpc" layoutX="214.0" layoutY="52.0"
prefHeight="23.0" prefWidth="48.0">
<font>
<Font size="13.0" />
</font>
</TextField>
<TextField fx:id="TF_cpsp" layoutX="337.0" layoutY="52.0"
prefHeight="23.0" prefWidth="48.0">
<font>
<Font size="13.0" />
</font>
</TextField>
<Button layoutX="99.0" layoutY="90.0" mnemonicParsing="false"
onAction="#BT_OK_Event" prefHeight="28.0" prefWidth="64.0" text="OK"
AnchorPane.bottomAnchor="164.0" AnchorPane.topAnchor="91.0">
<font>
<Font size="13.0" />
</font>
</Button>
<Button layoutX="236.0" layoutY="90.0" mnemonicParsing="false"
onAction="#BT_Cancel_Event" prefHeight="28.0" prefWidth="64.0" text="Cancel"
AnchorPane.bottomAnchor="164.0" AnchorPane.topAnchor="91.0">
<font>
<Font size="13.0" />
</font>
</Button>
<Text layoutX="38.0" layoutY="155.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="应返还的佣金:">
<font>
<Font size="16.0" />
</font>
</Text>
<Text layoutX="38.0" layoutY="195.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="销售额最高的配件是:">
<font>
<Font size="16.0" />
</font>
</Text>
<Text layoutX="38.0" layoutY="235.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="销售配件最多与最少数量相差:">
<font>
<Font size="16.0" />
</font>
</Text>
<TextField fx:id="TF_commission" layoutX="156.0" layoutY="135.0"
prefHeight="28.0" prefWidth="203.0">
<font>
<Font size="13.0" />
</font>
</TextField>
<TextField fx:id="TF_MostSale" layoutX="199.0" layoutY="175.0"
prefHeight="28.0" prefWidth="160.0">
<font>
<Font size="13.0" />
</font>
</TextField>
<TextField fx:id="TF_MaxMin" layoutX="269.0" layoutY="215.0"
prefHeight="28.0" prefWidth="91.0">
<font>
<Font size="13.0" />
</font>
</TextField>
</children>
</AnchorPane>
三、运行截图
4、结果截图