java开学第一周测试代码
Posted 今天123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java开学第一周测试代码相关的知识,希望对你有一定的参考价值。
Account.java
/*
信1705-3 20173612 陈阳阳
*/
package ATM;
public class Account {
private String accountID;//用户的账号
private String accountname;//账户的名称
private String operatedate;//操作的日期
private int operatetype;//操作的类型
private String accountpassword;//用户的密码
private int accountbalance;//账户的余额
private int amount;//操作流水的金额
public Account(String accountID, String accountname, String operatedate, int operatetype,
String accountpassword,int accountbalance,int amount) {
this.accountID = accountID;
this.accountname = accountname;
this.operatedate = operatedate;
this.operatetype = operatetype;
this.accountpassword = accountpassword;
this.accountbalance = accountbalance;
this.amount = amount;}
public String getAccountID() {
return accountID;
}
public void setAccountID(String accountID) {
this.accountID = accountID;
}
public String getAccountName() {
return accountname;
}
public void setAccountName (String accountName) {
this.accountname = accountName;
}
public String getOperatedate() {
return operatedate;
}
public void setOperatedate(String Operatedate) {
this.operatedate = Operatedate;
}
public int getOperatetype() {
return operatetype;
}
public void setOperatetype(int Operatetype) {
this.operatetype = Operatetype;
}
public String getAccountpassword() {
return accountpassword;
}
public void setAccountpassword(String accountpassword) {
this.accountpassword = accountpassword;
}
public int getAccountbalance() {
return accountbalance;
}
public void setAccountbalance(int accountbalance) {
this.accountbalance = accountbalance;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
AccountManager.java
package ATMOP;
import java.util.*;
public class AccountManager {
@SuppressWarnings({ "resource", "unused" })//(“unused”)未使用的变量 (“resource”)有泛型未指定类型
public static void main(String[] args) throws NumberErrorException {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
PATM atm=new ATM();
NumberErrorException nee=new NumberErrorException("输入非法!");
NotHundredTimesException nhte=new NotHundredTimesException("不是一百的整数倍!");
PasswordErrorException pee=new PasswordErrorException("输入错误的密码超过三次!");
int passwordErrorTimes=0;
int choose=-1,number=0,x=0;
String num="";
String pw="";
next:while(true){
System.out.println("******************************");
System.out.println(" 欢迎使用中国工商银行自助柜员系统 ");
System.out.println("******************************");
System.out.println("请输入您的账号:");
int kk=-1;
String c=in.next();
if(whetherNum(c))
kk=Integer.parseInt(c);
if(kk==0) break;
else if(kk!=1){
System.out.println("输入错误!");
continue;
}
System.out.println("************************************************");
System.out.println(" 欢迎***使用中国工商银行自助柜员系统 ");
System.out.println("************************************************");
System.out.println("请输入您的密码:");
pw=in.next();
if(atm.ifPass(pw)){
while(true){
showFace();
String cc=in.next();
if(whetherNum(cc))
choose=Integer.parseInt(cc);
switch(choose){
case 1:
System.out.println("******************************************");
System.out.println(" 欢迎***使用中国工商银行自助柜员系统 ");
System.out.println("*******************************************");
System.out.println("输入存款金额:");
while(true){
try{
num=in.next();
if(!whetherNum(num))
throw nee;
else if((x=Integer.parseInt(num)%100)!=0)
throw nhte;
else
break;
}
catch(NumberErrorException e){
System.out.println("输入正确的数字!");
}
catch(NotHundredTimesException e){
System.out.println("录入错误,请输入合法的整数(100的整数倍)!");
}
}
number=Integer.parseInt(num);
atm.save(number);
System.out.println("****************************************************");
System.out.println(" 欢迎***使用中国工商银行自助柜员系统 ");
System.out.println("****************************************************");
System.out.println("当前账户存款操作成功。");
System.out.println("当前账户余额为:"+atm.getRest()+"元");
break;
case 2:
System.out.println("*****************************************************");
System.out.println(" 欢迎***使用中国工商银行自助柜员系统 ");
System.out.println("*****************************************************");
System.out.println("当前用户每日可以支取2万元。");
System.out.println("请选择:");
int a[]={100,500,1000,1500,2000,5000};
for(int i=0;i<a.length;i++)
System.out.println((i+1)+"."+a[i]+"元");
System.out.println("7.其他金额");
String bb="";
int ch=-1;
bb=in.next();
if(whetherNum(bb)){
ch=Integer.parseInt(bb);
}
if(ch>=1&&ch<=6){
if(atm.withdraw(a[ch-1]))
{
System.out.println("*************************************************");
System.out.println(" 欢迎***使用中国工商银行自助柜员系统 ");
System.out.println("*************************************************");
System.out.println("当前账户取款操作成功! ");
}
else
System.out.println("账户余额不足!");
}
else if(ch==7){
System.out.println("*****************************************************");
System.out.println(" 欢迎***使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************");
System.out.println("请输入取款金额:");
while(true){
try{
num=in.next();
if(!whetherNum(num))
throw nee;
else if((x=Integer.parseInt(num)%100)!=0)
throw nhte;
else
break;
}
catch(NumberErrorException e){
System.out.println("输入正确的数字!");
}
catch(NotHundredTimesException e){
System.out.println("录入错误,请输入合法的整数(100的整数倍)!");
}
}
number=Integer.parseInt(num);
if(atm.withdraw(number))
System.out.println("取款成功!");
else
System.out.println("余额不足!");
}
else
System.out.println("输入有误!");
System.out.println("当前余额:"+atm.getRest()+"元");
break;
case 3:
System.out.println("******************************************************");
System.out.println(" 欢迎***使用中国工商银行自助柜员系统 ");
System.out.println("*******************************************************");
System.out.println("请输入转账账户:");
String s=in.next();
System.out.println("***************************************************");
System.out.println(" 欢迎***使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************");
System.out.println("请输入转账金额:");
while(true){
try{
num=in.next();
if(!whetherNum(num))
throw nee;
else if((x=Integer.parseInt(num)%100)!=0)
throw nhte;
else
break;
}
catch(NumberErrorException e){
System.out.println("输入正确的数字!");
}
catch(NotHundredTimesException e){
System.out.println("录入错误,请输入合法的整数(100的整数倍)!");
}
}
number=Integer.parseInt(num);
if(atm.transfer(s, number))
System.out.println("转账成功!");
else
System.out.println("转账失败!");
System.out.println("当前余额:"+atm.getRest()+"元");
break;
case 4:
System.out.println("输入当前密码:");
String M=in.next();
System.out.println("输入修改密码:");
String p=in.next();
if(p.length()==6&&whetherNum(p))
atm.setPassword(p);
else{
System.out.println("不是六位或者不是数字!");
}
break;
case 5:
System.out.println("当前余额:"+atm.getRest()+"元");
break;
default:
continue next;
}
}
}
else{
System.out.println("密码错误!");
try{
passwordErrorTimes++;
if(passwordErrorTimes>3)
throw pee;
}
catch(PasswordErrorException e){
System.out.println(e.getMessage()+"账户暂时冻结!");
return ;
}
}
}
}
//显示菜单
public static void showFace(){
System.out.println("**********************************************************");
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("*************************************************");
System.out.println("请选择:");
}
//判断字符串是否为整数(0-999999)
public static boolean whetherNum(String s){
boolean flag=true;
if(s.length()>=7)
return false;
char arr[]=s.toCharArray();
for(int i=0;i<s.length();i++){
if(arr[i]<‘0‘||arr[i]>‘9‘)
flag=false;
}
return flag;
}
}
class PersonalAccount{
private String passWord="123456";//密码
@SuppressWarnings("unused")
private String number;//银行卡号
private int money=0;
public int getMoney(){return money;}//余额
public void setPw(String s){passWord=s;}//设置密码
public void addMoney(int x){money+=x;}//加钱
public void minusMoney(int x){money-=x;}//减钱
public boolean whetherPwTrue(String s){//密码是否正确
if(s.equals(passWord))
return true;
else return false;
}
}
//abstract修饰类,会使这个类成为一个抽象类
abstract class PATM{
public abstract boolean withdraw(int x);//取款
public abstract void save(int x);//存款
public abstract boolean transfer(String s,int x);//转账
public abstract boolean ifPass(String s);//判断输入的密码是否正确
public abstract int getRest();//查询余额
public abstract void setPassword(String s);//设置密码
}
class ATM extends PATM{
private String numbers[]={"1234567891000000","11112222333344445"};//已有的账户卡号
private PersonalAccount account=new PersonalAccount();
public boolean withdraw(int x) {
if(x>account.getMoney())
return false;
else{
account.minusMoney(x);
return true;
}
}
public void save(int x) {
account.addMoney(x);
}
public boolean transfer(String s, int x) {
//转账
//先判断转到账户号是否存在
//再判断余额是否足够
boolean flag=false;
for(int i=0;i<numbers.length;i++)//比较s是否和t相等,返回一个boolean类型的值(即false或者true)
if(s.equals(numbers[i])) flag=true;//equals()方法在Object定义中是与==相等.就是比较2个对象引用是否相等
if(x>account.getMoney()) flag=false;
if(x<=account.getMoney()&&flag) account.minusMoney(x);;
return flag;
}
public boolean ifPass(String s) {
return account.whetherPwTrue(s);
}
public int getRest() {
return account.getMoney();
}
public void setPassword(String s) {
account.setPw(s);
}
}
@SuppressWarnings("serial")
class NumberErrorException extends Exception{
public NumberErrorException(String msg){
super(msg);
}
}
@SuppressWarnings("serial")
class PasswordErrorException extends Exception{
public PasswordErrorException(String msg){
super(msg);
}
}
@SuppressWarnings("serial")
class NotHundredTimesException extends Exception{
public NotHundredTimesException(String msg){
super(msg);
}
}
以上是关于java开学第一周测试代码的主要内容,如果未能解决你的问题,请参考以下文章