個人銀行口座管理システムc++回転Java(四-六)

61463 ワード

  • 個人銀行管理システムバージョン0.1(4章記録対応)1)Javaにはc++のnamespaceはなく、packageに変更されています.2)private,publicなどはJavaでは個々のメンバーの前に書く必要がある.3)Javaでは一般関数(抽象クラス,インタフェースなどを除く)の宣言を関数体から分離することは許されず,一緒に書く.4)c++内void test(int_x)const{}の場合constはその関数がクラスのメンバーを変更できないことを指し,Java内final void 5)test(){}の場合finalはその関数が書き換えられないことを示す.Javaにはconstがありません.6)Javaの出力は文がSystem.out.println(変数名+「文字列」)である.「<7)Javaにはmath.hなどのヘッダファイルを書く必要はありません.クラスライブラリにはあります.たとえば、Math.floor().8)c++でクラスを作成するオブジェクトは、直接クラス名オブジェクト名(パラメータ)で、Javaにはクラス名オブジェクト名=newクラス名(パラメータ)が必要です.
  • 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();
    	}
    }
    
    
  • 個人銀行管理システムバージョン0.2(第5章記録対応)1)Javaにはconst修飾子がなく、類似のfinalがあるが、両者の機能に差がある.2)Javaにおけるコンストラクション関数にid(id)という表記はなく、this.id=idが多い.3)c++呼び出しクラスの静的メンバー関数はクラス名::関数名、Javaはクラス名.関数名.
  • 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());
    	}
    }
    
  • 個人銀行管理システムバージョン0.3(6章記録対応)1)Javaにはc++のような&シンボルリファレンスがなく、オブジェクトを作成する際にnewを使用する.2)Javaではグローバル変数は許されない.これはパッケージ性を損なうからである.3)Javaでの異常終了exit(1)はSystem.exit(1)を呼び出す必要がある.4)Javaの文字列タイプはクラスStringであり、printは改行を出力せず、printlnは改行する.
  • 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));
    	}
    
    }