用Map 集合存储会员卡
package shopsystem; public class Card { private String name;// 姓名 private String cardNum;// 卡号 private String code;// 密码 private String date;// 开卡日期 private int score;// 积分 @Override public String toString() { return "开卡人姓名:" + this.name + ", 卡号:" + this.cardNum + ", 剩余积分:" + this.score + ", 开卡日期:" + this.date; } public Card() { } public Card(String name, String cardNum, String code, String date, int score) { this.name = name; this.cardNum = cardNum; this.code = code; this.date = date; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCardNum() { return cardNum; } public void setCardNum(String cardNum) { this.cardNum = cardNum; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
Card
package shopsystem; import java.text.SimpleDateFormat; import java.util.*; public class Action { static Scanner input = new Scanner(System.in); // 生成随机卡号 public static String cardNum() { StringBuffer buf = new StringBuffer(); for (int i = 0; i < 10; ++i) { Random r = new Random(); buf.append(r.nextInt(9)); } return buf.toString(); } // 开卡 public static void register() { System.out.println("请输入注册人姓名:"); String name = input.next(); System.out.println("请输入注册密码:"); String code; for (code = input.next(); code.length() < 6; code = input.next()) { System.out.println("密码设置的位置不能小于6位,请重新输入:"); } Date date = new Date(); SimpleDateFormat sim = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String riqi = sim.format(date); String cardNum; do { cardNum = cardNum(); } while (ShopSys.map.containsKey(cardNum)); System.out.println("开卡成功,送您100积分!您的会员卡号是:" + cardNum); ShopSys.map.put(cardNum, new Card(name, cardNum, code, riqi, 100)); } // 累计积分 public static void addScore() { System.out.println("请输入卡号:"); String cardNum = input.next(); System.out.println("请输入密码:"); String code = input.next(); if (ShopSys.map.containsKey(cardNum)) { boolean boo = ShopSys.map.get(cardNum).getCode().equals(code); if (boo) { System.out.println("请输入您此次消费金额(每消费1元加1积分):"); int score = input.nextInt(); ShopSys.map.get(cardNum).setScore(ShopSys.map.get(cardNum).getScore() + score); System.out.println("积分累计成功,当前积分为:" + ShopSys.map.get(cardNum).getScore()); } else { System.out.println("您输入的密码错误,积分累计失败!!"); } } else { System.out.println("对不起,没有找到该卡号!!"); } } // 查询会员卡信息 public static void inquire() { System.out.println("请输入您的会员卡号:"); String cardNum = input.next(); System.out.println("请输入密码:"); String code = input.next(); if (ShopSys.map.containsKey(cardNum)) { boolean boo = ShopSys.map.get(cardNum).getCode().equals(code); if (boo) { System.out.println("您查询的会员卡信息如下:\n" + ShopSys.map.get(cardNum)); } else { System.out.println("您输入的密码错误,查询失败!!"); } } else { System.out.println("对不起,没有找到该卡号!!"); } } // 修改密码 public static void alterCode() { System.out.println("请输入会员卡号:"); String cardNum = input.next(); System.out.println("请输入密码:"); String code = input.next(); if (ShopSys.map.containsKey(cardNum)) { boolean boo = ShopSys.map.get(cardNum).getCode().equals(code); if (boo) { String newCode; System.out.println("请输入新密码:"); for (newCode = input.next(); newCode.length() < 6; newCode = input.next()) { System.out.println("密码设置的位置不能小于6位,请重新输入:"); } ShopSys.map.get(cardNum).setCode(newCode); System.out.println("您的会员卡密码修改成功!!!"); } else { System.out.println("您输入的密码错误,修改密码失败!!"); } } else { System.out.println("对不起,没有找到该卡号!!"); } } // 积分兑换 public static void conversion() { System.out.println("请输入会员卡号:"); String cardNum = input.next(); System.out.println("请输入密码:"); String code = input.next(); if (ShopSys.map.containsKey(cardNum)) { boolean boo = ShopSys.map.get(cardNum).getCode().equals(code); if (boo) { System.out.println("请输入您需要兑换使用的积分(每100积分抵用0.1元,不足100积分不做抵用):"); int num = input.nextInt(); if (num < 100) { System.out.println("兑换积分不能小于100!"); } else { if (ShopSys.map.get(cardNum).getScore() - num > 0) { ShopSys.map.get(cardNum).setScore(ShopSys.map.get(cardNum).getScore() - num / 100 * 100); System.out.println("积分兑换成功,您已用会员卡积分抵用了" + (double) (num / 100) * 1.0D + "元!"); return; } else { System.out.println("对不起,你当前积分不足!!!"); } } } else { System.out.println("您输入的密码错误,积分兑换失败!!"); } } else { System.out.println("对不起,没有找到该卡号!!"); } } }
系统相应的功能
package shopsystem; import java.util.*; public class ShopSys { static Map<String,Card> map = new HashMap<String, Card>(); static Scanner input = new Scanner(System.in); public static void operation() { System.out.println("******************************XX超市会员管理系统****************************************"); String select = ""; start1: do { System.out.println("1、积分累计\n2、积分兑换\n3、查询积分\n4、修改密码\n5、开卡\n6、退出"); System.out.println("请选择:"); String str = input.next(); switch (str) { case "1": Action.addScore(); break; case "2": Action.conversion(); break; case "3": Action.inquire(); break; case "4": Action.alterCode(); break; case "5": Action.register(); break; case "6": System.out.println("正在退出系统......."); break start1; } System.out.println("输入0返回,输入其它信息退出系统,请输入:"); select = input.next(); } while (select.equals("0")); System.out.println("程序退出!"); } //测试 public static void main(String[] args) { operation(); } }
超市管理系统
实际效果
开卡
输入错误卡号或密码的效果
累计积分
查询积分(有点小bug重新运行后继续)
积分兑换
修改密码
退出
原文地址:https://www.cnblogs.com/zeng1997/p/11254543.html
时间: 2024-11-06 03:39:51