2014-01-28 51 views
-1
public class SelectedItems 
{ 
    public Item item; 
    public int quantity; 
    public double subtotal = 0.0; 

} 

public class Item 
{ 
    public int itemcode; 
    public string itemname; 
    public double unitprice = 0.0; 
} 

/* These are my classes */ 


class Program 
{ 
    public static ArrayList details = new ArrayList(); 
    public static ArrayList purchased = new ArrayList(); 


    static void Main(string[] args) 
    { 
     Item i1 = new Item(); 
     i1.itemcode = 1111; 
     i1.itemname = "Pants"; 
     i1.unitprice = 950.00; 

     Item i2 = new Item(); 
     i2.itemcode = 2222; 
     i2.itemname = "Dress"; 
     i2.unitprice = 850.00; 

     Item i3 = new Item(); 
     i3.itemcode = 3333; 
     i3.itemname = "Blouse"; 
     i3.unitprice = 650.00; 

     Item i4 = new Item(); 
     i4.itemcode = 4444; 
     i4.itemname = "Shirt"; 
     i4.unitprice = 1500.00; 

     Item i5 = new Item(); 
     i5.itemcode = 5555; 
     i5.itemname = "Belt"; 
     i5.unitprice = 1200.00; 


     details.Add(i1); 
     details.Add(i2); 
     details.Add(i3); 
     details.Add(i4); 
     // details.Add(i5); 

     details.Add(new Item { itemcode = 5555, itemname = "Belt", unitprice = 1200.00 }); 

     /*Console.WriteLine("Enter the code :"); 
     searchItem(Convert.ToInt32(Console.ReadLine())); 
     Console.ReadLine();*/ 

     Console.Write("Number Of Different Items:"); 
     int count = Convert.ToInt32(Console.ReadLine()); 

    while (count > 0) 
    { 
     SelectedItems stm = new SelectedItems(); 
     Console.WriteLine("Enter the code :"); 
     Item im = searchItem(Convert.ToInt32(Console.ReadLine())); 

     if (stm.item == null) 

      Console.WriteLine("no item"); 

     else 
     { 
      Console.WriteLine("Enter the quantity :"); 
      stm.quantity = Convert.ToInt32(Console.Read()); 
      stm.subtotal = stm.item.unitprice * (Convert.ToDouble(stm.quantity)); 

      purchased.Add(stm); 
     } 
     count--; 
    } 
    //printBill(); 
    } 
    public static Item searchItem(int code) 
    { 
     foreach (Item i in details) 
     { 
      if (code == i.itemcode) 
      { 
       return i; 
       Console.WriteLine("{0} : {1} : {2}", i.itemcode, i.itemname, i.unitprice); 
      } 
     } 
     return null; 
    } 

這總是返回沒有item.Can我明白了嗎?我張貼這早些時候,並能得到它正確的,現在還給頭疼需要解決這個問題

+4

你似乎濫用類!也許不要。此外,'請列出'而不是'ArrayList',並且'PascalCase'屬性。 – Ryan

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

0

您正在返回的搜索項到您的項目類的對象,但它什麼都不做。然後,您對您剛剛創建的SelectItem進行比較,因爲您尚未將項目添加到該項目,因此此時該項目爲Null。嘗試改變。

while (count > 0) 
{ 
    SelectedItems stm = new SelectedItems(); 
    Console.WriteLine("Enter the code :"); 
    Item im = searchItem(Convert.ToInt32(Console.ReadLine())); //You are returning an item here and doing nothing 
                   //with it. this is the only time you reference it 
    if (stm.item == null) //You are checking an empty(null) instance of your selectedItems class that you 
          // just created. 
     Console.WriteLine("no item"); 

    else 
    { 
     Console.WriteLine("Enter the quantity :"); 
     stm.quantity = Convert.ToInt32(Console.Read()); 
     stm.subtotal = stm.item.unitprice * (Convert.ToDouble(stm.quantity)); 

     purchased.Add(stm); 
    } 
    count--; 
} 

while (count > 0) 
{ 
    SelectedItems stm = new SelectedItems(); 
    Console.WriteLine("Enter the code :"); 
    Item im = searchItem(Convert.ToInt32(Console.ReadLine())); 

    if (im == null) //Check if searchItem returned an object. 

     Console.WriteLine("no item"); 

    else 
    { 
     stm.Item = im; // Added item to your selected items 
     Console.WriteLine("Enter the quantity :"); 
     stm.quantity = Convert.ToInt32(Console.Read()); 
     stm.subtotal = stm.item.unitprice * (Convert.ToDouble(stm.quantity)); 

     purchased.Add(stm); 
    } 
    count--; 
} 
+0

現在我得到一個FormatException在Item im = searchItem(Convert.ToInt32(Console.ReadLine()));'當我插入** 2 **計數,這發生在第二計數。 .. – user3079086

+0

既然你接受了我的回答,我想你已經明白了。問題在於,您正在使用'Console.Read'而不是'Console.ReadLine'來獲取導致格式異常的緩衝區中的CRLF數量。 –

+0

它工作完美..偉大,但我無法弄清楚如何導致例外既不是你的上述解釋,所以我可以得到一些鏈接或anywy你可以幫助我把它放到我的頭..它會很有幫助 – user3079086

0

試圖改變

if (stm.item == null) 

Console.WriteLine("no item"); 

if (String.IsNullOrEmpty(stm.item)) 

    Console.WriteLine("no item"); 
+0

這會使它顯示「無項目」≥經常,而問題顯然是它顯示太多。 – Ryan

0

嘗試改變環路這頂:

 SelectedItems stm = new SelectedItems(); 
     Console.WriteLine("Enter the code :"); 
     stm.item = searchItem(Convert.ToInt32(Console.ReadLine())); 

它看起來不像你指定的stm.item屬性。

0

ArrayList屬於C#沒有泛型的日子。這是 棄用贊成列表。你不應該使用在新 代碼的ArrayList面向.NET> = 2.0

嘗試使用

public static IList<Item> details = new List<Item>(); 
0

,因爲你寫它控制檯,它永遠不會,因爲它上面的return語句的執行。

更改它:

if (code == i.itemcode) 
{ 
    Console.WriteLine("{0} : {1} : {2}", i.itemcode, i.itemname, i.unitprice); 
    return i; 
} 

並分配結果提出反對你檢查空:

Item stm = new Item(); 
Console.WriteLine("Enter the code :"); 
stm= searchItem(Convert.ToInt32(Console.ReadLine())); 

if (stm == null){//other code} 
+0

,但有一個名爲stm(Selected Items)的類,我已經在其中調用了ITem類.... – user3079086

+0

,但是您將搜索結果分配給了'Item im = searchItem(Convert.ToInt32(Console.ReadLine())) ;'和檢查stm爲空 –

0

變化

if (stm.item == null) 
{ 
    Console.WriteLine("no item"); 
} 

if (im == null) 
{ 
    Console.WriteLine("no item"); 
} 

It Works !!

0

您沒有爲屬性stm.item分配任何值。因此,如果(stm.item == null)將始終爲真,它將始終爲空並且

我不知道你爲什麼使用如果(stm.item == null)那裏,但你必須做一些事情如下所示。

stm.item = "Test"; 
if (stm.item == null) 
    Console.WriteLine("no item"); 

有一個在searchItem功能的一些問題,以及

沒寫return語句後執行。

return i; 
Console.WriteLine("{0} : {1} : {2}", i.itemcode, i.itemname, i.unitprice); 
相關問題