LΓ mα»t lαΊp trΓ¬nh viΓͺn lΓ m viα»c khΓ‘ nhiα»u vα»i terminal, mΓ¬nh thΖ°α»ng xuyΓͺn thαΊ₯y cΓ‘c lα»nh chαΊ‘y trΓͺn console cΕ©ng cΓ³ thanh tiαΊΏn trΓ¬nh Δα» thα» hiα»n phαΊ§n trΔm hoΓ n thΓ nh cΓ΄ng viα»c cΕ©ng khΓ‘ hay ho. VΓ¬ vαΊy, trong bΓ i viαΊΏt nΓ y, chΓΊng ta cΓΉng thα» lΓ m mα»t thanh Progress Bar vα»i Java nhΓ©.
TrΖ°α»c tiΓͺn, mΓ¬nh phΓ‘c thαΊ£o ra giao diα»n mΓ mΓ¬nh sαΊ½ lΓ m. Giao diα»n khΓ‘ ΔΖ‘n giαΊ£n thΓ΄i. ΔαΊ§u tiΓͺn lΓ mα»t cΓ‘i spinner βquay quayβ Δα» chΓΊng ta biαΊΏt chΖ°Ζ‘ng trΓ¬nh vαΊ«n Δang hoαΊ‘t Δα»ng. TiαΊΏp theo lΓ thanh tiαΊΏn trΓ¬nh Δược fill theo ΔΓΊng tα» lα» phαΊ§n trΔm giΓ‘ trα» hiα»n tαΊ‘i so vα»i giΓ‘ trα» lα»n nhαΊ₯t. Cuα»i cΓΉng lΓ hiα»n thα» sα» liα»u hiα»n tαΊ‘i so vα»i giΓ‘ trα» lα»n nhαΊ₯t. Khi thΓ nnh cΓ΄ng thΓ¬ spinner sαΊ½ Δα»i thΓ nh dαΊ₯u tick, nαΊΏu cΓ³ lα»i thΓ¬ spinner sαΊ½ Δα»i thΓ nh dαΊ₯u x biα»u thα» thαΊ₯t bαΊ‘i.
Dα»±a vΓ o giao diα»n phΓ‘c thαΊ£o α» trΓͺn, chΓΊng ta cΓ³ thα» mΖ°α»ng tượng Δược class ProgressBar
sαΊ½ cΓ³ nhα»―ng phΖ°Ζ‘ng thα»©c nhΖ° sau:
public class ProgressBar
-----------------------------------------------------
void setMaxRange(double maxRange)
void setValue(double value)
void reportSuccess()
void reportError()
void render()
- setMaxRange(double maxRange): Set giΓ‘ trα» lα»n nhαΊ₯t của ProgressBar.
- setValue(double value): CαΊp nhαΊt giΓ‘ trα» hiα»n tαΊ‘i của ProgressBar.
- reportSuccess(): Gα»i phΖ°Ζ‘ng thα»©c nΓ y Δα» hiα»n thα» dαΊ₯u βtickβ bΓ‘o thΓ nh cΓ΄ng.
- reportError(): Hiα»n thα» dαΊ₯u βxβ bΓ‘o thαΊ₯t bαΊ‘i.
- render(): Chα»u trΓ‘ch nhiα»m tΓnh toΓ‘n vΓ print lΓͺn console.
BΓ i viαΊΏt nΓ y, mΓ¬nh dΓΉng Java Δα» cΓ i ΔαΊ·t. Vα»i cΓ‘c ngΓ΄n ngα»― khΓ‘c cΕ©ng tΖ°Ζ‘ng tα»± miα» n lΓ bαΊ‘n hiα»u tΖ° tΖ°α»ng cα»t lΓ΅i.
2.1. CΖ‘ bαΊ£n
TrΖ°α»c hαΊΏt, ta khai bΓ‘o class ProgressBar
vα»i cΓ‘c thuα»c tΓnh cΖ‘ bαΊ£n:
- width: Thα» hiα»n Δα» dΓ i của thanh tiαΊΏn trΓ¬nh.
- value: GiΓ‘ trα» hiα»n tαΊ‘i.
- maxRange: GiΓ‘ trα» lα»n nhαΊ₯t.
1 |
|
CΓ i ΔαΊ·t cΓ‘c setter cho value
vΓ maxRange
. BαΊ‘n cΓ³ thα» cΓ i ΔαΊ·t thΓͺm setter cho width
cΕ©ng Δược.
public void setMaxRange(double maxRange) {
this.maxRange = maxRange;
}
public void setValue(double value) {
this.value = value;
this.render();
}
TiαΊΏp theo chΓΊng ta sαΊ½ cΓ i ΔαΊ·t hΓ m render()
lΓ m nhiα»m vα»₯ in toΓ n bα» thanh tiαΊΏn trΓ¬nh lΓͺn mΓ n hΓ¬nh console. Δα» in Δược thanh tiαΊΏn trΓ¬nh, chΓΊng ta lΓ m cΓ‘c bΖ°α»c sau:
- TΓnh tα» lα» giα»―a giΓ‘ trα» hiα»n tαΊ‘i vΓ giΓ‘ trα» lα»n nhαΊ₯t, quy vα» sα» lượng Γ΄ trong Δα» dΓ i thanh tiαΊΏn trΓ¬nh.
- In ra sα» Γ΄ ΔΓ£ hoΓ n thΓ nh bαΊ±ng kΓ½ tα»±
#
. - In cΓ‘c Γ΄ cΓ²n lαΊ‘i bαΊ±ng kΓ½ tα»±
-
.
Δα» tiα»n tαΊ‘o chuα»i lαΊ·p, cΓ‘c bαΊ‘n cΓ³ thα» viαΊΏt hΓ m utility sau:
public class Strings {
public static String repeat(char c, int count) {
char[] arr = new char[count];
Arrays.fill(arr, c);
return new String(arr);
}
}
Khi ΔΓ³ hΓ m render()
sαΊ½ nhΖ° sau:
public void render() {
int currentBlock = (int) (value / maxRange * width);
char symbol="o";
String filledBlock = Strings.repeat('#', currentBlock);
String remainBlock = Strings.repeat('-', width - currentBlock);
String text = String.format("%s [%s%s] %.0f/%.0f\r", symbol, filledBlock, remainBlock, value, maxRange);
System.out.print(text);
}
Test thα» vα»i hΓ m main
.
public static void main(String[] args) throws InterruptedException {
final int MAX = 100;
Random rand = new Random();
ProgressBar p = new ProgressBar();
p.setMaxRange(MAX);
for (int i = 1; i <= 55; i++) {
p.setValue(i);
Thread.sleep(50 + rand.nextInt(300));
}
}
VΓ kαΊΏt quαΊ£ lΓ :
o [######################------------------] 55/100
CΕ©ng khΓ‘ giα»ng rα»i phαΊ£i khΓ΄ng π. KΓ½ tα»± o
mΓ¬nh ΔαΊ·t trΖ°α»c α» vα» trΓ ΔΓ³ Δα» thay thαΊΏ bαΊ±ng cΓ‘i spinner βquay quayβ α» bΖ°α»c tiαΊΏp theo.
2.2. Animation vα»i Timer
Δα» lΓ m animation, chΓΊng ta cαΊ§n mα»t chuα»i cΓ‘c kΓ½ tα»± thay Δα»i liΓͺn tα»₯c theo thα»i gian. α» ΔΓ’y mΓ¬nh sαΊ½ sα» dα»₯ng chuα»i kΓ½ tα»± nhΖ° bΓͺn dΖ°α»i vΓ khai bΓ‘o vΓ o class ProgressBar
.
private String animation = "β β β Ήβ Έβ Όβ ΄β ¦β §β β ";
private int animationIndex = 0;
private final Timer timer;
CΓ i ΔαΊ·t mα»t TimerTask
và o constructor của ProgressBar
Δα» tΔng animationIndex
sau mα»i 100 ms.
public ProgressBar() {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
animationIndex++;
render();
}
}, 100, 100);
}
Sα»a lαΊ‘i mα»t chΓΊt α» hΓ m render()
:
public void render() {
int currentBlock = (int) (value / maxRange * width);
char symbol = animation.charAt(animationIndex % animation.length());
String filledBlock = Strings.repeat('#', currentBlock);
String remainBlock = Strings.repeat('-', width - currentBlock);
String text = String.format("%s [%s%s] %.0f/%.0f\r", symbol, filledBlock, remainBlock, value, maxRange);
System.out.print(text);
}
KαΊΏt quαΊ£:
Tuy nhiΓͺn, sau khi hoΓ n tαΊ₯t, chΖ°Ζ‘ng trΓ¬nh khΓ΄ng dα»«ng lαΊ‘i, nΓ³ vαΊ«n tiαΊΏp tα»₯c βquay quayβ vΓ chΓΊng ta phαΊ£i reset lαΊ‘i Timer
Δα» tαΊ―t animation. Δα» bΓ‘o hiα»u hoΓ n tαΊ₯t, chΓΊng ta implement thΓͺm method stop()
bΓͺn trong class ProgressBar
vΓ gα»i method nΓ y khi muα»n dα»«ng animation.
public void stop() {
timer.cancel();
}
2.3. ThΓ nh cΓ΄ng vΓ thαΊ₯t bαΊ‘i
Sau khi kαΊΏt thΓΊc tΓ‘c vα»₯, cho dΓΉ lΓ thΓ nh cΓ΄ng hay thαΊ₯t bαΊ‘i, chΓΊng ta Δα»u phαΊ£i ngΖ°ng animation lαΊ‘i, khΓ΄ng hiα»n thα» spinner nα»―a mΓ thay vΓ o ΔΓ³ lΓ icon biα»u thα» thΓ nh cΓ΄ng hoαΊ·c thαΊ₯t bαΊ‘i.
Δα» lΓ m Δiα»u nΓ y, chΓΊng ta thΓͺm vΓ o 1 field flag
, Δα»ng thα»i cΓ i ΔαΊ·t method reportSuccess()
vΓ reportError()
.
private int flag = 0;
public void reportSuccess() {
this.flag = 1;
this.render();
this.stop();
}
public void reportError() {
this.flag = -1;
this.render();
this.stop();
}
PhΖ°Ζ‘ng thα»©c render()
cΕ©ng sα»a lαΊ‘i mα»t tΓ.
private char getSymbol() {
switch (flag) {
case 1: return '\u2714';
case -1: return '\u2718';
default: return animation.charAt(animationIndex % animation.length());
}
}
public void render() {
int currentBlock = (int) (value / maxRange * width);
char symbol = getSymbol();
String filledBlock = Strings.repeat('#', currentBlock);
String remainBlock = Strings.repeat('-', width - currentBlock);
String text = String.format("%s [%s%s] %.0f/%.0f\r", symbol, filledBlock, remainBlock, value, maxRange);
System.out.print(text);
}
VΓ giα» test lαΊ‘i vα»i hΓ m main()
vα»i trΖ°α»ng hợp thΓ nh cΓ΄ng.
public static void main(String[] args) throws InterruptedException {
final int MAX = 100;
Random rand = new Random();
ProgressBar p = new ProgressBar();
p.setMaxRange(MAX);
for (int i = 1; i <= MAX; i++) {
p.setValue(i);
Thread.sleep(rand.nextInt(50));
}
p.reportSuccess();
}
KαΊΏt quαΊ£ lΓ :
CΓΉng test trΖ°α»ng hợp thαΊ₯t bαΊ‘i luΓ΄n nΓ o.
public static void main(String[] args) throws InterruptedException {
final int MAX = 100;
Random rand = new Random();
ProgressBar p = new ProgressBar();
p.setMaxRange(MAX);
try {
for (int i = 1; i <= MAX; i++) {
p.setValue(i);
Thread.sleep(50 + rand.nextInt(50));
if (i >= 45) {
throw new RuntimeException("Error while process");
}
}
p.reportSuccess();
} catch (Exception ex) {
p.reportError();
}
}
VΓ kαΊΏt quαΊ£:
VαΊy lΓ chΓΊng ta ΔΓ£ hoΓ n thiα»n mα»t Progress Bar trΓͺn mΓ n hΓ¬nh console vα»i ngΓ΄n ngα»― Java. CΓ‘c bαΊ‘n cΓ³ thα» tham khαΊ£o source code của toΓ n bα» bΓ i viαΊΏt.
Khi nΓ o rαΊ£nh mΓ¬nh sαΊ½ hΖ°α»ng dαΊ«n cΓ‘c bαΊ‘n lΓ m thΓͺm mα»t tΓ mΓ u mΓ¨ vΓ o thanh Progress Bar nα»―a nhΓ© π.
#TαΊ‘o #Progress #Bar #trong #Java #Console