University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Oblig3.java 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. class Oblig3 {
  5. public static void main(String[] args) throws Exception {
  6. if (args.length != 1) {
  7. System.out.println("Bruk: java Oblig3 <filnavn>");
  8. System.exit(1);
  9. }
  10. Scanner fil = new Scanner(new File(args[0]));
  11. ArrayList<Bil> biler = new ArrayList<>();
  12. while (fil.hasNextLine()) {
  13. String[] strs = fil.nextLine().split("\\s+");
  14. if (strs[0].equals("BIL")) {
  15. Bil b = new Bil(strs[1]);
  16. biler.add(b);
  17. } else if (strs[0].equals("EL")) {
  18. Elbil b = new Elbil(strs[1]);
  19. b.batteriStorrelse = Integer.parseInt(strs[2]);
  20. biler.add(b);
  21. } else if (strs[0].equals("FOSSIL")) {
  22. Fossilbil b = new Fossilbil(strs[1]);
  23. b.co2Utslipp = Double.parseDouble(strs[2]);
  24. biler.add(b);
  25. } else if (strs[0].equals("LASTEBIL")) {
  26. Lastebil b = new Lastebil(strs[1]);
  27. b.co2Utslipp = Double.parseDouble(strs[2]);
  28. b.nyttevekt = Double.parseDouble(strs[3]);
  29. biler.add(b);
  30. } else if (strs[0].equals("PERSONFOSSILBIL")) {
  31. Personbil b = new Personbil(strs[1]);
  32. b.co2Utslipp = Double.parseDouble(strs[2]);
  33. b.antallPassasjerer = Integer.parseInt(strs[3]);
  34. biler.add(b);
  35. } else {
  36. throw new Exception("Ukjent greie: "+strs[0]);
  37. }
  38. }
  39. for (Bil b: biler) {
  40. if (b instanceof Personbil) {
  41. Personbil p = (Personbil)b;
  42. System.out.println(
  43. "Personbil "+p.bilnummer+":\n"+
  44. "\tAntall passasjerer: "+p.antallPassasjerer+"\n"+
  45. "\tCO2-utslipp: "+p.co2Utslipp
  46. );
  47. }
  48. }
  49. }
  50. }