Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何让我的计划更新100%产品销售额的百分比_Java_User Interface - Fatal编程技术网

Java 我如何让我的计划更新100%产品销售额的百分比

Java 我如何让我的计划更新100%产品销售额的百分比,java,user-interface,Java,User Interface,基本上,我的程序会获取一个销售价值,并进行更新。该计划还应更新该产品占所有产品总销售额的百分之一百。例如,如果我为胡萝卜输入100销售,那么所有销售的百分比应该是100%,因为这是唯一一个已经销售的产品,并且只有100销售。当你给potatos增加100个销售额时,每个销售额的百分比将变为50%,因为两个销售额之间有200个销售额。我将如何编写代码来实现这一点。我的尝试代码附在下面,任何进一步的问题请在下面评论 问题在于calculatepercentage方法和updatesalesactio

基本上,我的程序会获取一个销售价值,并进行更新。该计划还应更新该产品占所有产品总销售额的百分之一百。例如,如果我为胡萝卜输入100销售,那么所有销售的百分比应该是100%,因为这是唯一一个已经销售的产品,并且只有100销售。当你给potatos增加100个销售额时,每个销售额的百分比将变为50%,因为两个销售额之间有200个销售额。我将如何编写代码来实现这一点。我的尝试代码附在下面,任何进一步的问题请在下面评论

问题在于calculatepercentage方法和updatesalesaction方法中以百分比开头的语句。将销售额添加到特定产品时,百分比未更新

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
This is an initial starter version of a Java application that should be
extended in stages, eventually allowing the user to enter product sales figures
for a number of products, and display them in a table with %ages, a ranking and a pie chart.

This starter code just has storage for one product, and allows that product's sales figure 
to be updated and displayed. The display is plain, with no font changes and
no border lines for the table. All the processing is limited to just this one product,
and must be adapted for a whole array of products.

SBJ March 2016
 */

public class ProductChart extends JFrame implements ActionListener
{
    /**
     * Frame constants
     */
    private static final int FRAME_LOCATION_X = 350;
    private static final int FRAME_LOCATION_Y = 250;
    private static final int FRAME_WIDTH = 650;
    private static final int FRAME_HEIGHT = 400;

    /**
     * The maximum permitted number of products
     */
    private final int MAX_PRODUCTS = 10;

    /**
     * These arrays holds all the sales data:
     * Element 0 is unused, so array sizes are MAX_PRODUCTS+1.
     * The product number (from 1 to MAX_PRODUCTS) is the index in the arrays
     * where the product's data is held. 
     * Sales figures are counted quantities, so int.
     */
    private String[] productName;  // The name of each product
    private int[] productSales;    // The number of sales of each product
    private float[] percentage;    // The proportion of total sales for each product

    private int totalSales;        // Always the current total sales

    /**
     * Display area layout constants
     */
    private final int DISPLAY_WIDTH = 600;
    private final int DISPLAY_HEIGHT = 300;
    private final int PRODUCT_X = 30;   // Start of product number column
    private final int NAME_X = 75;      // Start of product name column
    private final int SALES_X = 225;    // Start of sales column
    private final int PERCENTAGE_X = 300;    //Start of percentage column
    private final int TABLE_LINES_Y = 12; //The number of horizontal lines required to draw the table
    private final int TABLE_LINES_X = 5; //The number of vertical ""

    /**
     * The main launcher method:
     * Configure the applications's window, initialize the sales data structures,
     * and make the applications visible.
     * @param args Unused
     */
    public static void main(String[] args)
    {
        ProductChart frame = new ProductChart();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(FRAME_LOCATION_X, FRAME_LOCATION_Y);
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.initializeSalesData();
        frame.createGUI();
        frame.setVisible(true);
        frame.setTitle("Product Chart 2312931");

    }

    /**
     * The GUI components
     */
    private JTextField productSalesEntryField;  // For entry of new product sales figures
    private JButton updateSalesButton;          // To request update of a sales figure
    private JPanel displayArea;                 // Graphics area for drawing the sales table
    private JTextField productRefEntryField;    // For entry of product refence number when updating sales figures

