Game changer
Game changer

Reputation: 41

ArrayList in java overwrites previous data, even being non static

Everytime I input a new Item, the previous one added to the list is overwritten. I tried to run the debug and it seems that instead of creating a new item, the old one is just replaced with the new information. I've tried to look it up many times but I cannot find the problem.

There is no static variables and the instance is inside the loop for.

package application;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;

import entities.Client;
import entities.Order;
import entities.OrderItem;
import entities.Product;
import entities.enums.OrderStatus;

public class Program {

    public static void main(String[] args) throws ParseException {
        
        
    Locale.setDefault(Locale.US);
    Scanner sc = new Scanner(System.in);
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    
    
    System.out.println("Enter Client Data: ");
    System.out.println("Name:");
    String name = sc.nextLine();
    
    System.out.println("Email: ");
    String email = sc.nextLine();
    
    System.out.println("Birth Date (DD/MM/YYYY): ");
    Date date = sdf.parse(sc.next());
    Client client = new Client(name, email, date);
    
    System.out.println("\nEnter order data ");
    System.out.println("Status: ");
    OrderStatus status = OrderStatus.valueOf(sc.next());
    
    
    System.out.println("How many items to this order?");
    Integer itemsQuantity = sc.nextInt();
    sc.nextLine();
    Double price = null;
    Integer quantity = null;
    String pname = null;
    Date dateS = null;
    Order order=null;
    
    for(int i=0; i<itemsQuantity; i++) {
        
        System.out.printf("Enter #%d item data: ", i+1);
        System.out.println("\nProduct Name: ");
        pname = sc.nextLine();
        System.out.println("Product Price: ");
        price = sc.nextDouble();
        System.out.println("Quantity: ");
        quantity = sc.nextInt();
        if(i>=0) {
            sc.nextLine();
        }
        dateS = new Date(System.currentTimeMillis());
        Product product = new Product(pname, price);
        OrderItem orderItem = new OrderItem(quantity, price, product);
        order = new Order(dateS, status, client);
        order.addItem(orderItem);
    }

That is the class Order

package entities;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import entities.enums.OrderStatus;

public class Order {

    private Date moment;
    private OrderStatus status;

    private Client client;
    private List<OrderItem> items = new ArrayList<OrderItem>();

    public Order() {
    }

    public Order(Date moment, OrderStatus status, Client client) {
        this.moment = moment;
        this.status = status;
        this.client = client;
    }

    public Date getMoment() {
        return moment;
    }

    public void setMoment(Date moment) {
        this.moment = moment;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public OrderStatus getStatus() {
        return status;
    }

    public void setStatus(OrderStatus status) {
        this.status = status;
    }
    
    public List<OrderItem> getItems() {
        return items;
    }

    public void setItems(List<OrderItem> items) {
        this.items = items;
    }

    public void addItem(OrderItem item) {
        items.add(item);
    }

    public void removeItem(OrderItem item) {
        items.remove(item);
    }

    public Double total() {
        Double totalp = null;

        for (OrderItem i : items) {
            totalp =+ i.subtotal();

        }
        return totalp;
    }
    
    public void print() {
        for (OrderItem x : items) {
            System.out.println(x.getQuantity() +"   "+ x.getPrice() + "   "+ x.getProduct().getName());
        }
    }
    
}

Upvotes: 0

Views: 287

Answers (1)

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

It is not overwritten the list in the Order. You are creating every time a new Order. So every time the new Order has an empty list of items and you add to it a single item having an order with a single item.

You need to move the code

order = new Order(dateS, status, client);

outside the for loop. You need also to put outside the for loop

dateS = new Date(System.currentTimeMillis());

but I suggest to remove the dateS at all and replace

order = new Order(dateS, status, client);

with

// It is not necessary to use the format new Date(System.currentTimeMillis())
// Because the default implementation of new Date take the 
// current time to init the date
order = new Order(new Date(), status, client);

Upvotes: 2

Related Questions