当前位置:文档之家› 基于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("\t\t\t欢迎使用商超购物管理系统\n");System.out.println("\t\t\t\t1.登录系统\n");System.out.println("\t\t\t\t2.退出 ");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("\t\t\t\t商超购物管理系统");System.out.println("********************************************** **********************************");System.out.println("\t\t\t\t1.商品维护");System.out.println("\t\t\t\t2.前台收银");System.out.println("\t\t\t\t3.商品管理");System.out.println("\t\t\t\t4.退出");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("\t\t\t\t商超购物管理系统商品维护");System.out.println("********************************************** **********************************");System.out.println("\t\t\t\t1.添加商品\t");System.out.println("\t\t\t\t2.更改商品\t");System.out.println("\t\t\t\t3.删除商品\t");System.out.println("\t\t\t\t4.显示所有商品\t");System.out.println("\t\t\t\t5.查询商品\t");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("商品名称\t\t\t" + "商品价格\t\t\t" + "商品数量\t\t\t"+ "备注");showgoods();break;case 5:System.out.println("执行查询商品操作");loadmenu5();break;default:break;}}/** 在商超购物管理系统中按2键时*/static void loadmenu6() {System.out.println("\t\t\t\t商超购物管理系统前台收银");System.out.println("********************************************** **********************************");System.out.println("\t\t\t\t1.购物结算");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("\t\t\t商超购物管理系统商品管理");System.out.println("********************************************** **********************************");System.out.println("\t\t\t\t1.库存查询");System.out.println("\t\t\t\t2.已出售商品");System.out.println("\t\t\t\t3.售货员管理");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++;//新信息添加后数组下标加1writeArray();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("员工姓名" + "\t\t\t密码");System.out.println(cname[k] + "\t\t\t" + 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("商品名称\t\t\t" + "商品价格\t\t\t" + "商品数量\t\t\t" + "备注");for (int i = 0; i < goodout.length; i++) {if (goodout[i] != 0) {goodprice[i]+ "\t\t\t" + 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("商品名称\t\t\t" + "商品价格\t\t\t" + "商品数量\t\t\t"+ "备注");goodprice[t]+ "\t\t\t" + goodnum[t] + "\t\t\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 + "\t\t\t" + goodprice[fl] +"\t\t\t"+ "购买数量" + bnum + "\t\t\t" + bnum * goodprice[fl]);goodnum[fl] = goodnum[fl] - bnum;//商品的数量要进行更新goodout[fl] = goodout[fl] + bnum;//商品的销售量相应的增加sum = sum + bnum * goodprice[fl];//计算购买商品的金额System.out.println("是否继续(y/n)");f = scan.next();} while (f.equals("y"));System.out.println("总计:" + sum);System.out.println("请输入实际交费金额:");double shipay = scan.nextDouble();System.out.println("找钱:" + (shipay - 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("商品名称\t\t\t" + "商品价格\t\t\t" + "商品数量\t\t\t" + "备注");if (k != null) {for (int i = 0; i < k.length; i++) {System.out.println(goodname[k[i]] + "\t\t\t" + goodprice[k[i]]+ "\t\t\t" + goodnum[k[i]] + "\t\t\t");}}}/** 按商品的关键字进行查找*/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("商品名称\t\t\t" + "商品价格\t\t\t" + "商品数量\t\t\t"+ "备注");if (k != null) {for (int i = 0; i < k.length; i++) {System.out.println(goodname[k[i]] + "\t\t\t"+ goodprice[k[i]] + "\t\t\t" + goodnum[k[i]]+ "\t\t\t");}}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[] newname = Arrays.copyOf(goodname, goodname.length);for (int i = 0; i < newnum.length - 1; i++) {for (int j = 0; j < newnum.length - i - 1; j++) { if (newnum[j] < newnum[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;//相应的下标在不同的数组里进行挑换,商品的信息才能保持一致}}}System.out.println("商品名称\t\t\t" + "商品价格\t\t\t" + "商品数量\t\t\t" + "备注");for (int k = 0; k < newnum.length; k++) {if (newprice[k] != 0) {System.out.println(newname[k] + "\t\t\t" + newprice[k]+ "\t\t\t" + newnum[k] + "\t\t\t");}}loadmenu5();}/** 按价格的升序进行查找*/static void priceshenxu() {int[] newnum = Arrays.copyOf(goodnum, goodnum.length);double[] newprice = Arrays.copyOf(goodprice, goodprice.length);String[] newname = Arrays.copyOf(goodname, goodname.length);for (int i = 0; i < newprice.length - 1; i++) {for (int j = 0; j < newprice.length - i - 1; 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;//相应的下标在不同的数组里进行挑换,商品的信息才能保持一致}}}System.out.println("商品名称\t\t\t" + "商品价格\t\t\t" + "商品数量\t\t\t" + "备注");for (int k = 0; k < newnum.length; k++) {if (newprice[k] != 0) {System.out.println(newname[k] + "\t\t\t" + newprice[k]+ "\t\t\t" + newnum[k] + "\t\t\t");}}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("商品名称\t\t" + "商品价格 \t\t" + "商品数量 \t\t" + "备注");System.out.println(goodname[b] + "\t\t" + goodprice[b]+ goodnum[b] + " \t\t");}/** 删除商品菜单*/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.length - 1; i++) { goodname[i] = goodname[i + 1];goodprice[i] = goodprice[i + 1];goodnum[i] = goodnum[i + 1];}goodname[goodname.length - 1] = null;goodprice[goodprice.length - 1] = 0;goodnum[goodnum.length - 1] = 0;}/** 查询所有商品*/static void showgoods() {int i = 0;while (goodprice[i] != 0) {System.out.print(goodname[i] + "\t\t\t" + goodprice[i] + "\t\t\t"+ goodnum[i] + "\t\t\t");if (goodnum[i] < 10) {System.out.println("\t\t\t*该商品已不足10件!");}System.out.println();i++;}loadmenu2();}/** 商品添加操作*/static void addgoods() {System.out.println("执行添加商品操作:\t");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 = (String[]) Arrays.copyOf(arr, arr.length);for (int i = 0; i < goodname.length; i++) {if (!goodname[i].equals("null")) {sign++;}}} else if ("goodprice".equals(name)) {br = new BufferedReader(new FileReader("d:\\gprice.txt"));String content2 = br.readLine();double[] arr = doubleToString(content2);goodprice = (double[]) Arrays.copyOf(arr, arr.length);} else if ("goodnum".equals(name)) {br = new BufferedReader(new 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 = (String[]) Arrays.copyOf(arr, arr.length);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 = (String[]) Arrays.copyOf(arr, arr.length);} else {br = new BufferedReader(new 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<arry.length;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(" ", "");。

相关主题