Skip to content

Instantly share code, notes, and snippets.

@peter279k
Created May 25, 2017 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter279k/9ccbee0f2ec7009027334e62b9f171eb to your computer and use it in GitHub Desktop.
Save peter279k/9ccbee0f2ec7009027334e62b9f171eb 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 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