patternjavaMinor
Download Progress Calculator
Viewed 0 times
downloadprogresscalculator
Problem
Here is my progress calculator class. It is used for downloads/uploads. It takes bytes in and calculates the percentage(to the nearest 5%) that should show on a progress bar. Please let me know how I could improve it. Thanks a lot!!
```
public class ProgressCalculator {
private int totalAmount;
private int progressAmount;
private int amountProgressItems;
private int progressBarPercentage;
//returns if the progress bar needs to change
public boolean progress(int amount) {
progressAmount = progressAmount + amount;
return updateProgressbarPercentage();
}
public int getCurrentValue() {
return progressBarPercentage;
}
//returns if the progressbar needs to change
public boolean addProgressItem(int itemAmount) {
totalAmount = totalAmount + itemAmount;
amountProgressItems++;
return updateProgressbarPercentage();
}
//returns true if has been updated;
private boolean updateProgressbarPercentage() {
int newProgressBarPercentage = round(calculateProgressPercentage());
if (progressBarPercentage == newProgressBarPercentage) {
return false;
} else {
progressBarPercentage = newProgressBarPercentage;
return true;
}
}
public int getAmountOfItems() {
return amountProgressItems;
}
public boolean removeProgressItem(int byteAmount) {
if (amountProgressItems > 0) {
totalAmount = totalAmount - byteAmount;
amountProgressItems--;
}
return updateProgressbarPercentage();
}
private int calculateProgressPercentage() {
double x = progressAmount;
double y = totalAmount;
double result = (x / y) * 100;
return (int) result;
}
private int round(int num) {
int temp = num % 5;
if (temp < 3)
return num - temp;
else
return num + 5 - temp;
}
public void
```
public class ProgressCalculator {
private int totalAmount;
private int progressAmount;
private int amountProgressItems;
private int progressBarPercentage;
//returns if the progress bar needs to change
public boolean progress(int amount) {
progressAmount = progressAmount + amount;
return updateProgressbarPercentage();
}
public int getCurrentValue() {
return progressBarPercentage;
}
//returns if the progressbar needs to change
public boolean addProgressItem(int itemAmount) {
totalAmount = totalAmount + itemAmount;
amountProgressItems++;
return updateProgressbarPercentage();
}
//returns true if has been updated;
private boolean updateProgressbarPercentage() {
int newProgressBarPercentage = round(calculateProgressPercentage());
if (progressBarPercentage == newProgressBarPercentage) {
return false;
} else {
progressBarPercentage = newProgressBarPercentage;
return true;
}
}
public int getAmountOfItems() {
return amountProgressItems;
}
public boolean removeProgressItem(int byteAmount) {
if (amountProgressItems > 0) {
totalAmount = totalAmount - byteAmount;
amountProgressItems--;
}
return updateProgressbarPercentage();
}
private int calculateProgressPercentage() {
double x = progressAmount;
double y = totalAmount;
double result = (x / y) * 100;
return (int) result;
}
private int round(int num) {
int temp = num % 5;
if (temp < 3)
return num - temp;
else
return num + 5 - temp;
}
public void
Solution
It's really hard to tell what this class does. From what you say, I feel like you have a number of
Why? Because it doesn't need any comments to describe what happens here: you just write it in English.
Update - In case you're absolutely sure you like your approach.
First, in this code:
I see magic like
Then:
When you divide
Regarding the whole idea, I'm not sure whether this code works at all, because it's hard to understand how one should use it.
TransferTasks and want to have a sort of ProgressTracker for them. So, as for me, your goal is to make user's code look like this:ProgressTracker progressTracker = new ProgressTracker();
progressTracker.setListener(new ProgressTrackerListener() {
@Override
public void onProgressChanged(double progressPercentage) {
// TODO: update your UI here
}
});
...
TransferTask transferTask = new UploadTask("/home/jiduvah/1.txt");
progressTracker.track(transferTask);
transferTask.start();Why? Because it doesn't need any comments to describe what happens here: you just write it in English.
Update - In case you're absolutely sure you like your approach.
First, in this code:
private int round(int num) {
int temp = num % 5;
if (temp < 3)
return num - temp;
else
return num + 5 - temp;
}I see magic like
5 and 3. Looks like it does exactly what you need to do, and you've even wrote about meaning of 5, but 3 is still a mystery.Then:
double x = progressAmount;
double y = totalAmount;
double result = (x / y) * 100;
return (int) result;When you divide
int by int, you get int. When you divide int by double or double by int, you get double. So you may just write:double result = 100 * progressAmount / (double)totalAmount;Regarding the whole idea, I'm not sure whether this code works at all, because it's hard to understand how one should use it.
Code Snippets
ProgressTracker progressTracker = new ProgressTracker();
progressTracker.setListener(new ProgressTrackerListener() {
@Override
public void onProgressChanged(double progressPercentage) {
// TODO: update your UI here
}
});
...
TransferTask transferTask = new UploadTask("/home/jiduvah/1.txt");
progressTracker.track(transferTask);
transferTask.start();private int round(int num) {
int temp = num % 5;
if (temp < 3)
return num - temp;
else
return num + 5 - temp;
}double x = progressAmount;
double y = totalAmount;
double result = (x / y) * 100;
return (int) result;double result = 100 * progressAmount / (double)totalAmount;Context
StackExchange Code Review Q#13121, answer score: 4
Revisions (0)
No revisions yet.