Bug 318 - Pressing reset button do not reset the table to default state
Summary: Pressing reset button do not reset the table to default state
Status: RESOLVED FIXED
Alias: None
Product: Laptop Recommendation System
Classification: Unclassified
Component: Laptop Recommendation System (show other bugs)
Version: unspecified
Hardware: All All
: --- blocker
Deadline: 2019-11-16
Assignee: Matthew
URL:
Depends on:
Blocks:
 
Reported: 2019-11-27 23:52 HKT by Matthew
Modified: 2019-11-28 01:01 HKT (History)
0 users

See Also:


Attachments
Actual result (593.33 KB, image/png)
2019-11-27 23:52 HKT, Matthew
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Matthew 2019-11-27 23:52:05 HKT
Created attachment 30 [details]
Actual result

Overview:
Pressing reset button do not reset the table after submitted input

Steps to Reproduce:
1) Input value(s) to any textfield in the recommendation panel (I inputted "Acer" in the Brand textfield)

2)Press the Submit button (after pressing the submit button the table should sort out laptops of brand Acer)

3)Press the Reset button

Actual Results:
The table remained unchanged

Expected Results:
The table should display the original data array list without any sorting

Build Date & Hardware:
Build 2019-11-15 on Mac OS 10.14.5 & Windows 10
Comment 1 Matthew 2019-11-28 00:48:17 HKT
Debug steps:
1) reproduced the bug as mentioned

2) print out the array list to see what happened

Observations:
The original array list has been modified from the submit action, then added to the table.

For each sorting/searching action, the original array list (imported data from txt file) should not be modified, a copy of the original array list should be created instead for any data processing so that the reset button can reset the table to the default state.

Suggested Solution:
In every function we need to process the data and return a new one to be displayed, we create a new array list to hold the array list passed in.
Comment 2 Matthew 2019-11-28 00:55:27 HKT
(In reply to Matthew from comment #1)
> Debug steps:
> 1) reproduced the bug as mentioned
> 
> 2) print out the array list to see what happened
> 
> Observations:
> The original array list has been modified from the submit action, then added
> to the table.
> 
> For each sorting/searching action, the original array list (imported data
> from txt file) should not be modified, a copy of the original array list
> should be created instead for any data processing so that the reset button
> can reset the table to the default state.
> 
> Suggested Solution:
> In every function we need to process the data and return a new one to be
> displayed, we create a new array list to hold the array list passed in.

Bug update:
The suggested solution does not work, even a new array list [laptopList] is created to hold the copy of the original array list [allLaptops], when we made changes to [laptopList], it modified [allLaptops] as well. 

Simplified function with bug:
public recommend (allLaptops, ...){
  ArrayList<Laptop> laptopList = allLaptops;
  .
  .
  .
  return laptopList;
}
Comment 3 Matthew 2019-11-28 01:01:20 HKT
(In reply to Matthew from comment #2)
> (In reply to Matthew from comment #1)
> > Debug steps:
> > 1) reproduced the bug as mentioned
> > 
> > 2) print out the array list to see what happened
> > 
> > Observations:
> > The original array list has been modified from the submit action, then added
> > to the table.
> > 
> > For each sorting/searching action, the original array list (imported data
> > from txt file) should not be modified, a copy of the original array list
> > should be created instead for any data processing so that the reset button
> > can reset the table to the default state.
> > 
> > Suggested Solution:
> > In every function we need to process the data and return a new one to be
> > displayed, we create a new array list to hold the array list passed in.
> 
> Bug update:
> The suggested solution does not work, even a new array list [laptopList] is
> created to hold the copy of the original array list [allLaptops], when we
> made changes to [laptopList], it modified [allLaptops] as well. 
> 
> Simplified function with bug:
> public recommend (allLaptops, ...){
>   ArrayList<Laptop> laptopList = allLaptops;
>   .
>   .
>   .
>   return laptopList;
> }

Suggested Solution:
Seems the declaration of laptopList cannot direct point to allLaptops, the expression of [ArrayList<Laptop> laptopList = allLaptops;] will create a new array list pointing to the address of each element of allLaptops. Therefore, when we make changes to laptopList, it actually modified the value of that element in both laptopList and allLaptops. 

We can declare the new array list in this way:
  ArrayList<Laptop> laptopList = new ArrayList<Laptop>(allLaptops);

It should first create a clean and new array list without referencing the original array list, then copy every elements from allLaptops to laptopList.