📄 taxcalculator.java
字号:
import src.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
import javax.swing.border.*;
/**The TaxCalculator for computing Tax Payment in java
* @author ShanNa CSA_05
* @version 2.0,2008-3-21
* @since JDK1.5
* @see Console
* @see TaxLevelTable
* @see SetBaseDlg
* @see TaxValue
*/
public class TaxCalculator extends JApplet{
/************************************
*Declare some basic type attributes*
************************************/
private double baseIncome;
private double income;
private double taxPayable;
private double payment;
private double amount,rate;
private double highAmount,lowAmount,highRate,lowRate;
private String amountStr,rateStr;
private String[] listString;
private int listSelectIndex;
private int itemNum;
private TaxLevelTable tlt=new TaxLevelTable();
/***********************************
*Declare the components attributes*
***********************************/
private DefaultListModel lItems=new DefaultListModel();
private JList taxTableList=new JList(lItems);
private JScrollPane jsp = new JScrollPane(taxTableList,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
private JButton
modifyBase=new JButton("Modify"),
updateItem=new JButton("Update"),
deleteItem=new JButton("Delete"),
insertItem=new JButton("Insert"),
result=new JButton("Result");
private JPanel jp1=new JPanel(),jp2=new JPanel(),jp3=new JPanel(),
jp4=new JPanel(),jp5=new JPanel(),jp6=new JPanel();
private JLabel
baseLb=new JLabel("RevenueBase:"),
inputLb= new JLabel("Income: "),
taxAmountLb=new JLabel("Tax Amount:"),
taxTableLb=new JLabel("--------------------------Tax payable rules--------------------------\n");
private JTextField
baseTF=new JTextField(10),
taxAmountTF=new JTextField(10),
incomeTF=new JTextField(10);
/**************************************************************
* Class implement ActionListener to respond to JButton action*
**************************************************************/
class ButtonListener implements ActionListener{
/**
* Respond to JButton action
* @throws ArrayOutOfBoundsException
* @return No return value
* @param e ActionEvent
*/
public void actionPerformed(ActionEvent e){
try{
/**********************************************
* update a item in the @see TaxLevelTable tlt*
* Constraints:the new values must between its*
* two neighbors. *
* ********************************************/
if(e.getSource()==updateItem){
TaxLevel tl=tlt.getTaxLevel(listSelectIndex-1);
if(listSelectIndex>1){
highAmount=tlt.getTaxLevel(listSelectIndex-2).getAmount();
highRate=tlt.getTaxLevel(listSelectIndex-2).getRate();
}
else if(listSelectIndex==1){
highAmount=100000000;
highRate=1.0;
}
if(listSelectIndex<itemNum+1){
lowAmount=tlt.getTaxLevel(listSelectIndex).getAmount();
lowRate=tlt.getTaxLevel(listSelectIndex).getRate();
}
else{
lowAmount=0.0;
lowRate=0.0;
}
TaxValue dlg=new TaxValue(String.valueOf(tl.getAmount()),String.valueOf(tl.getRate()),lowAmount,highAmount,lowRate,highRate);
Console.run(dlg, 400,125);
amountStr=dlg.GetTaxValue();
rateStr=dlg. GetRateValue();
amount=Double.parseDouble(amountStr);
rate=Double.parseDouble(rateStr);
tlt.update(listSelectIndex-1, amount, rate);
lItems.clear();
listDisplay();
}
/*************************************************
* Insert a new item into @see TaxLevelTable tlt*
* Constraints:the new values must between its *
* two neighbors. *
* ********************************************* */
if(e.getSource()==insertItem){
if(listSelectIndex>1){
highAmount=tlt.getTaxLevel(listSelectIndex-2).getAmount();
highRate=tlt.getTaxLevel(listSelectIndex-2).getRate();
}
else{
highAmount=100000000;
highRate=1.0;
}
if(listSelectIndex<itemNum+1){
lowAmount=tlt.getTaxLevel(listSelectIndex-1).getAmount();
lowRate=tlt.getTaxLevel(listSelectIndex-1).getRate();
}
else{
lowAmount=0.0;
lowRate=0.0;
}
TaxValue dlg=new TaxValue("0.0","0.0",lowAmount,highAmount,lowRate,highRate);
Console.run(dlg, 400,125);
amountStr=dlg.GetTaxValue();
rateStr=dlg. GetRateValue();
amount=Double.parseDouble(amountStr);
rate=Double.parseDouble(rateStr);
tlt.insert(new TaxLevel(amount,rate),listSelectIndex-1);
lItems.clear();
listDisplay();
}
/***********************************
*Delete a item from @TaxLevelTable*
***********************************/
if(e.getSource()==deleteItem){
tlt.delete(listSelectIndex-1);
lItems.clear();
listDisplay();
}
} catch(ArrayIndexOutOfBoundsException er){
JOptionPane.showMessageDialog(null,"INDEX OUT OF BOUNDS EXCEPTION!","ERRORS!",JOptionPane.OK_OPTION);
}
/*************************
*Modify the basic income*
*************************/
if(e.getSource()==modifyBase){
SetBaseDlg dlg=new SetBaseDlg();
String value;
Console.run(dlg, 300,100);
value=dlg.getBaseValue();
baseTF.setText(value);
baseIncome=Double.parseDouble(value);
}
/****************************************
*Show the tax payment of certain income*
****************************************/
if(e.getSource()==result){
payment=0;
income=Double.parseDouble(incomeTF.getText());
taxPayable=income-baseIncome;
int i=0;
while(i<itemNum){
if(taxPayable>tlt.getTaxLevel(i).getAmount()){
payment+=(taxPayable-tlt.getTaxLevel(i).getAmount())*tlt.getTaxLevel(i).getRate();
taxPayable=tlt.getTaxLevel(i).getAmount();
}
i++;
}
taxAmountTF.setText(String.valueOf(payment));
}
}
}
/*************************************************************************
* Class implement SelectionListener to respond to JList Selection Action*
*************************************************************************/
class listLisener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e){
listSelectIndex=taxTableList.getSelectedIndex();
}
}
/** Modify DefaultListModel items to change the Strings in JList
* @return No return value
* @exception exceptions No exceptions thrown
*/
public void listDisplay(){
itemNum=tlt.getCount();
listString=new String[itemNum];
listString=tlt.toStrings();
lItems.add(0," Level Amount TaxRate ");
for(int i=0;i<itemNum;i++)
lItems.add(i+1,listString[i]);
}
private ButtonListener b1=new ButtonListener();
public void init(){
//Give baseIncome a default value
baseIncome=1600.0;
/*************************************************
*Add the first five items to @see TaxLevelTable*
*************************************************/
tlt.insert(new TaxLevel(20000,0.25),0);
tlt.insert(new TaxLevel(5000,0.2),1);
tlt.insert(new TaxLevel(2000,0.15),2);
tlt.insert(new TaxLevel(500,0.1),3);
tlt.insert(new TaxLevel(0,0.05),4);
/*******************************************************
*Add the strings from @see TaxLevelTable to the List *
*******************************************************/
listDisplay();
Container cp=getContentPane();
cp.setLayout(new FlowLayout(FlowLayout.LEFT,10,15));
// TextFields only used to display values
taxAmountTF.setEditable(false);
baseTF.setEditable(false);
/*********************************
* Give TextFields default values*
*********************************/
baseTF.setText("1600.0");
taxAmountTF.setText("0.0");
/*******************************
*Create borders for components*
*******************************/
Border brd=BorderFactory.createMatteBorder(1, 1, 2,2,Color.BLACK);
taxTableList.setBorder(brd);
taxAmountTF.setBorder(brd);
baseTF.setBorder(brd);
/****************************************
*Set Layout Style for the Content Panes*
****************************************/
jp1.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
jp2.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
jp3.setLayout(new FlowLayout(FlowLayout.LEFT,60,5));
jp4.setLayout(new FlowLayout(FlowLayout.CENTER,35,5));
jp5.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
jp6.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
/********************************************
*Add items to the Content Panes for display*
********************************************/
jp1.add(baseLb);
jp1.add(baseTF);
jp1.add(modifyBase);
jp2.add(taxTableLb);
jp3.add(jsp);
jp4.add(updateItem);
jp4.add(deleteItem);
jp4.add(insertItem);
jp5.add(inputLb);
jp5.add(incomeTF);
jp6.add(taxAmountLb);
jp6.add(taxAmountTF);
jp6.add(result);
/*****************************************************************
*Add the Content Panes to the top-level Content Pane for Display*
*****************************************************************/
cp.add(jp1);
cp.add(jp2);
cp.add(jp3);
cp.add(jp4);
cp.add(jp5);
cp.add(jp6);
/**************************
*Register event listeners*
**************************/
taxTableList.addListSelectionListener(new listLisener());
updateItem.addActionListener(b1);
insertItem.addActionListener(b1);
deleteItem.addActionListener(b1);
modifyBase.addActionListener(b1);
result.addActionListener(b1);
}
/** Sole entry point to class & application
* @param args array of string arguments
* @return No return value
* @throws NullPointerException
*/
public static void main(String[] args){
/********************************************
*Set JApplet Looking according to user's OS*
********************************************/
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e){
e.printStackTrace();
}
/*****************
*Run the JApplet*
*****************/
Console.run(new TaxCalculator(), 400, 500);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -