個人銀行口座管理システムc++回転Java(四-六)
61463 ワード
package PersonalBank1;
//4_9.cpp
public class SavingsAccount //
{
private int id; //
private double balance; //
private double rate; //
private int lastDate; //
private double accumulation; //
// ,date ,amount ,desc
private void record(int date, double amount)
{
accumulation = accumulate(date);
lastDate = date;
amount = Math.floor(amount * 100 + 0.5) / 100; //
balance += amount;
System.out.println(date+"\t#"+id+"\t"+amount+"\t"+balance);
}
//
private double accumulate(int date)
{
return accumulation + balance * (date - lastDate);
}
//
public SavingsAccount(int date, int id, double rate)
{
this.id = id;
this.balance = 0;
this.rate = rate;
this.lastDate = date;
this.accumulation = 0;
System.out.println(date+"\t#"+id+" is created");
}
public final int getId() { return id; }
public double getBalance() { return balance; }
public double getRate() { return rate; }
//
public void deposit(int date, double amount)
{
if(amount < 0.0){
System.out.println("Deposit is error!");
record(date, 0);
}
else record(date, amount);
}
//
public void withdraw(int date, double amount)
{
if(amount < 0.0){
System.out.println("Withdraw is error!");
record(date, 0);
}
else if (amount > getBalance())
System.out.println("Error: not enough money");
else
record(date, -amount);
}
// , 1 1
public void settle(int date)
{
double interest = accumulate(date) * rate / 365; //
if (interest != 0)
record(date, interest);
accumulation = 0;
}
//
public void show()
{
System.out.println("#"+id+"\tBalance: "+balance);
}
public static void main(String[] args)
{
//
SavingsAccount sa0 = new SavingsAccount(1,21325302,0.015);
//SavingsAccount sa1 = new SavingsAccount(1,58320212,0.015);
//
sa0.deposit(5, -5000);
// sa1.deposit(25, 10000);
sa0.deposit(35, 5500);
sa0.withdraw(60, -3000);
// sa1.withdraw(60, 4000);
// 90 ,
sa0.settle(90);
//sa1.settle(90);
//
sa0.show(); //System.out.println();
//sa1.show(); System.out.println();
}
}
package PersonalBank2;
//account.cpp
public class SavingsAccount {
private int id; //
private double balance; //
private double rate; //
private int lastDate; //
private double accumulation; //
private static double total = 0; //
// ,date ,amount ,desc
private void record(int date, double amount)
{
accumulation = accumulate(date);
lastDate = date;
amount = Math.floor((amount * 100 + 0.5)) / 100; //
balance += amount;
total += amount;
System.out.println(date + "\t#" + id + "\t" + amount + "\t" + balance);
}
//
private double accumulate(int date)
{
return accumulation + balance * (date - lastDate);
}
//
public SavingsAccount(int date, int id, double rate)
{
this.lastDate = date;
this.id = id;
this.rate = rate;
this.accumulation = 0;
this.balance = 0;
System.out.println(date + "\t#" + id + " is created");
}
public int getId() { return id; }
public double getBalance() { return balance; }
public double getRate() { return rate; }
public static double getTotal() { return total; }
//
public void deposit(int date, double amount)
{
record(date, amount);
}
//
public void withdraw(int date, double amount)
{
if (amount > getBalance())
System.out.println("Error: not enough money");
else
record(date, -amount);
}
// , 1 1
public void settle(int date)
{
double interest = accumulate(date) * rate / 365; //
if (interest != 0)
record(date, interest);
accumulation = 0;
}
//
public void show()
{
System.out.println("#" + id + "\tBalance: " + balance);
}
}
package PersonalBank2;
//5_11.cpp
public class PersonalBank {
public static void main(String[] args) {
//
//SavingsAccount sa0 = new SavingsAccount(1, 21325302, 0.015);
SavingsAccount sa1 = new SavingsAccount(1, 58320212, 0.015);
//
//sa0.deposit(5, 5000);
sa1.deposit(25, 3000);
//sa0.deposit(45, 5500);
sa1.withdraw(60, 4000);
// 90 ,
//sa0.settle(90);
sa1.settle(90);
//
//sa0.show();
sa1.show();
System.out.println("Total: " + SavingsAccount.getTotal());
}
}
package PersonalBank3;
//date.cpp
public class Date {
// 1 , getMaxDay ,
final int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
private int year; //
private int month; //
private int day; //
private int totalDays; // 1 1
boolean isLeapYear() { //
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
public Date(int year, int month, int day) // 、 、
{
this.day = day;
this.month = month;
this.year = year;
if (day <= 0 || day > getMaxDay()) {
System.out.println("Invalid date: ");
show();
System.out.println();
System.exit(1);
}
int years = year - 1;
totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
if (isLeapYear() && month > 2) totalDays++;
}
public int getYear() { return year; }
public int getMonth() { return month; }
public int getDay() { return day; }
public int getMaxDay() //
{
if (isLeapYear() && month == 2)
return 29;
else
return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
}
public void show() //
{
System.out.print(getYear() + "-" + getMonth() + "-" + getDay());
}
//
int distance(Date date)
{
return totalDays - date.totalDays;
}
}
package PersonalBank3;
//account.cpp
public class SavingsAccount {
private String id; //
private double balance; //
private double rate; //
private Date lastDate; //
private double accumulation; //
private static double total = 0; //
// ,date ,amount ,desc
private void record(Date date, double amount, String desc)
{
accumulation = accumulate(date);
lastDate = date;
amount = Math.floor(amount * 100 + 0.5) / 100; //
balance += amount;
total += amount;
date.show();
System.out.println("\t#" + id + "\t" + amount + "\t" + balance + "\t" + desc);
}
//
private void error(String msg)
{
System.out.println("Error(#" + id + "): " + msg);
}
//
private double accumulate(Date date)
{
return accumulation + balance * date.distance(lastDate);
}
//
public SavingsAccount(Date date,String id, double rate)
{
this.id = id;
this.balance = 0;
this.rate = rate;
this.lastDate = date;
this.accumulation = 0;
date.show();
System.out.println( "\t#" + id + " created");
}
public String getId() { return id; }
public double getBalance() { return balance; }
public double getRate() { return rate; }
public static double getTotal() { return total; }
//
public void deposit(Date date, double amount, String desc)
{
record(date, amount, desc);
}
//
public void withdraw(Date date, double amount, String desc)
{
if (amount > getBalance())
error("not enough money");
else
record(date, -amount, desc);
}
// , 1 1
public void settle(Date date)//
{
double interest = accumulate(date) * rate / date.distance(new Date(date.getYear() - 1, 1, 1));
if (interest != 0)
record(date, interest, "interest");
accumulation = 0;
}
//
public void show()
{
System.out.println(id + "\tBalance: " + balance);
}
}
package PersonalBank3;
//6_25.cpp
public class PersonalBank {
public static void main(String[] args) {
//Date date = new Date(2008, 11, 1); //
/*
//
SavingsAccount accounts[] = {
new SavingsAccount(date, "S3755217", 0.015),
new SavingsAccount(date, "02342342", 0.015)
};
int n = accounts.length; //
//11
accounts[0].deposit(new Date(2008, 11, 5), 5000, "salary");
accounts[1].deposit(new Date(2008, 11, 25), 10000, "sell stock 0323");
//12
accounts[0].deposit(new Date(2008, 12, 5), 5500, "salary");
accounts[1].withdraw(new Date(2008, 12, 20), 20000, "buy a laptop");
//
System.out.println();
for (int i = 0; i < n; i++) {
accounts[i].settle(new Date(2009, 1, 1));
accounts[i].show();
//System.out.println();
}
System.out.println("Total: " + SavingsAccount.getTotal());
*/
Date date1 = new Date(2008, 11, 50);
//Date date2 = new Date(2009, 2, 1);
date1.show();
//System.out.println();
//System.out.println(date1.getMaxDay());
//System.out.println();
//date2.show();
//System.out.println();
//System.out.println(date2.getMaxDay());
//System.out.println();
//System.out.println(date2.distance(date1));
}
}