0% found this document useful (0 votes)
2 views

FO4

ssssssss

Uploaded by

gianghia10009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

FO4

ssssssss

Uploaded by

gianghia10009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bikestore_management;

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 {

private ArrayList<Bike> bikeList = new ArrayList<>();

public void addProduct() {


String name = "";
String id = "";
String brandId = "";
String categoryId = "";
int modelYear = 0;
double listPrice = 0;
Scanner sc = new Scanner(System.in);
//Nhập ID và check validation
do {
System.out.print("ID: ");
id = sc.nextLine();
if(unique_Id(id)== false) {
System.out.println("Validation Unique: True,please try again");
}
}while (unique_Id(id)==false);
//Nhập name và check validation
do {
System.out.print("Name: ");
name = sc.nextLine();
if(name.isEmpty()) {
System.out.println("Name is empty, please type in name:");
}
} while(name.isEmpty());

//Nhập Brand ID và check validation


do{
System.out.print("Brand ID: ");
brandId = sc.nextLine();
if(checkbrandIdInFileOrNot(brandId)== false ) {
System.out.print("Brand is not in brand list,please type in again: ");
}
}while(checkbrandIdInFileOrNot(brandId)== false);
//Nhập Category ID và check validation
do {
System.out.print("Category ID: ");
categoryId = sc.nextLine();
if(checkCategoryIdInFileOrNot(categoryId)== false) {
System.out.println("Invalid categoryId, please try again: ");
}
}while(checkCategoryIdInFileOrNot(categoryId)== false);
//Nhập Model Year và check validation
do {
System.out.print("Model Year: ");
modelYear = sc.nextInt();
if(modelYear>2024) {

System.out.println("Invalid Year, please try again");}


} while(modelYear >2024);
//Nhập list price và check validation
do {
System.out.print("List Price: ");
listPrice = sc.nextDouble();
if(listPrice<0) {
System.out.println("Invalid Price, please try again");
}
} while ( listPrice <0);
sc.nextLine();
// Tạo object với các thông tin trên
Bike bike = new Bike(id, name, brandId, categoryId, modelYear, listPrice);
bikeList.add(bike);
saveProductsToFile();
System.out.println("Add Bike Successfully");

}
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) {

System.out.println("Invalid Year, please try again");}


} while(newmodelYear >2024);
do {
System.out.println("List Price: ");
newlistPrice = sc.nextDouble();
if((char)newlistPrice=='\n') {
break;
}
if(newlistPrice<0) {
System.out.println("Invalid Price, please try again");
}
} while ( newlistPrice <0);
if(newname.isEmpty()) {

} 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;

while ((line = br.readLine()) != null) {


String[] lines = line.split(",");
if (lines[0].trim().equals(brand_Id.trim())) {
found = true;
break;
}
}
}catch (IOException e) {
System.out.println("An error occurred while reading the file.");
e.printStackTrace();

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;

while ((line = br.readLine()) != null) {


String[] lines = line.split(",");
if (lines[0].trim().equals(Category_Id.trim())) {
found = true;
break;
}
}
}catch (IOException e) {
System.out.println("An error occurred while reading the file.");
e.printStackTrace();

}
return found;
}
}

You might also like