604 銀行理財帳戶


第一題

class Account{
String name;
double rate;
int balance=0;
Account(){}
Account(String s){
name = s;
}
void setRate(double d){
rate = d;
}
void deposit(double x){
balance+=x;
}
void withdraw(double x){
balance-=x;
}
int balance(){
return balance;
}
void addInterest(){
balance=(int)(balance*(rate+1));
}

}

class DepositAccount extends Account{
DepositAccount(String s,int i){
super(s);
double r=1;
switch(i){
case 1:
r=0.03;break;
case 2:
r=0.04;break;
case 3:
r=0.05;break;
}
setRate(r);
}

}

class FreeAccount extends Account{
FreeAccount(String s){
super(s);
setRate(0.02);
}
}
class SpecialAccount extends Account{
SpecialAccount(String s){
super(s);
setRate(0.02);
}
boolean isEmpt(){
if(balance>10000){
return true;
}
else{
return false;
}
}

}

class FundAccount extends Account{
String fundname;
FreeAccount freeAccount;
  SpecialAccount specialaccount;
  double unit=0;
FundAccount(String s,String f,FreeAccount fr,SpecialAccount sp ){
super(s);
fundname=f;
freeAccount=fr;
specialaccount=sp;
}
void buy(double x ,double y){
if(specialaccount.isEmpt()){
freeAccount.withdraw(x);
}else{
freeAccount.withdraw(x*1.02);
}
unit+=(double)x/(double)y;
}
void sell(double u ,double x){
if(specialaccount.isEmpt()){
freeAccount.deposit(u*x);
}else{
freeAccount.deposit(x*u*0.98);
}
unit-=u;
}
int balance(int i){
return (int)(unit*i);
}
}

class JPD06_1 {
public static void main(String args[]) {
DepositAccount deposit = new DepositAccount("peter", 2);
deposit.deposit(5000);

FreeAccount free = new FreeAccount("peter");
free.deposit(20000);

SpecialAccount special = new SpecialAccount("peter");
special.deposit(10000);
deposit.addInterest();
free.addInterest();
special.addInterest();

        System.out.println("定期存款:" + deposit.balance());
System.out.println("活期存款:" + free.balance());
System.out.println("優惠存款:" + special.balance());

FundAccount fund = new FundAccount("peter", "A", free, special);
fund.buy(15000, 500);
System.out.println("基金現額:" + fund.balance(300));
System.out.println("活期餘額:" + fund.freeAccount.balance());
}
}

第二題

class FundAccount extends Account{
String fundname;
FreeAccount freeAccount;
  SpecialAccount specialaccount;
  double unit=0;
 
FundAccount(String s,String f,FreeAccount fr,SpecialAccount sp ){
super(s);
fundname=f;
freeAccount=fr;
specialaccount=sp;
}

void buy(double x ,double y){
if(specialaccount.isEmpt()){
freeAccount.withdraw(x);
}else{
freeAccount.withdraw(x*1.02);
}
unit+=(double)x/(double)y;
}

void sell(double u ,double x){
if(specialaccount.isEmpt()){
freeAccount.deposit(u*x);
}else{
freeAccount.deposit(x*u*0.98);
}
unit-=u;
}

int balance(int i){
return (int)(unit*i);
}

double getUnit(){
return unit;
}
}

class JPA06_2 {
public static void main(String args[]) {
DepositAccount deposit = new DepositAccount("peter", 2);
deposit.deposit(5000);
FreeAccount free = new FreeAccount("peter");

free.deposit(20000);

SpecialAccount special = new SpecialAccount("peter");

special.deposit(10000);

deposit.addInterest();

free.addInterest();

special.addInterest();
FundAccount fund = new FundAccount("peter", "A", free, special);
        fund.buy(15000, 500);
        special.withdraw(5000);
fund.buy(2000, 300);
System.out.println("基金餘額:" + fund.balance(300));
System.out.println("售出前活期餘額:" + fund.freeAccount.balance());
        fund.sell(fund.getUnit(), 400);
System.out.println("售出後活期餘額:" + fund.freeAccount.balance());
}
}

第三題

class InternetAccount{
FreeAccount freeAccount;
  SpecialAccount specialaccount;
  DepositAccount deposit;
  FundAccount fund;
  InternetAccount(){}

  void setDeposit(DepositAccount d){deposit = d;}

  void setFree(FreeAccount f){freeAccount=f;}

void setSpecial(SpecialAccount s){specialaccount=s;}

  void setFund(FundAccount fu){fund=fu;}

