代码实现如下:运行结果请自行查看
package com.biu.demo01;
public class MarketVipMgr {
//姓名 会员卡号 密码 会员积分 开卡日期
private String name;
private int cardId;
private String pwd;
private int score;
private String date;
@Override
public String toString() {
return "MarketVipMgr [name=" + name + ", cardId=" + cardId + ", pwd=" + pwd + ", score=" + score + ", date="
+ date + "]";
}
public MarketVipMgr() {
super();
}
public MarketVipMgr(String name, int cardId, String pwd, int score, String date) {
super();
this.name = name;
this.cardId = cardId;
this.pwd = pwd;
this.score = score;
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCardId() {
return cardId;
}
public void setCardId(int cardId) {
this.cardId = cardId;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
package com.biu.demo01;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
public class MarketVipMgrDao {
Scanner sc = new Scanner(System.in);
ArrayList<MarketVipMgr> vipList = new ArrayList<MarketVipMgr>();
//初始化
public void init() {
MarketVipMgr vip1 = new MarketVipMgr("张三",16542135,"123123",6666,"2019-09-19");
MarketVipMgr vip2 = new MarketVipMgr("李四",65411231,"123123",6666,"2019-09-19");
MarketVipMgr vip3 = new MarketVipMgr("王五",23156423,"123123",6666,"2019-09-19");
vipList.add(vip1);
vipList.add(vip2);
vipList.add(vip3);
}
//菜单
public void menu() {
System.out.println("*************************欢迎进入超市会员管理系统*************************");
System.out.println("1.开卡\n2.积分累积\n3.积分兑换\n4.查询剩余积分\n5.修改密码\n6.销卡\n7.查看会员信息\n0.退出");
System.out.println("*******************************************************************");
System.out.print("请选择:");
}
//程序开始
public void start() {
menu();
int choose = sc.nextInt();
switch (choose) {
case 1:
//1.开卡
addCard();
break;
case 2:
//2.积分累积
scoreAdd();
break;
case 3:
//3.积分兑换
scoreConvert();
break;
case 4:
//4.查询剩余积分
queryScore();
break;
case 5:
//5.修改密码
updatePwd();
break;
case 6:
//6.销卡
delCard();
break;
case 7:
//7.查看会员信息
showInfo();
break;
case 0:
//0.退出
System.out.println("您已成功退出~");
return;
default:
System.out.println("您的输入有误~");
break;
}
System.out.println();
start();
}
//1.开卡
public void addCard() {
MarketVipMgr vip = new MarketVipMgr();
System.out.print("请输入注册姓名:");
String name = sc.next();
vip.setName(name);
Random rd = new Random();
int cardId = rd.nextInt(99999999);
for (MarketVipMgr marketVipMgr : vipList) {
if(marketVipMgr.getCardId() == cardId) {
cardId = rd.nextInt(99999999);
}
}
vip.setCardId(cardId);
System.out.print("请输入注册密码:");
String pwd = "";
boolean flag = false;
do {
pwd = sc.next();
if(pwd.length()<6) {
System.out.println("会员密码不能小于6位!请重新输入密码:");
}else {
vip.setPwd(pwd);
flag = true;
}
} while (!flag);
vip.setScore(100);//开卡送100分
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String fdate = format.format(date);
vip.setDate(fdate);
System.out.println("恭喜您开卡成功!系统送您100积分,您的会员卡号是:" + cardId + ", 开卡日期为: " + vip.getDate());
vipList.add(vip);
}
//2.积分累积
public void scoreAdd() {
boolean flag = false;
System.out.print("请输入会员卡号:");
int cardId = sc.nextInt();
System.out.print("请输入会员卡密码:");
String pwd = sc.next();
for (MarketVipMgr marketVipMgr : vipList) {
if(marketVipMgr != null && cardId == marketVipMgr.getCardId() && pwd.equals(marketVipMgr.getPwd())) {
flag = true;
System.out.print("请输入您的消费金额(一元一分):");
int score = sc.nextInt();
marketVipMgr.setScore(score + marketVipMgr.getScore());
System.out.println("积分累积成功~");
break;
}
}
if(!flag) {
System.out.println("您的卡号或密码有误~");
}
}
//3.积分兑换
public void scoreConvert() {
boolean flag = false;
System.out.print("请输入会员卡号:");
int cardId = sc.nextInt();
System.out.print("请输入会员卡密码:");
String pwd = sc.next();
for (MarketVipMgr marketVipMgr : vipList) {
if(marketVipMgr != null && cardId == marketVipMgr.getCardId() && pwd.equals(marketVipMgr.getPwd())) {
flag = true;
System.out.print("请输入您需要兑换使用的积分(100积分抵用0.1元,不足100的积分不做抵用):");
int score = sc.nextInt();
if(score <= marketVipMgr.getScore()) {
marketVipMgr.setScore(marketVipMgr.getScore() - score);
System.out.println("您使用会员积分兑换了:"+score/100*0.1+"元");
}else {
System.out.println("您的积分不足,无法兑换~");
}
break;
}
}
if(!flag) {
System.out.println("您的卡号或密码有误~");
}
}
//4.查询剩余积分
public void queryScore() {
boolean flag = false;
System.out.print("请输入会员卡号:");
int cardId = sc.nextInt();
System.out.print("请输入会员卡密码:");
String pwd = sc.next();
for (MarketVipMgr marketVipMgr : vipList) {
if(marketVipMgr != null && cardId == marketVipMgr.getCardId() && pwd.equals(marketVipMgr.getPwd())) {
flag = true;
System.out.println("您的剩余积分为:" + marketVipMgr.getScore());
break;
}
}
if(!flag) {
System.out.println("您的卡号或密码有误~");
}
}
//5.修改密码
public void updatePwd() {
boolean flag = false;
System.out.print("请输入会员卡号:");
int cardId = sc.nextInt();
System.out.print("请输入会员卡密码:");
String pwd = sc.next();
for (MarketVipMgr marketVipMgr : vipList) {
if(marketVipMgr != null && cardId == marketVipMgr.getCardId() && pwd.equals(marketVipMgr.getPwd())) {
flag = true;
boolean isSame = false;
do {
System.out.print("请输入新密码:");
String newPwd1 = sc.next();
System.out.print("请再次输入新密码:");
String newPwd2 = sc.next();
if(newPwd1.equals(newPwd2) && newPwd1.length()>=6) {
marketVipMgr.setPwd(newPwd1);
System.out.println("密码修改成功~");
isSame = true;
}else {
System.out.println("两次输入的密码不一致或长度不足6位,请重新输入~");
}
} while (!isSame);
break;
}
}
if(!flag) {
System.out.println("您的卡号或密码有误~");
}
}
//6.销卡
public void delCard() {
boolean flag = false;
System.out.print("请输入会员卡号:");
int cardId = sc.nextInt();
System.out.print("请输入会员卡密码:");
String pwd = sc.next();
for (MarketVipMgr marketVipMgr : vipList) {
if(marketVipMgr != null && cardId == marketVipMgr.getCardId() && pwd.equals(marketVipMgr.getPwd())) {
flag = true;
vipList.remove(marketVipMgr);
System.out.println("销卡成功~");
break;
}
}
if(!flag) {
System.out.println("您的卡号或密码有误~");
}
}
//7.查看会员信息
public void showInfo() {
//三种ArrayList的遍历方式
Iterator<MarketVipMgr> iterator = vipList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// for(int i=0; i<vipList.size(); i++) {
// System.out.println(vipList.get(i));
// }
// for (MarketVipMgr marketVipMgr : vipList) {
// System.out.println(marketVipMgr);
// }
}
}
package com.biu.demo01;
public class Test {
public static void main(String[] args) {
MarketVipMgrDao mgr = new MarketVipMgrDao();
mgr.init();
mgr.start();
}
}