using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace ConsoleApplication5
{
//Sorting programming
//I create 3 classes Product, Sorting and Program.
//Product class has ID, name, price, taxrate, tax and total price.
//Soting class has compare method.
//Program class has user to input product, create their objects and sort by total price.
//Also, this result stores txt file.
class
Program
{
static
void Main(string[] args)
{
//Create ArrayList
ArrayList al = new
ArrayList();
//Input TaxRate
Console.WriteLine("Please input TaxRate.");
//Read TaxRate
string input = Console.ReadLine();
//Convert double
double taxrate = double.Parse(input);
//Set TaxRate value
Product.SetTaxRate(taxrate);
//Declare variable for do while loop
int i = 0;
//Start do while loop
do
{
//Input ID
Console.WriteLine("Please input product ID.");
//Read ID
string input1 = Console.ReadLine();
//Convert integer
int id = int.Parse(input1);
//Input Name
Console.WriteLine("Please input product Name.");
//Read Name
string name = Console.ReadLine();
//Input price
Console.WriteLine("Please input product Price.");
//Read price
string input2 = Console.ReadLine();
//Convert double
double price = double.Parse(input2);
//Create object
Product pr= new Product(id, name, price);
al.Add(pr);
//Ask to input another product
Console.WriteLine("Do you want to input another prodyct? yes-1 no-0");
//Read answer
string input3 = Console.ReadLine();
//Convert integer
i = int.Parse(input3);
//End of do while loop
}
while (i == 1);
//Display current Array order before sorting
foreach (Product pr in al)
{
//Set tax value
pr.SetTax();
//Set total price value
pr.SetTotalPrice();
//Display current value
Console.WriteLine("ID: "+pr.GetID() + "\nName: " + pr.GetName() + "\nPrice: " + pr.GetPrice() + "\nTax: " + pr.GetTax() + "\nTotalPrice: " + pr.GetTotalPrice()+"\n");
}
//Excute sorting
al.Sort(new Sorting());
try
{
//Initialize filestreem
FileStream fs = new
FileStream
(
//Set file access
@"E:\VirtualExpander_J\C#\Fileaccess\exampractice.txt",
FileMode.Create,
FileAccess.ReadWrite,
FileShare.Read
);
//Initialize stream writer
StreamWriter sw = new
StreamWriter(fs);
//Display current Array order after sorting
foreach (Product pr1 in al)
{
//Display current value
Console.WriteLine("ID: " + pr1.GetID() + "\nName: " + pr1.GetName() + "\nPrice: " + pr1.GetPrice() + "\nTax: " + pr1.GetTax() + "\nTotalPrice: " + pr1.GetTotalPrice() + "\n");
//Write current value
sw.WriteLine("ID: " + pr1.GetID() + "Name: " + pr1.GetName() + "Price: " + pr1.GetPrice() + "Tax: " + pr1.GetTax() + "TotalPrice: " + pr1.GetTotalPrice() + "\n");
}
//Close stream writer
sw.Close();
//Close file streem
fs.Close();
}
catch
{
Console.WriteLine("Try block error");
}
}
}
//Product template
class
Product
{
//Declare variables
int ID;
string Name;
double Price;
static
double TaxRate;
double Tax;
double TotalPrice;
//Create method return ID
public
int GetID()
{
return ID;
}
//Create method return Name
public
string GetName()
{
return Name;
}
//Create method return Price
public
double GetPrice()
{
return Price;
}
//Create static method set TaxRate
public
static
void SetTaxRate(double d)
{
TaxRate = d;
}
//Create ststic method return TaxRate
public
static
double GetTaxRate()
{
return TaxRate;
}
//Create method calculate Tax
public
void SetTax()
{
Tax = Price * TaxRate;
}
//Create method return Tax
public
double GetTax()
{
return Tax;
}
//Create method calculate TotalPrice
public
void SetTotalPrice()
{
TotalPrice = Price + Tax;
}
//Create method return TotalPrice
public
double GetTotalPrice()
{
return TotalPrice;
}
//Constructor of Product class
public Product(int id, string name, double price)
{
ID = id;
Name = name;
Price = price;
}
//End of product class
}
//Sorting
class
Sorting : IComparer
{
//Compare method
public
int Compare(object a, object b)
{
//Declare return value
int value = 0;
//Cast object a and b
Product prod = (Product)a;
Product prod1 = (Product)b;
//Get TotalPrice value
double comprice = prod.GetTotalPrice();
double comprice1 = prod1.GetTotalPrice();
//Compare each case
if (comprice > comprice1)
{
value = 10;
}
if (comprice == comprice1)
{
value = 0;
}
if (comprice < comprice1)
{
value = -10;
}
//return result
return value;
}
}
}
0 件のコメント:
コメントを投稿