模擬銀行存取款業務 java

實訓題5:模擬銀行存取款業務

實訓目的:本項目的主要功能是模擬銀行的存取款業務,當用戶登錄時需判斷銀行卡號和銀行卡密碼,當輸入的卡號和密碼都正確時,登錄成功,提示當前登錄的賬戶名,並進入下一步選擇操作類型。操作類型包括四種(存款、取款、餘額、退出),選擇存款或取款時,將進行存取款操作,此時需要輸入存取的金額,並進行正確的金額加減計算;選擇餘額時,顯示當前賬戶的餘額;輸退出時將退出整個系統。項目流程圖如下:

餘額忘記用文件保存了,先把代碼貼上來吧

package exam5;

import java.awt.*;
import java.io.*;

import javax.swing.*;

public class Bank 
{
	 static int temp=10000;
	static int tm=0;/*filename.createNewFile()
	File filename=new File("D:\\Eclipse Workspace\\Training\\user\\"+shuru1.getText()+"\\name.txt");fileID.mkdirs();
	File fileID=new File("D:\\Eclipse Workspace\\Training\\user\\"+shuru1.getText());if(!fileID.exists())*/
	private static void createAndShowGUI()
	{
		JFrame f =new JFrame("中國銀行");
		f.setSize(730, 500);
		f.setLocation(400,300);
		JTextArea showArea = new JTextArea(1,35);
		f.setResizable(true);//窗口是否可以尺寸大小
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JLabel l1 = new JLabel();//l1標籤
		ImageIcon icon = new ImageIcon("D:\\Eclipse Workspace\\Training\\img\\1.jpg");		 
		l1.setIcon(icon);//l1爲圖片標籤
		JButton b1 = new JButton("登 錄");
		JButton b2 = new JButton("退 出");
		JButton b3 = new JButton("注 冊");
		JPanel panel = new JPanel();		
		JLabel zhanghao = new JLabel("賬號:");
		JLabel mima = new JLabel("密碼:");
		JTextArea ja=new JTextArea(2,10);
		JTextField shuru1= new JTextField (20);
		JTextField shuru2= new JTextField (20);	
		panel.add(zhanghao);
		panel.add(shuru1);
		panel.add(mima);
		panel.add(shuru2);
		panel.add(b1);
		panel.add(b3);
		panel.add(b2);
		panel.add(showArea);
		panel.add(ja);		
		f.add(l1,BorderLayout.PAGE_START);		
		f.add(panel,BorderLayout.CENTER);
		f.setVisible(true);		
		
		b1.addActionListener(e ->
		{	String yonghu = "D:\\Eclipse Workspace\\Training\\user\\user.txt";	
			String key = "D:\\Eclipse Workspace\\Training\\user\\key.txt";
			//@SuppressWarnings("unused")
			String content1 = shuru1.getText();
			//@SuppressWarnings("unused")
			String content2 = shuru2.getText();
			try(FileReader reader1 = new FileReader(yonghu);
					FileReader reader2 = new FileReader(key);	
					BufferedReader br1 = new BufferedReader(reader2);	
			BufferedReader br = new BufferedReader(reader1)
					)
			{
				 String line;
				 String line1;
				 line1=br1.readLine();
				while ((line=br.readLine()) != null) 
				{
					if((line.equals(content1))&&(line1.equals(content2)))
						{f.setVisible(false);	
						denglu();}
					else
						showArea.append("用戶名或密碼輸入錯誤");
	            }
			}
				catch (IOException e1) 
				{e1.printStackTrace();}
					
			
		       
		});
		
		b2.addActionListener(e ->
		{
			System.exit(0);
		});
		
		b3.addActionListener(e ->
		{
			panel.removeAll();//移除佈局
			JLabel zhanghao1 = new JLabel("賬號:");
			JLabel mima1 = new JLabel("密碼:");	
			JTextField shuru3= new JTextField (20);
			JTextField shuru4= new JTextField (20);	
			JButton zhuce = new JButton("完 成");
			JButton back = new JButton("返 回");
			panel.add(zhanghao1);
			panel.add(shuru3);
			panel.add(mima1);
			panel.add(shuru4);
			panel.add(zhuce);
			panel.add(back);
			panel.validate();//刷新
			panel.repaint();//重畫
			
			zhuce.addActionListener(g ->
			{
				String content1 = shuru3.getText();//獲取註冊名字
				String content2 = shuru4.getText();//獲取註冊密碼
				File fp=new File("D:\\Eclipse Workspace\\Training\\user\\user.txt");
				try {
					PrintWriter pfp = new PrintWriter(fp);
					pfp.print(content1);
		
					pfp.close();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				//BufferedReader bu = new BufferedReader(new InputStreamReader());
				File fp1=new File("D:\\Eclipse Workspace\\Training\\user\\key.txt");
				try {
					PrintWriter pfp = new PrintWriter(fp1);
					pfp.print(content2);
		
					pfp.close();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			});
			
		back.addActionListener(w ->
			{
				panel.removeAll();//移除佈局
				panel.add(zhanghao);
				panel.add(shuru1);
				panel.add(mima);
				panel.add(shuru2);
				panel.add(b1);
				panel.add(b3);
				panel.add(b2);
				panel.add(showArea);	
				f.add(l1,BorderLayout.PAGE_START);		
				f.add(panel,BorderLayout.CENTER);
				f.setVisible(true);
				panel.validate();//刷新
				panel.repaint();//重畫
				});
	});
}

private static void denglu() 
{
	JFrame f =new JFrame("中國銀行");
	f.setSize(580, 500);
	f.setLocation(400,300);
	JTextArea showArea = new JTextArea(50,50);
	JLabel l1 = new JLabel();//l1標籤
	ImageIcon icon = new ImageIcon("D:\\Eclipse Workspace\\Training\\img\\2.jpg");		 
	l1.setIcon(icon);
	f.setResizable(true);//窗口是否可以尺寸大小
	f.setVisible(true);
	JTextField input = new JTextField(20);
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	JLabel label = new JLabel("歡迎,請在選擇你需要的操作");
	JButton b1 = new JButton("存 款");
	JButton b2 = new JButton("取 款");
	JButton b3 = new JButton("餘 額");
	JButton b4 = new JButton("退 出");
	JPanel panel = new JPanel();
	f.add(panel);
	panel.add(l1);
	panel.add(label);
	panel.add(b1);
	panel.add(b2);
	panel.add(b3);
	panel.add(b4);
	panel.add(input);
	panel.add(showArea);
	
	
	b1.addActionListener(e ->//存款
	{
		
		String tem = input.getText();
	if((tem!=null) && (!tem.trim().equals("")))
		
	{	
	int j= Integer.parseInt( tem);
	temp=temp+j;
		showArea.setText(" ");
		showArea.append("存款成功,當前餘額爲"+temp+"!");	
	}
	else
	{	
		showArea.setText(" ");
		showArea.append("錯誤!重輸!");
	}input.setText("");
	});
	b2.addActionListener(w ->//取款
	{		
		
		
		String tem = input.getText();
	if((tem!=null) && (!tem.trim().equals("")))
	{
		int j= Integer.parseInt( tem);
		if(j>temp)
		{
			showArea.setText(" ");
			showArea.append("你哪有這麼多的錢來取哦!");		
		}else
		{
			temp=temp-j;
			showArea.setText(" ");
			showArea.append("取款成功,當前餘額爲"+temp+"!");	
		}
	}
	else
		{	
			showArea.setText(" ");
			showArea.append("錯誤!重輸!");
		}
	input.setText("");
	});
	
	b3.addActionListener(w ->
	{		
		showArea.setText(" ");
		showArea.append("當前餘額爲"+temp+"!\n");
	});
	b4.addActionListener(w ->
	{		
		f.setVisible(false);
		createAndShowGUI();	
		
	});

}
private static void setDefaultCloseOperation(int disposeOnClose) {
	// TODO Auto-generated method stub
	
}

public static void main (String[] args)
{	
	SwingUtilities.invokeLater(Bank::createAndShowGUI);
}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章