Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TQC+ JAVA
import java.util.*;
import java.io.*;
class Warehouse {
public ArrayList<String> carList = new ArrayList<String>();
public Warehouse(ArrayList<String> carList) {
this.carList = carList;
}
public double TotalCost() {
double cost = 0;
for(int index=0;index<this.carList.size();index+=3) {
if(index + 1 >= this.carList.size()) {
break;
}
int cc = Integer.parseInt(this.carList.get(index+1));
if(this.carList.get(index).equals("B")) {
cost += (double)new BasicCar(cc, this.carList.get(index+2)).cost();
} else if(this.carList.get(index).equals("S")) {
cost += (double)new SLuxCar(cc, this.carList.get(index+2)).cost();
} else if(this.carList.get(index).equals("X")) {
cost += (double)0;
} else {
cost += (double)new LuxCar(cc, this.carList.get(index+2)).cost();
}
}
return cost;
}
public double TotalPirce() {
double price = 0.0;
for(int index=0;index<this.carList.size();index+=3) {
if(index + 1 >= this.carList.size()) {
break;
}
int cc = Integer.parseInt(this.carList.get(index+1));
if(this.carList.get(index).equals("B")) {
price += new BasicCar(cc, this.carList.get(index+2)).price();
} else if(this.carList.get(index).equals("S")) {
price += new SLuxCar(cc, this.carList.get(index+2)).price();
} else if(this.carList.get(index).equals("X")) {
price += (double)0;
} else {
price += new LuxCar(cc, this.carList.get(index+2)).price();
}
}
return price;
}
}
public class JPA06_5 {
public static void main(String args[]) {
Scanner sc = null;
try {
sc = new Scanner(new File("wrongdata.txt"));
} catch (FileNotFoundException e) {
System.out.println ("File not found!");
// Stop program if no file found
System.exit (0);
}
ArrayList<String> carList = new ArrayList<String>();
while(sc.hasNextLine()) {
String []str = sc.nextLine().split(" ");
if(!str[0].equals("B") && !str[0].equals("L") && !str[0].equals("S")) {
System.out.println("Incorrect input data: " + str[0] + " " + str[1] + " " + str[2]);
}
carList.add(str[0]);
carList.add(str[1]);
carList.add(str[2]);
}
Warehouse wh = new Warehouse(carList);
System.out.println("Total cost: " + wh.TotalCost());
System.out.println("Total price: " + wh.TotalPirce());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.