Account類:
成都創(chuàng)新互聯(lián)是專業(yè)的方城網(wǎng)站建設(shè)公司,方城接單;提供網(wǎng)站制作、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行方城網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
package?Test;
import?java.util.HashMap;
import?java.util.Map;
public?class?Account?{
private?String?cardId;//卡號
private?String?name;//姓名
private?String?password;//密碼
private?long?leftAmt;//剩余金額
public?MapObject,?ObjectgetAccountInfo(String?id){
//這里暫設(shè)一個用戶信息
//實際要建一個數(shù)據(jù)庫存放多個用戶的信息
MapObject,?Object?map?=?new?HashMapObject,?Object();
map.put("cardId",?"123");
map.put("name",?"qwe");
map.put("password",?"789");
map.put("leftAmt",?88);
//實際要通過sql語句在數(shù)據(jù)庫中查詢id對應(yīng)的賬戶信息
if(id?==?"123"){
//這里沒有考慮密碼,全部返回
return?map;
}
return?null;
}
public?void?setCardId(String?cardId)?{
this.cardId?=?cardId;
}
public?String?getCardId()?{
return?cardId;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?String?getName()?{
return?name;
}
public?void?setPassword(String?password)?{
this.password?=?password;
}
public?String?getPassword()?{
return?password;
}
public?void?setLeftAmt(long?leftAmt)?{
this.leftAmt?=?leftAmt;
}
public?long?getLeftAmt()?{
return?leftAmt;
}
}
userOperate類:
package?Test;
import?java.util.HashMap;
import?java.util.Map;
import?java.util.Scanner;
public?class?UserOperate?{
Account?account?=?new?Account();
public?void?operate(String?id){
System.out.println("歡迎您使用ATM系統(tǒng)");
System.out.println("1--存款?????2--查詢");
System.out.println("3--取款????0--退出");
System.out.println("請選擇操作類型【0-3】");
Scanner?in?=?new?Scanner(System.in);
int?type?=?in.nextInt();
while(type!=0){
if(type==1){
System.out.println("請存入鈔票");
type?=?in.nextInt();
}else
if(type==2){
System.out.println("您的銀行賬戶信息為:");
MapObject,?Object?map?=?new?HashMapObject,?Object();
map?=?account.getAccountInfo(id);
System.out.println(map);
type?=?in.nextInt();
}else
if(type==3){
System.out.println("請取走您的鈔票");
type?=?in.nextInt();
}else{
System.out.println("操作有誤,請重新操作");
type?=?in.nextInt();
}
}
System.out.println("請盡快取走您的卡,謝謝使用!再見!");
in.close();
}
}
測試類(我放在main里):
package?Test;?
public?class?test1?{
public?static?void?main(String[]?args){
//測試
UserOperate?userOperate?=?new?UserOperate();
//假設(shè)銀行卡插入后讀取到的id為123
String?id?=?"123";
userOperate.operate(id);
}
}
運行結(jié)果:
這是很簡單的一個思路,簡單跑跑可以。
也不知道你要什么樣的結(jié)果,如果是要一個完整的系統(tǒng),那具體還得建立數(shù)據(jù)庫,還有如何讀取賬戶信息,等等。
/*計算器*/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame frame;
JPanel panel;
JTextField tfShow;/*定義顯示文本框*/
JButton b1[]=new JButton[10]; /*數(shù)字按鈕*/
JButton b2[]=new JButton[6]; /*操作按鈕*/
boolean isNumber;/*判斷是否輸入多位數(shù)字的變量*/
double number;/*存儲輸入數(shù)值、顯示結(jié)果的變量*/
double result;/*存儲中間運算結(jié)果的變量*/
char operator;/*存儲當前操作符的成員變量*/
public Calculator(){
frame=new JFrame("計算器");
frame.setSize(300,300);/*指定框架窗口的大小*/
frame.setResizable(false);/*使框架窗口不可改變大小*/
JPanel contentPane=(JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(20,20,20,20));/*繪制框架的指定大小的空透明邊框*/
tfShow=new JTextField("0",25);/*指定屬性的文本域*/
tfShow.setHorizontalAlignment(JTextField.RIGHT);/*設(shè)置文本域中文本的對齊方式*/
isNumber=true;/*初始值設(shè)置*/
number=0;/*初始值設(shè)置*/
result=0;/*初始值設(shè)置*/
operator=' ';/*初始值設(shè)置*/
for(int i=0;ib1.length;i++){
b1[i]=new JButton(Integer.toString(i));/*創(chuàng)建數(shù)字按鈕*/
b1[i].setActionCommand(Integer.toString(i));
b1[i].addActionListener(this);
b1[i].setForeground(Color.blue);
}
String bs[]={"/","*","-","C","+","="};
for(int i=0;ib2.length;i++){
b2[i]=new JButton(bs[i]);/*創(chuàng)建操作按鈕*/
b2[i].setActionCommand(bs[i]);
b2[i].addActionListener(this);
b2[i].setForeground(Color.red);
}
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
panel.add(b1[1]);
panel.add(b1[2]);
panel.add(b1[3]);
panel.add(b2[0]);
panel.add(b1[4]);
panel.add(b1[5]);
panel.add(b1[6]);
panel.add(b2[1]);
panel.add(b1[7]);
panel.add(b1[8]);
panel.add(b1[9]);
panel.add(b2[2]);
panel.add(b1[0]);
panel.add(b2[3]);
panel.add(b2[4]);
panel.add(b2[5]);
frame.add(tfShow,BorderLayout.NORTH);/*將文本框放置在框架上方*/
frame.add(panel,BorderLayout.CENTER);/*將裝有按鈕組的panel放在框架的中心*/
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*設(shè)置框架窗口的默認窗口關(guān)閉操作*/
frame.setVisible(true);/*設(shè)置框架可見*/
}
public double getDisplay(){/*返回要顯示的結(jié)果*/
return number;
}
public void reDisplay(){/*刷新文本域的內(nèi)容*/
tfShow.setText(""+getDisplay());
}
/*對輸入數(shù)字的處理*/
public void numberProcess(int num){
if(isNumbernum!=0){
String s1=Integer.toString(num);
String s2=Integer.toString((int)(this.number));
this.number=Double.parseDouble(s2+s1);/*對多位數(shù)字的處理*/
}else{
this.number=num;
}
isNumber=true;/*輸入連續(xù)數(shù)字(即多位數(shù)字)時為真*/
}?
public void operationProcess(char operator){/*根據(jù)輸入的操作符改變當前操作符*/
switch(operator){
case '-':
this.operator='-';
break;
case '+':
this.operator='+';
break;
case '*':
this.operator='*';
break;
case '/':
this.operator='/';
break;
}
result=number;
isNumber=false;/*輸入操作符時表示輸入連續(xù)數(shù)字的標記變量為假*/
}?
public void clear(){
number=0;
result=0;
}??
public void equal(){/*計算運算結(jié)果*/
switch(operator){
case '-':
result=result-number;
break;
case '+':
result=result+number;
break;
case '*':
result=result*number;
break;
case '/':
result=result/number;
break;
case ' ':
result=number;
break;
}
number=result; /*把運算結(jié)果賦值給顯示變量*/
isNumber=false;
operator=' ';?
}?
public static void main(String args[]){
Calculator cal=new Calculator();/*創(chuàng)建計算器*/
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();/*獲取按鈕激發(fā)的操作事件的命令名稱*/
char c=command.charAt(0);/*將按鈕命令名稱的第一個字符賦值給一個字符c*/
switch(c){
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
int number=Integer.parseInt(command);
numberProcess(number);/*輸入數(shù)字的處理*/
break;
case '+':
case '-':
case '*':
case '/':
operationProcess(c);/*算數(shù)運算符的處理*/
break;
case '=':equal();break;/*計算運算結(jié)果*/
case 'C':clear();break;/*清零*/
}
reDisplay(); /*在文本域中顯示信息*/
}
}
運行截圖:
這是我曉得的最簡單的java小程序代碼了你可以看看:
package com.kenki.emp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;
import java.sql.*;
public class emp extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String code = request.getParameter("code");
String name = request.getParameter("name");
String pay = request.getParameter("pay");
System.out.println("empcode:" + code);
System.out.println("name:" + name);
System.out.println("pay:" + pay);
//創(chuàng)建驅(qū)動
new com.microsoft.jdbc.sqlserver.SQLServerDriver();
String strd =
"jdbc:microsoft:sqlserver://localhost:1433;databasename=emp_dates";
String username = "sa";
String pws = "";
try {
java.sql.Connection conn = java.sql.DriverManager.getConnection(
strd, username, pws);
String strs = "insert into emp values(?,?,?)";
java.sql.PreparedStatement pre = conn.prepareStatement(strs);
pre.setString(1, code);
pre.setString(2, name);
pre.setString(3, pay);
pre.execute();
pre.close();
conn.close();
//重定向至查詢頁面
out.println("成功保存??!");
response.sendRedirect("emp.html");
} catch (SQLException ss) {
ss.printStackTrace();
response.sendRedirect("/WebModule1/error.html");
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}