FO4
FO4
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
/**
*
* @author JWOS
*/
public class Manage_BikeStore {
}
public void searchProductByName() {
System.out.print("Please type in Name of Bike you want to search: ");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
// Rút khoảng cách + thành chữ thường để dễ xử lí
name.trim().toLowerCase();
int status = 0;
// Tạo 1 array list để lưu các xe đạp đã tìm được
ArrayList<Bike> foundBike = new ArrayList<Bike>();
for(Bike bk: bikeList) {
if(bk.getName().toLowerCase().contains(name)) {
foundBike.add(bk);
status =1;
}
}
if(foundBike.isEmpty()) {
System.out.println("Have no Product");
} else {
foundBike.sort(Comparator.comparingInt(Bike::getModel_Year));
for(Bike bkk: foundBike ) {
System.out.println(bkk);
}
}
}
public void updateProductInformation() {
String newname = "";
String newid = "";
String newbrandId = "";
String newcategoryId = "";
int newmodelYear = 0;
double newlistPrice = 0;
System.out.println("Please type in id: ");
Scanner sc = new Scanner(System.in);
String id_Input = sc.nextLine();
int status =0;
for (Bike bk : bikeList) {
if (bk.getId().equals(id_Input) ){
//
do {
System.out.println("Name: ");
newname = sc.nextLine();
if(newname.isEmpty()) {
break;
}
if(newname.isEmpty()) {
System.out.println("Name is empty, please type in name:");
}
} while(newname.isEmpty());
do{
System.out.println("Brand ID: ");
newbrandId = sc.nextLine();
if(newbrandId.isEmpty()) {
break;
}
if(checkbrandIdInFileOrNot(newbrandId)== false ) {
System.out.print("Brand is not in brand list,please type in again: ");
}
}while(checkbrandIdInFileOrNot(newbrandId)== false);
do {
System.out.println("Category ID: ");
newcategoryId = sc.nextLine();
if(newcategoryId.isEmpty()) {
break;
}
if(checkCategoryIdInFileOrNot(newcategoryId)== false) {
System.out.println("Invalid categoryId, please try again: ");
}
}while(checkCategoryIdInFileOrNot(newcategoryId)== false);
do {
System.out.println("Model Year: ");
newmodelYear = sc.nextInt();
if(newmodelYear==0) {
break;
}
if(newmodelYear>2024) {
} else {
bk.setName(newname);
}
if(newbrandId.isEmpty()) {
} else {
bk.setBrand_Id(newbrandId);
}
if(newcategoryId.isEmpty()) {
} else {
bk.setCategory_Id(newcategoryId);
}
if(String.valueOf(newmodelYear).isEmpty()) {
} else {
bk.setModel_Year(newmodelYear);
}
if(String.valueOf(newlistPrice).isEmpty()) {
} else {
bk.setList_Price(newlistPrice);
}
saveProductsToFile();
status = 1;
}
}
if(status ==0) {
System.out.println("Failed");
}
else {
System.out.println("Successfully");
}
}
public void deleteProduct() {
System.out.println("Please type in id of bike you want to delete");
Scanner sc = new Scanner(System.in);
String id_del = sc.nextLine();
for(Bike bk: bikeList) {
if(bk.getId().equals(id_del)) {
bikeList.remove(bk);
saveProductsToFile();
System.out.println("Delete completed");
return;
}
}
System.out.println("Failed to delete");
}
public void saveProductsToFile() {
try (BufferedWriter writer = new BufferedWriter(new
FileWriter("Product.txt"))) {
for (Bike bk : bikeList) {
writer.write(bk.getId() + "," + bk.getName() + "," +
bk.getBrand_Id() + "," +
bk.getCategory_Id() + "," + bk.getModel_Year() + "," +
bk.getList_Price());
writer.newLine();
}
} catch (Exception E ) {
System.out.println("Error in saving bike");
}
}
// private void loadProductFromFile() {
// File file = new File("Product.txt");
// if (!file.exists()) {
// return;
// }
// try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
// String line;
// while ((line = reader.readLine()) != null) {
// String[] phan = line.split(",");
// if (phan.length == 6) {
// String id = phan[0];
// String name = phan[1];
// String brandId = phan[2];
// String categoryId = phan[3];
// int modelYear = Integer.parseInt(phan[4]);
// double listPrice = Double.parseDouble(phan[5]);
// bikeList.add(new Bike(id, name, brandId, categoryId,
modelYear, listPrice));
// }
// }
// } catch (IOException e) {
// System.out.println("Failed");
// }
//
// }
public void printAllProduct() {
Collections.sort(bikeList, new Comparator<Bike>() {
@Override
public int compare(Bike p1, Bike p2) {
if (p1.getList_Price() != p2.getList_Price()) {
return Double.compare(p2.getList_Price(),
p1.getList_Price()); // Descending by price
} else {
return p1.getName().compareTo(p2.getName()); // Ascending by
name
}
}
});
for (Bike bk : bikeList ) {
System.out.println(bk);
}
}
public boolean unique_Id(String id) {
for(Bike bk: bikeList) {
if(bk.getId().compareToIgnoreCase(id)==0) {
return false;
}
}
return true;
}
public boolean checkbrandIdInFileOrNot(String brand_Id) {
boolean found = false;
try (BufferedReader br = new BufferedReader(new FileReader("D:\\Lab211\\
LAB211_FA24\\01_Brand.txt"))) {
String line;
return found;
}
public boolean checkCategoryIdInFileOrNot(String Category_Id) {
boolean found = false;
try (BufferedReader br = new BufferedReader(new FileReader("D:\\Lab211\\
LAB211_FA24\\01_Category.txt"))) {
String line;
}
return found;
}
}