第4次作业类测试代码+105032014162+张倩

一、类图

二、代码

  1 package demo04;
  2 import java.awt.event.ActionEvent;
  3 import java.awt.event.ActionListener;
  4 import java.awt.Color;
  5 import java.awt.Font;
  6 import javax.swing.JFrame;
  7 import javax.swing.JLabel;
  8 import javax.swing.JButton;
  9 import javax.swing.JTextField;
 10 import javax.swing.JOptionPane;
 11 public class caculate {
 12     public static void main(String args[]){
 13         JFrame frame = new JFrame("配件销售计算程序");
 14         frame.setLayout(null);
 15         //建立标签对象
 16         JLabel title = new JLabel("请输入销售数量:");
 17         JLabel headphone = new JLabel("耳机:");
 18         JLabel shell = new JLabel("手机壳:");
 19         JLabel protector = new JLabel("贴膜:");
 20         JLabel commission = new JLabel("应返还的佣金:");
 21         JLabel mostSale = new JLabel("销售额最高的配件是:");
 22         JLabel diffSale = new JLabel("销售配件最多与最少数量相差:");
 23         //建立单行文本框
 24         JTextField t_headphone = new JTextField();
 25         JTextField t_shell = new JTextField();
 26         JTextField t_protector = new JTextField();
 27         JTextField t_commission = new JTextField();
 28         JTextField t_mostSale = new JTextField();
 29         JTextField t_diffSale = new JTextField();
 30         //建立按钮对象
 31         JButton submit = new JButton("确定");
 32         JButton cancel = new JButton("清除");
 33
 34         //使用匿名内部类为“确定”按钮添加监听事件
 35         submit.addActionListener(new ActionListener()
 36         {
 37             @Override
 38             public void actionPerformed(ActionEvent arg0)
 39             {
 40                 int headphone = Commission.changeToInt(t_headphone.getText());
 41                 if(headphone < 0)
 42                 {
 43                     JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "出错", JOptionPane.ERROR_MESSAGE);
 44                     return;
 45                 }
 46                 int protector=Commission.changeToInt(t_protector.getText());
 47                 if(protector < 0)
 48                 {
 49                     JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "出错", JOptionPane.ERROR_MESSAGE);
 50                     return;
 51                 }
 52                 int shell=Commission.changeToInt(t_shell.getText());
 53                 if(shell < 0)
 54                 {
 55                     JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "出错", JOptionPane.ERROR_MESSAGE);
 56                     return;
 57                 }
 58                 float returnAns=Commission.commission(headphone, shell, protector);
 59                 String returnString=String.format("%.2f元", returnAns);
 60                 t_commission.setText(returnString);
 61                 String maxAns=Commission.mostSale(headphone, shell, protector);
 62                 t_mostSale.setText(maxAns);
 63                 int differenceAns=Commission.diffSale(headphone, shell, protector);
 64                 String differenceString=String.format("%d", differenceAns);
 65                 t_diffSale.setText(differenceString);
 66             }
 67
 68         });
 69         //使用匿名内部类为“清除”按钮添加监听事件
 70         cancel.addActionListener(new ActionListener()
 71         {
 72             @Override
 73             public void actionPerformed(ActionEvent arg0)
 74             {
 75                 t_headphone.setText("");
 76                 t_protector.setText("");
 77                 t_shell.setText("");
 78                 t_commission.setText("");
 79                 t_mostSale.setText("");
 80                 t_diffSale.setText("");
 81             }
 82         });
 83         //设置标签大小及位置
 84         Font ft = new Font("宋体",Font.BOLD,24);
 85         title.setFont(ft);
 86         title.setBounds(62, 62, 250, 25);
 87         headphone.setFont(ft);
 88         headphone.setBounds(80, 124, 300, 25);
 89         shell.setFont(ft);
 90         shell.setBounds(210, 124, 250, 25);
 91         protector.setFont(ft);
 92         protector.setBounds(380, 124, 155, 25);
 93         commission.setFont(ft);
 94         commission.setBounds(62, 248, 500, 25);
 95         mostSale.setFont(ft);
 96         mostSale.setBounds(62, 310, 900, 25);
 97         diffSale.setFont(ft);
 98         diffSale.setBounds(62, 372, 1000, 25);
 99         //设置文字大小及位置
