第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>

  

实验结果:

时间: 2024-08-06 20:03:06

第4次作业类测试代码 021 林明铁的相关文章

第4次作业类测试代码+074+林盼皇

(友情提示:代码部分较多,为了便于测试,项目码源已上传至链接:http://pan.baidu.com/s/1pLscU3T 密码:ug8i)  界面: 1.类图 2.界面和相应的功能. 本次实验是在原来的CalDate日期计算类的基础上,添加了两个方法int weekDay(int m,int d,int y)与String lastDate(int m,int d,int y),此外还编写了GUInterface界面.  a.实现lastDate 1 public String lastDa

第4次作业类测试代码+101+谢艳敏

类测试代码的具体要求如下: 界面操作说明补充: 点击OK,如果输入有效,进行相应的数值计算:如果数值不满足约束,则弹出错误说明,统一为"输入有误,请重新输入",然后回到初始输入状态. 点击Cancle,表示重置,清空前面的数据,回到初始状态. (2)NextDate函数问题 String  nextdate(int m,int d,int y) 建立界面,至少包含以下元素,但不限于此: 完成上一天方法:String lastDay(int m,int d,int y) ,完成周几的方法

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

第4次作业:准备类测试代码 类测试代码的具体要求如下: (1)设计三角形完整程序 已经完成的方法是:  String triangle(int a,int b,int c) 现在要求继续增加新的功能: 建立界面,至少包含以下元素,但不限于此: 完成面积的方法:float triangleArea(int a,int b,int c) ,完成周长的方法:int perimeter(int a,int b,int c) 要求: 1.        画出类图: 2.        完成界面和相应的功能

第4次作业类测试代码+105032014138+牟平

类测试代码的具体要求如下: 设计三角形完整程序 已经完成的方法是:  String triangle(int a,int b,int c) 现在要求继续增加新的功能: 建立界面,至少包含以下元素,但不限于此: 完成面积的方法:float triangleArea(int a,int b,int c) ,完成周长的方法:int perimeter(int a,int b,int c) 一.类图 二.功能界面 1 2 3 4 5 6 三.代码: import java.awt.EventQueue;

第4次作业类测试代码+105032014125+洪诗育

类测试代码的具体要求如下: 界面操作说明补充: 点击OK,如果输入有效,进行相应的数值计算:如果数值不满足约束,则弹出错误说明,统一为"输入有误,请重新输入",然后回到初始输入状态. 点击Cancle,表示重置,清空前面的数据,回到初始状态. NextDate函数问题 String  nextdate(int m,int d,int y) 建立界面,至少包含以下元素,但不限于此: 完成上一天方法:String lastDay(int m,int d,int y) ,完成周几的方法:in

第4次作业类测试代码+098+吴超

一.类图 二.代码与界面 简单的分层思想,代码目录如下: 计算日期的业务类操作代码:printDate.java;具体包括如下方法. 2.1  增加计算星期几的方法weekDay(),利用蔡勒(Zeller)公式.即w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1: 公式中的符号含义如下,w:星期:c:世纪-1:y:年(两位数):m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1.2月要看作上一年的13.14月来计算,比如2003年1月1日要看作2002年的13月

实验二 021 林明铁

源代码: 1 import java.util.Scanner; 2 3 public class Commission { 4 5 /* 6 * hp:耳机 80元 mpc:手机壳 10元 cpsp:手机贴膜 8元 7 */ 8 9 public static void main(String[] args) { 10 String line; 11 int hp = 0, mpc = 0, cpsp = 0; 12 String[] input = null; 13 float money

第4次作业类测试代码+028+刘孝天

一:类图 二:代码:  1:定义接口 1 package jframeTest; 2 /* 3 * @author lxt 4 * @date 2017年5月2日下午4:22:35 5 * @Param 6 */ 7 public interface InteUtil { 8 9 public int perimeter(int a,int b,int c); 10 public float triangleArea(int a,int b,int c); 11 public boolean I

第4次作业类测试代码+085+潘亭

一.类图设计如下 二.界面如下 功能演示 1.输入错误 2.不构成三角形 3.一般三角形 面积默认保留两位小数 4.直角三角形 5.等腰三角形 6.等边三角形 7.cancel演示 防止程序崩溃,默认重置为0 三.代码部分 1.Triangle类 1 package visualTriangle; 2 3 public class Triangle { 4 5 //judge the fields 6 public static boolean Check(int num) 7 { 8 if(n