62,616
社区成员
发帖
与我相关
我的任务
分享
pm = new PersonManage();
pm.add(new Person(personName, cardId, cardType, scoreAll, scorePerOne, cardDiscount));
pm.show();
这里你是想输出刚才添加的会员信息,应该是初始化一个 Person 对象,然后展示
public class PersonManage implements Operate<Person> {
private Person person=new Person(); // Person 类没有无参构造方法
public class PersonManage implements Operate<Person> {
private Person person=new Person();
private ArrayList<Person> pm ;
public PersonManage() {
super();
pm=new ArrayList<Person>();
}
public ArrayList<Person> getPm() {
return pm;
}
public void setPm(ArrayList<Person> pm) {
this.pm = pm;
}
@Override
public void add(Person person) {
pm.add(person);
}
@Override
public void delete(Person person) {
pm.remove(person);
}
@Override
public void change(Person person) {
pm.set(pm.indexOf(person), person);
}
@Override
public void show() {
person.toString();
}
}
购买和结算类:
public class BuyAndPay {
private GoodsManage gm;
private Goods goods;
private int amount;
private double money;
private String personId;
private PersonManage pm= new PersonManage();
private boolean flag;
HashMap<Goods, Integer> map = new HashMap<Goods, Integer>();
public void returnBuy() {
String choice = input.next();
while (choice.equals("y")) {
buy();
}
System.exit(0);
}
Scanner input = new Scanner(System.in);
public void buy() {
System.out.println("是否会员(y/n):");
String answer = input.next();
if(answer.equals("y")){
System.out.println("请输入会员卡编号:");
String personId= input.next();
flag=true;
}
System.out.println("请输入商品编号:");
String goodsId = input.next();
gm = new GoodsManage<Goods>();
for (int i = 0; i < gm.getGm().size(); i++) {
if (!goodsId.equals(((Goods) gm.getGm().get(i)).getGoodsId())) {
throw new IllegalArgumentException();
} else {
goods = (Goods) gm.getGm().get(i);
gm.show();
}
}
System.out.println("请输入购买数量:");
int amount = input.nextInt();
if (amount == 0) {
throw new IllegalArgumentException();
} else {
map.put(goods, amount);
}
System.out.println("是否继续购物(y/n):");
returnBuy();
}
public void pay(){
System.out.println("商品名称\t"+"商品数量\t"+"折后单价\t"+"金额\t");
Set<Entry<Goods, Integer>> entrys = map.entrySet();
for(Entry<Goods, Integer> entry:entrys){
double price=entry.getKey().getPrice()*entry.getKey().getPriceDiscount();
System.out.println(entry.getKey().getGoodsName()+"\t"+amount+"\t"+price+"\t"+price*amount);
money+=price*amount;
}
System.out.println("总金额:\t"+" \t"+" \t"+money);
double trueMoney=0;
double discount=0;
if(flag){
for (int i = 0; i < pm.getPm().size(); i++) {
if (personId.equals(pm.getPm().get(i).getCardId())) {
discount=pm.getPm().get(i).getDiscount();
trueMoney=discount*money;
break;
}
}
System.out.println("会员卡折扣:\t"+" \t"+" \t"+discount);
System.out.println("实际金额:\t"+" \t"+" \t"+trueMoney);
}
System.out.println("实际付款:");
double payMoney=input.nextDouble();
System.out.println("找零:"+(payMoney-trueMoney));
}
}
主控制系统类:
public class MainSystem {
Scanner input = new Scanner(System.in);
private PersonManage pm;
private GoodsManage<Goods> gm;
private BuyAndPay bap = new BuyAndPay();
public void returnStart() {
int num = input.nextInt();
while (num == 0) {
startSystem();
}
}
public void startSystem() {
System.out.println("欢迎使用收银系统");
System.out.println("------------------------------------");
System.out.println("1.添加会员信息");
System.out.println("2.添加商品信息");
System.out.println("3.查看会员信息");
System.out.println("4.查看商品信息");
System.out.println("5.开始购物");
System.out.println("6.结束购物并结算");
System.out.println("7.退出");
System.out.println("------------------------------------");
System.out.print("请选择:");
int choice = input.nextInt();
switch (choice) {
case 1 :
System.out.println("请输入会员姓名:");
String personName = input.next();
System.out.println("请输入会员卡编号:");
String cardId = input.next();
System.out.println("请输入会员卡类型:");
String cardType = input.next();
System.out.println("请输入会员卡总积分:");
int scoreAll = input.nextInt();
System.out.println("请输入会员卡每消费一块钱所得积分数:");
int scorePerOne = input.nextInt();
System.out.println("请输入会员卡享受折扣:");
double cardDiscount = input.nextDouble();
pm = new PersonManage();
pm.add(new Person(personName, cardId, cardType, scoreAll,
scorePerOne, cardDiscount));
pm.show();
System.out.print("输入0返回:");
returnStart();
case 2 :
System.out.println("请输入商品编号:");
String goodsId = input.next();
System.out.println("请输入商品名称:");
String goodsName = input.next();
System.out.println("请输入商品单价:");
double price = input.nextDouble();
System.out.println("请输入单价折扣:");
double priceDiscount = input.nextDouble();
gm = new GoodsManage<Goods>();
gm.add(new Goods(goodsId, goodsName, price, priceDiscount));
gm.show();
System.out.print("输入0返回:");
returnStart();
case 3 :
System.out.println("请输入会员卡编号:");
String personId = input.next();
for (int i = 0; i < pm.getPm().size(); i++) {
if (personId.equals(pm.getPm().get(i).getCardId())) {
pm.show();
}
}
System.out.print("输入0返回:");
returnStart();
case 4 :
System.out.println("请输入商品编号:");
String productId = input.next();
for (int i = 0; i < gm.getGm().size(); i++) {
if (productId.equals(gm.getGm().get(i).getGoodsId())) {
gm.show();
}
}
System.out.print("输入0返回:");
returnStart();
case 5 :
bap.buy();
System.out.print("输入0返回:");
returnStart();
case 6 :
bap.pay();
System.out.print("输入0返回:");
returnStart();
case 7 :
System.exit(0);
}
}
}
测试类就不贴了。还有好多细节需要完善的,但我首先需要的就是先运行起来,然后才能看有哪儿要完善的public class GoodsManage<Goods> implements Operate<Goods> {
private Goods goods;
private ArrayList<Goods> gm ;
public GoodsManage() {
super();
gm = new ArrayList<Goods>();
}
public ArrayList<Goods> getGm() {
return gm;
}
public void setGm(ArrayList<Goods> gm) {
this.gm = gm;
}
@Override
public void add(Goods person) {
gm.add(person);
}
@Override
public void delete(Goods person) {
gm.remove(person);
}
@Override
public void change(Goods person) {
gm.set(gm.indexOf(person), person);
}
@Override
public void show() {
goods.toString();
}
}
@Override
public void show() {
person.toString(); // 这个没有初始化
}
public interface Operate<T> {
public void add(T t);
public void delete(T t);
public void change(T t);
public void show();
}
public class Person {
private String personName;
private String cardId;
private String cardType;
private int scoreAll;//默认为0
private int scorePer;
private double cardDiscount;
public Person(String personName, String cardId, String cardType, int scoreAll,
int scorePer, double discount) {
super();
this.personName = personName;
this.cardId = cardId;
this.cardType = cardType;
this.scoreAll = scoreAll;
this.scorePer = scorePer;
this.cardDiscount = discount;
}
public String getName() {
return personName;
}
public void setName(String personName) {
this.personName = personName;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getCardType() {
return cardType;
}
public void setCardType(String cardType) {
this.cardType = cardType;
}
public int getScoreAll() {
return scoreAll;
}
public void setScoreAll(int scoreAll) {
this.scoreAll = scoreAll;
}
public int getScorePer() {
return scorePer;
}
public void setScorePer(int scorePer) {
this.scorePer = scorePer;
}
public double getDiscount() {
return cardDiscount;
}
public void setDiscount(double discount) {
this.cardDiscount = discount;
}
@Override
public String toString() {
return cardId+"\t"+personName+"\t"+scoreAll+"\t"+cardDiscount;
}
}
public class Goods {
private String goodsId;
private String goodsName;
private double price;
private double priceDiscount;
public Goods(String goodsId, String goodsName, double price,
double priceDiscount) {
super();
this.goodsId = goodsId;
this.goodsName = goodsName;
this.price = price;
this.priceDiscount = priceDiscount;
}
public String getGoodsId() {
return goodsId;
}
public void setGoodsId(String goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getPriceDiscount() {
return priceDiscount;
}
public void setPriceDiscount(double priceDiscount) {
this.priceDiscount = priceDiscount;
}
@Override
public String toString() {
return goodsId+"\t"+goodsName+"\t"+price;
}
}