2个回答
展开全部
3个类,开始...
1,
/**
*
* 自定义异常类
*
*/
public class MyException extends Exception {
//验证交易类型正确性
public void TransactionsInfo(){
System.out.println("交易失败,只能输入0或者1");
}
//验证金额正确性
public void amountInfo(){
System.out.println("交易失败,输入金额不能为负数或者字符");
}
}
2,
public class User {
/**
* 用户信息类
*/
private String name; //用户名
private float amount; //用户金额
public User(String name,float amount){
this.name = name;
this.amount = amount;
}
public float getAmount() {
return amount;
}
public void setAmount(float amount) {
this.amount = amount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 交易的方法,需要接受两个参数,一个是代表交易方式(取款,或者存款,),一个代表交易金额
public User Transactions() throws MyException{
System.out.println("请输入您的交易方式.0代表存款,1代表取款");//提示用户交易方式
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
String flag = null;
try {
flag = br1.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if("0".equals(flag)){
}else if("1".equals(flag)){
amount=-amount;//因为1代表取款,所以变成负数
}else{
new MyException().TransactionsInfo(); //打印类型错误
return this;
}
String amountString = null;
try {
amountString = br2.readLine();
} catch (IOException e) {
e.printStackTrace();
}
float amount = 0.0f;
try{
amount = Float.parseFloat(amountString);
}catch(Exception ex){
throw new MyException();
}
this.amount = this.amount+amount;//交易
return this; //返回用户信息
}
}
3,
public class MoneyTest {
/**
* 测试类
* @param args
*/
public static void main(String[] args){
User user = new User("张三",123.4f); //测试用户
System.out.println("用户"+user.getName()+"交易前的金额为:"+user.getAmount());
try {
user.Transactions();
} catch (MyException e) {
// TODO Auto-generated catch block
e.amountInfo();
}//调用方法
System.out.println("交易后的金额为"+user.getAmount());
}
}
1,
/**
*
* 自定义异常类
*
*/
public class MyException extends Exception {
//验证交易类型正确性
public void TransactionsInfo(){
System.out.println("交易失败,只能输入0或者1");
}
//验证金额正确性
public void amountInfo(){
System.out.println("交易失败,输入金额不能为负数或者字符");
}
}
2,
public class User {
/**
* 用户信息类
*/
private String name; //用户名
private float amount; //用户金额
public User(String name,float amount){
this.name = name;
this.amount = amount;
}
public float getAmount() {
return amount;
}
public void setAmount(float amount) {
this.amount = amount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 交易的方法,需要接受两个参数,一个是代表交易方式(取款,或者存款,),一个代表交易金额
public User Transactions() throws MyException{
System.out.println("请输入您的交易方式.0代表存款,1代表取款");//提示用户交易方式
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
String flag = null;
try {
flag = br1.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if("0".equals(flag)){
}else if("1".equals(flag)){
amount=-amount;//因为1代表取款,所以变成负数
}else{
new MyException().TransactionsInfo(); //打印类型错误
return this;
}
String amountString = null;
try {
amountString = br2.readLine();
} catch (IOException e) {
e.printStackTrace();
}
float amount = 0.0f;
try{
amount = Float.parseFloat(amountString);
}catch(Exception ex){
throw new MyException();
}
this.amount = this.amount+amount;//交易
return this; //返回用户信息
}
}
3,
public class MoneyTest {
/**
* 测试类
* @param args
*/
public static void main(String[] args){
User user = new User("张三",123.4f); //测试用户
System.out.println("用户"+user.getName()+"交易前的金额为:"+user.getAmount());
try {
user.Transactions();
} catch (MyException e) {
// TODO Auto-generated catch block
e.amountInfo();
}//调用方法
System.out.println("交易后的金额为"+user.getAmount());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询