第一篇:java實現超市管理系統
基于java小型超市管理系統
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.Scanner;
public class Mytest { static int sign=0;//goodname數組第一個空下標號
static int sign1=0;//cname數組第一個空下標號
static String[] cpass=new String[20];//存放售貨員名稱
static String[] cname=new String[20];//存放售貨員登入密碼
static String[] goodname=new String[20];//存放商品名稱
static double[] goodprice=new double[20];//存放商品價格
static int[] goodout=new int[20];//存放商品銷售量
static int[] goodnum=new int[20];//存放商品數量
public static void main(String[] args)throws Exception { cname[0]="ZhouTian";//初始的售貨員名稱
cpass[0]="723"; //初始的售貨員密碼 createArrays(); readArrays("goodname");//將文件中的內容讀入到相應的數組里
readArrays("goodprice"); readArrays("goodnum"); readArrays("cname"); readArrays("cpass"); readArrays("goodout"); menu();//登入之后加載的主菜單
} /** * 加載登入菜單
*/ private static void menu() {
Scanner scan = new Scanner(System.in);
System.out.println(" 歡迎使用商超購物管理系統 ");
System.out.println(" 1.登錄系統 ");
System.out.println(" 2.退出 ");
System.out.println("*************************************************");
System.out.println("請選擇,輸入數字: ");
int a = scan.nextInt();
switch (a) {
case 0:
System.out.println("退出!");//按0不登入
case 1:
System.out.println("請輸入用戶名:");// 查找售貨員姓名是否存在,與所對應的密碼是否匹配
String name = scan.next();
System.out.println("請輸入密碼:");
String pass = scan.next();
int flage = 0;
int ff = 0;
for (int i = 0; i < cname.length; i++) {
if (name.equals(cname[i])) {
flage = 1;//售貨員的名稱匹配flage=1;
ff = i;
break;
}
}
if (flage == 1) {//名稱匹配之后再匹配密碼
if (pass.equals(cpass[ff])) {
loadmenu1();//密碼也匹配之后再允許登入
} else {
System.out.println("密碼錯誤!");
}
} else {
System.out.println("該用戶名不具有權限");
}
break;
case 2:
System.out.println("謝謝您的使用!");
break;
default:
break;
} }
/** * ,創建文件,將數組信息存入到各文件中, */ static void createArrays() {
BufferedWriter bw = null;
try {
File file1 = new File("d:gname.txt");
if (!file1.exists()) {
bw
=
new
BufferedWriter(new FileWriter("d:gname.txt"));
String content1 = strToString(goodname);//數組內容轉換成字符串
bw.write(content1);//寫入
bw.close();
}
File file2 = new File("d:gprice.txt");
if (!file2.exists()) {
bw
=
new
BufferedWriter(new FileWriter("d:gprice.txt"));
String content2 = doubleToString(goodprice);//數組內容轉換成字符串
bw.write(content2);//寫入
bw.close();
}
File file3 = new File("d:gnum.txt");
if (!file3.exists()) {
bw
=
new
BufferedWriter(new FileWriter("d:gnum.txt"));
String content3 = intToString(goodnum);//數組內容轉換成字符串
bw.write(content3);//寫入
bw.close();
}
File file4 = new File("d:cname.txt");
if (!file4.exists()) {
bw
=
new
BufferedWriter(new FileWriter("d:cname.txt"));
String content4 = strToString(cname);//數組內容轉換成字符串
bw.write(content4);//寫入
bw.close();
}
File file5 = new File("d:cpass.txt");
if (!file5.exists()) {
bw
=
new
BufferedWriter(new FileWriter("d:cpass.txt"));
String content5 = strToString(cpass);//數組內容轉換成字符串
bw.write(content5);//寫入
bw.close();
}
File file6 = new File("d:gout.txt");
if (!file6.exists()) {
bw
=
new
BufferedWriter(new FileWriter("d:gout.txt"));
String content5 = intToString(goodout);//數組內容轉換成字符串
bw.write(content5);//寫入
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/** * 登入之后加載商超購物管理系統
*/ static void loadmenu1() {
System.out.println("歡迎進入商超購物管理系統!");
System.out.println(" 商超購物管理系統");
System.out
.println("********************************************************************************");
System.out.println(" 1.商品維護");
System.out.println(" 2.前臺收銀");
System.out.println(" 3.商品管理");
System.out.println(" 4.退出");
System.out.println("********************************************************************************");
System.out.println("請選擇,輸入數字或按0退出:");
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
switch (a) {
case 0:
System.out.println("謝謝您的使用!");
break;
case 1:
System.out.println("執行顯示商品維護菜單");
loadmenu2();
break;
case 2:
System.out.println("執行前臺收銀");
loadmenu6();
break;
case 3:
System.out.println("執行商品管理");
loadmenu7();
break;
default:
break;
} } /* * 在商超購物管理系統中按1鍵時 */ static void loadmenu2() {
System.out.println(" 商超購物管理系統 商品維護");
System.out
.println("********************************************************************************");
System.out.println(" 1.添加商品 ");
System.out.println(" 2.更改商品 ");
System.out.println(" 3.刪除商品 ");
System.out.println(" 4.顯示所有商品 ");
System.out.println(" 5.查詢商品 ");
System.out
.println("********************************************************************************");
System.out.println("請選擇,輸入數字或按0返回上一級菜單:");
Scanner scan = new Scanner(System.in);
int b = scan.nextInt();
switch (b) {
case 0:
loadmenu1();
break;
case 1:
addgoods();
break;
case 2:
System.out.println("執行更改商品操作");
loadmenu3();// 修改商品
break;
case 3:
System.out.println("執行商品刪除操作");
loadmenu4();// 商品刪除
break;
case 4:
System.out.println("顯示所有商品");
System.out.println("商品名稱 " + "商品價格 " + "商品數量 "
+ "備注");
showgoods();
break;
case 5:
System.out.println("執行查詢商品操作");
loadmenu5();
break;
default:
break;
} } /* * 在商超購物管理系統中按2鍵時
*/ static void loadmenu6() {
System.out.println(" 商超購物管理系統 前臺收銀");
System.out
.println("********************************************************************************");
System.out.println(" 1.購物結算");
Scanner scan = new Scanner(System.in);
int ch = scan.nextInt();
switch (ch) {
case 0:
loadmenu1();
break;
case 1:
pay();
break;
default:
break;
}
} /* * 在商超購物管理系統中按3鍵時
*/ static void loadmenu7() {
Scanner scan = new Scanner(System.in);
System.out.println(" 商超購物管理系統 商品管理");
System.out
.println("********************************************************************************");
System.out.println(" 1.庫存查詢");
System.out.println(" 2.已出售商品");
System.out.println(" 3.售貨員管理");
System.out.println("請選擇,輸入數字或按0返回上一級菜單:");
int ch = scan.nextInt();
switch (ch) {
case 0: loadmenu1();
break;
case 1:
System.out.println("執行庫存查詢操作:");
kusear();
break;
case 2:
System.out.println("執行已出售商品查詢操作");
besold();
break;
case 3:
System.out.println("執行售貨員管理操作");
salesman();
break;
default:
break;
}
} /* * 對售貨員信息的增添,修改 */ static void salesman()
{
Scanner scan = new Scanner(System.in);
System.out.println("
1、添加售貨員");
System.out.println("
2、修改售貨員信息");
System.out.println("請選擇,輸入數字或按0返回上一級菜單:");
int b = scan.nextInt();
switch (b) {
case 0:
loadmenu7();//返回上一級
break;
case 1:
addsale();//選擇1鍵,添加售貨員
break;
case 2:
modifysale();//更改售貨員的信息
break;
default:
break;
} } /* 添加售貨員 * */ static void addsale() {
Scanner scan = new Scanner(System.in);
String f;
// sign1 = sign1 + 1;
do {
System.out.println("添加售貨員姓名:");
String addname = scan.next();
add(cname, addname, sign1);//將新信息添加到相應數組中
System.out.println("添加售貨員登陸密碼:");
String addpass = scan.next();
add(cpass, addpass, sign1);//將新信息添加到相應數組中
// writeArray();
sign1++;//新信息添加后數組下標加1
writeArray();
System.out.println("是否繼續(y/n)");
f = scan.next();
// writeArray();
} while (f.equals("y"));
salesman(); } /* * 更改售貨員的信息 */ static void modifysale() {
String f;
Scanner scan = new Scanner(System.in);
System.out.println("輸入更改員工名稱:");
String newname = scan.next();
int k = searmember(newname);//查詢需要改的售貨員的信息,并返回其在數組中的下標
if (k >= 0) {//判斷售貨員是否存在
System.out.println("員工姓名" + " 密碼");
System.out.println(cname[k] + " " + cpass[k]);
System.out.println("選擇您要更改的內容:");
System.out.println("
1、更改員工姓名:");
System.out.println("
2、更改登錄密碼:");
int kk = scan.nextInt();
switch (kk) {
case 1:
System.out.println("請輸入已更改員工姓名:");
String newnname = scan.next();
cname[k] = newnname;
writeArray();
System.out.println("是否繼續(y/n)");
f = scan.next();
if (f.equals("y")) {
modifysale();
} else {
salesman();
}
break;
case 2:
System.out.println("請輸入已更改密碼:");
String newpass = scan.next();
cpass[k] = newpass;
writeArray();
System.out.println("是否繼續(y/n)");
// System.out.println("是否繼續(y/n)");
f = scan.next();
if (f.equals("y")) {
modifysale();
}
salesman();
break;
default:
break;
}
} } /* * 查詢ename姓名的售貨員在數組中的下標 */ static int searmember(String ename) {
for (int i = 0; i < cname.length; i++) {
if (cname[i].equals(ename)) {
return i;
}
}
return -1;// 不存在時
} /* * 查詢已出售的商品信息 */ static void besold()
{
System.out.println("商品名稱 " + "商品價格 " + "商品數量 " + "備注");
for (int i = 0; i < goodout.length; i++) {
if (goodout[i] != 0) {
System.out.println(goodname[i] + " " + goodprice[i]
+ " " + goodout[i]);
}
}
loadmenu7(); } /* * 查詢庫存中售的商品信息
*/ static void kusear() {
Scanner scan = new Scanner(System.in);
String f;
do {
System.out.println("輸入要查詢的商品名稱:");
String sname = scan.next();
int t = ndimsear(sname);//按確切的商品名稱進行查詢,即非模糊查詢,找出商品名稱信息的下標
if (t >= 0) {
System.out.println("商品名稱 " + "商品價格 " + "商品數量 "
+ "備注");
System.out.println(goodname[t] + " " + goodprice[t]
+ " " + goodnum[t] + " ");//按下標輸出商品相應的信息
} else {
System.out.println("沒有此商品");
}
System.out.println("是否繼續(y/n)");
f = scan.next();
} while (f.equals("y"));
loadmenu7();
} /* * 按確切的商品名稱進行查詢,即非模糊查詢,找出商品名稱信息的下標
*/ static int ndimsear(String s) {
for (int i = 0; i < goodname.length; i++) {
if (goodname[i].equals(s)) {
return i;
}
}
return -1;//沒查詢到時
}
/* * 商品結算
*/ static void pay() {
Scanner scan = new Scanner(System.in);
double sum = 0;
String f;
do {
dimsearch1();//按關鍵字進行模糊查詢,查找可能需要的商品
System.out.println("請選擇商品:");
String bnam = scan.next();//按查詢到的相關商品中選擇要購買的商品
System.out.println("請輸入購買數量:");
int bnum = scan.nextInt();
int fl = seargood(bnam);
System.out.println(bnam + " " + goodprice[fl] + " "
+ "購買數量" + bnum + " " + bnum * goodprice[fl]);
goodnum[fl] = goodnum[fl]sum));
writeArray();//將對數組的更改寫入到文件中
loadmenu1();//結束購買返回
} /* * 查找商品返回下標 */ static int seargood(String bbnam) {
for (int i = 0; i <= goodname.length; i++) {
if (goodname[i].equals(bbnam)) {
return i;
}
}
return -1; }
/* * 對商品進行各種查找
*/
static void loadmenu5() {
Scanner scan = new Scanner(System.in);
System.out.println("
1、按商品數量升序查詢");
System.out.println("
2、按商品價格升序查詢");
System.out.println("
3、輸入關鍵字查詢商品");
System.out.println("請選擇,輸入數字或按0返回上一級菜單:");
int choice = scan.nextInt();
switch (choice) {
case 0:
loadmenu2();//返回上一級
break;
case 1:
numshenxu();//按商品數量升序進行查找
break;
case 2:
priceshenxu();//按商品價格升序進行查找
break;
case 3:
dimsearch();//按商品的關鍵字進行查找
break;
default:
break;
}
}
static void dimsearch1() {
int[] k;
Scanner scan = new Scanner(System.in);
System.out.println("輸入商品關鍵字:");
String sear = scan.next();
k = Arrays.copyOf(index(sear), index(sear).length);
System.out.println("商品名稱 " + "商品價格 " + "商品數量 " + "備注");
if (k != null) {
for (int i = 0; i < k.length; i++) {
System.out.println(goodname[k[i]] + " " goodprice[k[i]]
+ " " + goodnum[k[i]] + " ");
}
}
} /* * 按商品的關鍵字進行查找
*/ static void dimsearch()
+ {
String f;
do {
int[] k;
Scanner scan = new Scanner(System.in);
System.out.println("輸入商品關鍵字:");
String sear = scan.next();
k = Arrays.copyOf(index(sear), index(sear).length);
System.out.println("商品名稱 " + "商品價格 " + "商品數量 "
+ "備注");
if (k != null) {
for (int i = 0; i < k.length; i++) {
System.out.println(goodname[k[i]] + " "
+ goodnum[k[i]]
+ " ");
}
goodprice[k[i]]
+
" "
+
}
System.out.println("是否繼續(y/n)");
f = scan.next(); } while (f.equals("y")); loadmenu2(); } static int[] index(String ss) { int countt = 0; int counttt = 0; for (int i = 0; i < goodname.length; i++) {
if (goodname[i].indexOf(ss) >= 0) {
countt++;
} } int[] location = new int[countt]; for (int i = 0; i < goodname.length; i++) {
if (goodname[i].indexOf(ss) >= 0) {
location[counttt] = i;
counttt++;
}
}
return location; } /* * 按商品數量升序進行查找
*/ static void numshenxu() {
int[] newnum = Arrays.copyOf(goodnum, goodnum.length);
double[]
newprice
=
Arrays.copyOf(goodprice, goodprice.length);
String[] goodname.length);
for (int i = 0; i < newnum.lengthi1; i++) {
for (int j = 0; j < newprice.length1; j++) {
if (newprice[j] < newprice[j + 1]) {
int temp = newnum[j];
newnum[j] = newnum[j + 1];
newnum[j + 1] = temp;
double temp1 = newprice[j];
newprice[j] = newprice[j + 1];
newprice[j + 1] = temp1;
String temp2 = newname[j];
newname[j] = newname[j + 1];
newname[j + 1] = temp2;//相應的下標在不同的數
newname
=
Arrays.copyOf(goodname, 組里進行挑換,商品的信息才能保持一致
}
}
}
System.out.println("商品名稱 " + "商品價格 " + "商品數量 " + "備注");
for (int k = 0; k < newnum.length; k++) {
if (newprice[k] != 0) {
System.out.println(newname[k] + " " + newprice[k]
+ " " + newnum[k] + " ");
}
}
loadmenu5(); } /* *更改商品信息的操作
*/ static void loadmenu3() {
Scanner scan = new Scanner(System.in);
System.out.println("輸入更改商品名稱:");
String nam = scan.next();
int b = showflage(nam);
print(b);
System.out.println("選擇您要更改的內容:");
System.out.println("
1、更改商品名稱::");
System.out.println("
2、更改商品價格:");
System.out.println("
3、更改商品數量:");
int ch = scan.nextInt();
switch (ch) {
case 0:
break;
case 1:
int counnt = 0;
String f = "y";
while (f.equals("y")) {
if (counnt != 0) {
// Scanner scan = new Scanner(System.in);
System.out.println("輸入更改商品名稱:");
nam = scan.next();
b = showflage(nam);//查找需要更改商品的下標
print(b);//輸出這個商品的各種信息
}
System.out.println("請輸入已更改商品名稱");
String cname = scan.next();
goodname[b] = cname;//更改名稱
System.out.println("是否繼續(y/n)");
f = scan.next();
counnt++;
writeArray();
}
loadmenu2();
break;
case 2:
counnt = 0;
f = "y";
while (f.equals("y")) {
if (counnt != 0) {
// Scanner scan = new Scanner(System.in);
System.out.println("輸入更改商品名稱");
nam = scan.next();
b = showflage(nam);
print(b);
}
System.out.println("請輸入已更改商品價格");
double cprice = scan.nextDouble();
goodprice[b] = cprice;//新價格寫入數組
System.out.println("是否繼續(y/n)");
f = scan.next();
counnt++;
writeArray();
}
loadmenu2();
break;
case 3:
counnt = 0;
f = "y";
while (f.equals("y")) {
if (counnt != 0) {
// Scanner scan = new Scanner(System.in);
System.out.println("輸入更改商品名稱");
nam = scan.next();
b = showflage(nam);
print(b);
}
System.out.println("請輸入已更改商品數量");
int cnum = scan.nextInt();
goodnum[b] = cnum;
counnt++;
writeArray();
System.out.println("是否繼續(y/n)");
f = scan.next();
}
loadmenu2();
break;
default:
break;
}
} /* * 查詢nama這個商品在goodname中的下標
*/ static int showflage(String nama) {
int flag = 0;
for (int i = 0; i < goodname.length; i++) {
if (goodname[i].equals(nama)) {
flag = i;
break;
}
}
return flag; }
static void print(int b) {
System.out.println("商品名稱 " + "商品價格 " + "商品數量 " + "備注");
System.out.println(goodname[b] + " " + goodprice[b] + " "
+ goodnum[b] + " ");
} /* * 刪除商品菜單
*/ static void loadmenu4() {
String f;
int d;
do {
Scanner scan = new Scanner(System.in);
System.out.println("請輸入要刪除的商品名稱:
String dname = scan.next();
d = showflage(dname);
delete(d);
System.out.println("是否繼續(y/n)");
f = scan.next();
writeArray();//刪除之后重新寫入文件
} while (f.equals("y"));
loadmenu2(); }
"); /* * 刪除商品 */ static void delete(int dd) {
for (int i = dd; i < goodname.length1] = null;
goodprice[goodprice.length1] = 0;
} /* * 查詢所有商品
*/ static void showgoods() {
int i = 0;
while (goodprice[i] != 0) {
System.out.print(goodname[i] +
" " goodprice[i] + " "
+
+ goodnum[i] + " ");
if (goodnum[i] < 10) {
System.out.println(" *該商品已不足10件!");
}
System.out.println();
i++;
}
loadmenu2();
} /* * 商品添加操作 */ static void addgoods() {
System.out.println("執行添加商品操作: ");
String a;
do {
Scanner scan = new Scanner(System.in);
System.out.println("輸入商品名稱:");
String na = scan.next();
add(goodname, na, sign);
System.out.println("輸入添加商品價格:");
double pr = scan.nextDouble();
add(goodprice, pr, sign);
System.out.println("輸入添加商品數量:");
int c = scan.nextInt();
add(goodnum, c, sign);
sign++;
System.out.println("是否繼續(y/n)");
a = scan.next();
writeArray();
} while (a.equals("y"));
loadmenu2();
} /* * 將數組信息寫入到相應的文件里,啟動程序時就要調用這個函數 */ static void writeArray() {
BufferedWriter bw = null;
try {
bw
=
new
BufferedWriter(new FileWriter("d:gname.txt"));
String content1 = strToString(goodname);
bw.write(content1);
bw.close();
bw
=
new
BufferedWriter(new FileWriter("d:gprice.txt"));
String content2 = doubleToString(goodprice);
bw.write(content2);
bw.close();
bw
=
new
BufferedWriter(new FileWriter("d:gnum.txt"));
String content3 = intToString(goodnum);
bw.write(content3);
bw.close();
bw = new BufferedWriter(new FileWriter("d:cname.txt"));
String content4 = strToString(cname);
bw.write(content4);
bw.close();
bw
=
new
BufferedWriter(new FileWriter("d:cpass.txt"));
String content5 = strToString(cpass);
bw.write(content5);
bw.close();
bw
=
new
BufferedWriter(new FileWriter("d:gout.txt"));
String content6 = intToString(goodout);
bw.write(content6);
bw.close();
} catch (IOException e) {
e.printStackTrace();
} } /* * 添加商品名稱 */ static void add(String[] aa, String n, int b) {
if (b == aa.length) {
aa = (String[]) Arrays.copyOf(aa, aa.length * 2);
}
aa[b] = n; } /* * 添加商品價格
*/ static void add(double[] aa, double pr, int b) {
if (b == aa.length) {
aa = (double[]) Arrays.copyOf(aa, aa.length * 2);
}
aa[b] = pr; } /* * 添加商品數量
*/ static void add(int[] aa, int cc, int b) {
if (b == aa.length) {
aa = (int[]) Arrays.copyOf(aa, aa.length * 2);
}
aa[b] = cc;
}
/** * 從文件中讀取信息到相應的數組里
*/ static void readArrays(String name) throws Exception {
BufferedReader br = null;
try {
if ("goodname".equals(name)) {
br
=
new
BufferedReader(new FileReader("d:gname.txt"));
String content1 = br.readLine();
String[] arr = strToString(content1);
goodname arr.length);
for (int i = 0; i < goodname.length; i++) {
if (!goodname[i].equals("null")) {
sign++;
=
(String[])
Arrays.copyOf(arr,
}
}
} else if ("goodprice".equals(name)) {
br
=
new
BufferedReader(new FileReader("d:gprice.txt"));
String content2 = br.readLine();
double[] arr = doubleToString(content2);
goodprice arr.length);
} else if ("goodnum".equals(name)) {
br
=
new
BufferedReader(new
=
(double[])
Arrays.copyOf(arr, FileReader("d:gnum.txt"));
String content3 = br.readLine();
int[] arr = intToString(content3);
goodnum = (int[]) Arrays.copyOf(arr, arr.length);
} else if ("cname".equals(name)) {
br
=
new
BufferedReader(new FileReader("d:cname.txt"));
String content4 = br.readLine();
String[] arr = strToString(content4);
cname arr.length);
= (String[]) Arrays.copyOf(arr,
for (int i = 0; i < cname.length; i++) {
if (!cname[i].equals("null")) {
sign1++;
}
}
} else if ("cpass".equals(name)) {
br
=
new
BufferedReader(new FileReader("d:cpass.txt"));
String content5 = br.readLine();
String[] arr = strToString(content5);
cpass arr.length);
} else {
br
=
new
BufferedReader(new
=
(String[])
Arrays.copyOf(arr, FileReader("d:gout.txt"));
String content6 = br.readLine();
int[] arr = intToString(content6);
goodout = (int[]) Arrays.copyOf(arr, arr.length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /* * 將字符串轉換成字符串數組 */ static String strToString(String[] arry) {
// String arrys="";
// for(int i=0;i
// {
// arrys=arry+arry[i]+",";
// }
// //arrys=arrys+arry[arry.length-1];
// System.out.println(arrys);
// return arrys;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arry.length; i++) {
sb.append(arry[i]).append(",");
}
sb.deleteCharAt(sb.length() - 1);
// System.out.println(sb.toString());
return sb.toString(); } /* * 將字符串轉換成字符串數組 */ static String[] strToString(String ar) {
String[] arry = ar.split(","); return arry; } /* * 將int型數組轉換成字符串 */ static String intToString(int[] arrr) {
String ss = "";
ss = Arrays.toString(arrr);
String sss
=
ss.replace("[",
"").replace("]", "").replace(" ", "");
第二篇:基于Java的高速公路收費管理系統的設計與實現
【摘 要】本文闡述了基于Struts2+Spring+Hibernate開源框架實現高速公路收費管理子系統的開發流程。本文首先對Struts
2、Hibernate、Spring等框架進行論述,其次描述了系統的需求分析與總體架構設計,包括系統的功能需求、性能需求、功能結構設計和體系結構設計等;然后詳細介紹了系統的各個模塊的具體實現,主要模塊有登陸模塊、數據錄入模塊、維護模塊等;最后,對系統進行的測試并對項目的主要工作和存在的不足做了論述。
【關鍵詞】Struts2;Spring;Hibernate
0 引言
基于B/S結構的Web程序的開發,有非常大的靈活性,允許用戶遠程訪問系統資源的同時能對數據平臺和管理訪問權限進行有效的保護。B/S結構軟件的所有的客戶端只是瀏覽器,也只需要管理服務器,更為快捷、方便、高效。與C/S體系結構相比,B/S體系結構不僅具有C/S體系結構的全部優點,而且具有客戶端零維護、可擴展性好、安全性好、資源重用性好等新的優點。
1 系統需求分析
計算機收費系統式收費站的主要組成部分[3]。每個收費站由網絡服務器、管理計算機構成的上端管理系統和若干個車道控制計算機構成 的車道控制系統組成局域網。高速公路計算機收費系統的目標是:按照每輛車的車型和實際行駛里程收費;提高收費的工作效率,盡量降低由于收費過程引起的交通延誤,提高高速公路的通行能力;實現高速公路的現代化營運管理為智能運輸系統的實現留有一定的接口?;谙到y的以上目標,收費系統應該具有以下功能:
1)實行計算機管理,盡量減少人為干預,保證車型判別的正確性。
2)系統能自動檢查設備運行狀態,能準確并及時地給出故障信息和故障排除信息。
3)系統應該是全天候、實時的,如收費車道應該在任何時間有車輛通過時能正常使用。
4)具有一定的報警功能,如遇搶劫時的遠程報警,與車輛沖關或進出口收費員判刑不一致時報警。
根據以上對系統需求的描述和功能的分析,對于收費管理系統,它主要處理的數據是收費數據,主要包括 4個模塊:數據登錄模塊,錄入模塊,維護模塊。
2 技術平臺
2.1 MVC模式和Struts框架
MVC某種語言所特有的設計思想,而是所有面向對象程序設計語言都應該遵守的規范[4]。MVC思想將應用分為三個基本部分:模型、視圖、控制器。這三個部分將以最少的耦合協同工作,從而提高應用的可擴展性及可維護性。
Struts2是Struts的下一代產品,是一個優雅的,可擴展的Java EE Web框架[5]。它是Struts和WebWork的技術基礎上進行合并的全新的Struts2框架。但是,Struts2沒有繼承Struts1的血統,而是繼承WebWork的血統。Struts框架作為MVC模式的典型實現,對Model、View和Controller都提供了對應的實現組件。
2.2 Spring框架
Spring框架是一個分層架構,由7個模塊組成,各個模塊之間是相互的。Spring其他模塊構建在核心容器之上,其中,核心容器定義了創建、配置和管理bean的方式。
3 系統實現
在系統需求分析和設計的基礎上,在系統的開發過程中,將SSH架構應用到實際的項目開發中,并且利用第三方報表開發軟件開發報表部分,極大地簡化了系統的開發,提高了效率。在整個系統的實現過程中,本文主要實現了收費管理系統模塊。
3.1 數據登錄模塊
用戶登錄模塊的設計中完全實現了MVC模式,實現了視圖與數據處理的分離。登錄頁面、修改密碼頁面以及登錄成功頁面構成視圖層;用戶登錄及修改密碼的請求應答控制由控制器實現;具體的用戶信息合法性由Struts2提供的客戶端輸入校驗完成,保證輸入信息的合法性。圖1是用戶登錄的簡要圖。
3.2 錄入模塊
數據錄入模塊主要就是報表文件的開發,設計報表文件,首先要打開設計器,連接到數據源,然后開始進行報表文件的設計。本模塊中報表文件,僅用于數據錄入,因此可以創建的報表應該選擇填報或參數表單。然后在快逸報表中的表格中填寫選項,設置選項的屬性要根據需求來設置,包括設置數據的格式,編輯的風格、是否可寫,能否為空等。
3.3 維護模塊
該模塊主要是負責維護人員名單的相關信息。由于本系統是內部人員使用的系統,用戶較少,不需要通過注冊來添加人員,而是通過該模塊來添加刪除人員或者修改其權限。
主要涉及到的信息包括:站號、用戶ID、用戶名、用戶密碼、班次編號、錄入權限、監視權限、統計權限、檢索權限、稽查權限、維護權限、票據管理權限、卡管理權限等,如表1所示。其中各種管理權限來自于用戶信息中purview字段,每個比特位代表一種權限。
4 結論
系統采用SSH(Struts2+Spring+Hibernate)輕量級框架,完成了高速公路收費管理系統的開發,對三個框架的使用和集成有了深刻的認識。本文詳細介紹了三個框架的背景、相關技術以及內部實現原理,包括MVC的開發模式、ORM技術以及IOC和AOP,這些對于系統的開發有非常重要的幫助。項目基于MVC的思想進行開發,Struts2框架負責了表示層和控制層;采用Hibernate框架以面向對象的方式來訪問數據庫,極大地簡化了數據庫操作;Spring框架將業務層又細分為業務邏輯層和數據持久層,使得接口和實現進一步的分離,使開發者從繁雜的事務管理中解脫出來,而專注于業務邏輯的處理。
【參考文獻】
[1]彭艷玲,胡松濤.高速公路收費現狀與存在問題研究[J].山西科技,2008(04):10-12.doi:10.3969/j.issn.1004-6429.2008.04.005.
[2]吳煒,丁月華,文貴華.ETC車道控制系統的設計與實現[J].計算機工程,2001(05):162-163.doi:10.3969/j.issn.1000-3428.2001.05.065.
[3]郭敏.高速公路收費系統[M].北京:人民交通出版社,2002,7:15.
[4]李剛.較輕量級JavaEE企業應用實踐[M].北京:電子工業出版社,2004,11:158.
[5][美]巴西亞,等,編.Java企業級持久化技術指南[M].葉斌,譯.北京:科學出版社,2010,6:137.
[6]白廣元.Java Web整合開發完全自學手冊[S].北京:機械工業出版社,2009,8:144.
[責任編輯:丁艷]
第三篇:Java五子棋實現報告
一、實驗目的
(1) 使用Java編寫五子棋程序
(2) 掌握編寫過程的一些類和類成員的使用,并且了解五子棋制作過程的一些步驟和了解一些算法。
二、實驗環境
在電子樓2樓,裝有My Eclipse 的計算機上進行
三、實驗內容
編寫一個五子棋程序。 程序主要實現以下功能:
1.實現五子棋界面,要求人性化界面簡潔美觀; 2.實現黑白色輪流下棋功能,能提示下棋方; 3.實現連成五子自動判斷功能; 4.實現時間設置功能,認輸功能;
核心代碼如下:
1.我的第一步是設計界面。在這個過程的要使用到以下的步驟:
1.使用MyEclipse 創建一個Project關于這個工程,加入兩個類,分別是ChessFrame和Chess,而ChessFrame是這個工程中最重要的一個部分。創建過程中要繼承JFrame類,并且要繼承Runnable 和 MouseListener 這兩個接口,分別是用來監聽鼠標的移動和時間的變化。 2.在給這個JFrame設置大小和顏色等一些東西。 這里最主要的是使用了兩個函數,是以前沒見過的:
1.這個是用來設置默認的窗口關閉事件的
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 2.這兩個是用來獲得屏幕的大小的
Toolkit.getDefaultToolkit().getScreenSize().height; Toolkit.getDefaultToolkit().getScreenSize().width;
3.把本地的背景圖片加到這個窗口中,作為這個窗口的背景圖:
使用的到的類有BufferedImage和 ImageIO這兩個類,這兩個類是專門用來處理這種功能的。主要的代碼如下:
BufferedImage bg = null; bg = ImageIO.read(new File("e:/background.jpg")); g.drawImage(bg,0,0,this); 這樣這個圖片就加到了這個窗口的背景去了。
這里有一個改進就是雙緩沖技術,可以防止閃屏。這個技術就是在界面和內存都有一幅圖,我們的改動不是直接畫界面,而是內存中先畫好然后把內存中直接顯示出來,那樣就沒有了一前一后的,那就沒有了閃屏的事情發生了。
4.就是畫棋盤和寫一些游戲信息:
這步比較簡單,也沒什么可以說的,用到的一些畫線的函數,和寫String的一些函數。
以上做完以后就可以出現以下的界面:
1.我的第二步就是增加對mouse的點擊和移動的處理,這樣以后就可以用來下棋。在這個過程的要使用到以下的步驟: 1.增加對mouse的監視,然后重寫它的一些函數:
1.我們在ChessFrame的構造函數中加入了addMouseListener()函數,然后重寫關于這個的四個函數,我們這這里不重寫所有的函數,就改寫一個MousePress函數就可以了。改寫的代碼如下:
public void mousePress(MouseEvent arg0) {
this.x = arg0.getX(); this.y = arg0.getY(); if (this.canplay)
} this.repaint(); 現在要處理的就是加上棋子了,我在這個做了一下小改動可以更加準確的定位下棋的位置:
if (x >= 10 && x <= 374 && y >= 72 && y <= 450) {
int sx1 = (this.x72) / 20; int yx1 = (this.x72) % 20; if (yx1 >= 10) { } if (yy2 >= 10) { } sy2++; sx1++; 就是這個,我們通過監控鼠標的就可以得到點擊的X,Y坐標,我在這里通過對它求余,假如余數大于10(就是一個格子的一半)那就給+1,不然不變。后面的畫棋子是很簡單的一個過程,使用的是現成的函數(drawoval)
2.這部要做的就是記錄棋子和五子連的判斷:
1.我們通過一個數組int給保存棋子,如果是0就是沒子,1表示是黑子,2表示白子。在每次重繪的時候遍歷這個數組顯示,這樣就可以保存棋子了。 2.判斷五子連起來然后獲勝的算法: 這個算法是鑒戒例子上的:
private boolean isline(int x, int y, int xchange, int ychange, int color) {
int tempx = xchange; int tempy = ychange; int count = 1; while (x + xchange >= 0 && y + ychange >= 0
&& color == chess1[x + xchange][y + ychange]) { count++; if (xchange != 0) { } if (ychange != 0) { if (ychange < 0) { xchange++;
}
}
}
} ychange--; ychange++; } else { xchange = tempx; ychange = tempy; while (xychange >= 0
} if (count >= 5) {
} return true; return false; } else { && color == chess1[xychange]) { count++; if (xchange != 0) { } if (ychange != 0) {
} if (ychange < 0) {
} ychange--; ychange++; } else { xchange++; 中心思想就是要判斷就要判斷8個方向,其中有的只是相反的方向,我們使用關于X,Y的相對的變化來判斷,并且使用count來計數,如果到了5個就可以判斷勝利啦。
1.我的第三步就是時間的變化和一些鍵的設置
1.關于時間的變化主要是在于線程的使用,這里我們這個JFrame它繼承了Runnable 這個接口,不過我們要重寫Run函數,這個run函數的代碼如下: public void run() {
if (this.second > 0) {
while (true) {
this.repaint(); if (this.isblack) {
this.ss1--;
if (this.ss1 == 0) {
JOptionPane
.showMessageDialog(this, "黑方超時~~~~¡¤游戲結束");
this.message1="0:0:0";
{
"白方超時~~~~¡¤¡¤游戲結束"); this.message2="0:0:0"; this.canplay = false;
{
{ }
this.ss2--;
if (this.ss2 == 0) {
this.message1 = this.ss
1/ 3600 + " : "
+ (this.ss1 / 60this.ss1 / 60 *
this.ss1=-1;
this.canplay = false;
}else if(this.ss1>0) 3600 * 60) 60this.ss2 / 3600 + " : "
+ (this.ss2this.ss2 / 3600 * 60 * 60);
this.repaint(); }else { }
try {
}
Thread.sleep(1000); e.printStackTrace(); } catch (InterruptedException e) { 這個函數是用來事時間的變化,所以每操作以后就讓線程sleep(1000)就可以了,那樣就是一秒鐘。這樣就可以倒計時了。 2.現在我們要點擊一個按鈕要它有響應。
這里我們其實就沒用到什么button,那為什么會有反應呢,因為我們通過鼠標的點擊范圍來確定要響應什么函數,這就是這里面的秘密.
在public void mouseClicked(MouseEvent arg0) 函數里有下面這些if的判斷
if (x1 >= 404 && x1 <= 473 && y1 >= 74 && y1 <= 103) if (x1 >= 404 && x1 <= 473 && y1 >= 127 && y1 <= 155) if (x1 >= 404 && x1 <= 473 && y1 >= 179 && y1 <= 208) if (x1 >= 404 && x1 <= 473 && y1 >= 286 && y1 <= 316) if (x1 >= 404 && x1 <= 473 && y1 >= 338 && y1 <= 367) if (x1 >= 404 && x1 <= 473 && y1 >= 390 && y1 <= 419) 這就是為了確定鼠標的范圍,然后相對調用函數。
其他還有就是一些Debug處理,在程序已經處理了,結果的還是蠻可以處理的,不過只能是人人對戰。
四、實驗心得與小結
這次試驗總的來說沒有想像中那么難,還有視屏的幫助,感覺起還是很容易動手的,不過由于自己沒上心,沒有做比較大的改動,最主要的人機的對戰沒做出來是滿遺憾的,不過通過這個試驗,讓我學習了不少的知識,比如雙緩沖,mouselistener 等許多許多的知識,還有關于五子棋算法的設計也是一部分。不管怎么說還是有學到知識的,沒什么改動,但對現有的知識的掌握還是很可以的。
五、指導教師評議
成績評定:
指導教師簽名:
第四篇:Java登陸驗證模塊的實現及小結
登陸驗證模塊的實現
登陸頁面驗證首先是驗證賬號, 驗證碼是否填寫,密碼長度是否符合要求,驗證碼是否正確。這幾個驗證是通過自定義的幾個$.fn擴展函數實現的($.fn.remote=Function(url,msg, msgCnt){})實現的,一般將這些自定義的函數放在一個js文件中,便于維護。
自定義函數一般包含相應的提示字符串,及節點對象$(“#id”)等參數。通過函數對節點的var value = this.val();value是否為空或空字符串的判斷,相應的改變節點text屬性的值。 1驗證碼正確驗證
1)驗證碼正確與否的驗證是通過采用了Ajax技術的函數實現的。對應的action的result的屬性為json(result要配置ok的參數),action類要有布爾類型的名為ok的屬性,通過返回值ok,更改相應的改變節點text屬性的值。
如:
$.fn.remote=function(url,msg,msgCnt){ var value=this.val();//獲得表單提交的數據,如你所填入的驗證碼/資費名稱
var name=this.attr("name");//獲得表單名稱,如驗證碼文本框的name:verifiCode
var b=false; $.ajax({url:url+"?"+name+"="+value,dataType:"json",async:false,success:function(data){
if(data){
$(msgCnt).text("");
b=true;
}else{
$(msgCnt).text(msg);
}
}
}); return b; }; 2)驗證碼的點擊更新是利用了result=stream的屬性(要配置name="inputName",name="contentType"的參數),點擊發送請求,對應的action(該類必須有InputStream類的屬性,且名字要和result相應參數的屬性值相同)生成驗證碼并存入session。
2賬戶和密碼的驗證
當前面一系列的驗證結果都滿足條件時,才能提交表單??啥x一個函數實現: if(b1 && b2 && b4) {$("#loginForm").submit();} 相應action 接受到數據后,根據賬戶和密碼調用DAO的相關方法,查詢返回admin。
如果admin存在則轉發到主頁面(通過result在admin的不同狀態下返回的字符串實現不同的type="redirectAction"),此時將admin存入session。為后面的登陸檢查驗證做準備。
session.put(KEY_ADMIN, admin); 如果不存在就,還是停留在登陸頁面。
3檢查登陸驗證。
為了防止未登錄而訪問數據,通過struts2的自定義攔截器功能實現登陸檢查驗證。
可通過實現接口Interceptor或者抽象類AbstractInterceptor,我選擇繼承AbstractIntercepto,相對較簡潔。在接口定義的方法intercept(ActionInvocation in)實現驗證邏輯。
通過ActionInvocation的getInvocationContext()方法獲得action上下文對象ActionContext。通過action上下問對象獲得session對象,在session對象中獲取admin對象:
Admin admin = (Admin)session.get(Constants.KEY_ADMIN); 如果admin為空,則通過return 返回字符串及全局result回到登陸頁面。 如果admin不為空,則通過ActionInvocation對象調用invoke()方法,調用后面的action。
權限管理:
每個用戶具有不同的角色,不同的角色具有不同的權限。當用戶登陸的時候,根據用戶對象取得用戶所具有的角色,根據角色id在privileges.xml(通過privilegereader類)文件中獲得用戶具有的所有權限,然后將此所有權限的集合存入session。當用戶每一次發送請求時都要通過攔截器,在攔截器中截取用戶的請求路徑(request.getServletPath()),并檢查是否存在于session中的用戶所具有權限中。如果沒有就返回到提示頁面,如果有就調用invoke()方法執行后面的action,result。
攔截器的配置:
1. 首先注冊攔截器
2. 然后定義攔截器棧,將默認攔截器棧也加進來。
3. 為當前package中所有Action指定默認的攔截器:
資費管理模塊:
資費管的功能主要有資費信息的增加、管理、刪除和修改等功能。采用三大框架整合技術實現的。
在applicationContext.xml文件中配置連接池(用戶名、密碼、驅動包、連接路徑),將連接池注入SessionFactory中,在SessionFactory中配置Hibernate框架參數:如方言dialect,show_sql等,指定映射描述。然后開啟組件掃描:
因為采用注解的形式,所以dao要繼承HibernateDaoSupport,定義一個set方法調用父類的super.setSessionFactory(sf)方法,實例化HibernateTemplate。 在dao中利用getHibernateTemplate()實現一列的增刪改查方法。 List list = getHibernateTemplate().find(hql,name);
關于事務:
在applicationContext.xml文件中定義事務管理Bean,然后定義通知,可以在里面()指定目標組件中的哪些方法采用哪種事務管理方式。然后定義,指定切入點,將指定的dao當做目標。然后關聯通知和切入點:。
將dao用@Respository掃描進組件。
第五篇:使用java輸入輸出流實現文件的復制:
public class Test {
public static void copy(File source,File target){
InputStream in=null;
OutputStream out=null;
try {
in=new BufferedInputStream(new FileInputStream(source));out=new BufferedOutputStream(new FileOutputStream(target));byte[] buff=new byte[1024];
while(in.read(buff)>0){
out.write(buff);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
if(in!=null)
in.close();
if(out!=null)
out.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}