    /**
     * Helper method to build the GUI
     */
    private void createGUI()
    {
        // Standard window set up
        Container window = getContentPane();
        window.setLayout(new FlowLayout());
        window.setBackground(Color.lightGray);

        // Product reference entry label and text field
        JLabel productRefEntryLabel = new JLabel("Product Ref #:");
        productRefEntryField = new JTextField(2);
        window.add(productRefEntryLabel);
        window.add(productRefEntryField);       

        // Product sales entry label and text field
        JLabel productSalesEntryLabel = new JLabel("Product sales:");
        productSalesEntryField = new JTextField(5);
        window.add(productSalesEntryLabel);
        window.add(productSalesEntryField);

        // Button to add new sales figure
        updateSalesButton = new JButton("Update sales");
        updateSalesButton.addActionListener(this);
        window.add(updateSalesButton);

        // The drawing area for displaying all data
        displayArea = new JPanel()
        {
            // paintComponent is called automatically when a screen refresh is needed
            public void paintComponent(Graphics g)
            {
                // g is a cleared panel area
                super.paintComponent(g); // Paint the panel's background
                paintScreen(g);          // Then the required graphics
            }
        };
        displayArea.setPreferredSize(new Dimension(DISPLAY_WIDTH, DISPLAY_HEIGHT));
        displayArea.setBackground(Color.white);
        window.add(displayArea);
    }

    /**
     * Initializes product arrays:
     * A set of product names,
     * With sales initially 0.
     * 
     * Note: In the arrays, the first element is unused, so Bread is at index 1
     */
    private void initializeSalesData()
    {
        productName = new String[MAX_PRODUCTS+1];
        productName[1] = "Bread";   // Note: First element is unused, so Bread is at index 1
        productName[2] = "Milk";
        productName[3] = "Eggs";
        productName[4] = "Cheese";
        productName[5] = "Cream";
        productName[6] = "Butter";
        productName[7] = "Jam";
        productName[8] = "Chocolate Spread";
        productName[9] = "Corn Flakes";
        productName[10] = "Sugar";

        productSales = new int[MAX_PRODUCTS+1];
        for(int i=0; i< MAX_PRODUCTS; i++)
        {
            productSales[i] = 0;
        }

        percentage = new float[MAX_PRODUCTS+1];
        for(int i=0; i< MAX_PRODUCTS; i++)
        {
            percentage[i] = 0;
        }

    }

