今天预习了一些知识,用它完成了一个系统项目。
首先是定义了一些属性和方法的会员类!
public class Member {
private String name;
private String cardid="";
private String cardpassword="";
private int point;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Member() {
}
public Member(String name, String cardId, String cardPassword, int point) {
this.name = name;
cardid = cardId;
cardpassword = cardPassword;
this.point = point;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCardId() {
return cardid;
}
public void setCardId(String cardId) {
cardid = cardId;
}
public String getCardPassword() {
return cardpassword;
}
public void setCardPassword(String cardPassword) {
cardpassword = cardPassword;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
@Override
public String toString() {
return "您的个人信息为:\n" +
"姓名='" + name + '\'' +
", 会员卡号='" + cardid + '\'' +
", 会员卡密码='" + cardpassword + '\'' +
", 积分=" + point +
", 消费额度=" + money +
".";
}
}
然后是主要执行的超市类!
import java.util.*;
public class SuperMarket {
Scanner input = new Scanner(System.in);
// Member member = new Member();
List<Member> memberList = new ArrayList<>();
public void welcome(){
System.out.println("**********************************欢迎来到XX超市!*" +
"*********************************");
System.out.println("1、积分累计\t2、积分兑换\t3、查询剩余积分\t" +
"4、修改密码\t5、开卡\t6、个人信息\t7、退出");
System.out.println("********************************************" +
"***************************************");
System.out.print("请选择:");
}
public void opencard(){
Member member = new Member();
boolean b1=false;
do {
System.out.print("请输入姓名:");
String str = input.next();
member.setName(str);
System.out.print("请输入密码:");
String str2=input.next();
if(str2.length()<6||str2.length()>12){
System.out.println("输入错误,密码在6到12位之间");
continue;
}
member.setCardPassword(str2);
System.out.print("请再次输入密码:");
if (member.getCardPassword().equals(input.next())) {
System.out.println("开卡成功!");
Random CardId=new Random();
int cardid = CardId.nextInt(89999999)+10000000;
System.out.println("您的会员卡号为:"+cardid);
member.setCardId(cardid+"");
member.setPoint(member.getPoint()+100);
System.out.println("赠送您100积分!");
memberList.add(member);
break;
} else {
System.out.println("密码输入错误!请重新输入!");
b1=true;
}
}while (b1);
}
public void showmoney(){
// System.out.print("您今天的消费了多少:");
// member.setMoney(member.getMoney()+input.nextDouble());
// System.out.println("剩余消费额度为:"+member.getMoney());
}
public boolean addpoint(){
System.out.print("请输入您的卡号:");
String st1=input.next();
System.out.print("请输入您的密码:");
String st2=input.next();
Member member1=null;
boolean b=false;
for (int i = 0; i <memberList.size() ; i++) {
if(st1.equals(memberList.get(i).getCardId())&&st2.equals(memberList.get(i).getCardPassword())){
member1 = memberList.get(i);
b=true;
break;
}
}
if(!b){
System.out.println("卡号和密码输入错误!");
return false;
}
System.out.print("您今天的消费额为多少:");
double v = member1.getMoney() + input.nextInt();
System.out.print("每10点剩余消费额度可累计1点积分,是否继续累计(y/n):");
if(input.next().equals("y")){
member1.setMoney(v);
member1.setPoint((int) (member1.getMoney() * 0.1 + member1.getPoint()));
System.out.println("您获得:"+member1.getMoney()+"点积分!");
return true;
}else{
return false;
}
}
public void menu(){
System.out.println("***********************************");
System.out.println("1、水果(100)\t2、服饰(400)\t" +
"3、玩具(300)\t4、神秘礼品(500)");
System.out.println("***********************************");
System.out.print("请选择:");
}
public boolean usepoint(){
int i=input.nextInt();
System.out.print("请输入您的卡号:");
String st1=input.next();
System.out.print("请输入您的密码:");
String st2=input.next();
Member member1=null;
for (int j = 0; j <memberList.size() ; j++) {
if(st1.equals(memberList.get(j).getCardId())&&st2.equals(memberList.get(j).getCardPassword())){
member1 = memberList.get(j);
break;
}else{
return false;
}
}
boolean b=true;
switch (i){
case 1:
if(member1.getPoint()-100<0){
System.out.println("积分不足!");
b=false;
break;
}
member1.setPoint(member1.getPoint()-100);
break;
case 2:
if(member1.getPoint()-400<0){
System.out.println("积分不足!");
b=false;
break;
}
member1.setPoint(member1.getPoint()-400);
break;
case 3:
if(member1.getPoint()-300<0){
System.out.println("积分不足!");
b=false;
break;
}
member1.setPoint(member1.getPoint()-300);
break;
case 4:
if(member1.getPoint()-500<0){
System.out.println("积分不足!");
b=false;
break;
}
member1.setPoint(member1.getPoint()-500);
break;
default:
System.out.println("操作错误,已退出!");
b=false;
break;
}
return b;
}
public void showpoint(){
System.out.print("请输入您的卡号:");
String st1=input.next();
System.out.print("请输入您的密码:");
String st2=input.next();
Member member1=null;
boolean b=false;
for (int i = 0; i <memberList.size() ; i++) {
if(st1.equals(memberList.get(i).getCardId())&&st2.equals(memberList.get(i).getCardPassword())){
member1 = memberList.get(i);
b=true;
break;
}
}
if(!b){
System.out.println("卡号和密码输入错误!");
return;
}
System.out.println("您现在的积分为:"+member1.getPoint());
}
public void recardpassword(){
System.out.print("请输入您的卡号:");
String st1=input.next();
System.out.print("请输入您的密码:");
String st2=input.next();
Member member1=null;
for (int i = 0; i <memberList.size() ; i++) {
if(st1.equals(memberList.get(i).getCardId())&&st2.equals(memberList.get(i).getCardPassword())){
member1 = memberList.get(i);
System.out.print("请输入新密码:");
String s1 = input.next();
if(s1.length()<6&&s1.length()>12){
System.out.println("输入错误!密码在6~12位之间!");
continue;
}
System.out.print("请再次输入新密码:");
if (s1.equals(input.next())) {
member1.setCardPassword(s1);
System.out.println("修改成功!");
break;
} else {
System.out.println("新密码输入不符合,请重试!");
continue;
}
}else{
System.out.println("密码输入错误,无法修改!");
}
}
}
public String showmessige(){
System.out.print("请输入您的卡号:");
String st1=input.next();
System.out.print("请输入您的密码:");
String st2=input.next();
Member member1=null;
boolean b=false;
for (int i = 0; i <memberList.size() ; i++) {
if(st1.equals(memberList.get(i).getCardId())&&st2.equals(memberList.get(i).getCardPassword())){
member1 = memberList.get(i);
b=true;
break;
}
}
if(!b){
System.out.println("卡号和密码输入错误!");
return "*";
}
return member1.toString();
}
public void start() {
boolean sign=true;
do {
welcome();
int num = input.nextInt();
if(num!=5&&num<7&&num>0){
if(memberList.size()==0){
System.out.println("请先开卡!");
continue;
}
}
switch (num) {
case 1:
if(addpoint()){
System.out.println("累计成功!");
}else{
System.err.println("累计失败!");
}
break;
case 2:
menu();
if(usepoint()){
System.out.println("兑换成功!");
}else{
System.err.println("兑换失败!");
}
break;
case 3:
showpoint();
break;
case 4:
recardpassword();
break;
case 5:
opencard();
break;
case 6:
String s=showmessige();
System.out.println(s);
break;
case 7:
System.out.println("退出成功!");
System.exit(1);
break;
default:
System.err.println("输入错误,请重试!");
}
}while (sign);
}
}
最后就是一个简单的测试类!
1 public class Test { 2 public static void main(String[] args) { 3 SuperMarket superMarket = new SuperMarket(); 4 superMarket.start(); 5 } 6 }
OK!完成,自己完成的就是很有成就感(*^▽^*)