Hi i need help with a personal project. i'm developing a software to manage shows(event), and i have problem to save and object into my arraylist inside class.
i have 3 object, artist,event, contract, every time user sign a contract,between an artist and agent, i want to be able to save the artist into the agentContract inside the agent class. because i need a list of artist for each agent. My problem is every time i try to save than, my program save all the artist in the first agent, only. Can someone help me???
this is my artist class:
public class Agent extends Person implements Serializable {
float dueAmount;
int percentage;
public static ArrayList<Artist> AgentArtists = new ArrayList<Artist>();
public Agent()
{
super();
dueAmount = 0;
percentage = 0;
}
.....
.....
public static ArrayList<Artist> getAgentArtists()
{
return AgentArtists;
}
public void setAgentArtists(Agent age,Artist art)
{
age.AgentArtists.add(art);
}
i'm using this method to sign the contract and save the artist into the artistlist of agent
public void SignWithArtist(Artist art, Agent age)
{
if (age.getAgentArtists().add(art) == true)
{
System.out.println("\n" + art.getName() + " is signing the contract\n");
ProgManage.PauseScreen(1000);
for(int i=0; i<=1; i++)
{
System.out.print("*");
ProgManage.PauseScreen(1000);
System.out.print("**");
ProgManage.PauseScreen(1000);
System.out.print("***");
ProgManage.PauseScreen(1000);
System.out.print("****");
ProgManage.PauseScreen(1000);
}
System.out.println("\nContract signed by " + art.getName());
ProgManage.PauseScreen(1000);
} else {
System.out.println("\n*ERROR* Artist not added");
ProgManage.PauseScreen(1000);
}
}
Try this code :
public class Agent extends Person implements Serializable {
float dueAmount;
int percentage;
public ArrayList<Artist> agentArtists;
public Agent()
{
super();
agentArtists = new ArrayList<Artist>();
dueAmount = 0;
percentage = 0;
}
public ArrayList<Artist> getAgentArtists()
{
return agentArtists;
}
public void setAgentArtists(Artist art)
{
agentArtists.add(art);
}
}
Related
I am able to find the card I want in passCard() but I been stumped on moving that card into the hand.
public class CardContainer
{
protected Card[] cards;
this is where i am trying to pass the card i find into the hand. i am able to find the card but been stuck after that.
public boolean passCard(CardContainer cc, Card c)
{
for(int i = 0; i < cards.length; i++)
{
if(cards[i].equals(c))
{
//this is where im trying to make magic happen
return true;
}
}
return false;
}
}
public class Hand extends CardContainer {
private String playerName;
public Hand(String name, int numCards)
{
playerName = name;
cards = new Card[numCards];
// manually entered a card to test if it works
cards[1] = new Card(1,'s');
}
}
public class TestDemo {
public static void main(String[] args) {
Hand h = new Hand("name", 10);
System.out.println(deck.passCard(h, new Card(2,'s')));
// prints manually entered card in hand class
h.printCards();
}
In your passCard function you can simply initialize an element in Cards (This is assuming you want to initialize it at position i):
public boolean passCard(CardContainer cc, Card c)
{
for(int i = 0; i < cards.length; i++)
{
if(cards[i].equals(c))
{
h.cards[i] = this.cards[i];
return true;
}
}
return false;
}
Since cards is only protected:
The protected modifier specifies that the member can only be accessed within its own package
Meaning that your Hand class can access it.
Item.java
I have an item with variables name and weight
package com.company;
public class Item {
String name;
Double weight;
}
Bag.java
the bag can receive up to 20 kg items.
package com.company;
public class Bag {
Item [] myBags;
int itemCount;
Double currentWeight;
Bag(){
itemCount = 0;
currentWeight = 0.0;
myBags = new Item[50];
}
boolean canAddItem(Item item) {
if (currentWeight + item.weight > 20) {
return false;
} else {
return true;
}
}
void AddItem(Item item){
myBags[itemCount] = item;
currentWeight = currentWeight + myBags[itemCount].weight;
itemCount++;
}
}
Main.java
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
Bag myBag = new Bag();
/*item 1*/
Item fishing_rod = new Item();
fishing_rod.name = "rod1";
fishing_rod.weight = 10.4;
if(myBag.canAddItem(fishing_rod)){
myBag.AddItem(fishing_rod);}
/*item 2*/
Item axe = new Item();
axe.name = "axe1";
axe.weight = 2.2;
if(myBag.canAddItem(axe)){
myBag.AddItem(axe);}
System.out.println(myBag.currentWeight);
//System.out.println(myBag.myBags); i want to show that's here
}
}
I added two objects to the bag . I want to show all the objects I have added in the bag.
How can I show ? How can I show this in java ?
Thanks for your help
You have to override the toString() method in your class:
For Item class first:
#Override
public String toString() {
return "Name: "+ name + ": weight "+ weight +"Kg";
}
then for Bag class
#Override
public String toString() {
return Arrays.toString(myBags);
}
In this way every time you print your bag you will get the content of it.
In Bag class:
public void printBag() {
for(int i =0; i< itemCount; i++) {
System.out.println("Name: " + myBags[i].name);
System.out.println("Weight: " + myBags[i].weight);
}
}
In main method:
myBag.printBag();
Define a function inside your Bag class:
public void printBag() {
for(Item element : myBags) {
System.out.println('Name: ' + element.name);
System.out.println('Weight: ' + element.weight);
}
}
And call it from the main:
myBag.printBag();
I am trying to select a random method from the ones created inside the class. Is there a way to create an ArrayList and pass methods to it? I have attempted to do just that but I am getting an error when I try to add the method to the array.
public class Monkey{
private int energy;
String[] food = {"Meat", "Fish", "Bugs", "Grain"};
ArrayList<Activity> monkeyActivity = new ArrayList<>();
public Monkey(int energy) {
this.energy = energy;
}
public int getEnergy() {
System.out.println("Monkey energy level: " + energy);
return energy;
}
public void sound() {
System.out.println("Monkey: Oooo Oooo~!");
energy -= 3;
monkeyActivity.add(sound()); //I get an error message here when trying
//to add the method to the array
}
public void play(){
if (energy >= 8){
System.out.println("Monkey begins to play.");
energy -= 8;
}else {
System.out.println("Monkey does not have enough energy to play");
}
System.out.println("Energy remaining: " + energy);
}
public void eat(){
Random random = new Random();
int index = random.nextInt(food.length);
System.out.println("Monkey beings to eat " + food[index]);
energy += 5;
System.out.println("Energy remaining: " + energy);
}
public void sleep(){
System.out.println("Monkey is sleeping: Zzz...");
energy += 10;
System.out.println("Energy remaining: " + energy);
}
}
This is the separate class I have made for the generic Activity..
public class Activity {
private String sleep;
private String eat;
private String sound;
private String play;
public Activity(String sleep, String eat, String sound, String play) {
this.sleep = sleep;
this.eat = eat;
this.sound = sound;
this.play = play;
}
public String getSleep() {
return sleep;
}
public String getEat() {
return eat;
}
public String getSound() {
return sound;
}
public String getPlay() {
return play;
}
public void setSleep(String sleep) {
this.sleep = sleep;
}
public void setEat(String eat) {
this.eat = eat;
}
public void setSound(String sound) {
this.sound = sound;
}
public void setPlay(String play) {
this.play = play;
}
}
You are mixing up concepts.
technical issues:
return value clash
public void sound() {
// ...
monkeyActivity.add(sound());
The return value of your method sound() is void (which means no return value), but you try to add its (not existing) return value as element to the List. This is what your compiler complains about.
unintended recursion
public void sound() {
System.out.println("Monkey: Oooo Oooo~!");
energy -= 3;
monkeyActivity.add(sound());
In the last line you do a recursive call which means you call exactly the same method this code is in. If that happens unintended it almost ever results in a StackOverflowError.
writing classes without proper analysis
You have a class Activity.
But if you have a closer look this is not a single activity (as the classes name implies) but it is all possible activities.
As a result your collection monkeyActivity cannot hold single activities as elements.
Doing a wild guess I think what you wanted is more like this:
interface Activity{
void do();
}
public class Monkey{
private int energy;
String[] food = {"Meat", "Fish", "Bugs", "Grain"};
List<Activity> monkeyActivity = new ArrayList<>();
// ...
public void sound() {
monkeyActivity.add(new Activity(){
public void do(){
System.out.println("Monkey: Oooo Oooo~!");
energy -= 3;
}
});
}
You may store each method as Runnable, as any "action" is no-arg void method satisfying Runnable functional interface:
List<Runnable> actions = Arrays.asList(this::sound, this::play, this::eat, this::sleep);
to execute random method, just:
Random rnd = new Random();
actions.get(rnd.nextInt(actions.size())).run();
Hello i am writing a program that creates a method that can remove items from an arraylist and add them to another ArrayList (under certain conditions). This is the method I am supposed to create:
A method called giveAwayFish() which represents a person
returning his fish to the pond and/or giving them away to another fisher.
It will go through all of this person's fish ( the one giving the fish away) and see if the other fisher ( the one who will be receiving the fish) is willing to keep any. If the other fisher wants any, they are to be given to that fisher. If the fisher is unwilling to keep the fish, then these fish must be returned to the pond.
I tried writing out this method about a hundred times and I can not for the life of me figure out what to do. I was able to remove all the fish from the persons array but I do not know how to add them back. This is what I need help with.
Here is my code if it helps:
import java.util.*;
public class Fisher
{
private String name;
private Fish [] fishCaught;
private int numFishCaught;
private int keepSize;
public static int LIMIT = 10;
public String getName()
{
return this.name;
}
public int getNumFishCaught()
{
return this.numFishCaught;
}
public int getKeepSize()
{
return this.keepSize;
}
public Fisher(String n, int k)
{
name = n;
keepSize = k;
}
public String toString()
{
return(this.name + " with " + this.numFishCaught + " fish as follows:");
}
private ArrayList<Fish> fishesCaught = new ArrayList<Fish>();
public void keep(Fish fish)
{
if(this.numFishCaught < LIMIT)
{
fishesCaught.add(fish);
numFishCaught++;
}
}
public boolean likes(Fish fish)
{
if(fish.size >= this.keepSize && fish.species != "Sunfish")
{
return true;
}
else
{
return false;
}
}
public void listFish()
{
System.out.println(this.toString());
for(Fish fish : fishesCaught)
{
System.out.println(fish.toString());
}
}
public void goFishingIn(Pond pond)
{
Fish fish = pond.catchAFish();
if(likes(fish))
{
this.keep(fish);
}
else
{
pond.add(fish);
}
}
public void giveAwayFish(Fisher fisher, Pond pond)
{
Fish fish = fishesCaught;
if(fisher.likes(fish))
{
fishesCaught.clear();
this.numFishCaught = 0;
}
}
}
Biggest problem here is (yes, there are lots of other problems), in your giveAwayFish(), you wrote
Fish fish = fishesCaught;
However fishesCaught is a List<Fish>. That can't even compile.
I believe what you want to do is something like (in psuedo code):
for (Fish fish : fishesCaught) {
if (fisher.like(fish)) {
fisher.keep(fish);
} else {
pond.addFish(fish);
}
}
fishesCaught.clear();
I am trying to practice with Polymorphism and using classes. I wrote a superclass called Card. I then wrote 3 subclasses called: IDCard, CallingCard, and DriverLicense. I then wrote another class called Billfold which is supposed to contain slots for two of the cards.
I am supposed to write a BillfoldTester program which adds two objects of different subclasses to a Billfold object.
In BillfoldTester, a DriverLicense object and a CallingCard object are instantiated and added to a Billfold, which refers to these objects with Card references.
I don't really understand how to do this. I created two Card objects but I am trying to add it to my Billfold and it wont work. I tried Billfold a = new Card (x); but it's not right... Any help is much appreciated.
public class BillfoldTester
{
public static void main (String[]args)
{
Card x= new IDCard("Julie", 1995);
Card j= new DriverLicense("Jess", 1997);
//Having trouble trying to put the objects into my Billfold and print it.
}
}
public class Billfold extends Card
{
private String card1;
private String card2;
void addCard(String Card)//Not sure if this should be String
{
card1=Card;
}
}
public class Card
{
private String name;
public Card()
//This is my superclass
{
name = "";
}
public Card(String n)
{
name = n;
}
public String getName()
{
return name;
}
public boolean isExpired()
{
return false;
}
public String format()
{
return "Card holder: " + name;
}
}
public class IDCard extends Card
{
//This is one of my subclasses
private int IDNumber;
public IDCard (String n, int id)
{
super(n);
this.IDNumber=id;
}
public String format()
{
return super.format() + IDNumber;
}
}
The polymorphism example. Not sure if the functionally is exactly what you need, but you can see the whole idea (I hope). See the showAllFormat() method of Billfold class.
The whole point is inside different format() methods of the DriverLicense and IDCard. Depending on the 'real' (or initially assigned) object the different method will be called even if you just only refer to 'Card' class.
NOTE:
You didn't provide your DriverLicense implementation, and my is just from head. I have a bit different constructor to show this sub-classes may be totally different.
import java.util.ArrayList;
import java.util.List;
class Billfold {
List<Card> list = new ArrayList<Card>(10);
void addCard(Card card) // Q: Not sure if this should be String
// A: You would like to add a Card
{
list.add(card);
}
void showAllFormat() {
// go polymorphism !...
// when you call this general 'format()' you see the subclasses
// 'format()' is executed, not from 'Card' class
for(Card x: list) {
System.out.println(x.format());
}
}
}
class Card {
private String name; /* owner */
public Card() //This is my superclass
{
name = "";
}
public Card(String n) {
name = n;
}
public String getName() {
return name;
}
public boolean isExpired() {
return false;
}
public String format() {
return "Card holder: " + name;
}
}
class IDCard extends Card {
//This is one of my subclasses
private int IDNumber;
public IDCard(String n, int id) {
super(n);
this.IDNumber = id;
}
public String format() {
return "(ID)" + super.format() + " " + IDNumber;
}
}
class DriverLicense extends Card {
private String type;
public DriverLicense(String n, String type) {
super(n);
this.type = type;
}
public String format() {
return "(DL)" + super.format() + " TYPE: " + type;
}
}
public class BillfoldTester {
public static void main(String[] args) {
Card x = new IDCard("Julie", 1995);
Card j = new DriverLicense("Jess", "AB");
Billfold bf = new Billfold();
bf.addCard(x);
bf.addCard(j);
bf.showAllFormat();
}
}
This is wrong. A Billfold is not a Card; it HAS Cards.
public class Billfold
{
List<Card> cards = new ArrayList<Card>();
void addCard(Card card) {
if (card != null) {
this.cards.add(card);
}
}
}
Prefer composition over inheritance.
You should have Billfold class have two Card objects, not two Strings:
public class Billfold
{
Card card1;
Card card2;
void addCard(Card card) {
if (card != null) {
if (card1 != null) {
this.card1 = card;
} else {
this.card2 = card;
}
}
}
Ok, you're largely on the right track, just a couple of things:
void addCard(String Card)//Not sure if this should be String
{
card1=Card;
}
You're right, this should be:
void addCard(Card card)
{
card1=card;
}
then to add them:
public class BillfoldTester
{
public static void main (String[]args)
{
Card x= new IDCard("Julie", 1995);
Card j= new DriverLicense("Jess", 1997);
Billfold bf = new Billfold();
Billfold.addCard(x);
Billfold.addCard(j);
}
}
Then add a method to Billfold to print the cards in it.
Edit: Oh yeah, and duffymo is totally right, you don't need to extends Card on Billfold