It's my first year in college studying Java so please excuse my ignorance in programming. This is my 5th Task in my Sequential Programming assignment, where we had to design a program for a packing company to place orders and print a receipt. Could you please give me any advice you may have on my code?
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);
}
}