0% found this document useful (0 votes)
3 views7 pages

Hàng Hóa

The document outlines a Java program for managing a list of goods (HangHoa) with classes for different types of goods, including HangDienMay and HangThuCong. It includes methods for reading data from a file, calculating total prices, filtering goods by manufacturer, and summarizing sales by date and month. The program also allows for the removal of items from the inventory and displays various statistics related to the goods.

Uploaded by

messsiis102
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)
3 views7 pages

Hàng Hóa

The document outlines a Java program for managing a list of goods (HangHoa) with classes for different types of goods, including HangDienMay and HangThuCong. It includes methods for reading data from a file, calculating total prices, filtering goods by manufacturer, and summarizing sales by date and month. The program also allows for the removal of items from the inventory and displays various statistics related to the goods.

Uploaded by

messsiis102
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/ 7

Hàng Hóa

001 12/12/2020 100 200 TN


002 10/12/2020 150 130 NK
003 11/10/2020 110 210 Nam Son
004 21/10/2020 110 210 TN
005 25/11/2020 121 590 TN
006 21/10/2020 110 250 Dien Luc
007 11/12/2020 115 420 TN
008 25/5/2020 130 510 TN
009 21/10/2020 110 210 Duy Tan
NgayThang

package hanghoa;

public class NgayThang {


private int ngay, thang, nam;

public NgayThang() {
}

public NgayThang(int ngay, int thang, int nam) {


this.ngay = ngay;
this.thang = thang;
this.nam = nam;
}
public NgayThang(String st){
String s[]= st.split("/");
ngay = Integer.parseInt(s[0]);
thang = Integer.parseInt(s[1]);
nam = Integer.parseInt(s[2]);
}

@Override
public String toString() {
return ngay+"/"+thang+"/"+nam;
}

public int getNgay() {


return ngay;
}

public void setNgay(int ngay) {


this.ngay = ngay;
}

public int getThang() {


return thang;
}

public void setThang(int thang) {


this.thang = thang;
}

public int getNam() {


return nam;
}

public void setNam(int nam) {


this.nam = nam;
}
public static void main(String[] args) {
NgayThang a=new NgayThang("12/12/20202");
System.out.println(a);
}
}
HangHoa

package hanghoa;

public abstract class HangHoa {


private String maH;
private int soLuong;
private NgayThang ngayNhap;
private double donGia;

public HangHoa() {
}

public HangHoa(String maH, NgayThang ngayNhap, int soLuong, double donGia) {


this.maH = maH;
this.soLuong = soLuong;
this.ngayNhap = ngayNhap;
this.donGia = donGia;
}

public String getMaH() {


return maH;
}

public void setMaH(String maH) {


this.maH = maH;
}

public int getSoLuong() {


return soLuong;
}

public void setSoLuong(int soLuong) {


this.soLuong = soLuong;
}

public NgayThang getNgayNhap() {


return ngayNhap;
}

public void setNgayNhap(NgayThang ngayNhap) {


this.ngayNhap = ngayNhap;
}

public double getDonGia() {


return donGia;
}

public void setDonGia(double donGia) {


this.donGia = donGia;
}
@Override
public String toString() {
return maH + " " + ngayNhap + " " + soLuong +" " + donGia + ".";
}

public abstract double thanhTien();

}
HangDienMay

package hanghoa;
public class HangDienMay extends HangHoa {
private String noiSanXuat;

public HangDienMay() {
}

public HangDienMay(String maH, NgayThang ngayNhap, int soLuong, double


donGia,String noiSanXuat) {

super(maH, ngayNhap , soLuong,donGia);


this.noiSanXuat = noiSanXuat;
}

public String getNoiSanXuat() {


return noiSanXuat;
}

public void setNoiSanXuat(String noiSanXuat) {


this.noiSanXuat = noiSanXuat;
}

@Override
public String toString() {
return "HangDienMay{" +super.toString()+ " " + noiSanXuat +" thanh tien=
"+thanhTien()+ '}';
}

public double thanhTien() {


if (noiSanXuat.equalsIgnoreCase("TN")){
return super.getSoLuong()*super.getDonGia();
}else{
if(noiSanXuat.equalsIgnoreCase("NK")){
return super.getSoLuong()*super.getDonGia()*1.2;
}else return -1;
}
}

public static void main(String[] args) {


HangDienMay a= new HangDienMay("001", new NgayThang("12/05/2023") , 100,
200,"NK");
System.out.println(a);
}
}
HangThuCong

package hanghoa;
public class HangThuCong extends HangHoa {
private String nhaSanXuat;

public HangThuCong() {
}

public HangThuCong(String maH, NgayThang ngayNhap, int soLuong, double donGia,


String nhaSanXuat) {
super(maH, ngayNhap, soLuong, donGia);
this.nhaSanXuat = nhaSanXuat;
}

public String getNhaSanXuat() {


return nhaSanXuat;
}

public void setNhaSanXuat(String nhaSanXuat) {


this.nhaSanXuat = nhaSanXuat;
}
@Override
public String toString() {
return "HangThuCong{" +super.toString()+ " " + nhaSanXuat +" thanh tien=
"+thanhTien()+ '}';
}
@Override
public double thanhTien() {
return super.getSoLuong()*super.getDonGia();
}

public static void main(String[] args) {


HangThuCong a= new HangThuCong("001", new NgayThang("12/05/2023") , 100,
200,"TN");
System.out.println(a);
}

}
DanhSachHangHoa

