1)设计一个账户Account类,属性包括账号、余额、利率、开户日期等信息,方法包括各属性的访问器和修改器、存款、取款以及查询余额等操作。画出该类的UML类图,并编写测试程序进行测试。
(2)汽车的风挡玻璃雨刷(Wiper)是由带刻度盘(Dial)的控制杆(Lever)控制的。这种控制杆有四个位置:停止、间歇、低速和高速,刻度盘有三个位置,分别是数字1、2和3。刻度盘位置指示三种间歇速度,刻度盘的位置只有当控制杆在间歇位置时才有意义。以下表格给出了挡风玻璃雨刷对应控制杆和刻度盘的工作速度(每分钟摇摆次数):
控制杆 |
停止 |
间歇 |
间歇 |
间歇 |
低速 |
高速 |
刻度盘 |
- |
1 |
2 |
3 |
- |
- |
雨刷 |
0 |
4 |
6 |
12 |
30 |
60 |
注意:控制杆和刻度盘在调节的时候,只能按照顺序调节,例如控制杆只能从停止←→间歇←→低速←→高速,同样,刻度盘的调节过程也必须是1←→2←→3。 采用面向对象编程实现上述雨刷的模拟调节过程。
public class Account { private int id = 0; protected double balance = 0; private double annualInterestRate = 0; private Date dateCreated = new Date(); Account(){ } Account(int newId,double bal){ id = newId; balance = bal; } public int getId() { return id; } public void setId(int newId) { id = newId; } public double getBalance() { return balance; } public void setBalance(double bal) { balance = bal; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double newAnnualInterestRate) { annualInterestRate = newAnnualInterestRate; } public Date getDateCreated() { return dateCreated; } public boolean deposit(double money) { if(money > 0) { balance += money; return true; } else return false; } public boolean withDraw(double money) { if(balance > money) { balance -= money; return true; } else return false; } } public class AccountTest1 { public static void main(String[] args) { // TODO 自动生成的方法存根 Account A = new Account(12138,5000); System.out.println("账户"); if(A.withDraw(2500)) System.out.println("Withdraw success!"); else System.out.println("Withdraw fail!"); if(A.deposit(3000)) System.out.println("deposit success!"); else System.out.println("deposit fail!"); System.out.println(A.getBalance()); System.out.println(A.getDateCreated()); }
public class Agent { Brush brush; Dial dial; Lever lever; String s; Agent(){ } Agent(Brush bru,Dial dia,Lever lev){ brush = bru; dial = dia; lever = lev; } public void calculateSpeed() { int speed = 0; switch(lever.getLeverPosition()) { case 1:s="停止";speed = 0;break; case 2:s="间歇"; switch(dial.getDialPosition()) { case 1:speed = 4;break; case 2:speed = 6;break; case 3:speed = 12;break; };break; case 3:s="低速";speed = 30;break; case 4:s="高速";speed = 60;break; } brush.setSpeed(speed); } } public class Brush { private int speed; Brush(){ } Brush(int spe){ speed = spe; } public int getSpeed() { return speed; } public void setSpeed(int spe) { speed = spe; } } public class Dial { private int position; Dial(){ } Dial(int pos){ position = pos; } public int getDialPosition() { return position; } public void dialPositionUp() { if(position < 3) position++; else System.out.println("刻度盘在最高档位了!"); } public void dialPositionDown() { if(position > 1) position--; else System.out.println("刻度盘在最低档位了!"); } } public class Lever { private int position; Lever(){ } Lever(int pos){ position = pos; } public int getLeverPosition() { return position; } public void LeverPositionUp() { if( position < 4) position++; else System.out.println("控制杆在最高档位了!"); } public void LeverPositionDown() { if(position > 1) position--; else System.out.println("控制杆在最低档位了!"); } } import java.util.Scanner; public class BrushTest { public static void meun() { System.out.println("What you want to do"); System.out.println("1.Leverup"); System.out.println("2.Leverdown"); System.out.println("3.Dialup"); System.out.println("4.Dialdown"); } public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner input = new Scanner(System.in); Agent A = new Agent(new Brush(0),new Dial(),new Lever(1)); meun(); while(true) { int number = input.nextInt(); if(number == 0) break; switch(number) { case 1:A.lever.LeverPositionUp();break; case 2:A.lever.LeverPositionDown();break; case 3:A.dial.dialPositionUp();break; case 4:A.dial.dialPositionDown();break; } A.calculateSpeed(); System.out.println("控制杆:" +A.s); System.out.println("刻度盘:"+A.dial.getDialPosition()); } System.out.println("控制杆 刻度盘 雨刷"); System.out.println(A.s+" "+A.dial.getDialPosition()+" "+A.brush.getSpeed()); input.close(); } }