(继承,多态) 银行的客户分为两类,储蓄账户(SavingAccount)和信用账户(CreditAccount),区别在于储蓄账户不允许透支,而信用账户可以透支,并允许用户设置自己的透支额度。

(继承,多态)

银行的客户分为两类,储蓄账户(SavingAccount)和信用账户(CreditAccount),区别在于储蓄账户不允许透支,而信用账户可以透支,并允许用户设置自己的透支额度。

注意:CreditAccount需要多一个属性 ceiling 透支额度

为这两种用户编写相关的类,同时要求编写Bank类,属性:

1.当前所有的账户对象的集合,存放在数组中

2.当前账户数量


Account类
package com.work1;

public class Account {

	protected long aid;//账户编号
	protected String aname;//账户姓名
	protected String username;//用户名
	protected String password;//密码
	protected String email;//邮箱
	protected double money;//余额
	public long getAid() {
		return aid;
	}
	public void setAid(long aid) {
		this.aid = aid;
	}
	public String getAname() {
		return aname;
	}
	public void setAname(String aname) {
		this.aname = aname;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	
	
}
Bank类
package com.work1;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Bank {
	
	private static List list=new ArrayList();
	
	//开户
	public void  openAccount(){
		Scanner input =new Scanner(System.in);
		System.out.println("请选择开户类型:");
		System.out.println("1: 储蓄账户");
		System.out.println("2: 信用账户");
		int choice=input.nextInt();
		switch(choice){
			case 1:
				long aid=System.currentTimeMillis();//账户编号
				System.out.println("请输入账户姓名:");
				String name=input.next();
				System.out.println("请输入用户名:");
				String username=input.next();
				System.out.println("请输入密码:");
				String password=input.next();
				System.out.println("请输入邮箱:");
				String email=input.next();
				Account ac=new SavingAccount(aid,name,username,password,email,0.0);
				list.add(ac);
				break;
			case 2:
				long aid1=System.currentTimeMillis();//账户编号
				System.out.println("请输入账户姓名:");
				String name1=input.next();
				System.out.println("请输入用户名:");
				String username1=input.next();
				System.out.println("请输入密码:");
				String password1=input.next();
				System.out.println("请输入邮箱:");
				String email1=input.next();
				System.out.println("请设置透支额度:");
				double ceilling=input.nextDouble();
				Account ac1=new CreditAccount(aid1,name1,username1,password1,email1,ceilling,0.0);
				list.add(ac1);
				break;
		}
		System.out.println("开户成功");
	}
	
	
	//登录
	public void  login(){
		Scanner input =new Scanner(System.in);
		System.out.println("请输入用户名");
		String username=input.next();
		System.out.println("请输入密码");
		String password=input.next();
		Account at=null;
		for(Account ac:list){
			if(ac.getUsername().equals(username)&&ac.getPassword().equals(password)){
				at=ac;
			}
		}
		if(at!=null){
			System.out.println("登录成功");
			System.out.println("请选择功能:");
			System.out.println("1: 存款");
			System.out.println("2: 取款");
			System.out.println("3: 查询余额");
			int choice=input.nextInt();
			switch(choice){
		     	case 1:
		     		System.out.println("欢迎使用存款功能:");
		     		saveMoney(at);
		     		break;
		     	case 2:
		     		System.out.println("欢迎使用取款功能:");
		     		takeMoney(at);
		     		break;
		     	case 3:
		     		loopMoney(at);
		     		break;
			}
		}else{
			System.out.println("账号或者密码错误");
		}
		
	}
	
	//存款
	public void  saveMoney(Account at){
		Scanner input =new Scanner(System.in);
		System.out.println("请输入存款金额");
		double money=input.nextDouble();
		at.setMoney(money);
	}
	
	//查看余额:
	public void loopMoney(Account at){
		System.out.println("账户余额:"+at.getMoney());
	}
	
	
	//取款
	public void  takeMoney(Account at){
		Scanner input =new Scanner(System.in);
		System.out.println("请输入取款金额");
		double money=input.nextDouble();
		if(at.getMoney()>=money){
			System.out.println("取款成功");
			at.setMoney(at.getMoney()-money);
		}
	}
}

/**
 * 信用账户 
 *
 */
public class CreditAccount extends Account {

	private double ceiling;//透支额度
	
	public CreditAccount(long aid,String aname,String username,String password,String email,double ceiling,double money){
		this.aid=aid;
		this.aname=aname;
		this.username=username;
		this.password=password;
		this.email=email;
		this.ceiling=ceiling;
		this.money=money;
	}
	
	
	

	public double getCeiling() {
		return ceiling;
	}

	public void setCeiling(double ceiling) {
		this.ceiling = ceiling;
	}
	
	
	
}

import java.util.Scanner;

/**
 * 消费者
 */
public class Customer {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		Bank bk=new Bank();
		int choice;
		do{
		System.out.println("请选择功能:");
		System.out.println("1:开户:");
		System.out.println("2:登录:");
		choice=input.nextInt();
		switch(choice){
			case 1:
				bk.openAccount();
				break;
			case 2:
				bk.login();
		}
		}while(choice!=0);
	}
}


/**
 * 储蓄账户
 *
 */
public class SavingAccount extends Account {

	public SavingAccount(long aid,String aname,String username,String password,String email,double money){
		this.aid=aid;
		this.aname=aname;
		this.username=username;
		this.password=password;
		this.email=email;
		this.money=money;
	}
}


你可能感兴趣的:(java小题型代码)