Monday, September 30, 2019
Retail Calculator
package retail. calculator; import java. awt. *; import java. awt. event. *; import java. text. DecimalFormat; import javax. swing. *; public class RetailCalculator extends JFrame { //create ojects JLabel department; JLabel number; JLabel name; JLabel price; JLabel discount; JLabel sale; JComboBox dept; JTextField itemNum; JTextField itemName; JTextField itemPrice; JTextField itemDisc; JTextField salePrice; JButton calculate; JButton clear; public RetailCalculator() { //set object variables super(ââ¬Å"Retail Sales Calculatorâ⬠); //window title setSize(250, 250); //set window size etDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); //window close GridLayout grid = new GridLayout(7, 2); setLayout(grid); department = new JLabel(ââ¬Å"Departmentâ⬠); dept = new JComboBox(); dept. addItem(ââ¬Å"Selectâ⬠); dept. addItem(ââ¬Å"Apperalâ⬠); dept. addItem(ââ¬Å"Automotiveâ⬠); dept. addItem(ââ¬Å"Cosmeticsâ⬠); dept. addItem(ââ¬Å"Jewlryâ⬠); dept. addItem(â â¬Å"Petsâ⬠); dept. addItem(ââ¬Å"Pharmacyâ⬠); //add ItemListenerâ⬠¦ combobox and jtextfield dept. addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { String str = (String)dept. getSelectedItem(); itemNum. setText(str); //end public void }); //end item listener number = new JLabel(ââ¬Å"Item Numberâ⬠); itemNum = new JTextField(10); name = new JLabel(ââ¬Å"Item Nameâ⬠); itemName = new JTextField(10); price = new JLabel(ââ¬Å"Original Priceâ⬠); itemPrice = new JTextField(10); discount = new JLabel(ââ¬Å"Discountâ⬠); itemDisc = new JTextField(10); sale = new JLabel(ââ¬Å"Sale Priceâ⬠); salePrice = new JTextField(10); salePrice. setEditable(false); calculate = new JButton(ââ¬Å"Calculateâ⬠); clear = new JButton(ââ¬Å"Clearâ⬠); //add objects to JFrame add(department); add(dept); add(number); add(itemNum); add(name); add(itemName); dd(price); add(itemPrice); add(discount); add(itemDisc); add(sale); add( salePrice); add(calculate); add(clear); //add event listener to calculate sale price calculate. addActionListener(new ActionListener() { public void actionPerformed(ActionEvent retail) { String input1; String input2; double origPrice; double percOff; double clearance; input1 = itemPrice. getText(); input2 = itemDisc. getText(); origPrice = Double. parseDouble(input1); percOff = Double. parseDouble(input2)/100; clearance = origPrice ââ¬â (origPrice * percOff); DecimalFormat df = new DecimalFormat(ââ¬Å"$#,###. #â⬠); salePrice. setText(df. clearance); //output to jtextfield } }); //clear feilds clear. addActionListener (new ActionListener() { public void actionPerformed(ActionEvent event) { //JButton event dept. setSelectedIndex(0); //clear itemNum. setText(null); //clear item number itemName. setText(null); //clear item name itemPrice. setText(null); //clear item name itemDisc. setText(null); //clear item name salePrice. setText(null); //clear item name } }); setVisible(tru e); } public static void main(String[] args) { RetailCalculator rc = new RetailCalculator(); } }
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.