-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightSource.java
More file actions
71 lines (60 loc) · 2.09 KB
/
LightSource.java
File metadata and controls
71 lines (60 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package bork;
import java.util.*;
/**
* Class that extends the item class for the items that can be used as light sources
* and maintains the value specific for light sources
* @author Woodruff
*/
public class LightSource extends Item {
private boolean isLit;
private int fuel;
static class NoActiveLightSourceException extends Exception {}
/**
* Constructor for LightSources reads in from input scanner for values
* @param s scanner object for the initialization of the light source
* @throws NoItemException Signals that there is no Item left to create
* @throws Dungeon.IllegalDungeonFormatException Signals that the format of the input file is incorrect
*/
LightSource(Scanner s) throws NoItemException, Dungeon.IllegalDungeonFormatException
{
super(s);
fuel = Integer.valueOf(s.nextLine());
isLit = false;
if (!s.nextLine().equals(Dungeon.SECOND_LEVEL_DELIM))
throw new Dungeon.IllegalDungeonFormatException("No '" +
Dungeon.SECOND_LEVEL_DELIM + "' after light source.");
}
/**
* Changes the state of the light source
*/
public void toggleLight() {
if (isLit)
isLit = false;
else
isLit = true;
}
/**
* Returns the amount of fuel left in the light source
* @return int and int value of the number of turns/commands before the source is out of fuel
*/
public int getFuel() {
return fuel;
}
/**
* Returns whether or not the source is currently lit
* @return boolean the value of the state of the source
*/
public boolean isLit() {
return isLit;
}
/**
* Decrements the remaining fuel in the light source.
*/
public void reduceFuel() {
fuel--;
if(fuel <= 0) {
toggleLight();
System.out.println("The " + this.primaryName + " has run out of fuel.");
}
}
}