package hanghoa;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class DanhSachHangHoa {


ArrayList<HangHoa> a= new ArrayList<>();
public void docFile(String fileName){
try {
File f= new File(fileName);
if(f.exists()){
System.out.println("Ok");
Scanner read = new Scanner(f);
while(read.hasNext()){
String lines[] = read.nextLine().split(" ");
String ma= lines[0];
NgayThang tg = new NgayThang(lines[1]);
int sl = Integer.parseInt(lines[2]);
double dg= Double.parseDouble(lines[3]);
if(lines[4].equalsIgnoreCase("TN")||
lines[4].equalsIgnoreCase("NK")){
a.add(new HangDienMay(ma,tg,sl,dg,lines[4]));
}else{
a.add(new HangThuCong(ma,tg,sl,dg,lines[4]));
}

}
}else{
System.out.println("ko doc dc file!");
}
} catch (FileNotFoundException | NumberFormatException e) {
}
}

public void xuat(ArrayList a){


for(Object x: a)
System.out.println(x);
System.out.println();
}
public ArrayList tachHang(String nsx){
ArrayList b = new ArrayList();
for(HangHoa x: a)
if(x.getClass().getSimpleName().equalsIgnoreCase("HangDienMay")){
if(((HangDienMay)x).getNoiSanXuat().equalsIgnoreCase(nsx)){
b.add(x);
}

}
return b;
}
public ArrayList<HangHoa> tach(String Loai){
ArrayList<HangHoa> b = new ArrayList<>();
for(HangHoa x:a){
if(x.getClass().getSimpleName().equalsIgnoreCase(Loai)){
b.add(x);

}
}
return b;
}
public double tongThanhTien(ArrayList<HangHoa> tien){
double s=0;
for(HangHoa x: tien){
s=s+x.thanhTien();
}
return s;
}

public double maxHoaDon() {


double max = 0;
for (HangHoa x : a) {
double tongHoaDon = x.thanhTien();
if (tongHoaDon > max) {
max = tongHoaDon;
}
}
return max;
}
public void xoaHoaDon(){
Scanner keyboard = new Scanner(System.in);
System.out.print("nhap ma hang can xoa: ");
String x = keyboard.next();
for (int i=0;i<a.size();i++){
if(a.get(i).getMaH().equalsIgnoreCase(x)){
a.remove(i);
break;
}
}
System.out.println("DS hang sao khi xoa mat hang co ma "+x+" la: ");
xuat( a);
}
public void tongTheoNgay() {
ArrayList<NgayThang> tg = new ArrayList<>();
for (HangHoa x : a) {
boolean kt = true;
for (NgayThang y : tg) {
if (x.getNgayNhap().equals(y)) {
kt = false;
break;
}
}
if (kt) {
double s = 0;
for (HangHoa z : a) {
if (x.getNgayNhap().equals(z.getNgayNhap())) {
s = s + z.thanhTien();
}
}
tg.add(x.getNgayNhap());
System.out.println("Tong tien ngay " + x.getNgayNhap()+ "= " + s);
}
}
}

public void tongTheoThang() {


ArrayList<NgayThang> tg = new ArrayList<>();
for (HangHoa x : a) {
boolean kt = true;
for (NgayThang y : tg) {
if (x.getNgayNhap().getThang() == y.getThang()) {
kt = false;
break;
}
}
if (kt) {
double s = 0;
for (HangHoa z : a) {
if (x.getNgayNhap().getThang() == z.getNgayNhap().getThang()) {
s = s + z.thanhTien();
}
}
tg.add(x.getNgayNhap());
System.out.println("Tong ten thang " + x.getNgayNhap().getThang() + "/"
+ x.getNgayNhap().getNam() + "= " + s);
}
}
}
public void output(){
System.out.println("Danh sach hang hoa doc tu file:");
System.out.println("1.DS Hang Hoa: ");
xuat(a);
System.out.println("2.DS hang dien may TN: ");
xuat(tachHang("TN"));
System.out.println("DS hang dien may NK: ");
xuat(tachHang("NK"));
System.out.println("DS hang thu cong: ");
xuat(tach("HangThuCong"));
System.out.println("3.Tong thanh tien hang dien may:
"+tongThanhTien(tach("HangDienMay")));
System.out.println("Tong thanh tien hang thu cong:
"+tongThanhTien(tach("HangThuCong")));
System.out.println("Tong thanh tien hang NK:
"+tongThanhTien(tachHang("NK")));
System.out.println("Tong thanh tien hang TN:
"+tongThanhTien(tachHang("TN")));
System.out.print("4.");
xoaHoaDon();
System.out.println("5.Hoa don co tong tien lon nhat: "+maxHoaDon());
System.out.println("6.");
tongTheoNgay();
System.out.println("7.");
tongTheoThang();

}
public static void main(String[] args) {
DanhSachHangHoa a= new DanhSachHangHoa();
a.docFile("C:\\HangHoa.txt");
a.output();
}

You might also like