⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 C#开发
💻 CS
📖 第 1 页 / 共 2 页
字号:
			{
				this.ChangedFlag=true;
			}			
		}
		public void ResetChangedFlag()
		{
			lock(this.ChangeListners)
			{
				this.ChangedFlag=false;
			}
		}
		public bool GetChangedFlag()
		{
			return this.ChangedFlag;
		}

		public MoneyDb()
		{
			ds=new DataSet();
			DataTable dt_Accounts=new DataTable("Accounts");
			DataTable dt_Transaction=new DataTable("Transaction");
			DataTable dt_TransactionType=new DataTable("TransactionType");

			dt_Accounts.Columns.Add(new DataColumn("AccountID",System.Type.GetType("System.Int32")));
			dt_Accounts.Columns.Add(new DataColumn("Name",System.Type.GetType("System.String")));
			dt_Accounts.Columns.Add(new DataColumn("Balance",System.Type.GetType("System.Double")));
			dt_Accounts.Constraints.Add("PrimaryKey",dt_Accounts.Columns["AccountID"],true);

			dt_TransactionType.Columns.Add(new DataColumn("TypeName",System.Type.GetType("System.String")));
			//dt_TransactionType.Constraints.Add("PrimaryKey",dt_TransactionType.Columns["TypeName"],true);

			dt_Transaction.Columns.Add(new DataColumn("ID",System.Type.GetType("System.Int32")));
			dt_Transaction.Columns.Add(new DataColumn("Type",System.Type.GetType("System.String")));
			dt_Transaction.Columns.Add(new DataColumn("Account",System.Type.GetType("System.Int32")));
			dt_Transaction.Columns.Add(new DataColumn("Amount",System.Type.GetType("System.Double")));
			dt_Transaction.Columns.Add(new DataColumn("Date",System.Type.GetType("System.DateTime")));
			dt_Transaction.Columns.Add(new DataColumn("Note",System.Type.GetType("System.String")));
			//dt_Transaction.Constraints.Add("TransactionTypeConstraint",dt_TransactionType.Columns["TypeName"],dt_Transaction.Columns["Type"]);
			dt_Transaction.Constraints.Add("AccountIDConstraint",dt_Accounts.Columns["AccountID"],dt_Transaction.Columns["Account"]);

			ds.Tables.AddRange(new DataTable[]{dt_Accounts,dt_TransactionType,dt_Transaction});
			
			this.sourcefile=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase )+"\\moneydb.xml";
		}

		public static MoneyDb LoadFromFile(string filename)
		{
			MoneyDb moneydb=new MoneyDb();
			System.Xml.XmlTextReader reader=new System.Xml.XmlTextReader(filename);
			moneydb.ds.ReadXml(reader);
			moneydb.sourcefile=filename;
			return moneydb;
		}

		public void RegisterChangeListener(IMoneyDbChangeListener listener)
		{
			this.ChangeListners.Add(listener);
		}

		public void RemoveChangeListener(IMoneyDbChangeListener listener)
		{
			this.ChangeListners.Remove(listener);
		}

		private void FireAccountChangeEvent()
		{
			for(int i=0;i<this.ChangeListners.Count;i++)
			{
				IMoneyDbChangeListener listener=this.ChangeListners[i] as IMoneyDbChangeListener;
				if(listener!=null)
				{
					listener.OnAccountChanged();
				}
			}
		}

		private void FireBalanceChangeEvent()
		{
			for(int i=0;i<this.ChangeListners.Count;i++)
			{
				IMoneyDbChangeListener listener=this.ChangeListners[i] as IMoneyDbChangeListener;
				if(listener!=null)
				{
					listener.OnBalanceChanged();
				}
			}
		}

		private void FireTransactionChangeEvent()
		{
			for(int i=0;i<this.ChangeListners.Count;i++)
			{
				IMoneyDbChangeListener listener=this.ChangeListners[i] as IMoneyDbChangeListener;
				if(listener!=null)
				{
					listener.OnTransactionChanged();
				}
			}
		}

		public void CommitToFile(string filename)
		{
			if(this.GetChangedFlag()!=true)
			{
				return;
			}
			lock(this)
			{
				System.Xml.XmlTextWriter writer=new System.Xml.XmlTextWriter(filename,System.Text.Encoding.UTF8);
				ds.WriteXml(writer,System.Data.XmlWriteMode.IgnoreSchema);
			}
			this.ResetChangedFlag();
			
		}

		public void CommitToFile()
		{
			this.CommitToFile(this.sourcefile);
		}

		public void FillTransactionTypes()
		{
			lock(this)
			{
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Dining Out"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Food"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Newspapger & Mag"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Clothing"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Movie/Video"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Sports"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Book"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Automobile"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Night Life"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Inventory Purchase"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Job Expense"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Cell Phone"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Cable TV"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Electricity"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Mortege"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Gas"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Telephone"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"House Rent"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Insuarance"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Water"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Computer"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Education"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Gift"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"House Cleaning"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Healthcare"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Pet"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Vacation"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Withdrawal"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Unknown Expense"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Investment Income"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Salary"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Reimbursement"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Deposit"});
				ds.Tables["TransactionType"].Rows.Add(new object[]{"Unknown Income"});
				this.SetChangedFlag();
			}
		}
		
		public void FillSampleData()
		{
			this.FillTransactionTypes();

			ds.Tables["Accounts"].Rows.Add(new object[]{0,"Cash",150.5});
			ds.Tables["Accounts"].Rows.Add(new object[]{1,"CMB Credit Card",-3200});

			ds.Tables["Transaction"].Rows.Add(new object[]{0,"Food",0,-22,System.DateTime.Now,"for lunch"});
			ds.Tables["Transaction"].Rows.Add(new object[]{1,"Book",0,-10.5,System.DateTime.Now,"magazine"});
			ds.Tables["Transaction"].Rows.Add(new object[]{2,"Unknown Expense",1,-698,System.DateTime.Now,"jean"});
			ds.Tables["Transaction"].Rows.Add(new object[]{3,"Food",1,-38,System.DateTime.Now,"dinner"});
			ds.Tables["Transaction"].Rows.Add(new object[]{4,"Salary",0,10200,System.DateTime.Now,"Salary"});
		}

		private int GetLatestTransactionID()
		{
			int rowcount=ds.Tables["Transaction"].Rows.Count;
			if(rowcount==0)
			{
				return -1;
			}
			else
			{
				return System.Int32.Parse(ds.Tables["Transaction"].Rows[rowcount-1]["ID"].ToString());
			}
		}

		private int GetLatestAccountID()
		{
			int rowcount=ds.Tables["Accounts"].Rows.Count;
			if(rowcount==0)
			{
				return -1;
			}
			else
			{
				return System.Int32.Parse(ds.Tables["Accounts"].Rows[rowcount-1]["AccountID"].ToString());
			}
			
		}

		public void NewTransaction(string Type,int Account,double Amount,System.DateTime Date,string Note)
		{
			lock(this)
			{
				ds.Tables["Transaction"].Rows.Add(new object[]{this.GetLatestTransactionID()+1,Type,Account,Amount,Date,Note});
				this.SetBalance(Account,this.GetBalance(Account)+Amount);
				this.FireTransactionChangeEvent();
				this.SetChangedFlag();
			}
		}

		public double GetBalance(int AccountID)
		{
			DataRow[] drs=ds.Tables["Accounts"].Select("AccountID="+AccountID);
			if(drs.Length>0)
			{
				return System.Double.Parse(drs[0]["Balance"].ToString());
			}
			else
				return 0;
		}

		public void SetBalance(int AccountID,double newvalue)
		{
			DataRow[] drs=ds.Tables["Accounts"].Select("AccountID="+AccountID);
			if(drs.Length>0)
			{
				drs[0]["Balance"]=newvalue;
			}
			this.FireBalanceChangeEvent();
		}

		public void DeleteTransaction(int TransactionID)
		{
			lock(this)
			{
				DataRow[] drs=ds.Tables["Transaction"].Select("ID="+TransactionID);
				foreach(DataRow dr in drs)
				{
					double amount=System.Double.Parse(dr["Amount"].ToString());
					int accountid=System.Int32.Parse(dr["Account"].ToString());
					this.SetBalance(accountid,this.GetBalance(accountid)-amount);
					ds.Tables["Transaction"].Rows.Remove(dr);
				}
				this.FireTransactionChangeEvent();
				this.SetChangedFlag();
			}
		}

		public void ClearTransactions(int AccountID)
		{
			lock(this)
			{
				DataRow[] drs=ds.Tables["Transaction"].Select("Account="+AccountID);
				foreach(DataRow dr in drs)
				{
					ds.Tables["Transaction"].Rows.Remove(dr);
				}
				this.FireTransactionChangeEvent();
				this.SetChangedFlag();
			}
		}

		public int GetAccountId(string AccountName)
		{
			DataRow[] drs=ds.Tables["Accounts"].Select("Name='"+AccountName+"'");
			foreach(DataRow dr in drs)
			{
				return System.Int32.Parse(dr["AccountID"].ToString());
			}
			return -1;
		}

		public void NewAccount(string Name,double initBalance)
		{
			lock(this)
			{
				ds.Tables["Accounts"].Rows.Add(new object[]{this.GetLatestAccountID()+1,Name,initBalance});
				this.FireAccountChangeEvent();
				this.SetChangedFlag();
			}
		}

		public void DeleteAccount(string Name)
		{
			int id=this.GetAccountId(Name);
			if(id!=-1)
			{
				this.DeleteAccount(id);
			}
		}

		public void DeleteAccount(int AccountID)
		{
			lock(this)
			{
				DataRow[] drs=ds.Tables["Accounts"].Select("AccountID="+AccountID+"");
				foreach(DataRow dr in drs)
				{
					DataRow[] trans=ds.Tables["Transaction"].Select("Account="+dr["AccountID"].ToString());
					foreach(DataRow transactionRow in trans)
					{
						ds.Tables["Transaction"].Rows.Remove(transactionRow);
					}
				}
				ds.Tables["Accounts"].Rows.Remove(ds.Tables["Accounts"].Rows.Find(AccountID));
				this.FireAccountChangeEvent();
				this.SetChangedFlag();
			}
		}

	
		public DataTable GetAccounts()
		{
			return ds.Tables["Accounts"];
		}

		public DataRow[] GetTransactions(int AccountID)
		{
			return ds.Tables["Transaction"].Select("Account="+AccountID);
		}

		public DataRow GetTransactionInfo(int TransactionID)
		{
			DataRow[] rows=ds.Tables["Transaction"].Select("ID="+TransactionID);
			if(rows.Length>0)
			{
				return rows[0];
			}
			else
			{
				return null;
			}
		}

		public string[] GetTypes()
		{
			string[] types=new string[ds.Tables["TransactionType"].Rows.Count];
			for(int i=0;i<ds.Tables["TransactionType"].Rows.Count;i++)
			{
				types[i]=ds.Tables["TransactionType"].Rows[i]["TypeName"].ToString();
			}
			return types;
		}

		public void AddNewExpenseIncomeType(string typename)
		{
			lock(this)
			{
				ds.Tables["TransactionType"].Rows.Add(new object[]{typename});
				this.SetChangedFlag();
			}
		}

		public void RemoveExpenseIncomeType(string typename)
		{
			lock(this)
			{
				DataRow[] drs=ds.Tables["TransactionType"].Select("TypeName='"+typename+"'");
				foreach(DataRow dr in drs)
				{
					ds.Tables["TransactionType"].Rows.Remove(dr);
				}
				this.SetChangedFlag();
			}
		}

		public void Transfer(int FromAccount,int ToAccount,double Amount)
		{
			this.NewTransaction("Withdrawal",FromAccount,0-Amount,System.DateTime.Now,"transfer "+FromAccount+"->"+ToAccount);
			this.NewTransaction("Deposit",ToAccount,Amount,System.DateTime.Now,"transfer "+FromAccount+"->"+ToAccount);
		}

		public bool SetAccountName(string originalName,string newName)
		{
			lock(this)
			{
				DataRow[] rows=ds.Tables["Accounts"].Select("Name='"+newName+"'");
				if(rows.Length>0)
				{
					return false;
				}

				DataRow[] drs=ds.Tables["Accounts"].Select("Name='"+originalName+"'");
				foreach(DataRow row in drs)
				{
					row["Name"]=newName;
				}
				this.FireAccountChangeEvent();
				this.SetChangedFlag();
				return true;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -