Bài 2: 1. Doanh nghiệp

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Bài 2 :

1. Doanh nghiệp

import java.util.Scanner;

public class DoanhNghiep implements IDoanhNghiep {


private int maNV = 100000;
private int count = 0;
private NhanVien[] mangNV = new NhanVien[500];
public DoanhNghiep(){}
public void addNV(){
mangNV[++count - 1] = new NhanVien(maNV + count, "Tran", "Tu Quang", 22, 1); //optional
mangNV[++count - 1] = new NhanVien(maNV + count, "Tran1", "Tu Quang1", 19, 2); //optional
mangNV[++count - 1] = new NhanVien(maNV + count, "Tran", "Tu Quang1", 22, 3); //optional
mangNV[++count - 1] = new NhanVien();
mangNV[count - 1].setMaNV(maNV + count);
mangNV[count - 1].add();
}
public void editNV() {
Scanner keyboard = new Scanner(System.in);
displayMangNhanVien(); //optional
System.out.print("Nhap ma so: ");
int index = findByMaNV(keyboard.nextInt());
keyboard.nextLine();
if (index > -1) mangNV[index].add();
else
System.out.println("Khong ton tai nhan vien");
}
public int findByMaNV(int maNV){
for(int i = 0; i < count; i++)
if (maNV == mangNV[i].getMaNV())
return i;
return -1;
}
//optional
public void swap(int i, int j) {
NhanVien temp = mangNV[i];
mangNV[i] = mangNV[j];
mangNV[j] = temp;
}
public void sortByMaNV(){
for(int i = 0; i < count+1; i++)
for(int j = i + 1; j < count; j++) {
if (mangNV[i].getMaNV() > mangNV[j].getMaNV()) swap(i, j);
}
displayMangNhanVien(); //optional
};
public void sortByPB(){
for(int i = 0; i < count+1; i++)
for(int j = i + 1; j < count; j++) {
if (mangNV[i].getPbNV() > mangNV[j].getPbNV()) swap(i, j);
}
displayMangNhanVien(); //optional
};
public void displayMangNhanVien() {
System.out.println("------DANH SACH NHAN VIEN------");
System.out.println("Ma so\tHo va ten\tTuoi\tPhong ban");
for(int i = 0; i < count; i++)
if (mangNV[i] != null)
mangNV[i].display();
}
}

2. IDoanhnghiep
/**
* IDoanhNghiep
*/
public interface IDoanhNghiep {
public void addNV();
public void editNV();
public int findByMaNV(int maNV);
public void sortByMaNV();
public void sortByPB();
public void displayMangNhanVien();
}

3. Nguoi
public abstract class Nguoi {
protected String firstName;
protected String lastName;
protected int age;
public Nguoi() {}
public Nguoi(String firstName, String lastName, int age){
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFullname() {
return firstName + " " + lastName;
}
public abstract void add();
public abstract void display();
}

4. Nhân viên

import java.util.InputMismatchException;
import java.util.Scanner;
public class NhanVien extends Nguoi {
private int maNV;
private int pbNV;
public NhanVien(){}
public NhanVien(int maNV, String firstName, String lastName, int age, int pbNV) {
super(firstName, lastName, age);
this.maNV = maNV;
this.pbNV = pbNV;
}
public int getMaNV() {
return maNV;
}
public void setMaNV(int maNV) {
this.maNV = maNV;
}
public int getPbNV() {
return pbNV;
}
public void setPbNV(int pbNV) {
this.pbNV = pbNV;
}
public void add() {
Boolean done = false;
Scanner keyboard = new Scanner(System.in);
while (!done) {
try {
System.out.print("- Nhap ten:\n+ Ho: ");
firstName = keyboard.nextLine();
done = true;
} catch (InputMismatchException e) {
keyboard.nextLine();
System.out.println("Error: Invalid Ho");
}
}
done = false;
while (!done) {
try {
System.out.print("+ Ten: ");
lastName = keyboard.nextLine();
done = true;
} catch (InputMismatchException e) {
keyboard.nextLine();
System.out.println("Error: Invalid Ten");
}
}
done = false;
while (!done) {
try {
System.out.print("- Tuoi: ");
age = keyboard.nextInt();
done = true;
} catch (InputMismatchException e) {
keyboard.nextLine();
System.out.println("Error: Invalid Tuoi");
}
}
keyboard.nextLine();
done = false;
System.out.println("1. Ky thuat\n2. Ke toan\n3. Quan ly\n4. Ban hang");
while (!done) {
try {
System.out.print("- Phong ban: ");
pbNV = keyboard.nextInt();
if (pbNV > 4 || pbNV < 1) throw new Exception("Error: Invalid Phong ban");
done = true;
} catch (InputMismatchException e) {
keyboard.nextLine();
System.out.println("Error: Invalid Phong ban");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

public void display(){


System.out.print(getMaNV() + "\t" + getFullname() + "\t" + getAge() + "\t");
switch(getPbNV()) {
case 1: System.out.println("Ban hang"); break;
case 2: System.out.println("Ke toan"); break;
case 3: System.out.println("Ky thuat"); break;
case 4: System.out.println("Quan ly"); break;
}
}
}

5. Test Doanh nghiệp


import java.util.InputMismatchException;
import java.util.Scanner;

public class TestDoanhNghiep {


public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
DoanhNghiep dn1 = new DoanhNghiep();
int key;
while(true) {
System.out.println("--------BUSINESS--------");
System.out.println("1. Them nhan vien");
System.out.println("2. Xuat nhan vien");
System.out.println("3. Chinh sua thong tin nhan vien");
System.out.println("4. Sap xep theo ma so");
System.out.println("5. Sap xep theo phong ban");
try {
System.out.print("Nhap lua chon: ");
key = keyboard.nextInt();
switch (key) {
case 1: dn1.addNV(); break;
case 2: dn1.displayMangNhanVien(); break;
case 3: dn1.editNV(); break;
case 4: dn1.sortByMaNV(); break;
case 5: dn1.sortByPB(); break;
}
if (key == 0) break;
} catch (InputMismatchException e) {
keyboard.nextLine();
}
}
keyboard.close();
}
}

You might also like