这是我大学学习Java的第一年,所以请原谅我在编程方面的无知。这是我在顺序编程任务中的第五项任务,我们必须为一家包装公司设计一个程序,以便下订单和打印收据。你能给我一些关于我密码的建议吗?
package Lab;
import java.util.ArrayList;
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;
import javax.swing.UIManager.*;
public class Taskk5 extends JFrame {
private static String seller, qStr;
private static int quantity = 0, ppu = 0, bdiscount = 0, discount = 0, cinput, input;
int price = quantity * ppu, TotalCost = price - (price / 100 * discount);
//============================================== instance variables
JTextArea print = new JTextArea(10, 40);
JTextArea nopurchase = new JTextArea(10, 10);
//====================================================== constructor
public Taskk5() {
DecimalFormat d = new DecimalFormat("'£'0.00");
DecimalFormat p = new DecimalFormat("0'%'");
//... Set textarea's initial text, scrolling, and border.
print.setText("Type of Seller\tQuantity \tPrice per Unit\n"
+ "----------------------------------"
+ "--------------------------------\n"
+ seller + "\t " + quantity + " \t " + ppu
+ "\n--------------------------------"
+ "----------------------------------"
+ "\nSpecial Customer Discount: " + p.format(discount)
+ "\n--------------------------------"
+ "----------------------------------\n"
+ "\nTotal Cost: " + d.format(TotalCost));
JScrollPane scrollingArea = new JScrollPane(print);
//... Get the content pane, set layout, add to center
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
//... Set window characteristics.
this.setContentPane(content);
this.setTitle("Print");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rowData[][] = {
{"No. Units", "Price per unit £", "No. Units", "Price per unit £"},
{"1- 6", "50", "1-4", "60"},
{"7-11", "40", "5-9", "50"},
{"12-21", "30", "10-15", "40"},
{"22-60", "20", "16-50", "30"},
{"Over 60", "15", "Over 50", "25"}};
Object columnNames[] = {"Wholesalers", "", "Retailers", ""};
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(370, 160);
frame.setVisible(true);
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
ArrayList<String> MenuList = new ArrayList<String>();
MenuList.add("Wholesaler");
MenuList.add("Retailer");
Object[] options = MenuList.toArray();
ArrayList<String> CustomerList = new ArrayList<String>();
CustomerList.add("Special Customer");
CustomerList.add("Normal Customer");
Object[] customer = CustomerList.toArray();
do {
// Output message for the user to select which unit they wish to convert
// The input variable is the result of the optiondialog, this allows Java to identify which option the user has selected
int input = JOptionPane.showOptionDialog(null,
"Which kind of distributor are you?", "Packing Company Orders",
JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
null, options, MenuList.get(1));
if (input == JOptionPane.CLOSED_OPTION) {
System.exit(0);
}
// This is the if statement for the first option, which is "Fahrenheit to Celsius"
try {
if (input == 0) {
seller = "Wholesaler";
qStr = JOptionPane.showInputDialog(null, "Please enter the number of units you require");
quantity = Integer.parseInt(qStr);
if (quantity >= 1 && quantity <= 6) {
ppu = 50;
} else if (quantity >= 7 && quantity <= 11) {
ppu = 40;
} else if (quantity >= 12 && quantity <= 20) {
ppu = 30;
} else if (quantity >= 21 && quantity <= 60) {
ppu = 20;
} else if (quantity > 60) {
ppu = 15;
}
cinput = JOptionPane.showOptionDialog(null,
"Which kind of Customer are you?", "Packing Company Orders",
JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
null, customer, CustomerList.get(0));
if (cinput == JOptionPane.YES_OPTION) {
discount = 10;
}
JFrame win = new Taskk5();
win.setVisible(true);
} else if (input == 1) {
seller = "Retailer";
qStr = JOptionPane.showInputDialog(null, "Please enter the number of units you require");
if (qStr == null) {
System.exit(0);
}
quantity = Integer.parseInt(qStr);
if (quantity >= 1 && quantity <= 4) {
ppu = 60;
;
} else if (quantity >= 5 && quantity <= 9) {
ppu = 50;
;
} else if (quantity >= 10 && quantity <= 15) {
ppu = 40;
;
} else if (quantity >= 16 && quantity <= 50) {
ppu = 30;
;
} else if (quantity > 50) {
ppu = 25;
;
}
cinput = JOptionPane.showOptionDialog(null,
"Which kind of Customer are you?", "Packing Company Orders",
JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
null, customer, CustomerList.get(0));
if (cinput == JOptionPane.YES_OPTION) {
discount = 10;
}
JFrame win = new Taskk5();
win.setVisible(true);
}
} catch (NumberFormatException e) {
//... Output message for empty input box
JOptionPane.showMessageDialog(null, "Please input a value before pressing enter");
}
} while (input != JOptionPane.CLOSED_OPTION);
}
}发布于 2015-03-24 21:59:53
绝不是详尽无遗的回顾,但以下是一些一般性的建议:
qStr在第12行,d和p在第22和23行。Object作为类型。您知道,这些将是String数组。float或double值,但是由于你在处理金钱,它让我认为它是你可以在你的脑海中为未来保留的东西:https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currencyprivate static...!当你刚开始编程的时候,它确实使事情变得很方便,但是它会给你带来很多麻烦。https://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil;在157,160等.bdiscount我在秋千方面的经验很少,所以我不能对此发表评论。还有,对不起,如果我的线路号码断了,我会很快复制和粘贴.希望这能有所帮助!
发布于 2015-03-24 22:56:57
如果您发现自己使用了大量的库类,或者正在与熟悉它的其他人打交道,您可以选择使用通配符。我个人更喜欢进口个人,无论多少次电话,但无论你选择,不要多余。
也就是说,当您导入javax.swing.*;时,也不必import javax.swing.JFrame;,但是请注意,导入javax.swing.UIManager.*仍然是正确的,因为您需要所有UIManager的子类,这些子类不会包含在初始的通配符导入中。
在很大程度上,你做得不错,但也有一些突出的例子,比如CustomerList (应该是customerList ),cinput (应该是cInput ),应该有一个更好的名字(比如customerInput ),问问自己,用一个更有表现力的名字来称呼像cinput这样的东西(虽然稍微长一些),会与那些读了这些代码的人,即使是你自己,在一段时间后,对此进行权衡,并想知道电影到底是什么?
现在,虽然它能工作,而且很棒,但它并不是直观的,只是在它继续的时候让用户大吃一惊。
为了说明我的观点,请想象一下,如果您以某种方式用三个独立的OptionPanes制作了一个计算器程序,一个请求第一个输入,另一个请求附加输入,第三个询问是否需要额外的输入,另一个最终询问要应用于输入的操作。
想象一下,使用这个程序一次,并发现你无法关闭它,直到你通过所有这些弹出窗口。
但我的计算器例子也是为了指出一个更明显的改进。
当你真的需要抓住用户的注意力时,它们是你应该保存的弹出窗口,比如突然出现的错误信息,或者当它们的特定输入是重要的时候。
更不用说,因为您只提供了一个二进制选择,更好的实现方法是使用JComboBox,让用户选择适合他们需要的选项。
使用您最喜欢的水平布局管理器,将当前“弹出”的两个组合框放置在文本框中,并为输入设置一个可以计算和显示结果的按钮。
失效条件
你为什么要包括外来的分号?把他们移走。
您的if块受此影响很大,除了这个问题之外,它们本身也值得改进,除了检查一个数量是否为<= x之外,您还必须确保这是>= z,这是乏味的。这是最好的做法,首先处理不想要的条件和错误检查,如果你确保输入是积极的。
if (quantity < 1) {
// tell user that quantity need be positive
}然后,如果块只有一个条件检查,则所有这些代码都更具有可读性、可写性,并提高了性能。
如果您添加了我的first检查,您可以继续下面的其他条件块:
else if (quantity <= 6) {
ppu = 50;
} else if (<= 11) {
ppu = 40;
} else if (quantity <= 20) {
ppu = 30;
}https://codereview.stackexchange.com/questions/84151
复制相似问题