Reading a text file into an arraylist of fueldispensers.
[highlight = java]
public class FuelPump
{
//Text file
private boolean poweredOn; // 1 premium 2.34 100
private FuelDispenser activeDispenser; //2 regular 2.10 100
private double currentSalePrice; // 3 midgrade 2.78 100
private int gallonsPumped; // 4 diesel 2,78 100
private ArrayList<FuelDispenser> dispensers;
private Payment payment;
public FuelPump() throws Exception
{
dispensers = new ArrayList<FuelDispenser>();
Scanner input = new Scanner(new File("FuelDispenser_config.txt"));
int IndexValue ;
String FuelType ;
double Price ;
int Amount ;
while(input.hasNext())
{
IndexValue = input.nextInt();
FuelType = input.next();
Price = input.nextDouble();
Amount = input.nextInt();
dispensers.add(new FuelDispenser(IndexValue, FuelType, Price, Amount));
}
input.close();
currentSalePrice = 0.0;
gallonsPumped = 0;
payment = new Payment();
turnOff();
}
public void welcome()
{
System.out.println("Welcome to CSE Gas Station");
}
public void askForPaymentType()
{
int response = 0;
System.out.println("Select Payment Method");
do{
System.out.println("1. Credit");
System.out.println("2. Cash");
System.out.println("3. Shutdown/Exit Processing");
Scanner sc = new Scanner(System.in);
response = sc.nextInt();
}while( response < 0 || response > 4 );
if(response == 1)
payment = new CreditCardPayment();
if(response == 2)
payment = new CashPayment();
if(response == 3)
System.exit(0);
}
public void askForFuelType()
{ int input = 0;
int j = 0; double b = 0.0; String s = "";
Scanner sc = new Scanner(System.in);
System.out.println("Select Fuel Type:");
do{
for(int i = 0; i < dispensers.size(); i++)
{
j = dispensers.get(i).getFuelIndex();
System.out.print(j);
System.out.print(".");
s = dispensers.get(i).getGrade();
System.out.print(s);
System.out.print(" :");
b = dispensers.get(i).getPrice();
System.out.println(b);
}
input = sc.nextInt();
}while(input < 1 || input > 4);
activeDispenser = dispensers.get(input);
}
public void processPayment()
{
payment.processPayment();
}
public void pump()
{
int gallonsPumped;
double d = payment.getAmount();
double b = activeDispenser.getPrice();
int gallonsRequested = 0;
if(poweredOn == true && b != 0.0 && d != 0.0)
gallonsRequested = (int)(payment.getAmount()/b);
gallonsPumped = activeDispenser.pumpFuel(gallonsRequested);
currentSalePrice = gallonsPumped * activeDispenser.getPrice();
payment.setAmount(currentSalePrice);
}
public void printReceipt()
{
payment.printReceipt(activeDispenser.getGrade(), gallonsPumped, activeDispenser.getPrice());
}
public void thankYouForYourBusiness()
{
System.out.println("Thank you for your business");
}
public void turnOn()
{
poweredOn = true;
}
public void turnOff()
{
poweredOn = false;
}
public String toString()
{
return "hello";
}
}
[/highlight]
Re: Reading a text file into an arraylist of fueldispensers.
Please edit your post and wrap the code in code tags to preserve its formatting.
Do you have a problem or question about your code?
Please post it.
Re: Reading a text file into an arraylist of fueldispensers.
I am reading a text file FuelDispenser_config.txt . it contains data
1 Premium 3.32 100
2 Regular 3.76 100
3 MidGrade 3.56 50
4 Diesel 3.19 20
After reading it into arraylist dipensers whem i try to output its contents
output is
0 null 0.0 0
0 null 0.0 0
0 null 0.0 0
0 null 0.0 0
How do i fix it?
Re: Reading a text file into an arraylist of fueldispensers.
You need to either edit your post and wrap code tags around the code
or post a new version of your code with code tags.
See: BB Code List - Java Programming Forums - The Java Community
Check that the variables you are reading the data into are the same ones you are using when you print. It looks like there could be two different variables, one that gets the data and one that does not get any data.
Where is the definition of the FuelDispenser class?