15,911,030 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
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=