Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Start a new Java Project and class file named after the program task with your i

ID: 3851180 • Letter: S

Question

Start a new Java Project and class file named after the program task with your initials (i.e.- CostEstimator_MH) The user will be given a choice of 5 items as shown below (also shown is the price and weight): User will be asked to select A-E for the item, upper or lower case should be allowed. 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. The user will enter the quantity. Calculate the extended Cost (quantity x cost). Calculate the total weight (quantity x weight). Based on the quantity, calculate a discount amount (discount x extended cost), and sub-total (extended cost - discount amount) using the following: a. 25 items, 15% discount 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" - if the user enters jAvA (exact case) for the answer, make the total cost exist1.00.0 If the total cost of merchandise is over exist1000, free shipping. If not over exist1000, the shipping cost is exist10 + exist2 per pound, so that ordering 2 switches would cost exist10 + (exist2 times 6 lbs) = exist22. Calculate the final cost. Display all data for the purchaser that would be of interest to them to verify costs.

Explanation / 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();
      
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote