这是我上大学的第一年,我用我自己的基本知识设计了一个代码。我设计了一个程序来计算批发商或零售商的商品价格。特殊客户如果点击按钮,也会得到特别的折扣。请看一看我的代码,并给我任何反馈,我如何实现它。如果你认为我做得很好,请告诉我。
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);
}
}https://codereview.stackexchange.com/questions/84152
复制相似问题