java作业:创建一个银行,银行里面有5个账户(账号,密码,钱)。要求:1、开户;2、开户以后才能
两个实体类
实体类1:
public class Bank {
private List<Card> list;
public Bank() {
super();
}
public Bank(List<Card> list) {
super();
this.list = list;
}
public List<Card> getList() {
return list;
}
public void setList(List<Card> list) {
this.list = list;
}
}
实体类2:
public class Card {
private String id;
private String password;
private String isOpen; //判断是否开户 0开户 1没开户
private double money;
public Card() {
super();
}
public Card(String id, String password, String isOpen ,double money) {
super();
this.id = id;
this.password = password;
this.isOpen = isOpen;
this.money = money;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getIsOpen() {
return isOpen;
}
public void setIsOpen(String isOpen) {
this.isOpen = isOpen;
}
@Override
public String toString() {
return "Card [id=" + id + ", password=" + password + ", isOpen=" + isOpen + ", money=" + money + "]";
}
}
逻辑处理类:
public class TestService {
public Bank addUserInfo(){
Card card = new Card("1","1","0",10000);
Card card1 = new Card("2","2","1",20000);
Card card2 = new Card("3","3","1",30000);
Card card3 = new Card("4","4","1",40000);
Card card4 = new Card("5","5","1",50000);
List<Card> list = new ArrayList<>();
list.add(card);
list.add(card1);
list.add(card2);
list.add(card3);
list.add(card4);
Bank bank = new Bank();
bank.setList(list);
List<Card> list1 = bank.getList();
return bank;
}
public Card login(Bank bank , String id,String password){
Card card1 = null;
List<Card> list = bank.getList();
for (Card card : list) {
if(id.equals(card.getId()) && password.equals(card.getPassword())){
card1 = card;
break;
}
}
return card1;
}
}
测试类:
public class Test {
public static void main(String[] args) {
TestService service = new TestService();
Bank bank = service.addUserInfo();
System.out.println("****************************");
System.out.println("*********欢迎进入银行系统********");
System.out.println("****************************");
Scanner input = new Scanner(System.in);
System.out.println("请输入卡号");
String id = input.next();
System.out.println("请输入密码");
String password = input.next();
Card card = service.login(bank , id, password);
if(card != null){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败 ");
}
System.out.println("请选择你要的操作");
System.out.println("1.取款 2.查询余额 3.注销");
Scanner input1 = new Scanner(System.in);
String aa = input1.next();
if(aa.equals("3")){
System.out.println("注销成功");
}else if(aa.equals("2")){
System.out.println("卡号是:"+card.getId()+"余额是:"+card.getMoney());
}else if(aa.equals("1")){
if(card.getIsOpen().equals("1")){
System.out.println("对不起您的卡号没有开户,不能取款");
}else{
System.out.println("请输入取款金额");
Scanner input2 = new Scanner(System.in);
String bb = input2.next();
if(Double.parseDouble(bb) > card.getMoney() ){
System.out.println("余额不足");
}else{
card.setMoney(card.getMoney() - Double.parseDouble(bb) );
System.out.println("取款成功,取款金额"+bb+"余额是:"+card.getMoney());
}
}
}
}
}
这个是我简单写的逻辑,看你的需求应该就是这样,没有牵扯到数据库。。运行效果如图:
需要源码的话你把邮箱发给我,具体逻辑你还需要自己再修改一下,我只是把基本功能写出来了,还有开户操作我还没写,我只是把1这个用户默认成开户状态了。
推荐于2017-11-22 · 知道合伙人软件行家