import java.text.NumberFormat; import java.util.Scanner; public class InvoiceApp
ID: 3552376 • Letter: I
Question
import java.text.NumberFormat;
import java.util.Scanner;
public class InvoiceApp {
public static void main(String[] args)
{
// display a welcome message
System.out.println("Welcome to the Invoice Total Calculator");
System.out.println(); // print a blank line
Scanner sc = new Scanner(System.in);
String choice = "y";
while(choice.equalsIgnoreCase("y"))
{
// get user entries
String customerType = Validator.getString(sc,
"Enter customer type (r/c): ");
double subtotal = Validator.getDouble(sc,
"Enter subtotal: ", 0, 10000);
// calculate the discount amount and total
double discountPercent = getDiscountPercent(subtotal, customerType);
double discountAmount = subtotal * discountPercent;
double total = subtotal - discountAmount;
// format the values
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
String sSubtotal = currency.format(subtotal);
String sCustomerType = "";
if (customerType.equalsIgnoreCase("r"))
sCustomerType = "Retail";
else if (customerType.equalsIgnoreCase("c"))
sCustomerType = "College";
String sDiscountPercent = percent.format(discountPercent);
String sDiscountAmount = currency.format(discountAmount);
String sTotal = currency.format(total);
// format and display the results
String message = "Subtotal: " + sSubtotal + " "
+ "Customer type: " + sCustomerType + " "
+ "Discount percent: " + sDiscountPercent + " "
+ "Discount amount: " + sDiscountAmount + " "
+ "Total: " + sTotal + " ";
System.out.println();
System.out.println(message);
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
static double getDiscountPercent(double subtotal, String type)
{
double discountPercent = 0.0;
if (type.equalsIgnoreCase("r"))
{
if (subtotal >= 500)
discountPercent = .2;
else if (subtotal >= 250 && subtotal < 500)
discountPercent =.15;
else if (subtotal >= 100 && subtotal < 500)
discountPercent =.1;
else if (subtotal < 100)
discountPercent =.0;
}
else if (type.equalsIgnoreCase("c"))
{
discountPercent = .2;
}
else
discountPercent = .05;
return discountPercent;
}
}
import java.text.NumberFormat;
import java.util.Scanner;
package murach.forms;
import murach.business.InvoiceCalculations;
import java.text.NumberFormat;
/**
*
* @author Tanya Rhodes
*/
public class InvoiceForm extends javax.swing.JFrame {
/**
* Creates new form InvoiceForm
*/
public InvoiceForm() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
calculateButton = new javax.swing.JButton();
exitButton = new javax.swing.JButton();
customerTypeTextField = new javax.swing.JTextField();
subtotalTextField = new javax.swing.JTextField();
discountPercentTextField = new javax.swing.JTextField();
discountAmountTextField = new javax.swing.JTextField();
totalTextField = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Customer Type:");
jLabel2.setText("Subtotal:");
jLabel3.setText("Discount Percent:");
jLabel4.setText("Discount Amount:");
jLabel5.setText("Total:");
calculateButton.setText("Calculate");
calculateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calculateButtonActionPerformed(evt);
}
});
exitButton.setText("Exit");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
customerTypeTextField.setText(" ");
customerTypeTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
customerTypeTextFieldActionPerformed(evt);
}
});
subtotalTextField.setText(" ");
subtotalTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
subtotalTextFieldActionPerformed(evt);
}
});
discountPercentTextField.setText(" ");
discountPercentTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
discountPercentTextFieldActionPerformed(evt);
}
});
discountAmountTextField.setText(" ");
totalTextField.setText(" ");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(subtotalTextField)
.addComponent(discountPercentTextField)
.addComponent(discountAmountTextField)
.addComponent(totalTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
.addComponent(customerTypeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(calculateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(37, 37, 37)
.addComponent(exitButton)))
.addContainerGap(54, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(customerTypeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jLabel2))
.addComponent(subtotalTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(discountPercentTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(discountAmountTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(totalTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(calculateButton)
.addComponent(exitButton))
.addGap(63, 63, 63))
);
pack();
}// </editor-fold>
private void customerTypeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void subtotalTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void discountPercentTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
double c = Double.parseDouble(customerTypeTextfield.getText());
double s = Double.parseDouble(subtotalTextField.getText());
double d= Double.parseDouble(discountPercentTextField());
NEED HELP ERRORS
}
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InvoiceForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton calculateButton;
private javax.swing.JTextField customerTypeTextField;
private javax.swing.JTextField discountAmountTextField;
private javax.swing.JTextField discountPercentTextField;
private javax.swing.JButton exitButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField subtotalTextField;
private javax.swing.JTextField totalTextField;
// End of variables declaration
}
package murach.business;
public class InvoiceCalculations
{
public static double calculateDiscountPct(String customerType, double subtotal)
{
double discountPercent = 0;
if (customerType.equalsIgnoreCase("R"))
{
if (subtotal < 100)
discountPercent = 0;
else if (subtotal >= 100 && subtotal < 250)
discountPercent = .1;
else if (subtotal >= 250)
discountPercent = .2;
}
else if (customerType.equalsIgnoreCase("C"))
{
if (subtotal < 250)
discountPercent = .2;
else
discountPercent = .3;
}
else
discountPercent = .1;
return discountPercent;
}
}
package murach.forms;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class SwingValidator
{
public boolean isPresent(JTextComponent c, String title)
{
if (c.getText().length() == 0)
{
showMessage(c, title + " is a required field.");
c.requestFocusInWindow();
return false;
}
return true;
}
public boolean isInteger(JTextComponent c, String title)
{
try
{
int i = Integer.parseInt(c.getText());
return true;
}
catch (NumberFormatException e)
{
showMessage(c, title + " must be an integer.");
c.requestFocusInWindow();
return false;
}
}
public boolean isDouble(JTextComponent c, String title)
{
try
{
double d = Double.parseDouble(c.getText());
return true;
}
catch (NumberFormatException e)
{
showMessage(c, title + " must be a valid number.");
c.requestFocusInWindow();
return false;
}
}
private void showMessage(JTextComponent c, String message)
{
JOptionPane.showMessageDialog(c, message, "Invalid Entry",
JOptionPane.ERROR_MESSAGE);
}
}
Explanation / Answer
import java.util.Scanner; class Invoice{ String partNumber; String partDescription; int itemPurchased; double pricePerItem; Invoice(){ partNumber = ""; partDescription = ""; itemPurchased = 0; pricePerItem = 0.0; } String getPartNumber(){ return partNumber; } String getPartDescription(){ return partDescription; } int getItemPurchased(){ return itemPurchased; } double getPricePerItem(){ return pricePerItem; } double getInvoiceAmount(){ return (itemPurchased * pricePerItem); } void setPartNumber(String pn){ partNumber = pn; } void setPartDescription(String pd){ partDescription = pd; } void setItemPurchased(int ip){ itemPurchased = ip; } void setPricePerItem(double ppi){ pricePerItem = ppi; } } class InvoiceDemo { public static void main(String args[]) { Scanner sc = new Scanner(System.in); Invoice invoice = new Invoice(); System.out.print(" Enter part number :"); invoice.setPartNumber(sc.nextLine()); System.out.print("Enter part description :"); invoice.setPartDescription(sc.nextLine()); System.out.print("Enter item purchased :"); invoice.setItemPurchased(sc.nextInt()); System.out.print("Enter price per item :"); invoice.setPricePerItem(sc.nextDouble()); System.out.print(" Detail of items purchasing-->"); System.out.print(" Part number :" + invoice.getPartNumber()); System.out.print(" Part description :" + invoice.getPartDescription()); System.out.print(" Total Billing Amount :" + invoice.getInvoiceAmount()); }}Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.