首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >决定批发商或零售商的价格

决定批发商或零售商的价格
EN

Code Review用户
提问于 2015-03-15 20:15:32
回答 1查看 48关注 0票数 5

这是我上大学的第一年,我用我自己的基本知识设计了一个代码。我设计了一个程序来计算批发商或零售商的商品价格。特殊客户如果点击按钮,也会得到特别的折扣。请看一看我的代码,并给我任何反馈,我如何实现它。如果你认为我做得很好,请告诉我。

代码语言:javascript
复制
import java.awt.Dimension;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;

public class packingcompany extends JPanel {

    JTable jt;

    public packingcompany() {
        String[] columns = {"Wholesalers No.Units", "Wholesalers(£)", "Retailers No.Units", "Retailers (£)"};

        String[][] data = {{"1-6", "£50", "1-4", "£60"},
        {"7-11", "£40", "5-9", "£50"},
        {"12-21", "£30", "10-15", "£40"},
        {"22-60", "£20", "16-50", "£30"},
        {"60+", "£15", "50+", "£25"}};

        jt = new JTable(data, columns) {
            public boolean isCellEditable(int data, int columns) {
                return false;
            }
        };

        jt.setPreferredScrollableViewportSize(new Dimension(550, 80));
        jt.setFillsViewportHeight(true);

        JScrollPane jps = new JScrollPane(jt);
        add(jps);
    }

    public static void main(String[] args) {
        JFrame jf = new JFrame();
        packingcompany t = new packingcompany();
        jf.setTitle("The Packing Company");
        jf.setSize(600, 150);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(t);

        String input, sinput, result;
        int option, spec;
        int wsquantity = 0, rtquantity = 0;
        double wstotal = 0, rttotal = 0, special;

        JTextArea display = new JTextArea();       //stating that i want a JText area   
        NumberFormat money = NumberFormat.getCurrencyInstance();
        do {
            ArrayList<String> buttonChoice = new ArrayList<String>();
            buttonChoice.add("Wholesalers");
            buttonChoice.add("Retailers");
            buttonChoice.add("Exit");

            Object[] options = buttonChoice.toArray(); // i am turning the arraylist to objects 

            option = JOptionPane.showOptionDialog(null, "Select Type of Service", "The Packing Company",
                    JOptionPane.PLAIN_MESSAGE, JOptionPane.QUESTION_MESSAGE,
                    null, options, buttonChoice.get(0));

            switch (option) {

                case 0:

                    do {
                        sinput = JOptionPane.showInputDialog("Enter The Quantity You Require:");
                        try {
                            wsquantity = Integer.parseInt(sinput);
                        } catch (Exception nfe) {
                            JOptionPane.showMessageDialog(null, "We are not sure what you entered or did.");
                        }

                        if (wsquantity <= 6) {
                            wstotal = wsquantity * 50;
                        } else if (wsquantity <= 11) {
                            wstotal = wsquantity * 40;
                        } else if (wsquantity <= 21) {
                            wstotal = wsquantity * 30;
                        } else if (wsquantity <= 60) {
                            wstotal = wsquantity * 20;
                        } else if (wsquantity <= 500) {
                            wstotal = wsquantity * 15;
                        }

                        if (wsquantity > 500) {
                            JOptionPane.showMessageDialog(null, "We apologies but you can not order more than 500 units with us at once.");

                        }

                    } while (wsquantity > 500);

                    ArrayList<String> Special = new ArrayList<String>();
                    Special.add("Special Customer");
                    Special.add("Standard Customer");

                    Object[] specials = Special.toArray(); // i am turning the arraylist to objects 

                    spec = JOptionPane.showOptionDialog(null, "Which are you?", "The Packing Company",
                            JOptionPane.PLAIN_MESSAGE, JOptionPane.QUESTION_MESSAGE,
                            null, specials, Special.get(0));

                    if (spec == 0) {
                        special = wstotal - (wstotal / 100 * 10);
                        result = "                      The Packing Company for Wholesalers"
                                + "\n------------------------------------------------------------------------"
                                + "------------------"
                                + "\n\nYou Ordered " + wsquantity
                                + " Units" + "\nOriginal Price: " + money.format(wstotal) + "\n\nTotal for our Special Customers : " + money.format(special);
                        JOptionPane.showMessageDialog(null, result);
                        break;
                    } else if (spec == 1) {

                        result = "                      The Packing Company for Wholesalers"
                                + "\n------------------------------------------------------------------------"
                                + "------------------"
                                + "\n\nYou Ordered " + wsquantity
                                + " Units" + "\nTotal: " + money.format(wstotal)
                                + "\n\n\n\nWant to find out how you can become a Special Customer?" + "\nCall Us Now! 0800 3453456";
                        JOptionPane.showMessageDialog(null, result);
                        break;
                    }
                case 1:
                    do {
                        sinput = JOptionPane.showInputDialog("Enter The Quantity You Require:");
                        try {
                            rtquantity = Integer.parseInt(sinput);
                        } catch (Exception nfe) {
                            JOptionPane.showMessageDialog(null, "We are not sure what you entered.");
                        }
                        if (rtquantity <= 4) {
                            rttotal = rtquantity * 60;
                        } else if (rtquantity <= 9) {
                            rttotal = rtquantity * 50;
                        } else if (rtquantity <= 15) {
                            rttotal = rtquantity * 40;
                        } else if (rtquantity <= 50) {
                            rttotal = rtquantity * 30;
                        } else if (rtquantity <= 500) {
                            rttotal = rtquantity * 25;
                        }

                        if (rtquantity > 500) {
                            JOptionPane.showMessageDialog(null, "We apologies but you can not order more than 500 units with us at once.");

                        }
                        special = rttotal - (rttotal / 100 * 10);

                    } while (rtquantity > 500);

                    ArrayList<String> Special2 = new ArrayList<String>();
                    Special2.add("Special Customer");
                    Special2.add("Standard Customer");

                    Object[] specials2 = Special2.toArray(); // i am turning the arraylist to objects 

                    spec = JOptionPane.showOptionDialog(null, "Which are you?", "The Packing Company",
                            JOptionPane.PLAIN_MESSAGE, JOptionPane.QUESTION_MESSAGE,
                            null, specials2, Special2.get(0));

                    if (spec == 0) {
                        special = rttotal - (rttotal / 100 * 10);
                        result = "                      The Packing Company for Wholesalers"
                                + "\n------------------------------------------------------------------------"
                                + "------------------"
                                + "\n\nYou Ordered " + rtquantity
                                + " Units" + "\nOriginal Price: " + money.format(rttotal) + "\n\nTotal for our Special Customers : " + money.format(special);
                        JOptionPane.showMessageDialog(null, result);
                        break;

                    } else if (spec == 1) {
                        result = "                       The Packing Company for Retailers"
                                + "\n------------------------------------------------------------------------"
                                + "------------------"
                                + "\n\nYou Ordered " + rtquantity
                                + " Units" + "\nTotal: " + money.format(rttotal)
                                + "\n\n\n\nWant to find out how you can become a Special Customer?" + "\nCall Us Now! 0800 3453456";
                        JOptionPane.showMessageDialog(null, result);
                        break;
                    }
                case 2:
                    JOptionPane.showMessageDialog(null, "Thank you for your services." + "\nThe Packing Company");
                    System.exit(0);
            }
        } while (option != 2);
    }
}
EN

回答 1

Code Review用户

发布于 2015-03-15 21:20:42

几点意见:

  • 类名的约定是camel大小写,第一个单词大写;例如PackingCompany而不是packingcompany -有关java命名约定的详细信息,请参阅这里
  • 无论如何,你的打包公司课程对我来说做得太多了;对于面向对象的设计,你应该试着遵循单一责任原则
  • 与其依赖于specoption的神奇数字,不如使用枚举
票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/84152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档