100         Font fft=new Font("宋体",Font.BOLD,14);
101         t_headphone.setBounds(150, 124, 50, 25);
102         t_shell.setBounds(310, 124, 50, 25);
103         t_protector.setBounds(450, 124, 50, 25);
104         t_commission.setBounds(230, 248, 200, 25);
105         t_commission.setFont(fft);
106         t_commission.setForeground(Color.black);//设置字体颜色
107         t_mostSale.setBounds(300, 310, 200, 25);
108         t_mostSale.setFont(fft);
109         t_mostSale.setForeground(Color.black);
110         t_diffSale.setBounds(400, 372, 150, 25);
111         t_diffSale.setFont(fft);
112         t_diffSale.setForeground(Color.black);
113         //设置按钮位置及大小
114         submit.setBounds(180, 187, 62, 31);
115         cancel.setBounds(320, 187, 62, 31);
116         //把所有组件添加到frame中
117         frame.add(title);frame.add(headphone);frame.add(shell);frame.add(protector);
118         frame.add(commission);frame.add(mostSale);frame.add(diffSale);
119         frame.add(t_headphone);frame.add(t_shell);frame.add(t_protector);
120         frame.add(t_commission);frame.add(t_mostSale);frame.add(t_diffSale);
121         frame.add(submit);frame.add(cancel);
122         //设置显示画布大小及显示的位置
123         frame.setSize(600, 490);
124         frame.setLocation(300,300);
125         frame.setVisible(true);
126     }
127
128 }
package demo04;

public class Commission {
    //判断输入的值是否符合要求
    public static int changeToInt(String number)
    {
        int result=0;
        try{
            result = Integer.parseInt(number);
        }
        catch (Exception e){
            result=-1;
        }
        return result;
    }

    //计算应该返还的佣金
    public static float commission(int headphone,int shell,int protector)
    {
        int all = headphone*80+shell*10+protector*8;
        float result = 0;
        if(headphone>=0&&shell>=0&&protector>=0)
        {
            if(all<1000&&all>=0)
            {
                result = (float) (all*0.1);
            }
            else if(all>=1000 && all<=1800)
            {
                result = (float) (100+(all-1000)*0.15);
            }
            else if(all>1800)
            {
                result = (float) (100+800*0.15+(all-1800)*0.2);
            }
        }
        return result;

    }

    //计算销售额最高的配件
    public static String mostSale(int headphone, int shell, int protector)
    {
        int headphoneSales = headphone * 80;
        int shellSales = shell * 10;
        int protectorSales = protector * 8;
        String result="";
        if(headphoneSales > shellSales && headphoneSales > protectorSales)
        {
            result+="耳机";
        }

        if(shellSales > headphoneSales && shellSales > protectorSales)
        {
                result+="手机壳";
        }
        if(protectorSales > headphoneSales && protectorSales > shellSales)
        {
                result+="贴膜";
        }
        if(headphoneSales==shellSales&&shellSales==protectorSales)
        {
            result+="三种配件销售额一样";
        }
        if(headphoneSales==shellSales&&headphoneSales>protectorSales)
        {
            result+="耳机和手机壳";
        }
        if(headphoneSales==protector&&headphoneSales>shellSales)
        {
            result+="耳机和贴膜";
        }
        if(shellSales==protectorSales&&shellSales>headphoneSales)
        {
            result+="手机壳和贴膜";
        }
        return result;
    }

    //销售配件最多与最少数量相差:
    public static int diffSale(int headphone, int shell, int protector){
        int max=Math.max(Math.max(headphone, shell), protector);
        int min=Math.min(Math.min(headphone, shell), protector);
        return max-min;
    } 

}

三、界面

时间: 2024-12-29 19:34:10

第4次作业类测试代码+105032014162+张倩的相关文章

第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次作业类测试代码+101+谢艳敏

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

第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次作业类测试代码+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次作业类测试代码+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月

第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

第5次作业类测试代码+140+阮晨曦

1. 代码链接 http://www.cnblogs.com/chenxxiaol/p/6804119.html 2. 界面设计 3. 等价类测试 分析题目 得出对输入的条件要求为 (1)      整数 (2)      三个数 (3)      1≤a≤100 (4)      1≤b≤100 (5)      1≤c≤100 (6)      a<b+c (7)      b<a+ c (8)      c<a+ b (9)    等腰三角形 (10)   等边三角形 (11)