Recently, I will write an experiment on java. The experimental contents are as follows:
1. Define an abstract bank card class, including basic attributes, relevant processing methods, and methods including debit card and credit card. And define an overdraft interface, which allows credit cards to overdraw.
2. Design withdrawal and overdraft are abnormal.
3. design an operation interface to handle this.
Generally speaking, the topic is not difficult. You can use the command line or graphical interface for the operation page. Here, in order to learn in advance, you can enter the learning of graphical interface. The graphic design here takes a certain time. Of course, the biggest obstacle in graphic design is how to operate. Here is the event response related to graphics. Generally speaking, you can use listeners to operate
Of course, the UI interface design this time is relatively general, and there is no in-depth research on many aspects. In short, it is just an introduction to desktop development! However, there is still a connection database here, and there is no time to design, so there is no good login operation, which is easy to do.
After all, getting started is getting started, and there is no need to pay too much attention. The key is to realize the response and the operation of the listener
Layout first:
Login interface:
All the following java files can be written out separately, instead of all in Main.
Main function of this time
Main.java
import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main{ public static void main(String[] args){ //loginJFrame f=new loginJFrame(); try{ new MainUI(); }catch(Exception ex){ ex.printStackTrace(); } } }
Introduce three classes
BankCard.java
abstract class BankCard { /* Basic properties */ String cardNumber; String userName; String idNumber; String password; double balance; /** * Construction method * Card number construction and ID number construction methods are provided here. Note that the construction is only the generation of bank card rather than business content. */ public BankCard(){} BankCard(String cardNumber){ this.cardNumber=cardNumber; } BankCard(String idNumber,String uerName){ this.idNumber=idNumber; this.userName=userName; } /** * General business processing method * Setting method * 1.Deposit 2 Withdrawal 3 Change Password */ void deposit(double balance){ this.balance+=balance; } void withdraw(double balance){ this.balance-=balance; } void revisePassword(String password){ this.password=password; } /** * Query method * 1.Query balance 2 Query password */ double checkBalance(){ return balance; } String checkPassword(){ return password; } public String toString(){ return cardNumber; } }
DebitCard.java
import javax.swing.*; class getMoneyException extends Exception{ String s; public getMoneyException(){ s="Insufficient balance on card"; } public String toString(){ return s; } } public class DebitCard extends BankCard{ /* Basic properties */ String cardNumber; String userName; String idNumber; String password; double balance; /** * Settings and query methods are the same as abstract classes, so they are no longer written and directly inherited, * However, note that initialization must be completed, otherwise an error will occur. */ public DebitCard(){} DebitCard(String cardNumber){ this.cardNumber=cardNumber; } DebitCard(String idNumber,String uerName){ this.idNumber=idNumber; this.userName=userName; } void deposit(double balance){ this.balance+=balance; } /** * Judge the withdrawal information and rewrite the withdrawal method in BankCard * @throws getMoneyException */ void judgeBalance() throws getMoneyException{ if(this.balance<0) throw new getMoneyException(); } void withdraw(double balance){ this.balance-=balance; try{ judgeBalance(); }catch(getMoneyException ex){ JOptionPane.showMessageDialog(null, "The balance is insufficient. Please re-enter and the withdrawal is invalid"); this.balance+=balance; } } }
CreditCard.java
import javax.swing.*; interface Overdraft { /** * Write the overdraft method. Note that the interface is generally an abstract class by default * Overdraft here returns a bool value. Overdraft is not allowed by default */ default boolean isOverdraft(){ return false; } } class OverdraftException extends Exception{ String s; public OverdraftException(){ s="Exceeding the maximum credit limit(20000 Ten thousand yuan)"; } public String toString(){ return s; } } public class CreditCard extends BankCard implements Overdraft{ /* Basic properties */ String cardNumber; String userName; String idNumber; String password; double balance; /** * Settings and query methods are the same as abstract classes, so they are no longer written and directly inherited, * However, note that initialization must be completed, otherwise an error will occur. */ public CreditCard(){} CreditCard(String cardNumber){ this.cardNumber=cardNumber; } CreditCard(String idNumber,String uerName){ this.idNumber=idNumber; this.userName=userName; } public boolean isOverdraft(){ return true; } /** * Set overdraft method */ void judgeOverdraft() throws OverdraftException{ if(this.balance>20000) throw new OverdraftException(); } void setOverdraft(double balance) { this.balance+=balance; try{ judgeOverdraft(); }catch(OverdraftException ex){ JOptionPane.showMessageDialog(null, "Overdraft of more than 20000 yuan,Invalid overdraft"); this.balance+=balance; } } }
Next, we design the ui interface, which is divided into the following steps. First, we define the total ui diagram, that is, the login and registration interface, then call the button method and rewrite an interface, then design the ui interface of the base note and the credit card.
Here are the ui files:
MainUI.java
import javax.swing.*; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class MainUI extends JFrame { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); final int width = 500; final int height = 300; private static JTextField utext; private static JTextField ptext; private static JFrame jf; public MainUI(){ int i=0,j=0; JPanel jPanel = new JPanel(); JPanel jPanelt = new JPanel(); JButton register = new JButton("Sign in"); JButton logon = new JButton("register"); JLabel text1 = new JLabel("=Welcome to the bank card system="); JLabel username = new JLabel("user name"); JLabel password = new JLabel("dense code"); JTextField nametext = new JTextField(15); JTextField passtext = new JPasswordField(15); JComboBox sel = new JComboBox(); Box hbox = Box.createHorizontalBox(); sel.addItem("Debit Card"); sel.addItem("Credit Card"); sel.setSelectedIndex(-1); hbox.add(register); hbox.add(Box.createHorizontalStrut(50)); hbox.add(sel); hbox.add(Box.createHorizontalStrut(50)); hbox.add(logon); DebitAction user1=new DebitAction(); CreditAction user2=new CreditAction(); sel.addItemListener(new ItemListener() { @Override //Add listening to the drop-down box public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED) { if(sel.getSelectedItem()=="Debit Card") { register.removeActionListener(user2); logon.removeActionListener(user2); register.addActionListener(user1); //logon.addActionListener(user1); } if(sel.getSelectedItem()=="Credit Card") { register.removeActionListener(user1); logon.removeActionListener(user1); register.addActionListener(user2); logon.addActionListener(user2); } } } }); Box ubox = Box.createHorizontalBox(); ubox.add(username); ubox.add(Box.createHorizontalStrut(10)); ubox.add(nametext); Box pbox = Box.createHorizontalBox(); pbox.add(password); pbox.add(Box.createHorizontalStrut(10)); pbox.add(passtext); text1.setFont(new Font("Song style",Font.BOLD,15)); jPanelt.add(text1,BorderLayout.CENTER); Box box = Box.createVerticalBox(); box.add(Box.createVerticalStrut(30)); box.add(jPanelt); box.add(Box.createVerticalStrut(30)); box.add(ubox); box.add(Box.createVerticalStrut(20)); box.add(pbox); box.add(Box.createVerticalStrut(35)); box.add(hbox); jPanel.setBackground(Color.pink); jPanel.add(box); add(jPanel); setTitle("Bank card usage"); setBounds((screenSize.width-width)/2,(screenSize.height-height)/2,width,height); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true); utext=nametext; ptext=passtext; jf=this; } public static JFrame GetMainUI() { return jf; } public static JTextField getUtext() { return utext; } public static JTextField getPtext() { return ptext; } }
DebitCardUI.java
import javax.swing.*; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; import javax.swing.table.TableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * This is for debit cards */ public class DebitCardUI extends JFrame{ public DebitCardUI(){ JLabel a=new JLabel("deposit"); JLabel b=new JLabel("withdraw money"); JTextField ta=new JTextField(10); JTextField tb=new JTextField(10); JButton c=new JButton("Check the balance"); JButton d=new JButton("Submit"); DebitCard ac=new DebitCard(); /** * Set button response, */ d.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub double m=Double.parseDouble(ta.getText());double ct=Double.parseDouble(tb.getText()); ac.deposit(m); ac.withdraw(ct); } }); /** * Query button and prompt box */ c.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String s=Double.toString(ac.balance); JOptionPane.showMessageDialog(null, s); } }); JPanel ja=new JPanel(); ja.add(a);ja.add(ta); ja.add(b);ja.add(tb); ja.add(c); ja.add(d); //ja.add(c);ja.add(tc); add(ja); setSize(500,300); setVisible(true); } }
CreditCardUI.java
import javax.swing.*; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; import javax.swing.table.TableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * This is for credit cards */ public class CreditCardUI extends JFrame{ public CreditCardUI(){ JLabel a=new JLabel("deposit"); JLabel b=new JLabel("make an overdraft"); JTextField ta=new JTextField(10); JTextField tb=new JTextField(10); JButton c=new JButton("Check the balance"); JButton d=new JButton("Submit"); CreditCard ac=new CreditCard(); /** * Set button response, */ d.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub double m=Double.parseDouble(ta.getText());double ct=Double.parseDouble(tb.getText()); ac.deposit(m); ac.setOverdraft(ct); } }); /** * Query button and prompt box */ c.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String s=Double.toString(ac.balance); JOptionPane.showMessageDialog(null, s); } }); JPanel ja=new JPanel(); ja.add(a);ja.add(ta); ja.add(b);ja.add(tb); ja.add(c); ja.add(d); //ja.add(c);ja.add(tc); add(ja); setSize(500,300); setVisible(true); } }
Call ActionListener method
CreditCardAction.java
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; public class CreditAction implements ActionListener { @Override public void actionPerformed(ActionEvent actionEvent) { // TODO Auto-generated method stub if(actionEvent.getActionCommand()=="Sign in"){ new CreditCardUI(); //MainUI.GetMainUI().dispose(); //return; } } }
DebitAction.java
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class DebitAction implements ActionListener { @Override public void actionPerformed(ActionEvent actionEvent) { // TODO Auto-generated method if(actionEvent.getActionCommand()=="Sign in"){ new DebitCardUI(); //MainUI.GetMainUI().dispose(); //return; } } }