2012-08-11 52 views
0

是否有格式化文本的方法,我試圖使這看起來像接近儘可能看起來像薪資樁,但看到像名稱/地址的東西可以不同的人...手動格式不適合每個人。C#格式文本的餅乾切割機輸出不同大小的文本

我認爲在2-年奇數年的CLI之後,可能會有這樣的解決方案。

此刻的程序按照我希望的方式工作。它只是我試圖微調的輸出。

using System; 
using System.IO; 

namespace Proj10 
{ 
class Program 
{ 
    const int ARRAY_TWO = 2; 
    static void Main() 
    { 
     int count = 0; 
     Employee[] emp = new Employee[10]; 
     //emp[0] = new Employee(); 
     //emp[0].SetEmpNum(1); 

     string environment = System.Environment.GetFolderPath 
     (System.Environment.SpecialFolder.Personal) + "\\"; 

     Console.WriteLine("Enter a file name in My Documents: "); 
     string input = Console.ReadLine(); 

     string path = environment + input; 

     StreamReader myFile = new StreamReader(path); 

     bool check = true; 

     do 
     { 

      if (myFile.EndOfStream) 
      { 
       break; 
      } 
      int num = int.Parse(myFile.ReadLine()); 
      string name = myFile.ReadLine(); 
      string address = myFile.ReadLine(); 
      string hourNworked = myFile.ReadLine(); 
      double[] values = new double[ARRAY_TWO]; 
      sort(hourNworked, ref values); 

      emp[count] = new Employee(); 
      emp[count].SetEmpNum(num); 
      emp[count].SetEmpName(name); 
      emp[count].SetEmpAddress(address); 
      emp[count].SetEmpHrlyPay(values[0]); 
      emp[count].SetEmpHrsWrked(values[1]); 



      //while count < 10 && !myfile.EOF(); 

      //print loop using count 
      //emp[0].GetEmpNum(); 
      //emp[0].CalcSalary(); 

      /*Integer[] number = new Integer[5]; 
      for (int i = 0; i < 5; i++) 
       number[i] = new Integer(i); 
      for (int i = 0; i < 5; i++) 
       Console.WriteLine(number[i].GetValue()); 
      */ 

      count++; 


     } while (count < 10); 

     Console.WriteLine("\n\nAuthor: Daniel Demers"); 
     Console.WriteLine("CS 1400 X01\n"); 

     for (int i = 0; i < 2; i++) 
     { 

      Console.WriteLine("|-----------------------------------------------------------------------|"); 
      Console.WriteLine("| Employee Number {0}             |", emp[i].GetEmpNum()); 
      Console.WriteLine("|-----------------------------------------------------------------------|"); 
      Console.WriteLine("| Employee Name: {0}           |", emp[i].GetEmpName()); 
      Console.WriteLine("| Emplyee Address: {0}            | |", emp[i].GetEmpAddress()); 
      Console.WriteLine(); 
      Console.WriteLine(); 
      Console.WriteLine(emp[i].CalcSalary()); 
     } 

     Console.ReadLine(); 
    } 

    public static void sort(string hourNworked, ref double[] values) 
    { 
     char[] rules = { ' ' }; 
     string[] splitArray = hourNworked.Split(rules); 
     values[0] = double.Parse(splitArray[0]); 
     values[1] = double.Parse(splitArray[1]); 
    } 

    public class Employee 
    { 
     private int empNum; 
     private string name; 
     private string address; 
     private double hrlyPay; 
     private double hrsWrkd; 
     private const double FED_TAX = .20; 
     private const double STATE_TAX = .075; 
     private double GROSS; 
     private double overTime; 
     private double overTimePay; 
     private double overTimeHours; 
     private double NET; 
     private double regTime; 
     private double fTaxAmount; 
     private double sTaxAmount; 


     public Employee() 
     { 
      empNum = 0; 
     } 

     public void SetEmpNum(int n) 
     { 
      empNum = n; 
     } 

     public int GetEmpNum() 
     { 
      return empNum; 
     } 
     public void SetEmpName(string n) 
     { 
      name = n; 
     } 
     public string GetEmpName() 
     { 
      return name; 
     } 
     public void SetEmpAddress(string a) 
     { 
      address = a; 
     } 
     public string GetEmpAddress() 
     { 
      return address; 
     } 
     public void SetEmpHrlyPay(double h) 
     { 
      hrlyPay = h; 
     } 
     public double GetEmpHrlyPay() 
     { 
      return hrlyPay; 
     } 
     public void SetEmpHrsWrked(double w) 
     { 
      hrsWrkd = w; 
     } 
     public double GetEmpHrsWrkd() 
     { 
      return hrsWrkd; 
     } 
     public double CalcSalary() 
     { 
      if (hrsWrkd > 40) 
      { 
       overTimeHours = hrsWrkd - 40; 
       overTimePay = hrlyPay * 1.5; 
       overTime = overTimePay * overTimeHours; 
       regTime = (hrsWrkd - overTimeHours) * hrlyPay; 
       GROSS = overTime + regTime; 

       fTaxAmount = (regTime + overTime) * FED_TAX; 
       sTaxAmount = (regTime + overTime) * STATE_TAX; 

       NET = GROSS - (fTaxAmount + sTaxAmount); 

       return NET; 
      } 
      else 
      { 
       regTime = hrlyPay * hrsWrkd; 
       fTaxAmount = regTime * FED_TAX; 
       sTaxAmount = regTime * STATE_TAX; 

       NET = regTime - (fTaxAmount + sTaxAmount); 

       return NET; 
      } 
     } 
    } 
} 
} 
+0

Cookie cutter?什麼? – 2012-08-11 18:09:26

+0

@PeterLillevold - OP可能意味着模板與佔位符。 – Oded 2012-08-11 18:10:09

回答

2

它已經存在了很長一段時間,但我認爲你正在尋找String.PadRight。有了它,你可以把你所有的弦放到一個固定長度,然後一切都會排成一列。

+0

確切地說,謝謝 – Dan 2012-08-11 18:27:12