最近要写一个关于java的实验,实验内容如下: 1.定义一个抽象的银行卡类,含有基本属性,并定义有关的处理方法,且分别定义含有借记卡、信用卡类的方法。并定义一个透支接口,信用卡可以透支。 2设计取款、透支异常。 3.设计一个操作界面,对此进行处理。
总的来说,题目不难,关于操作页面可以使用命令行或者图形界面,这里为了提前学习便进入到图形界面的学习,这里的图形设计起来需要一定的时间,当然在图形设计上面最大的障碍就是如何进行操作,这里就是涉及到了图形的事件响应了,一般来说可以使用监听器来操作 当然本次的UI界面设计得比较一般,对于很多方面没有进行深入的研究,总之,只是桌面开发的入门菜啦!不过在这里还有连接数据库也没有时间设计了,故没有很好的登陆操作,这些也都比较好弄。 入门毕竟是入门了,也不需要讲究太多了,关键去实现响应和监听器的操作 先放图: 登录界面:
下面所有java文件可以单独写出,不用全部写在Main中。 本次的主函数 Main.java
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main{
public static void main(String[] args){
try{
new MainUI();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
介绍三个类 BankCard.java
abstract class BankCard {
String cardNumber;
String userName;
String idNumber;
String password;
double balance;
public BankCard(){}
BankCard(String cardNumber){
this.cardNumber=cardNumber;
}
BankCard(String idNumber,String uerName){
this.idNumber=idNumber;
this.userName=userName;
}
void deposit(double balance){
this.balance+=balance;
}
void withdraw(double balance){
this.balance-=balance;
}
void revisePassword(String password){
this.password=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="卡上余额不足";
}
public String toString(){
return s;
}
}
public class DebitCard extends BankCard{
String cardNumber;
String userName;
String idNumber;
String password;
double balance;
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;
}
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,
"余额不足请重新输入,并且取出无效");
this.balance+=balance;
}
}
}
CreditCard.java
import javax.swing.*;
interface Overdraft {
default boolean isOverdraft(){
return false;
}
}
class OverdraftException extends Exception{
String s;
public OverdraftException(){
s="超过最高信用限额(20000万元)";
}
public String toString(){
return s;
}
}
public class CreditCard extends BankCard implements Overdraft{
String cardNumber;
String userName;
String idNumber;
String password;
double balance;
public CreditCard(){}
CreditCard(String cardNumber){
this.cardNumber=cardNumber;
}
CreditCard(String idNumber,String uerName){
this.idNumber=idNumber;
this.userName=userName;
}
public boolean isOverdraft(){
return true;
}
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,
"透支超过2万元,透支无效");
this.balance+=balance;
}
}
}
接着设计UI 界面,这里分为如下的步骤,先是定义总的ui图,就是登陆与注册界面,然后调用按钮的方法并自己重写一个接口,再设计借记卡、信用卡的ui界面。 下面是ui文件: 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("登录");
JButton logon = new JButton("注册");
JLabel text1 = new JLabel("=欢迎使用银行卡系统=");
JLabel username = new JLabel("用户名");
JLabel password = new JLabel("密 码");
JTextField nametext = new JTextField(15);
JTextField passtext = new JPasswordField(15);
JComboBox sel = new JComboBox();
Box hbox = Box.createHorizontalBox();
sel.addItem("借记卡");
sel.addItem("信用卡");
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
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED)
{
if(sel.getSelectedItem()=="借记卡")
{
register.removeActionListener(user2);
logon.removeActionListener(user2);
register.addActionListener(user1);
}
if(sel.getSelectedItem()=="信用卡")
{
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("宋体",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("银行卡使用");
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;
public class DebitCardUI extends JFrame{
public DebitCardUI(){
JLabel a=new JLabel("存款");
JLabel b=new JLabel("取款");
JTextField ta=new JTextField(10);
JTextField tb=new JTextField(10);
JButton c=new JButton("查询余额");
JButton d=new JButton("提交");
DebitCard ac=new DebitCard();
d.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
double m=Double.parseDouble(ta.getText());double ct=Double.parseDouble(tb.getText());
ac.deposit(m);
ac.withdraw(ct);
}
});
c.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
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);
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;
public class CreditCardUI extends JFrame{
public CreditCardUI(){
JLabel a=new JLabel("存款");
JLabel b=new JLabel("透支");
JTextField ta=new JTextField(10);
JTextField tb=new JTextField(10);
JButton c=new JButton("查询余额");
JButton d=new JButton("提交");
CreditCard ac=new CreditCard();
d.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
double m=Double.parseDouble(ta.getText());double ct=Double.parseDouble(tb.getText());
ac.deposit(m);
ac.setOverdraft(ct);
}
});
c.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
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);
add(ja);
setSize(500,300);
setVisible(true);
}
}
调用ActionListener方法 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) {
if(actionEvent.getActionCommand()=="登录"){
new CreditCardUI();
}
}
}
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) {
if(actionEvent.getActionCommand()=="登录"){
new DebitCardUI();
}
}
}
|