12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.io.File;
-
- class Oblig3 {
- public static void main(String[] args) throws Exception {
- if (args.length != 1) {
- System.out.println("Bruk: java Oblig3 <filnavn>");
- System.exit(1);
- }
-
- Scanner fil = new Scanner(new File(args[0]));
- ArrayList<Bil> biler = new ArrayList<>();
-
- while (fil.hasNextLine()) {
- String[] strs = fil.nextLine().split("\\s+");
-
- if (strs[0].equals("BIL")) {
- Bil b = new Bil(strs[1]);
- biler.add(b);
- } else if (strs[0].equals("EL")) {
- Elbil b = new Elbil(strs[1]);
- b.batteriStorrelse = Integer.parseInt(strs[2]);
- biler.add(b);
- } else if (strs[0].equals("FOSSIL")) {
- Fossilbil b = new Fossilbil(strs[1]);
- b.co2Utslipp = Double.parseDouble(strs[2]);
- biler.add(b);
- } else if (strs[0].equals("LASTEBIL")) {
- Lastebil b = new Lastebil(strs[1]);
- b.co2Utslipp = Double.parseDouble(strs[2]);
- b.nyttevekt = Double.parseDouble(strs[3]);
- biler.add(b);
- } else if (strs[0].equals("PERSONFOSSILBIL")) {
- Personbil b = new Personbil(strs[1]);
- b.co2Utslipp = Double.parseDouble(strs[2]);
- b.antallPassasjerer = Integer.parseInt(strs[3]);
- biler.add(b);
- } else {
- throw new Exception("Ukjent greie: "+strs[0]);
- }
- }
-
- for (Bil b: biler) {
- if (b instanceof Personbil) {
- Personbil p = (Personbil)b;
- System.out.println(
- "Personbil "+p.bilnummer+":\n"+
- "\tAntall passasjerer: "+p.antallPassasjerer+"\n"+
- "\tCO2-utslipp: "+p.co2Utslipp
- );
- }
- }
- }
- }
|