• All
  • [post-views]

import java.util.Scanner

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

class SportTeam
{
    protected String name;
    protected int score=0;

    public SportTeam(String na) { name = na; }
    public String getName() { return name; }
    public int getScore() { return score; }
    public void calculateScore() { /* override this method in child class */ }
    public void print() { /* override this method in child class */ }
}

class Football extends SportTeam
{
    private final String league;
    private int wins,loses,draws;
    
    public Football (String[]buf)
    {
        super (buf[1].trim());
        league = buf[2].trim();
        wins = Integer.parseInt(buf[3].trim());
        draws = Integer.parseInt(buf[4].trim());
        loses = Integer.parseInt(buf[5].trim());
    }
    public void calculateScore() 
    {
        score = wins*3 + draws ;
    }
    public void print() 
    {
        String na;
        int sc;
        na = getName();
        sc = getScore();
        System.out.printf("%-20s (%-14s) score = %-3d\n",na,league,sc);
    }
    
    
}

class FormulaOne extends SportTeam
{
    private final String nationality;
    private int []finish = new int[10];
    
    public FormulaOne (String[]buf)
    {
        super (buf[1].trim());
        nationality = buf[2].trim();
        for(int i=0;i<10;i++)
        {
            finish[i] = Integer.parseInt(buf[i+3].trim());
        }
    }
     public void calculateScore() 
    {
        int i;
        for(i=0;i<10;i++)
        {
            if(finish[i] == 1) score = score + 25;
            if(finish[i] == 2) score = score + 18;
            if(finish[i] == 3) score = score + 15;
            if(finish[i] == 4) score = score + 12;
            if(finish[i] == 5) score = score + 10;
            if(finish[i] == 6) score = score + 8;
            if(finish[i] == 7) score = score + 6;
            if(finish[i] == 8) score = score + 4;
            if(finish[i] == 9) score = score + 2;
            if(finish[i] == 10)score = score + 1;
        }
    }
    public void print() 
    {
        String na;
        int sc;
        na = getName();
        sc = getScore();
        System.out.printf("%-20s (%-14s) score = %-3d\n",na,nationality,sc);
    }
    
}

public class Main 
{
    public static void main(String[] args) 
    {
        try 
        {
            int i=0;
            String football="1",fomula="2";
            Scanner scan  = new Scanner(new File("input.txt"));
            SportTeam []team = new SportTeam[8];
            
            while(scan.hasNext())
            {
                String [] buf = scan.nextLine().split(",");
                
                if(buf[0].equals(football))
                {
                    team[i] = new Football(buf);
                    team[i].calculateScore();
                    team[i].print();
                    //System.out.printf("%d", i);
                }
                
                else if(buf[0].equals(fomula))
                {
                    team[i] = new FormulaOne(buf);
                    team[i].calculateScore();
                    team[i].print();
                }
            }
            
            scan.close();
        }
        
        catch(Exception e) 
        {
            System.err.println("An error occurs. End program.");
            System.exit(-1);
	}
    }
    
}
>

Mayong_ness

อยากเขียนเรื่องราวข้อมูลต่างๆ เพื่อให้คนที่สนใจได้เข้ามาอ่าน..อาจจะไม่ได้เขียนทุกวัน..นานๆที่มาเขียนนะคับ..ถ้าข้อมูลใดไม่ถูกต้องก็ ติชมกันได้นะคับ..

บทความอื่นๆที่น่าสนใจ...