11章 编程练习题 11.8

兴趣使然,最近开始自学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 }
时间: 2024-07-31 07:47:55

11章 编程练习题 11.8的相关文章

c++primer plus 第11章 编程题第7题

#pragma once #ifndef COMPLEX0_H_ #define COMPLEX0_H_ #include<iostream> class Complex { private: double real; double imgn; public: Complex(); Complex(double a, double b); ~Complex(); Complex operator+(const Complex & a) const; Complex operator-(

算法导论第11章散列表11.1直接寻址表

/* * IA_11.1DirectAddressTables.cpp * * Created on: Feb 11, 2015 * Author: sunyj */ #include <stdint.h> #include <iostream> #include <string.h> // DIRECT-ADDRESS-SEARCH(T, k) // return T[k] // DIRECT-ADDRESS-INSERT(T, x) // T[x.key] = x

第三章编程练习题3.1

package exercise3; import java.util.Scanner; public class Main1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); double pan

第二章编程练习题2.8

import java.util.Scanner; public class Main7 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number = input.nextInt(); System.out.println((char)number); } }

第二章编程练习题2.6

import java.util.Scanner; public class Main5 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number = input.nextInt(); int last = number % 10; int shengxia = number/10; int second = shengxia%10; shengxia = sheng

第二章编程练习题2.9

public class Main8 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); int amount = input.nextInt(); int re = amount; int number1 = re/100; re = re%100; int number2 = re/25; re = re%25; int number3 =

第二章编程练习题2.7

import java.util.Scanner; /** * 给一个分钟的数字,算出来这些分钟代表多少年和多少天 * @author Administrator * */ public class Main6 { public static void main(String[] args) { Scanner input = new Scanner(System.in); long minuts = input.nextLong(); long numberOfDays = minuts/(2

第二章编程练习题2.2

import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); double radius = input.nextDouble();//圆的半径 double length = input.nextDouble();//圆的高度 double area = Math.PI* radius *radius;

第二章编程练习题2.5

import java.util.Scanner; /** * 财务应用程序,计算小费 * @author Administrator * */ public class Main3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); double number = input.nextDouble(); double rate = input.nextDouble(); doubl