Write three classes: Fish, FishTank, FishTankManagerApp The program should allow
ID: 3840496 • Letter: W
Question
Write three classes: Fish, FishTank, FishTankManagerApp
The program should allow a user to manage up to 10 fish tanks.
The user should be able to add up to 5 fish to a tank.
Allow the user to create and name a fish, specify if it is aggressive and add it to the tank.
You can only have one aggressive fish in each tank.
Each time a user adds a fish, make sure it will fit and is the only aggressivefish in the tank.
Allow the user to see all the fish in a particular tank.
Allow the user to remove a fish from a particular tank.
Allow the user to see how long each fish has been in a tank.
Explanation / Answer
Code :
Fish :
package fishtankmanagerapp;
public class Fish {
String name;
boolean isAgressive;
public Fish(String n, boolean a){
name = n;
isAgressive = a;
}
}
FishTank.java :
package fishtankmanagerapp;
import java.time.LocalDateTime;
import java.util.ArrayList;
public class FishTank {
String tankName;
ArrayList<Fish> fishes;
ArrayList<LocalDateTime> fishAddedTime;
Fish aggressiveFish;
static int fishTankCount = 0;
FishTank(String n){
tankName = n;
fishes = new ArrayList<Fish>();
fishAddedTime = new ArrayList<LocalDateTime>();
aggressiveFish = null;
}
void addFish(Fish f){
if(fishes.size() < 5){
fishes.add(f);
fishAddedTime.add(LocalDateTime.now());
}
else{
System.out.println(" Fish tank is full already");
}
}
void removeFish(Fish f){
if(fishes.contains(f)){
fishes.remove(f);
System.out.println(" Fish has been removed from tank");
}
else{
System.out.println(" Fish is not in tank. So cannot be removed");
}
}
void CheckFishTimeInTank(Fish f){
if(fishes.contains(f)){
int i = fishes.indexOf(f);
System.out.println(" FIsh has been in tank at time: " + fishAddedTime.get(i).toString());
}
else{
System.out.println(" Fish has been removed from tank.");
}
}
void addAggressiveFish(Fish f){
if(aggressiveFish == null){
aggressiveFish = f;
fishes.add(f);
}
else{
System.out.println(" There is already one aggressive fish. Another aggressive fish can not be added. ");
}
}
}
FishTankManagerApp.java :
package fishtankmanagerapp;
import java.util.ArrayList;
import java.util.Scanner;
public class FishTankManagerApp {
static ArrayList<Fish> fishes = new ArrayList<Fish>();
static ArrayList<FishTank> fishtanks = new ArrayList<FishTank>();
public static void main(String[] args) {
boolean flag = true ;
int choice ;
Scanner sc = new Scanner(System.in);
while(flag){
System.out.println(" 1.Add new Fish");
System.out.println(" 2.Add new Tank");
System.out.println(" 3.Add Fish to tank");
System.out.println(" 4.Remove Fish from tank");
System.out.println(" 5.List all fish with respective tanks.");
System.out.println(" 6.Exit");
System.out.println(" Choose an operation :");
choice = sc.nextInt();
switch(choice){
case 1:
System.out.println("Name of fish :");
String name = sc.next();
System.out.println(" Is the fish aggressive ? (1/0) 1= yes, 0= no");
int a= sc.nextInt();
fishes.add(new Fish(name, (a==1)?true:false));
break;
case 2:
if(FishTank.fishTankCount >= 10){
System.out.println(" Fish tank can not be added. 10 tanks are ther already.");
}
else{
System.out.println(" Enter Tank name :");
String n = sc.next();
FishTankManagerApp.fishtanks.add(new FishTank(n));
FishTank.fishTankCount ++;
System.out.println(" Fish Tank added.");
}
break;
case 3:
for(int i =0; i<fishes.size(); i++){
System.out.println(" "+fishes.get(i).name);
}
System.out.println(" Select fish to be added");
String n = sc.next();
Fish f = null;
for(int i =0; i<fishes.size(); i++){
if(n.equals(fishes.get(i).name)){
f= fishes.get(i);
}
}
if(f == null){
System.out.println(" No such fish exist. Try again");
break;
}
for(int i =0; i<fishtanks.size(); i++){
System.out.println(" "+fishtanks.get(i).tankName);
}
System.out.println(" Select Tank into fish has to be added");
String nt = sc.next();
FishTank ft = null;
for(int i =0; i<fishtanks.size(); i++){
if(nt.equals(fishtanks.get(i).tankName)){
ft= fishtanks.get(i);
}
}
if(ft == null){
System.out.println(" No such fish tank exist. Try again");
break;
}
if(f.isAgressive){
ft.addAggressiveFish(f);
System.out.println(" Fish has been added to tank");
}
else{
ft.addFish(f);
}
break;
case 4 :
for(int i =0; i<fishtanks.size(); i++){
System.out.println(" "+fishtanks.get(i).tankName);
}
System.out.println(" Select Tank from which fish has to be deleted");
String ntt = sc.next();
FishTank ftt = null;
for(int i =0; i<fishtanks.size(); i++){
if(ntt.equals(fishtanks.get(i).tankName)){
ftt= fishtanks.get(i);
}
}
if(ftt == null){
System.out.println(" No such fish tank exist. Try again");
break;
}
System.out.println(" List of fishes in selected tank:");
for(int i =0; i<ftt.fishes.size(); i++){
System.out.println(" "+ftt.fishes.get(i).name);
}
System.out.println(" Select a fish to be deleted.");
String nnn = sc.next();
Fish fff= null;
for(int i =0; i<ftt.fishes.size(); i++){
if(nnn.equals(ftt.fishes.get(i).name)){
fff = ftt.fishes.get(i);
}
}
if(fff == null){
System.out.println(" No such fish exist.");
}
ftt.fishes.remove(fff);
System.out.println(" Fish has been removed.");
break;
case 5:
System.out.println("Fish name -----------tank name");
for(int i =0; i< fishes.size(); i++){
FishTank tank = null;
for(int j =0; j< fishtanks.size(); j++){
if(fishtanks.get(j).fishes.contains(fishes.get(i))){
tank = fishtanks.get(j);
}
}
if(tank != null){
System.out.println(" "+ fishes.get(i).name +"----------"+ tank.tankName);
}
else{
System.out.println(" "+ fishes.get(i).name + "------------ no tank ");
}
}
break;
case 6 :
flag = false;
break;
default : System.out.println(" Enter valid choice");
}
}
}
}
Output :
run:
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
1
Name of fish :
fish1
Is the fish aggressive ? (1/0) 1= yes, 0= no
1
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
1
Name of fish :
fish2
Is the fish aggressive ? (1/0) 1= yes, 0= no
1
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
1
Name of fish :
fish3
Is the fish aggressive ? (1/0) 1= yes, 0= no
0
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
1
Name of fish :
fish4
Is the fish aggressive ? (1/0) 1= yes, 0= no
0
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
1
Name of fish :
fish5
Is the fish aggressive ? (1/0) 1= yes, 0= no
0
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
2
Enter Tank name :
tank1
Fish Tank added.
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
2
Enter Tank name :
tank3
Fish Tank added.
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
3
fish1
fish2
fish3
fish4
fish5
Select fish to be added
fish1
tank1
tank3
Select Tank into fish has to be added
tank1
Fish has been added to tank
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
3
fish1
fish2
fish3
fish4
fish5
Select fish to be added
fish2
tank1
tank3
Select Tank into fish has to be added
tank1
There is already one aggressive fish. Another aggressive fish can not be added.
Fish has been added to tank
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
3
fish1
fish2
fish3
fish4
fish5
Select fish to be added
fish3
tank1
tank3
Select Tank into fish has to be added
tank1
Fish has been added to tank
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
3
fish1
fish2
fish3
fish4
fish5
Select fish to be added
fish4
tank1
tank3
Select Tank into fish has to be added
tank2
No such fish tank exist. Try again
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
3
fish1
fish2
fish3
fish4
fish5
Select fish to be added
fish5
tank1
tank3
Select Tank into fish has to be added
tank2
No such fish tank exist. Try again
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
5
Fish name -----------tank name
fish1----------tank1
fish2------------ no tank
fish3----------tank1
fish4------------ no tank
fish5------------ no tank
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
3
fish1
fish2
fish3
fish4
fish5
Select fish to be added
fish4
tank1
tank3
Select Tank into fish has to be added
tank3
Fish has been added to tank
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
3
fish1
fish2
fish3
fish4
fish5
Select fish to be added
fish5
tank1
tank3
Select Tank into fish has to be added
tank3
Fish has been added to tank
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
5
Fish name -----------tank name
fish1----------tank1
fish2------------ no tank
fish3----------tank1
fish4----------tank3
fish5----------tank3
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
4
tank1
tank3
Select Tank from which fish has to be deleted
tank3
List of fishes in selected tank:
fish4
fish5
Select a fish to be deleted.
fish5
Fish has been removed.
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
5
Fish name -----------tank name
fish1----------tank1
fish2------------ no tank
fish3----------tank1
fish4----------tank3
fish5------------ no tank
1.Add new Fish
2.Add new Tank
3.Add Fish to tank
4.Remove Fish from tank
5.List all fish with respective tanks.
6.Exit
Choose an operation :
6
BUILD SUCCESSFUL (total time: 5 minutes 57 seconds)
If you have any doubts, you can ask in comment section.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.