package shopping;
import java.util.Date;
public class Customer {
private int id;
private String birthday;
private int score;
public Customer() {
super();
// TODO Auto-generated constructor stub
}
public Customer(int id, String birthday, int score) {
super();
this.id = id;
this.birthday = birthday;
this.score = score;
}
@Override
public String toString() {
return "Customer [id=" + id + ", birthday=" + birthday + ", score=" + score + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
if (id != other.id)
return false;
return true;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
package shopping;
public class Goods {
private int id;
private String name;
private double price;
private int num;
public Goods() {
super();
// TODO Auto-generated constructor stub
}
public Goods(int id, String name, double price, int num) {
super();
this.id = id;
this.name = name;
this.price = price;
this.num = num;
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", price=" + price + ", num=" + num + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Goods other = (Goods) obj;
if (id != other.id)
return false;
return true;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
package shopping;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class GoodsManager {
private Map<Integer, Goods> map = new HashMap<Integer, Goods>();
private Map<Integer, Goods> map0 = new HashMap<Integer, Goods>();
public void init() {
map.put(1, new Goods(1,"addidas运动鞋",880,0));
map.put(2, new Goods(2,"Kappa网球裙",100,0));
map.put(3, new Goods(3,"网球拍",990,0));
map.put(4, new Goods(4,"addidasT恤",400,0));
map.put(5, new Goods(5,"Nike运动鞋",900,0));
map.put(6, new Goods(6,"Kappa网球",200,0));
map.put(7, new Goods(7,"KappaT恤",300,0));
}
public Collection<Goods> showall() {
return map.values();
}
public void add(int id,int num) {
map0.put(id, new Goods(id,map.get(id).getName(),map.get(id).getPrice(),num));
}
public Collection<Goods> showbuy() {
return map0.values();
}
}
package shopping;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;
public class RunProgram {
private static Scanner input = new Scanner(System.in);
private static ShoppingManager sm = new ShoppingManager();
private static GoodsManager gm = new GoodsManager();
public static void main(String[] args) {
sm.init();
gm.init();
String user;
String password;
String user0 = "manager";
String password0 = "0000";
int n;
while(true) {
System.out.println("\n\t欢迎使用我行我素购物管理系统1.0版\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.println("\t1. 登 录 系 统\n");
System.out.println("\t2. 更 改 管 理 员 密 码\n");
System.out.println("\t3. 退 出\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.print("请选择,输入数字:");
n= input.nextInt();
switch(n) {
case 1:
int x=3;
do {
System.out.print("请输入用户名:");
user = input.next();
System.out.print("请输入密码:");
password = input.next();
if(user.equals(user0) && password.equals(password0)) {
while(true) {
System.out.println("\t欢迎使用我行我素购物管理系统\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.println("\t1. 客 户 信 息 管 理\n");
System.out.println("\t2. 购 物 结 算\n");
System.out.println("\t3. 真 情 回 馈\n");
System.out.println("\t4. 注 销\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.print("请选择,输入数字:");
n= input.nextInt();
switch(n) {
case 1:
while(true) {
System.out.println("我行我素购物管理系统 > 客户信息管理\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.println("\t1. 显 示 所 有 客 户 信 息\n");
System.out.println("\t2. 添 加 客 户 信 息\n");
System.out.println("\t3. 修 改 客 户 信 息\n");
System.out.println("\t4. 查 询 客 户 信 息\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.print("请选择,输入数字或按'n'返回上一级菜单:");
String s = input.next();
switch(s) {
case "n":
break;
case "1":
show();
if(input.next().equals("n")) {
continue;
}else {
break;
}
case "2":
add();
if(input.next().equals("n")) {
continue;
}else {
break;
}
case "3":
update();
if(input.next().equals("n")) {
continue;
}else {
break;
}
case "4":
find();
if(input.next().equals("n")) {
continue;
}else {
break;
}
}
break;
}
continue;
case 2:
accounts();
if(input.next().equals("n")) {
continue;
}else {
break;
}
case 3:
System.out.println("我行我素购物管理系统 > 真情回馈\n");
while(true) {
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.println("\t1. 幸 运 大 放 送\n");
System.out.println("\t2. 幸 运 抽 奖\n");
System.out.println("\t3. 生 日 问 候\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.print("请选择,输入数字或按'n'返回上一级菜单:");
String s = input.next();
switch(s) {
case "n":
break;
case "1":
max();
if(input.next().equals("n")) {
continue;
}else {
break;
}
case "2":
System.out.println("我行我素购物管理系统 > 幸运抽奖\n");
System.out.println("是否开始(y/n):");
if(input.next().equals("y")) {
System.out.println("幸运客户获赠MP3:1711");
System.out.println("请按'n'返回上一级菜单:");
if(input.next().equals("n")) {
continue;
}else {
break;
}
}else {
System.out.println("请按'n'返回上一级菜单:");
if(input.next().equals("n")) {
continue;
}else {
break;
}
}
case "3":
birthday();
if(input.next().equals("n")) {
continue;
}else {
break;
}
}
break;
}
continue;
case 4:
break;
}
break;
}
break;
}else {
System.out.println("输入错误!您还有"+(--x)+"次机会!\n");
}
}while(x>0);
if(x==0) {
System.out.println("对不起!您3次均输入错误!");
break;
}
continue;
case 2:
System.out.print("请输入用户名:");
user = input.next();
System.out.print("请输入密码:");
password = input.next();
if(user.equals("manager") && password.equals("0000")) {
System.out.print("请输入新的用户名:");
user = input.next();
user0 = user;
System.out.print("请输入新的密码:");
password = input.next();
password0 = password;
System.out.print("用户名和密码已更改!");
}else {
System.out.print("抱歉,你没有权限修改!");
}
continue;
case 3:
System.out.println("\n谢谢您的使用!");
break;
default:
break;
}
break;
}
}
private static void birthday() {
System.out.println("我行我素购物管理系统 > 生日问候\n");
System.out.print("请输入今天的日期(月/日<用两位表示>):");
String date = input.next();
Collection<Customer> s = sm.show();
boolean b = true;
for(Customer c : s) {
if(c.getBirthday().equals(date)) {
System.out.println("今天是"+c.getId()+"会员的生日,生日快乐!");
b = false;
}
}
if(b) {
System.out.println("今天没有过生日的会员!");
}
System.out.println("\n请按'n'返回上一级菜单:");
}
private static void max() {
System.out.println("我行我素购物管理系统 > 幸运大放送\n");
Collection<Customer> c = sm.show();
Customer cus = Collections.max(c,new ScoreComparator());
System.out.println("具有最高积分的会员是:"+cus.getId()+"\t"+cus.getBirthday()+"\t"+cus.getScore());
System.out.println("恭喜!获赠礼品: 一个价值¥12000.0的苹果笔记本电脑\n");
System.out.print("请按'n'返回上一级菜单:");
}
private static void accounts() {
System.out.println("我行我素购物管理系统 > 购物结算\n");
System.out.println("*************************************");
System.out.println("请选择购买的商品编号:");
Collection<Goods> c = gm.showall();
for(Goods g : c) {
System.out.println(g.getId()+":"+g.getName());
}
System.out.println("*************************************\n");
System.out.print("请输入会员号:");
int id = input.nextInt();
Customer customer = sm.find(id);
if(c==null) {
System.out.println("此会员号不存在");
}else {
while(true) {
System.out.print("请输入商品编号:");
int gid = input.nextInt();
System.out.print("请输入数目:");
int number = input.nextInt();
gm.add(gid, number);
System.out.print("是否继续(y/n)");
if(input.next().equals("y")) {
continue;
}else {
break;
}
}
System.out.println("\n*****************消费清单*********************");
System.out.println("物品\t单价\t个数\t金额\n");
Collection<Goods> c0 = gm.showbuy();
double sum = 0;
for(Goods g : c0) {
System.out.println(g.getName()+"\t¥"+g.getPrice()+"\t"+g.getNum()+"\t¥"+g.getNum()*g.getPrice());
sum += g.getNum()*g.getPrice();
}
double discount = 0.85;
System.out.println("折扣:"+discount);
System.out.println("金额总计:¥"+sum*discount);
System.out.print("请输入实际交费金额:");
double actual = input.nextDouble();
System.out.println("实际交费:¥"+actual);
System.out.println("找钱:¥"+(actual-sum*discount));
int score = 66;
System.out.println("本次购物所获的积分是:"+score+"\n");
sm.update2(id, score);
}
System.out.println("请按'n'返回上一级菜单:");
}
private static void find() {
System.out.println("我行我素购物管理系统 > 客户信息管理 > 查询客户信息\n");
System.out.print("请输入会员号:");
int id = input.nextInt();
Customer c = sm.find(id);
if(c==null) {
System.out.println("此会员号不存在");
}else {
System.out.println("会员号\t生日\t积分");
System.out.println("------------|------------|---------------");
System.out.println(c.getId()+"\t"+c.getBirthday()+"\t"+c.getScore());
}
System.out.println("\n要继续查询吗(y/n):");
if(input.next().equals("y")) {
find();
}else {
System.out.print("请按'n'返回上一级菜单:");
}
}
private static void update() {
System.out.println("我行我素购物管理系统 > 客户信息管理 > 修改客户信息\n");
System.out.print("请输入会员号:");
int id = input.nextInt();
Customer c = sm.find(id);
if(c==null) {
System.out.println("此会员号不存在");
}else {
System.out.println("会员号\t生日\t积分");
System.out.println("------------|------------|---------------");
System.out.println(c.getId()+"\t"+c.getBirthday()+"\t"+c.getScore());
}
while(true) {
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.println("\t1.修 改 会 员 生 日.\n");
System.out.println("\t2.修 改 会 员 积 分.\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.print("请选择,输入数字:");
int i = input.nextInt();
switch(i) {
case 1:
System.out.print("请输入修改后的生日:");
String birthday = input.next();
sm.update1(id, birthday);
System.out.println("生日信息已更改!");
System.out.println("是否修改其他属性(y/n):");
if(input.next().equals("y")) {
continue;
}else {
System.out.print("\n请按'n'返回上一级菜单:");
break;
}
case 2:
System.out.print("请输入修改后的会员积分:");
int score = input.nextInt();
sm.update2(id, score);
System.out.println("会员积分已更改!");
System.out.println("是否修改其他属性(y/n):");
if(input.next().equals("y")) {
continue;
}else {
System.out.print("\n请按'n'返回上一级菜单:");
break;
}
default:
System.out.println("输入错误");
break;
}
break;
}
}
private static void add() {
System.out.println("我行我素购物管理系统 > 客户信息管理 > 添加客户信息\n");
System.out.print("请输入会员号(<4位整数>):");
int id = input.nextInt();
System.out.print("请输入会员生日(月/日<用两位数表示>):");
String birthday = input.next();
System.out.print("请输入积分:");
int score = input.nextInt();
sm.add(id, birthday, score);
System.out.println("新会员添加成功!");
System.out.println("继续添加会员吗?(y/n)");
if(input.next().equals("y")) {
add();
}else {
System.out.print("请按'n'返回上一级菜单:");
}
}
private static void show() {
System.out.println("我行我素购物管理系统 > 客户信息管理 > 显示客户信息\n");
System.out.println("会员号\t生日\t积分");
System.out.println("------------|------------|---------------");
Collection<Customer> collection = sm.show();
for(Customer customer : collection) {
System.out.println(customer.getId()+"\t"+customer.getBirthday()+"\t"+customer.getScore());
}
System.out.print("\n请按'n'返回上一级菜单:");
}
}
package shopping;
import java.util.Comparator;
public class ScoreComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Customer c1 = (Customer)o1;
Customer c2 = (Customer)o2;
return c1.getScore()-c2.getScore();
}
}
package shopping;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class ShoppingManager {
private Map<Integer, Customer> map = new HashMap<Integer, Customer>();
public void add(int id,String birthday,int score) {
Customer customer = new Customer(id,birthday,score);
map.put(id, customer);
}
public void init() {
map.put(1900, new Customer(1900,"08/05",2000));
map.put(1711, new Customer(1711,"07/13",4000));
map.put(1623, new Customer(1623,"06/26",5000));
map.put(1545, new Customer(1545,"04/08",2200));
map.put(1464, new Customer(1464,"08/16",1000));
map.put(1372, new Customer(1372,"12/23",3000));
map.put(1286, new Customer(1286,"12/21",10080));
}
public Collection<Customer> show() {
return map.values();
}
public void update1(int id,String birthday) {
map.put(id, new Customer(id,birthday,map.get(id).getScore()));
}
public void update2(int id,int score) {
map.put(id, new Customer(id,map.get(id).getBirthday(),score));
}
public Customer find(int id) {
if(map.containsKey(id)) {
return map.get(id);
}else {
return null;
}
}
}