2016-04-12 22 views
1

Programlama dersi için ev ödevimi bitirmeye çalışıyorum, maalesef aptallığım yüzünden, bunun yarısı boyunca sıkışıp kalıyorum. Oluşturduğum ArrayList sürekli 0 endeksinin üzerine yazıyor. İşteJAVA farklı sınıf ArrayList nesnesine nesne ekleyerek, sürekli 0 dizinini değiştirin ve daha büyük olsun yok

oluşturarak faturalar için benim sınıfları şunlardır:

import java.util.ArrayList; 

public class Order 
{ 
    private String customerName; 
    private ArrayList<LineItem> items = new ArrayList<LineItem>(); 

    public Order(String customerName) 
    { 
     this.customerName = customerName; 
    } 

    public String getCustomerName() 
    { 
     return customerName; 
    } 

    public ArrayList<LineItem> getItems() 
    { 
     return this.items; 
    } 

    public double getOrderTotal() 
    { 

     double totalOrder = items.get(0).getTotalPrice(); 
     return totalOrder; 
    } 

    public void addItem(String description,double unitPrice, int quantity) 
    { 
     LineItem object = new LineItem(description,unitPrice,quantity); 
     items.add(object); 
    } 

    public String toString() 
    { 
     String.format("%n%-20s%-15s%-15f%-15f",items.get(0).getDescription(), 
    items.get(0).getQuantity(),items.get(0).getUnitPrice(),getOrderTotal()); 

     return p;`       

    } 

} 

public class LineItem 
{ 
    private String description; 
    private double unitPrice; 
    private int quantity; 

    public LineItem(String description,double unitPrice, int quantity) 
    { 
     this.description = description; 
     this.unitPrice = unitPrice; 
     this.quantity = quantity; 
    } 
    public String getDescription() 
    { 
     return this.description; 
    } 

    public double getUnitPrice() 
    { 
     return this.unitPrice; 
    } 

    public int getQuantity() 
    { 
     return this.quantity; 
    } 

    public double getTotalPrice() 
    { 
     return this.unitPrice * this.quantity; 
    } 
    public String toString() 
    { 
     return String(this.description,this.unitPrice,this.quantity)  
    } 
} 

VE bir parçası ... LOOP ise ana sınıfı için

do 
{ 

    customerName = AssignmentHelper.getRequiredStringInput("Please enterthe customer's name: ","A customer name must be entered!"); 

    newOrder = new Order(customerName); 
    newOrder.addItem(description,unitPrice,quantity); 

} while(Character.toString(userAnswer).equalsIgnoreCase ("Y")); 

cevap

4

Kişisel döngü her tekrarında yeni Order nesnesi oluşturur:

newOrder = new Order(customerName); 

Her Order nesnesi yeni bir boşalttı ArrayList, bu yüzden ilk endeksin her zaman üzerine yazıldığını görüyoruz.

Tüm öğeleri tek bir ArrayList tutmak istiyorsanız, do-while döngüsünden önce tek bir Order nesnesi oluşturmalısınız.

newOrder = new Order(customerName); 
do { 
    ... 
    newOrder.addItem(description,unitPrice,quantity); 
} while(Character.toString(userAnswer).equalsIgnoreCase ("Y")); 
+0

OMG, bu gerçekten çok aptalca bir hataydı –

+0

ArrayList aracılığıyla nasıl tost yapabileceğinizi biliyor musunuz? (ToString(). İdeal olarak, LineItem'den TO String yöntemini geçersiz kılacağını varsayalım. –

+0

@DenysFiialko Tam olarak ne yapmak istiyorsun? ArrayList'in içeriğini yazdırmak? – Eran

0

Döngüde, iki alana (müşteri adı ve satır öğelerinin Arraylist'i) sahip yeni bir sipariş nesnesi oluşturulur. Başlangıçta ArrayList boş olacaktır. AddItem nesnesine yapılan çağrı yeni bir Lineitem nesnesi oluşturur ve bunu ArrayList öğelerine ekler. Bu nedenle, lineItem nesnesi her zaman dizin sıfıra eklenecektir.

Sadece yeni bir müşteri eklendiğinde yeni bir nesne oluşturun. Anahtarın müşteri adı olduğu bir HashMap'iniz olabilir. Anahtar zaten mevcutsa, aynı nesneyi ekleyin, yeni bir sipariş nesnesi oluşturun ve Harita'ya.

+0

Teşekkür ederim! Hızlı yanıt için teşekkür ederiz –