    /**
     * Event handler for button clicks.
     *
     * One action so far:
     * o Update the sales figure for a product
     */
    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource() == updateSalesButton)
            updateSalesAction();

        // And refresh the display
        repaint();
    }

    /**
     * Action updating a sales figure: the new sales figure is fetched 
     * from a text fields, parsed (converted to an int), and action is taken.
     */
    private void updateSalesAction()
    {
        // Fetch the new sales details
        int productRef = Integer.parseInt(productRefEntryField.getText());
        int newSales = Integer.parseInt(productSalesEntryField.getText());
        if (productRef>0&&productRef<=MAX_PRODUCTS)
        {
            // Update the sales tables
            productSales[productRef] = newSales;
            totalSales = totalSales + newSales;
            percentage[productRef] = calculatePercentage(productSales[productRef-1], totalSales); 
        }


    }

    /**
     * Redraw all the sales data on the given graphics area
     */
    public void paintScreen(Graphics g)
    {
        // Draw a table of the sales data, with columns for the product number,
        // product name, and sales

        //Heading
        g.setFont(new Font("default", Font.BOLD, 16));
        g.drawString("Product sales data:", 20, 20);

        // Table column headers
        g.setFont(new Font("default", Font.BOLD, 12));
        g.drawString("No", PRODUCT_X, 60);
        g.drawString("Name", NAME_X, 60);
        g.drawString("Sales", SALES_X, 60);
        g.drawString("Percentage", PERCENTAGE_X, 60);

        // The table of sales data
        g.setFont(new Font("default", Font.PLAIN, 12));
        int y = 80;// The y coordinate for each line
        int yIncrement = 20;

        for(int i = 0; i<productName.length-1; i++)
        {
            g.drawString(""+Integer.toString(i+1), PRODUCT_X, y+i*yIncrement);          // First column: product number

        }
        for(int i = 0; i<productName.length-1; i++)
        {
            g.drawString(productName[i+1], NAME_X, y+i*yIncrement);    // Second column: product name

        }
        for(int i = 0; i<productSales.length-1; i++)
        {
            g.drawString(Integer.toString(productSales[i+1]), SALES_X, y+i*yIncrement); // Third column: sales figure 
        }
        for(int i = 0; i<percentage.length-1; i++)
        {
            g.drawString(Float.toString(percentage[i+1]), PERCENTAGE_X, y+i*yIncrement);
        }
        for(int i = 0; i<TABLE_LINES_Y ; i++)
        {
            //g.drawLine(
        }
    }

    public float calculatePercentage(int sales, int totalSales)
    {
        float percentage = sales/totalSales;
        return percentage;     

    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
/**
这是Java应用程序的初始启动版本,应该
分阶段扩展,最终允许用户输入产品销售数据
对于许多产品,将它们显示在带有%年龄的表格中,并显示在排名和饼图中。
此起始代码只存储一个产品,并允许该产品的销售数字
要更新和显示。显示是普通的,没有字体变化和
表没有边框线。所有加工仅限于这一种产品,
而且必须适应一系列的产品。
SBJ 2016年3月
*/
公共类ProductChart扩展JFrame实现ActionListener
{
/**
*帧常数
*/
私有静态最终整数帧位置=350;
私有静态最终整数帧位置Y=250;
专用静态最终整数帧_宽度=650;
专用静态最终整型框架高度=400;
/**
*产品的最大允许数量
*/
私人最终int MAX_产品=10;
/**
*这些阵列保存所有销售数据:
*元素0未使用,因此数组大小为MAX_乘积+1。
*产品编号(从1到最大产品)是数组中的索引
*保存产品数据的位置。
*销售数字是按数量计算的,所以int。
*/
私有字符串[]productName;//每个产品的名称
private int[]productSales;//每个产品的销售数量
私人浮动[]百分比;//每种产品在总销售额中所占的比例
private int totalSales;//始终为当前总销售额
/**
*显示区布局常数
*/
专用最终整数显示\u宽度=600;
专用最终整数显示高度=300;
private final int PRODUCT_X=30;//产品编号列的开头
private final int NAME_X=75;//产品名称列的开头
private final int SALES_X=225;//开始销售列
private final int PERCENTAGE_X=300;//百分比列的开头
private final int TABLE_LINES_Y=12;//绘制表格所需的水平线数
私有final int TABLE_line_X=5;//垂直
/**
*主要的发射器方法:
*配置应用程序窗口,初始化销售数据结构,
*并使应用程序可见。
*@param args未使用
*/
公共静态void main(字符串[]args)
{
ProductChart框架=新ProductChart();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(frame_LOCATION_X,frame_LOCATION_Y);
框。设置尺寸(框宽、框高);
frame.initializeSalesData();
frame.createGUI();
frame.setVisible(true);
框架.setTitle(“产品图表2312931”);
}
/**
*GUI组件
*/
私有JTextField productSalesEntryField;//用于输入新产品销售数字
私有JButton updateSalesButton;//请求更新销售数据
private JPanel displayArea;//用于绘制销售表的图形区域
私有JTextField productRefEntryField;//用于在更新销售数字时输入产品参考号
/**
*帮助器方法来构建GUI
*/
私有void createGUI()
{
//标准窗口设置
容器窗口=getContentPane();
setLayout(新的FlowLayout());
窗。背景(颜色。浅灰色);
//产品参考条目标签和文本字段
JLabel productRefEntryLabel=新JLabel(“产品参考号:”);
productRefEntryField=新的JTextField(2);
添加(productRefEntryLabel);
添加(productRefEntryField);
//产品销售条目标签和文本字段
JLabel productSalesEntryLabel=新的JLabel(“产品销售:”);
productSalesEntryField=新的JTextField(5);
添加(productSalesEntryLabel);
添加(productSalesEntryField);
//按钮添加新的销售数字
updateSalesButton=newjbutton(“更新销售”);
updateSalesButton.addActionListener(此);
添加(updateSalesButton);
//用于显示所有数据的绘图区域
displayArea=newJPanel()
{
//当需要刷新屏幕时,会自动调用paintComponent
公共组件(图形g)
{
//g是一个已清除的面板区域
super.paintComponent(g);//绘制面板的背景
paintScreen(g);//然后是所需的图形
}
};
displayArea.setPreferredSize(新尺寸(显示宽度、显示高度));
显示区.背景(颜色.白色);
添加(显示区域);
}
/**
*初始化产品阵列:
*一组产品名称,
*最初的销售额为0。
* 
*注意:在数组中,第一个元素未使用,因此Bread位于索引1处
public float calculatePercentage(int sales, int totalSales)
{
    return 100.0f * sales / totalSales;
}
private void updateSalesAction()
{
    // Fetch the new sales details
    int productRef = Integer.parseInt(productRefEntryField.getText());
    int newSales = Integer.parseInt(productSalesEntryField.getText());
    if (productRef>0&&productRef<=MAX_PRODUCTS)
    {
        // Update the sales tables
        productSales[productRef] = newSales;
        totalSales = 0;
        for (int i = 1; i <= MAX_PRODUCTS; i++)
            totalSales += productSales[i];
        for (int i = 1; i <= MAX_PRODUCTS; i++)
            percentage[i] = calculatePercentage(productSales[i], totalSales);
    }
}