第4次作业类测试代码 021 林明铁

Posted mtLin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第4次作业类测试代码 021 林明铁相关的知识,希望对你有一定的参考价值。

1、类图:

 

界面设计:

 

 

文件结构:

代码:

Commission.java

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;
    }
}

Main.java

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);
    }
}

MainController.java

package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;

public class MainController implements Initializable {
    @FXML
    private TextField TF_hp;
    @FXML
    private TextField TF_mpc;
    @FXML
    private TextField TF_cpsp;
    @FXML
    private TextField TF_commission;
    @FXML
    private TextField TF_MostSale;
    @FXML
    private TextField TF_MaxMin;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub

    }

    public void BT_OK_Event() {
        Commission commission = new Commission();
        String hp, mps, cpsp;
        int headphone, mobilePhoneShell, protector;
        hp = TF_hp.getText();
        mps = TF_mpc.getText();
        cpsp = TF_cpsp.getText();
        String input = hp + " " + mps + " " + cpsp;
        float salary = commission.calculate(input);
        if (salary != -1) {
            headphone = Integer.parseInt(hp);
            mobilePhoneShell = Integer.parseInt(mps);
            protector = Integer.parseInt(cpsp);
            String mostsale = mostSale(headphone, mobilePhoneShell, protector);
            int diffsal = diffSale(headphone, mobilePhoneShell, protector);

            TF_commission.setText(String.valueOf(salary));
            TF_MostSale.setText(mostsale);
            TF_MaxMin.setText(String.valueOf(diffsal));
        } else {
            TF_commission.setText("您输入有误,请重新输入!");
            TF_MostSale.clear();
            TF_MaxMin.clear();
        }
    }

    public void BT_Cancel_Event() {
        TF_hp.clear();
        TF_mpc.clear();
        TF_cpsp.clear();
        TF_commission.clear();
        TF_MostSale.clear();
        TF_MaxMin.clear();
    }

    private int diffSale(int headphone, int mobilePhoneShell, int protector) {
        int diffsal = 0;
        int max, min;
        max = Math.max(Math.max(headphone, mobilePhoneShell), protector);
        if (max == headphone) {
            min = Math.min(mobilePhoneShell, protector);
        } else if (max == mobilePhoneShell) {
            min = Math.min(headphone, protector);
        } else {
            min = Math.min(headphone, mobilePhoneShell);
        }
        diffsal = max - min;
        return diffsal;
    }

    private String mostSale(int headphone, int mobilePhoneShell, int protector) {
        int hp, mps, cpsp, max;
        String mostsale = null;
        hp = headphone * 80;
        mps = mobilePhoneShell * 10;
        cpsp = protector * 8;
        max = Math.max(Math.max(hp, mps), cpsp);
        if (max == hp) {
            mostsale = "耳机";
        } else if (max == mps) {
            mostsale = "手机壳";
        } else {
            mostsale = "贴膜";
        }
        return mostsale;
    }

}

 Main.fxml

<?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次作业类测试代码 021 林明铁的主要内容,如果未能解决你的问题,请参考以下文章

实验三 021 林明铁

实验二 021 林明铁

第4次作业类测试代码+105032014166+张珍珍

第4次作业类测试代码+105032014045+杨铭河

第4次作业类测试代码+063+陈彧

第4次作业类测试代码+105032014065+方绎杰