码迷,mamicode.com
首页 > 其他好文 > 详细

11章 编程练习题 11.8

时间:2017-07-29 15:17:19      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:退出   int()   input   练习题   地方   als   其他   sage   一个人   

兴趣使然,最近开始自学java,之前的题做了就删,怪可惜的。

自己一个人闷头写代码也不好,今天开始就把做好的题都贴出来求鞭策吧!

要求:

一个Account类储存账户

一个Transaction类储存交易信息

利用ArrayList储存每一条交易信息

最后输出账户清单,显示账户名字、利率、余额和所有交易。

总结:

要注意保存每次交易后的余额balance,调用类方法时该类是否要转换。

代码写完感觉自己的算法逻辑还不够好,经常需要转一次弯的地方自己要转几次弯才到。

搞定一道题,经验值又UP了www

 

 1 package test.com;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test {
 6 
 7     public static void main(String[] args) {
 8         
 9         
10         Scanner input=new Scanner(System.in);
11         
12         System.out.println("请依次输入账户名,账户ID,账户余额,年利率");
13         String name=input.next();
14         int id=input.nextInt();
15         double balance=input.nextDouble();
16         double rate=input.nextDouble();
17         Account test = new Account(name, id, balance, rate);
18         while(true){
19             System.out.println("输入w取钱,d存钱,其他退出并显示信息:");
20             String step1=input.next();
21             
22             if(step1.equals("w")){
23                 System.out.println("请输入数额:");
24                 double amount=input.nextDouble();
25                 test.setWithdraw(amount);
26                 
27                 continue;
28             }
29             if(step1.equals("d")){
30                 System.out.println("请输入数额:");
31                 double amount=input.nextDouble();
32                 test.setDeposit(amount);
33                 
34                 continue;
35             }else{
36                 System.out.println("账户名:"+name+"\t年利率:"+rate+"%\n");
37                 test.message();
38                 break;
39             }
40         
41         }
42         
43     }
44 
45 }
  1 package test.com;
  2 
  3 import java.text.SimpleDateFormat;
  4 import java.util.ArrayList;
  5 import java.util.Date;
  6 import java.util.Scanner;
  7 
  8 public class Account {
  9     private int id=0;//用户ID
 10     private double balance=100;//收支
 11     private double annualInterestRate=0;//年利率
 12     private Date dateCreated;//开户日期
 13     private double withdraw=0;//取款
 14     private double deposit=0;//存款
 15     private String name;
 16     
 17     Scanner input = new Scanner(System.in);
 18     
 19     public Account(){
 20         
 21     }
 22     
 23     
 24     public Account(int num){
 25         
 26     }
 27     
 28     public Account(String name,int id,double balance,double annualInterestRate){
 29             this.setName(name);
 30             this.setId(id);
 31             this.setBalance(balance);
 32             this.setAnnualInterestRate(annualInterestRate);
 33         }
 34     
 35     //创建ArrayList储存含有交易信息的对象
 36     ArrayList transactions=new ArrayList();
 37     //打印所有存取款信息
 38     public void message(){
 39         for(int i=0;i<transactions.size();i++){
 40             ((Transaction)transactions.get(i)).message();
 41             }
 42     }
 43     
 44     
 45     
 46     //显示日期
 47     public  Date getDateCreated(){
 48         this.dateCreated=new Date();
 49         SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd ‘at‘ hh:mm:ss a zzz");
 50         System.out.println("Current Date: " + ft.format(dateCreated));
 51         return null;    
 52     }
 53 
 54     
 55 //get set 方法
 56     public double getBalance() {
 57         return balance;
 58     }
 59 
 60     public void setBalance(double num) {
 61         this.balance=num;
 62     }
 63 
 64     public double getWithdraw() {
 65         return withdraw ;
 66     }
 67 
 68     public void setWithdraw(double num) {
 69         Transaction test=new Transaction(‘w‘, num, this.getBalance());
 70         this.setBalance(test.getBalance());
 71         transactions.add(test);
 72         
 73     }
 74 
 75     public double getDeposit() {
 76         return deposit;
 77     }
 78 
 79     public void setDeposit(double num) {
 80         Transaction test=new Transaction(‘d‘, num, this.getBalance());
 81         this.setBalance(test.getBalance());
 82         transactions.add(test);
 83         
 84     }
 85 
 86     public int getId() {
 87         return id;
 88     }
 89 
 90     public void setId(int id) {
 91         this.id = id;
 92     }
 93 
 94     public double getAnnualInterestRate() {
 95         return annualInterestRate;
 96     }
 97 
 98     public void setAnnualInterestRate(double annualInterestRate) {
 99         this.annualInterestRate = annualInterestRate;
100     }
101 
102 
103     public String getName() {
104         return name;
105     }
106 
107 
108     public void setName(String name) {
109         this.name = name;
110     }
111     
112     
113 }
 1 package test.com;
 2 
 3 public class Transaction {
 4     private char type;//交易类型 w取钱d存钱
 5     private double amount=0;//交易的数额
 6     private double balance=0;//余额
 7     
 8     public Transaction(char type,double amount,double balance){
 9         this.setType(type);
10         this.setAmount(amount);
11         this.setBalance(balance);
12         
13     }
14     //每次存取款的信息
15     public void message(){
16         Account date=new Account();
17         date.getDateCreated();
18         if(this.type==‘w‘){
19             System.out.println("取款额为:"+this.getAmount());
20             System.out.println("账户余额为:"+this.getBalance()+"\n");
21         }
22         if(this.type==‘d‘){
23             System.out.println("存款额为:"+this.getAmount());
24             System.out.println("账户余额为:"+this.getBalance()+"\n");
25         }
26         
27         
28     }
29 //get set 
30     public char getType() {
31         return type;
32     }
33 
34     public void setType(char type) {
35         this.type = type;
36     }
37 
38     public double getAmount() {
39         return amount;
40     }
41 
42     public void setAmount(double amount) {
43         this.amount = amount;
44     }
45 
46     public double getBalance() {
47         return balance;
48     }
49 
50     public void setBalance(double balance) {
51         if(this.type==‘w‘){
52             this.balance = balance-amount;
53         }
54         if(this.type==‘d‘){
55             this.balance += balance+amount;
56         }
57     }
58     
59     
60 }

 

11章 编程练习题 11.8

标签:退出   int()   input   练习题   地方   als   其他   sage   一个人   

原文地址:http://www.cnblogs.com/nero3/p/7255634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!