1) Design an account Account class, the attributes include account number, balance, interest rate, account opening date and other information, and the methods include accessors and modifiers for each attribute, deposit, withdrawal, and balance query operations. Draw the UML class diagram of the class, and write the test program to test.
( 2) The windshield wiper ( Wiper) of the car is controlled by a lever (Lever) with a dial (Dial). This lever has four positions: stop, intermittent, low speed and high speed, and the dial has three positions, numbers 1, 2 and 3. The position of the dial indicates the three intermittent speeds, the position of the dial is only meaningful when the lever is in the intermittent position. The following table gives the operating speeds (swings per minute) of the windshield wipers for the levers and dials:
Controller |
stop |
intermittent |
intermittent |
intermittent |
low speed |
high speed |
dial |
- |
1 |
2 |
3 |
- |
- |
wiper |
0 |
4 |
6 |
12 |
30 |
60 |
Note: When the control lever and dial are adjusted, they can only be adjusted in sequence. For example, the control lever can only be adjusted from stop←→intermittent←→low speed←→ high speed. Similarly, the adjustment process of the dial must also be 1←→2← →3. Object-oriented programming is used to realize the simulation adjustment process of the wiper.
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 auto-generated method stub Account A = new Account(12138,5000); System.out.println("Account"); 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; Liver liver; String s; Agent(){ } Agent(Brush bru,Dial dia,Lever lev){ brush = bru; dial = he; liver = lev; } public void calculateSpeed() { int speed = 0; switch(lever.getLeverPosition()) { case 1:s="停止";speed = 0;break; case 2:s="Intermittent"; 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("The dial is in the highest gear!"); } public void dialPositionDown() { if(position > 1) position--; else System.out.println("The dial is in the lowest gear!"); } } 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("The joystick is in the highest gear!"); } public void LeverPositionDown() { if(position > 1) position--; else System.out.println("The lever is in the lowest gear!"); } } 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 auto-generated method stub 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("Joystick:" +As); System.out.println("Dial:"+A.dial.getDialPosition()); } System.out.println("lever dial wiper"); System.out.println(A.s+" "+A.dial.getDialPosition()+" "+A.brush.getSpeed()); input.close(); } }