-
Notifications
You must be signed in to change notification settings - Fork 0
/
Apple.java
86 lines (85 loc) · 1.65 KB
/
Apple.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Created by Noah Shaw
*/
public class Apple {
//Attributes
private String type;
private double weight;
private double price;
//Constructors
public Apple() //Default
{
this.type = "Gala";
this.weight = 0.5;
this.price = 0.89;
}
public Apple(String aType, double aWeight, double aPrice) //Parameter instructor
{
//Call mutators
this.setType(aType);
this.setWeight(aWeight);
this.setPrice(aPrice);
}
//Accessors
public String getType()
{
return this.type;
}
public double getWeight()
{
return this.weight;
}
public double getPrice()
{
return this.price;
}
//Mutators
public void setType(String aType)
{
if(aType.equalsIgnoreCase("Red Delicious") || aType.equalsIgnoreCase("Golden Delicious") || aType.equalsIgnoreCase("Gala") || aType.equalsIgnoreCase("Granny Smith"))
{
this.type = aType;
}
else
{
System.out.println("Invalid type.");
this.type = "Gala";
}
}
public void setWeight(double aWeight)
{
if(aWeight > 0 && aWeight < 2)
{
this.weight = aWeight;
}
else
{
System.out.println("Invalid weight.");
this.weight = 0.5;
}
}
public void setPrice (double aPrice)
{
if(aPrice > 0)
{
this.price = aPrice;
}
else
{
System.out.println("Invalid price.");
this.price = 0.89;
}
}
//Methods
public String toString()
{
return "Name: "+this.type+" Weight: "+this.weight+" Price: "+this.price;
}
public boolean equals(Apple aApple)
{
return aApple != null &&
this.type.equals(aApple.getType()) &&
this.weight == aApple.getWeight() &&
this.price == aApple.getPrice();
}
}