Uploaded by Quang Đăng

package Demo1

advertisement
package Demo1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
*
* @author Mr.Chien
*/
public class Main{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
/**
* @param args the command line arguments
*/
public static void main1(String[] args) throws IOException {
// TODO code application logic here
Person P = new Person();
P.input();
P.show();
Person P2 = new Person("P02","Nguyen Van A", "Hanoi", "Goalkeeper", 1000);
P2.show();
}
public static void main(String[] args) throws IOException {
ArrayList<Player> listPlayer = new ArrayList<Player>();
ArrayList<Coach> listCoach = new ArrayList<Coach>();
Manager m=new Manager(listPlayer, listCoach);
while(true){
System.out.println("============================================");
System.out.println("1. Input a list of players. ");
System.out.println("2. Input a list of coaches. ");
System.out.println("3. Show list of players. ");
System.out.println("4.Show list of coaches. ");
System.out.println("5. Update the information of players.");
System.out.println("6. Count the coaches that have years of experience >=3. ");
System.out.println("7. Sum of the salary of the players that are the striker. ");
System.out.println("8. Who have the max salary? ");
System.out.println("9. Sort the list of players by ascending shirt number. ");
System.out.println("10. Sort descending salaries of experienced coaches =3. ");
System.out.println("0. Exit.");
System.out.printf("Enter choice: ");
int option = Integer.parseInt(in.readLine());
switch(option){
case 0:{
return;
}
case 1:{
System.out.println("Enter n:");
int p = Integer.parseInt(in.readLine());
m.inputPlayer(p);
break;
}
case 2:{
System.out.println("Enter c:");
int c = Integer.parseInt(in.readLine());
m.inputCoach(c);
break;
}
case 3:{
m.showPlayer();
break;
}
case 4:{
m.showCoach();
break;
}
case 5:{
break;
}
case 6:{
break;
}
case 7:{
break;
}
case 8:{
break;
}
case 9:{
break;
}
case 10:{
break;
}
}
}
}
}
package Demo1;
import static Demo1.Main.in;
import java.io.IOException;
/**
*
* @author Mr.Chien
*/
public class Person {
private String code;
protected String name;
String address, position;
public double salary;
public Person() {
}
public Person(String code, String name, String address, String position, int salary) {
this.code = code;
this.name = name;
this.address = address;
this.position = position;
this.salary = salary;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void show(){
System.out.println("Code:"+this.getCode());
System.out.println("Name:"+this.name);
System.out.println("Position:"+this.position);
System.out.println("Address:"+this.address);
System.out.println("Salary:"+this.salary);
}
@Override
public String toString() {
return code + "\t" + name + "\t" + address + "\t" + position + "\t" + salary;
}
public void input() throws IOException{
System.out.println("Enter code:");
this.setCode(in.readLine());
System.out.println("Enter name:");
this.name=in.readLine();
System.out.println("Enter address:");
this.address = in.readLine();
System.out.println("Enter position:");
this.position = in.readLine();
System.out.println("Enter salary");
this.salary=Double.parseDouble(in.readLine());
}
}
package Demo1;
import java.io.IOException;
/**
*
* @author Mr.Chien
*/
public class Player extends Person{
private int shirtNumber;
public Player() {
}
public Player(String code, String name, String address, String position, int salary, int shirtNumber) {
super(code, name, address, position, salary);
this.shirtNumber = shirtNumber;
}
public int getShirtNumber() {
return shirtNumber;
}
public void setShirtNumber(int shirtNumber) {
this.shirtNumber = shirtNumber;
}
@Override
public void input() throws IOException{
super.input();
System.out.println("Enter shirt number:");
this.shirtNumber=Integer.parseInt(Main.in.readLine());
}
@Override
public String toString() {
return super.toString() + "\t" + shirtNumber;
}
}
package Demo1;
import java.io.IOException;
/**
*
* @author Mr.Chien
*/
public class Coach extends Person{
private int year;
public Coach() {
}
public Coach(String code, String name, String address, String position, int salary, int year) {
super(code, name, address, position, salary);
this.year = year;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public void input() throws IOException{
super.input();
System.out.println("Enter years of experience:");
int year=Integer.parseInt(Main.in.readLine());
}
public String toString(){
return super.toString() + "\t" + year;
}
}
package Demo1;
import java.io.IOException;
import java.util.ArrayList;
public class Manager {
ArrayList<Player> listPlayer;
ArrayList<Coach> listCoach;
Manager(ArrayList<Player> listPlayer, ArrayList<Coach> listCoach) {
this.listPlayer=listPlayer;
this.listCoach=listCoach;
}
public void inputPlayer(int n) throws IOException {
for (int i = 0; i < n; i++) {
Player p=new Player();
p.input();
listPlayer.add(p);
}
}
public void showPlayer() {
for (Player item : listPlayer) {
//item.show();
System.out.println(item.toString());
}
}
public void inputCoach(int c) throws IOException {
for(int i = 0; i < c; i++){
Coach hlv = new Coach();
hlv.input();
listCoach.add(hlv);
}
}
public void showCoach(){
for (Coach item : listCoach) {
System.out.println(item.toString());
}
}
public void updatePlayer(){
}
}
Finish the manager class
Viết lại main:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in);
public static void main(String[] args) throws IOException {
ArrayList<Player> listPlayer = new ArrayList<Player>();
ArrayList<Coach> listCoach = new ArrayList<Coach>();
Manager manager = new Manager(listPlayer, listCoach);
while (true) {
System.out.println("============================================");
System.out.println("1. Input a list of players.");
System.out.println("2. Input a list of coaches.");
System.out.println("3. Show list of players.");
System.out.println("4. Show list of coaches.");
System.out.println("5. Update the information of players.");
System.out.println("6. Count the coaches that have years of experience >= 3.");
System.out.println("7. Sum of the salary of the players that are strikers.");
System.out.println("8. Who has the max salary?");
System.out.println("9. Sort the list of players by ascending shirt number.");
System.out.println("10. Sort descending salaries of experienced coaches >= 3.");
System.out.println("0. Exit.");
System.out.print("Enter choice: ");
int option = Integer.parseInt(in.readLine());
switch (option) {
case 0:
return;
case 1:
System.out.print("Enter the number of players: ");
int numberOfPlayers = Integer.parseInt(in.readLine());
manager.inputPlayer(numberOfPlayers);
break;
case 2:
System.out.print("Enter the number of coaches: ");
int numberOfCoaches = Integer.parseInt(in.readLine());
manager.inputCoach(numberOfCoaches);
break;
case 3:
manager.showPlayer();
break;
case 4:
manager.showCoach();
break;
case 5:
// Implement updating player information
break;
case 6:
int experiencedCoaches = manager.countExperiencedCoaches();
System.out.println("Number of coaches with 3 or more years of experience: " +
experiencedCoaches);
break;
case 7:
double totalSalaryOfStrikers = manager.sumSalaryOfStrikers();
System.out.println("Total salary of strikers: " + totalSalaryOfStrikers);
break;
case 8:
manager.showMaxSalary();
break;
case 9:
manager.sortPlayersByShirtNumber();
System.out.println("Sorted players by ascending shirt number:");
manager.showPlayer();
break;
case 10:
manager.sortExperiencedCoachesByDescendingSalary();
System.out.println("Sorted experienced coaches by descending salary:");
manager.showCoach();
break;
}
}
}
}
Viết lại Manager:
import java.io.IOException;
import java.util.ArrayList;
public class Manager {
ArrayList<Player> listPlayer;
ArrayList<Coach> listCoach;
public Manager(ArrayList<Player> listPlayer, ArrayList<Coach> listCoach) {
this.listPlayer = listPlayer;
this.listCoach = listCoach;
}
public void inputPlayer(int n) throws IOException {
for (int i = 0; i < n; i++) {
Player p = new Player();
p.input();
listPlayer.add(p);
}
}
public void showPlayer() {
for (Player item : listPlayer) {
System.out.println(item.toString());
}
}
public void inputCoach(int c) throws IOException {
for (int i = 0; i < c; i++) {
Coach hlv = new Coach();
hlv.input();
listCoach.add(hlv);
}
}
public void showCoach() {
for (Coach item : listCoach) {
System.out.println(item.toString());
}
}
public void changePlayer(int playerCode, int option, int value) {
for (Player player : listPlayer) {
if (player.getCode().equals(playerCode)) {
if (option == 0) {
player.setShirtNumber(value);
} else {
player.setSalary(value);
}
}
}
}
public int countExperiencedCoaches() {
int count = 0;
for (Coach coach : listCoach) {
if (coach.getYear() >= 3) {
count++;
}
}
return count;
}
public double sumSalaryOfStrikers() {
double sum = 0;
for (Player player : listPlayer) {
if (player.getPosition().equalsIgnoreCase("striker")) {
sum += player.getSalary();
}
}
return sum;
}
public void showMaxSalary() {
double maxSalary = 0;
String maxSalaryPlayer = "";
for (Player player : listPlayer) {
if (player.getSalary() > maxSalary) {
maxSalary = player.getSalary();
maxSalaryPlayer = player.getName();
}
}
System.out.println("Player with the max salary: " + maxSalaryPlayer + ", Salary: " + maxSalary);
}
public void sortPlayersByShirtNumber() {
listPlayer.sort((p1, p2) -> p1.getShirtNumber() - p2.getShirtNumber());
}
public void sortExperiencedCoachesByDescendingSalary() {
listCoach.sort((c1, c2) -> Double.compare(c2.getSalary(), c1.getSalary()));
}
}
Download