Skip to content

Instantly share code, notes, and snippets.

@peter279k
Last active May 25, 2017 12:44
Show Gist options
  • Save peter279k/0c40a9991f9f1016659f3008afaf6bb7 to your computer and use it in GitHub Desktop.
Save peter279k/0c40a9991f9f1016659f3008afaf6bb7 to your computer and use it in GitHub Desktop.
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 int TotalCost() {
int 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 += new BasicCar(cc, this.carList.get(index+2)).cost();
} else if(this.carList.get(index).equals("S")) {
cost += new SLuxCar(cc, this.carList.get(index+2)).cost();
} else if(this.carList.get(index).equals("X")) {
System.out.println("Incorrect input data: X");
cost += 0;
} else {
cost += 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_4 {
public static void main(String args[]) {
Scanner sc = null;
try {
ArrayList<String> carList = new ArrayList<String>();
sc = new Scanner(new File("data.txt"));
while(sc.hasNextLine()) {
String []str = sc.nextLine().split(" ");
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());
} catch(FileNotFoundException e) {
System.out.println("File not found!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment