Java实现购物管理系统
程序员文章站
2023-12-11 16:31:40
本文实例为大家分享了java实现购物管理系统的具体代码,供大家参考,具体内容如下
重点内容
java分支结构练习
动态数组
注册表读写删
代码:...
本文实例为大家分享了java实现购物管理系统的具体代码,供大家参考,具体内容如下
重点内容
java分支结构练习
动态数组
注册表读写删
代码:
package endless; import java.util.arraylist; import java.util.random; /** * @java01.person1.java * @author 当前用户:endless作者 :endless * @version 创建时间:2017年7月26日 下午6:29:16 * //实现商品及会员信息管理(添加,删除,显示) * //实现超市结算 */ import java.util.scanner; import java.util.prefs.backingstoreexception; import java.util.prefs.preferences; public class persion { static int s; static boolean huiyuan,xhuiyuan; static string bke="百事可乐",ke="500ml可乐一瓶",m="5公斤面粉",ss="1个苏泊尔炒菜锅",o="欧莱雅爽肤水一瓶"; static arraylist<string>array=new arraylist<string>(); static preferences goodsnodes = preferences.systemroot().node("goods"); //创建节点 static preferences goodschildnode = goodsnodes.node( "goods"); //创建子节点 public static void main(string []args) { outline(); system.out.println("\t欢迎使用购物管理系统"); system.out.println("\t1.登陆系统\n\t2.退出程序\n "); outline(); int n =scanint(); if(n == 1){ login(); }else if( n== 2){ system.out.println("已退出"); system.exit(0); }else { system.out.println("请重新选择:"); main(null); } } public static void outline(){ system.out.println("* * * * * * * * * * * * * * * * * * * * * * *\n"); } public static int scanint(){ int x = 0; try { scanner scan =new scanner(system.in); x = scan.nextint(); } catch (exception e) { system.out.println("输入有误,请输入数字:"); } return x; } public static string scanstr(){ string x = null; try { scanner scan =new scanner(system.in); x = scan.next(); } catch (exception e) { system.out.println("输入有误,请输入字符串:"); } return x; } public static void login(){ system.out.println(" * * * * * * * * * 登陆 * * * * * * * * * * * *\n请输入密码\n:"); int n1 =scanint(); if(n1==(123123)){ system.out.println("登陆成功\n"); showmain(); }else{ system.out.println("密码错误"); system.out.println("请重新输入密码\n:"); login(); } } private static void showmain() { system.out.println("\n\n\t欢迎使用购物管理系统\n"); outline(); system.out.println("\t\t 1. 会 员 信 息 管 理\n"); system.out.println("\t\t 2. 货 物 信 息 管 理\n"); system.out.println("\t\t 3. 购 物 结 算\n"); system.out.println("\t\t 4. 注 销\n"); outline(); system.out.print("请选择,输入数字:"); int n2 =scanint(); switch (n2) { case 1: new user(); break; case 2: goods(); break; case 3: jiesuan(); break; case 4: system.out.println("注销成功"); main(null); default: system.out.println("没有您要的选项请重新输入"); showmain(); break; } } private static void goods() { //商品管理 :添加商品: 编号 品名 价格 // 删除:选择标号删除 outline(); system.out.println("\t\t商品信息管理\n"); system.out.println("\t\t1,显示商品\n"); system.out.println("\t\t2,添加物品\n"); system.out.println("\t\t3,删除商品\n"); system.out.println("\t\t4,返回上级\n"); outline(); int n = scanint(); if (n==1){ showgoods(); }else if(n==2){ addgoods(); }else if(n ==3){ delgoods(); }else if(n==4) { showmain(); }else { goods(); } } private static void delgoods() { system.out.println("请选择要删除商品的id:\n按0返回上级"); try { scanner scan =new scanner(system.in); int x = scan.nextint(); boolean remove=false; for (int i=0;i<array.size();i++){ if(x==integer.parseint(array.get(i))){ remove=true; } } if (remove){ goodschildnode.remove(integer.tostring(x)); system.out.println("删除成功"); goods(); }else if (x==0) { goods(); }else { system.out.println("没有此商品!"); delgoods(); } new user(); } catch (exception e) { system.out.println("请输入数字"); delgoods(); } } private static void showgoods() { try { int leng =goodschildnode.keys().length; syncread(); if(leng==0){ system.out.println("没有商品"); }else{ try { system.out.println("当前有"+goodschildnode.keys().length+"种商品"); for(int i = 0;i<=goodschildnode.keys().length;i++){ string shows =goodschildnode.get(array.get(i), ""); system.out.println(shows); system.out.println("──────────────────────────────────────────────"); } system.out.println("00000000"); } catch (exception e) { } } } catch (backingstoreexception e) { } system.out.println("111111111"); goods(); } private static void syncread() { try { string[] arr= goodschildnode.keys(); goodschildnode.sync(); array.clear(); for(int i=0;i<arr.length;i++){ array.add(arr[i]); } } catch (exception e) { } } private static int check(int x) { random n=new random(); syncread(); for(int i=0;i<array.size();i++){ if(x==integer.parseint(array.get(i))){ check(n.nextint(999)+1); } } return x; } private static void addgoods(){ outline(); system.out.println("\t\t请输入商品信息"); system.out.println("\t\t品名,价格(元),单位"); system.out.println("\t\t回车换行区分"); outline(); syncread(); string name =scanstr(); string nuit =scanstr(); int price = scanint(); random n=new random(); string id ; id=integer.tostring(check(n.nextint(999)+1)); try{ goodschildnode.put(id, "id:"+id+" 品名:"+name+" 价格:"+price+" 单位:"+nuit); syncread(); system.out.println("第"+(goodschildnode.keys().length)+"种商品添加成功"); system.out.println("当前商品"+goodschildnode.keys().length+"种\n"+array+"\n"+array.size()); goods(); } catch (exception e) { goods(); } } static void jiesuan(){ try{ system.out.println("是否会员?是选1 否选0"); int x= scanint(); if (x==1){ huiyuan = true; scanner sc =new scanner(system.in); system.out.println("请输入您的会员号:"); int hui = sc.nextint()/100%10; if(hui==9){ system.out.println("恭喜你属于幸运会员,可以享受全场八折优惠"); xhuiyuan = true; }else{ xhuiyuan = false; } }else if (x==0) { huiyuan = false; } system.out.println("请输入消费金额:"); scanner scan =new scanner(system.in); if(scan.hasnext()){ s=scan.nextint(); outline(); system.out.println("是否参加优惠换购活动:" ); system.out.println("1:满50元,加2元换购"+bke ); system.out.println("2:满100元,加3元换购" +ke); system.out.println("3:满100元,加10元换购" +m ); system.out.println("4:满200元,加10元换购" +ss ); system.out.println("5:满200元,加20元换购 " +o ); system.out.println("0:不换购\n请选择:\n" ); outline(); os(); } } catch (exception e) { system.out.println("请输入数字"); jiesuan(); } } static void out(string n , double s){ if(!(n==null)){ system.out.println("成功换购"+n); } system.out.println("本次消费总金额:"+s+"元"); if (huiyuan && xhuiyuan){ system.out.println("尊敬的幸运会员,本次消费享受8折优惠"); double x =s*0.8; system.out.println("折扣为您节省:"+(s-x)+"元"); }else if (huiyuan) { system.out.println("尊敬的会员,本次消费享受9折优惠"); double x =s*0.9; system.out.println("折扣为您节省:"+(s-x)+"元"); } system.out.println("实际消费总金额:"+s+"元"); showmain(); } static void os(){ switch (scanint()) { case 0: out(null,s); case 1: if(s>=52){ out(bke,s+2); }else{ err(); } case 2: if(s>=103){ out(ke,s+3); }else{ err(); } break; case 3: if(s>=110){ out(m,s+10); }else{ err(); } break; case 4: if(s>=210){ out(ss,s+10); }else{ err(); } break; case 5: if(s>=220){ out(o,s+20); }else{ err(); } break; default: system.out.println("请重新选择"); os(); } } static void err(){ system.out.println("金额不足\n"); main(null); } static class user{ static arraylist<string>array=new arraylist<>(); static preferences nodes = preferences.systemroot().node("user"); //创建节点 static preferences childnode = nodes.node( "user"); //创建子节点 public user() { outline(); system.out.println("\t\t1,添加用户"); system.out.println("\t\t2,删除用户"); system.out.println("\t\t3,显示用户"); system.out.println("\t\t4,返回上级"); outline(); try { int x=scanint(); switch (x) { case 1: add(); break; case 2: deluser(); break; case 3: show(); break; case 4: showmain(); break; default: system.out.println("没有您要的选项!请重新选择"); new user(); break; } } catch (exception e) { new user(); } } private void deluser() throws backingstoreexception { system.out.println("请选择要删除用户的id:\n按0返回上级"); try { scanner scan =new scanner(system.in); int x = scan.nextint(); boolean remove=false; for (int i=0;i<array.size();i++){ if(x==integer.parseint(array.get(i))){ remove=true; } } if (remove){ childnode.remove(integer.tostring(x)); system.out.println("删除成功"); }else if (x==0) { new user(); }else { system.out.println("没有此用户!"); deluser(); } new user(); } catch (exception e) { system.out.println("请输入数字"); deluser(); } } private static void show() { try { int leng =childnode.keys().length; syncread(); if(leng==0){ system.out.println("没有用户"); }else{ system.out.println("当前有"+childnode.keys().length+"位用户"); for(int i = 0;i<=childnode.keys().length;i++){ string shows =childnode.get(childnode.keys()[i], ""); system.out.println(shows); system.out.println("──────────────────────────────────────────────"); } } } catch (backingstoreexception e) { } new user(); } private static void syncread() { try { string[] arr= childnode.keys(); childnode.sync(); array.clear(); for(int i=0;i<arr.length;i++){ array.add(arr[i]); } } catch (exception e) { } } private static void add(){ preferences nodes = preferences.systemroot().node("user"); //创建节点 preferences childnode = nodes.node( "user"); //创建子节点 outline(); system.out.println("\t\t请输入用户信息"); system.out.println("\t\t姓名,6位卡号,年龄,电话"); system.out.println("\t\t回车换行区分"); outline(); syncread(); string name =scanstr(); int card = scanint(); int age =scanint(); string tel =scanstr(); random n=new random(); string id ; id = integer.tostring(n.nextint(999)+1); try{ childnode.put(id, "id:"+id+" 姓名:"+name+" 卡号:"+card+" 年龄:"+age+" 电话:"+tel); syncread(); system.out.println("第"+(childnode.keys().length)+"位会员添加成功"); system.out.println("当前用户"+childnode.keys().length+"位\n"+array+"\n"+array.size()); new user(); } catch (exception e) { new user(); } } } }
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。