IST Programming Challenge #1 Cost Estimator (75 points) 1. Start a new Java Proj
ID: 3851186 • Letter: I
Question
IST Programming Challenge #1 Cost Estimator (75 points) 1. Start a new Java Project and class file named after the program task with your initials e.- Cost Estimator MH 2. The user will be given a choice of 5 items as shown below (also shown is the price and weight): A) Router $1495.50 3 pounds $695.50 B) Switch 3 pounds $99.99 C) 100 Ft Cat 5 5 pounds $49.95 D) NIC 1 pound $9.95 1 pound E) Patch Cable 3. User will be asked to select A-E for the item, upper or lower case should be allowed. 4. Based on the selection using the switch-case structure, store the name of the item in a variable, the price in another and the weight in yet another. If none of the correct choices are made, exit the program. 5. The user willenter the quantity. 6. Calculate the extended Cost (quantity xcost). Calculate the total weight (quantity xweight). 7. Based on the quantity, calculate a discount amount (discount x extended cost), and sub-total (extended cost discount amount) using the following a. 10 items, no discount b. 10-25 items, 10% discount 25 items, 15% discount 8. Ask if they are in Illinois. If yes, calculate a 6.25% sales tax If no, 0%. The state tax should be in your code as a constant grouped with the variables. install an "Easter Egg" the user enters jAvA exact case) for the answer, make the total cost $1.00 9. lf the total cost of merchandise is over $1000, free shipping. If not over $1000, the shipping cost is $10+$2 per pound, so that ordering 2 switches would cost $10 ($2 x 6 lbs) $22. 10. Calculate the final cost. 11. Display all data for the purchaser that would be of interest to them to verify costs. You may use Scanner or Dialogs for user input/output. Ensure you validate all possible options items, costs, discounts, etc, to ensure they all work correctly withExplanation / Answer
// Product.java
public class Product
{
String name;
double unitPrice;
int quantity;
public Product(String name, double price)
{
this.name = name;
this.unitPrice = price;
}
public void setQuantity(int qty)
{
this.quantity = qty;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public int getQuantity() {
return quantity;
}
}
// ProductCost.java
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ProductCost extends JFrame {
private String[] PRODUCT_NAMES={"Mobile Phone","Shoes","Head phones","Camera","Bag"};
private double[] PRODUCT_COSTS={65.75, 18.56, 6.81, 449.0, 12};
ArrayList<Product> productList;
private JComboBox jcombox;
JTextField txtPrice, txtQty;
JTextArea txtSummary;
JTextArea txtTotal;
JCheckBox chkShipOut;
JButton butAdd;
public ProductCost()
{
super("Calculate product cost");
setSize(500,600);
createUI();
productList = new ArrayList<Product>();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void createUI()
{
JPanel prodPanel;
JPanel summaryPanel = new JPanel();
jcombox = new JComboBox<>(PRODUCT_NAMES);
jcombox.setEditable(false);
prodPanel = new JPanel(new GridLayout(3, 2));
prodPanel.setBorder(BorderFactory.createTitledBorder("Enter product details:"));
prodPanel.add(new JLabel("Choose a product: "));
prodPanel.add(jcombox);
prodPanel.add(new JLabel("Price: "));
prodPanel.add(txtPrice=new JTextField());
prodPanel.add(new JLabel("Quantity: "));
prodPanel.add(txtQty=new JTextField());
chkShipOut = new JCheckBox("Shipping outside Illinois");
butAdd = new JButton("Add");
summaryPanel.add(chkShipOut);
summaryPanel.add(butAdd);
prodPanel.setPreferredSize(new Dimension(400,200));
txtSummary=new JTextArea(50,5);
JScrollPane scrSummary=new JScrollPane(txtSummary);
txtSummary.setText("");
txtSummary.setEditable(false);
Container c=getContentPane();
JPanel tempPanel = new JPanel();
tempPanel.setBorder(BorderFactory.createEmptyBorder(40, 40,40,40));
tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.Y_AXIS));
tempPanel.add(prodPanel);
tempPanel.add(summaryPanel);
tempPanel.add(scrSummary);
//tempPanel.add(txtTotal =new JTextArea("Total Cost"));
c.add(tempPanel);
jcombox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
txtPrice.setText(String.format("%.2f",PRODUCT_COSTS[jcombox.getSelectedIndex()]));
}
});
butAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addProduct();
}
});
jcombox.setSelectedIndex(0);
txtPrice.setText(""+PRODUCT_COSTS[0]);
}
private void addProduct()
{
int idx = jcombox.getSelectedIndex();
if(idx == -1)
{
JOptionPane.showMessageDialog(this, "Please select a product name.");
return;
}
String qty_str = txtQty.getText();
if(qty_str == null || qty_str.trim().equals(""))
{
JOptionPane.showMessageDialog(this, "Please enter product quantity.");
return;
}
try
{
Product p = new Product((String)jcombox.getSelectedItem(),Double.parseDouble(txtPrice.getText()));
p.setQuantity(Integer.parseInt(qty_str));
productList.add(p);
updateTotal();
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(this,"Invalid numeric value!"+e.getMessage());
}
}
private void updateTotal()
{
double totalCost = 0;
Product p;
String summary="Product Name Price Quantity Total ";
summary += "________________________________________________";
double discRate, discAmt;
for(int i = 0; i <productList.size(); i++)
{
p = productList.get(i);
totalCost += p.getUnitPrice() * p.getQuantity();
String prodLine=p.getName()+" "+String.format("%.2f", p.getUnitPrice());
prodLine += " "+p.getQuantity()+" "+String.format("%.2f", p.getQuantity() * p.getUnitPrice());
summary += " "+prodLine;
}
summary += " ________________________________________________";
summary +=" Total cost before discount: "+String.format("%.2f", totalCost);
if(totalCost < 100)
discRate = 0;
else if(totalCost < 500)
discRate = 5;
else if (totalCost < 1000)
discRate = 10;
else if(totalCost < 10000)
discRate = 15;
else
discRate=20;
discAmt = totalCost * discRate / 100;
summary +=" Discount amount: "+String.format("%.2f", discAmt);
totalCost -= discAmt;
summary +=" Total cost after discount: "+String.format("%.2f", totalCost);
if(!chkShipOut.isSelected())
{
double salesTax = totalCost * 6.25 / 100;
summary += " Illinois sales tax amount (6.25%): "+String.format("%.2f", salesTax);
totalCost += salesTax;
}
summary += " Final cost: "+String.format("%.2f", totalCost);
txtSummary.setText(summary);
}
public static void main(String[] args) {
ProductCost pc = new ProductCost();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.