Click here to Skip to main content
15,911,030 members

Comments by Juan Carlos Pérez Novoa (Top 1 by date)

Juan Carlos Pérez Novoa 28-Apr-22 5:22am View    
Deleted
Here is the full code of this one class. I didn't touch any other class so they have no reason to not work.

package P6b;

import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;

public class CarDB {

LinkedHashMap <string, car=""> cityCars = new LinkedHashMap <string, car="">();

private int coches = 0;
private int cochesEH = 0;

public void readCityCarsFile (String fichero)throws FileNotFoundException {
Scanner input = new Scanner (System.in);
File file = new File(fichero);
Scanner inputfichero = new Scanner(file);


do {
String linea = inputfichero.nextLine();
if (linea.startsWith("#")) { //COMPROBAMOS SI UNA LINEA EMPIEZA POR #, SI ES EL CASO, LA SALTAMOS
continue;
}
else {

String[] infocar = linea.split(";"); //SI NO ES UN COMENTARIO, SE DIVIDE A PARTIR DE :

if (infocar[0].equals("C") == true) { //EN FUNCION DEL TIPO DE COCHE QUE ESTEAMOS LEYENDO, REALIZAREMOS UNA SERIE DE PRUEBAS SOBRE SUS ATRIBUTOS ANTES DE CREAR EL OBJETO


if (CombustionCar.isValidPlate(infocar[1]) == true)
{
int mecPower = Integer.parseInt(infocar[3]);
if (CombustionCar.isValidmechanicalPower(mecPower) == true){
cityCars.put(infocar[1], new CombustionCar(infocar[1], infocar[2], mecPower));
coches++;}
}
}

if (infocar[0].equals("E") == true ) {

if (ElectricCar.isValidPlate(infocar[1]) == true)
{
int elecPower = Integer.parseInt(infocar[3]);
infocar[4] = infocar[4].replace(",", ".");
double batCharge = Double.parseDouble(infocar[4]);
if (ElectricCar.isValidelectricPower(elecPower) == true)
{if (ElectricCar.isValidbatteryCharge(batCharge) == true){
cityCars.put(infocar[1], new ElectricCar(infocar[1], infocar[2], elecPower, batCharge));
coches++;
cochesEH++;}
else{}
}else {}
}else{}
} else{}

if (infocar[0].equals("H") == true) {
if (HybridCar.isValidPlate(infocar[1]) == true)
{
int mhpower = Integer.parseInt(infocar[3]);
int mepower = Integer.parseInt(infocar[4]);
infocar[5] = infocar[5].replace(",", ".");
double bathCharge = Double.parseDouble(infocar[5]);
if (HybridCar.isValidmechanicalPower(mhpower) == true)
{if (HybridCar.isValidelectricPower(mepower) == true)
{if (HybridCar.isValidbatteryCharge(bathCharge) == true){
cityCars.put(infocar[1], new HybridCar(infocar[1], infocar[2], mhpower, mepower, bathCharge));
coches++;
cochesEH++;}
else{}
}else{}
}else{}
}else{}
}else {}

}

} while(inputfichero.hasNext() == true); //SEGUIRA LEYENDO FILAS DE COCHES MIENTRAS HAYA CONTENIDO EN EL FICHERO

inputfichero.close();
}


public Car getCarFromPlate(String plate) {
return cityCars.get(plate);
}


public int computeTotalPower () {
int Power = 0;
int TotalPower = 0;
ArrayList <car> temp = new ArrayList<car>(cityCars.values());

Car[] arrayCityCars = temp.toArray(new Car[cityCars.size()]);
for (int i=0; i<citycars.size(); i++)="" {="" sumamos="" el="" valor="" total="" de="" potencia="" cada="" tipo="" coche
="" power="arrayCityCars[i].getTotalPower();
" totalpower="" +="Power;
" }
="" return="" totalpower;
="" }

="" public="" float="" computeaveragebatterylevel()="" {
=""
="" double="" for="" (int="" i="0;" i<citycars.size();="" if="" (citycars.get(i)="" instanceof="" electriccar)="" car="" cochee="cityCars.get(i);
" hybridcar)="" cocheh="cityCars.get(i);
" (float)totalpower="" cocheseh;
="" void="" savecarstofile="" (string="" filename)throws="" filenotfoundexception="" file="" fichero="new" file(filename);="" se=