  int getTotalBalance(){
  return deposit.balance+freeAccount.balance+specialaccount.balance;
  }

}
class JPA06_3 {
public static void main(String args[]) {
DepositAccount deposit = new DepositAccount("peter", 2);
deposit.deposit(5000);
FreeAccount free = new FreeAccount("peter");
free.deposit(20000);
SpecialAccount special = new SpecialAccount("peter");
special.deposit(10000);
deposit.addInterest();
free.addInterest();
special.addInterest();
        FundAccount fund = new FundAccount("peter", "A", free, special);
fund.buy(15000, 500);
        special.withdraw(5000);
fund.buy(2000, 300);
fund.sell(fund.getUnit(), 400);

InternetAccount internet = new InternetAccount();

internet.setDeposit(deposit);
internet.setFree(free);
internet.setSpecial(special);
internet.setFund(fund);

System.out.println("存款總額:" + internet.getTotalBalance());
}
}

第四題

import java.util.*;
class MultiFund{
HashMap <String,FundAccount> list;
MultiFund(){
list =new HashMap<String,FundAccount>();
}
void addFund(String f, FundAccount fn){
list.put(f,fn);
}
void printEachUnit(){
for(String key:list.keySet()){
System.out.println(key+":"+list.get(key).getUnit());
}
}
int getFundBalance(String s, int x){
return list.get(s).balance(x);
}
}

class JPA06_4 {
public static void main(String args[]) {
DepositAccount deposit = new DepositAccount("peter", 2);
deposit.deposit(5000);

FreeAccount free = new FreeAccount("peter");
free.deposit(20000);

SpecialAccount special = new SpecialAccount("peter");
special.deposit(10000);
deposit.addInterest();
free.addInterest();
special.addInterest();

    FundAccount fund = new FundAccount("peter", "A", free, special);
fund.buy(15000, 500);
special.withdraw(5000);
fund.buy(2000, 300);
        fund.sell(fund.getUnit(), 400);

InternetAccount internet = new InternetAccount();
internet.setDeposit(deposit);
internet.setFree(free);
internet.setSpecial(special);
internet.setFund(fund);

MultiFund multi = new MultiFund();
multi.addFund("A", fund);
FundAccount fundB = new FundAccount("peter", "B", free, special);
fundB.buy(2000, 50);
multi.addFund("B", fundB);

FundAccount fundC = new FundAccount("peter", "C", free, special);
fundC.buy(5000, 30);

multi.addFund("C", fundC);
System.out.println("活期餘額:" + free.balance());
multi.printEachUnit();
        System.out.println("B 基金餘額: " + multi.getFundBalance("B", 100));
    }
}

第五題

class Account{
String name;
double rate;
int balance=0;
Account(){}
Account(String s){
name = s;
}
void setRate(double d){
rate = d;
}
void deposit(double x){
balance+=x;
}
void withdraw(int x) throws Exception{
if(balance()<x){
throw new Exception(name+"提款金額:"+x+"大於存款金額:"+balance);
}
else{
balance-=x;
}
}
int balance(){

return balance;
}

void addInterest(){
balance=(int)(balance*(rate+1));
}

}
class FundAccount extends Account{
String fundname;
FreeAccount freeAccount;
  SpecialAccount specialaccount;
  double unit=0;
FundAccount(String s,String f,FreeAccount fr,SpecialAccount sp ){
super(s);
fundname=f;
freeAccount=fr;
specialaccount=sp;
}
void buy(double x ,double y){
try{
if(specialaccount.isEmpt()){
freeAccount.withdraw((int)x);
}else{
freeAccount.withdraw((int)(x*1.02));
}
unit+=(double)x/(double)y;
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
void sell(double u ,double x){

if(specialaccount.isEmpt()){
freeAccount.deposit(u*x);
}else{
freeAccount.deposit(x*u*0.98);
}
unit-=u;
}
int balance(int i){
return (int)(unit*i);
}
double getUnit(){
return unit;
}
}
class JPA06_5 {
public static void main(String args[]) {

DepositAccount deposit = new DepositAccount("peter", 2);
deposit.deposit(5000);
FreeAccount free = new FreeAccount("peter");
free.deposit(20000);
SpecialAccount special = new SpecialAccount("peter");
special.deposit(10000);
deposit.addInterest();
free.addInterest();
special.addInterest();

FundAccount fund = new FundAccount("peter", "A", free, special);
fund.buy(15000, 500);

try {
special.withdraw(5000);
fund.buy(2000, 300);

fund.sell(fund.getUnit(), 400);

InternetAccount internet = new InternetAccount();
internet.setDeposit(deposit);
internet.setFree(free);
internet.setSpecial(special);
internet.setFund(fund);

MultiFund multi = new MultiFund();
multi.addFund("A", fund);
FundAccount fundB = new FundAccount("peter", "B", free, special);
fundB.buy(2000, 50);
multi.addFund("B", fundB);
FundAccount fundC = new FundAccount("peter", "C", free, special);
fundC.buy(5000, 30);
multi.addFund("C", fundC);

            fund.buy(14000, 300);

} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}

沒有留言:

張貼留言