This site requires JavaScript, please enable it in your browser!
Greenfoot back
gacha321
gacha321 wrote ...

2019/6/4

Buying and Selling an Item

1
2
3
gacha321 gacha321

2019/6/4

#
Hello guys, I have a problem again with my game :( my game should have can buy, sell, and look at our item that have been bought. My farm Game has two store. Garden store (buy, sell and look our plants) and Farm Store(buy, sell and look our animals). But the problem is when we buy some item, we must making a name for that item :(( For example: I buy a cow so I must give that cow a name and we must save the name in the file formated .txt. but if we want to sell our Item, it can delete a name in the file txt too. so when we look at our item it's show the file txt that include our items. And I don't have any idea for doing that :( can anyone help me?? I don't understand how to use file formated .txt in greenfoot :((( And how can we use the Scanner.in like usually we code in java ?? :(
gacha321 gacha321

2019/6/4

#
I have read about I/O method in java but I don't understand :( I'm asking to my friend but he said that is better to use JOptionPane :(( my game must have an save/load feature too but I'm so blank about it all :( I;m sorry for make all of you so confuse ;(
gacha321 gacha321

2019/6/4

#
Heeelppp T_T
danpost danpost

2019/6/4

#
You already have a name -- for example 'cow'. You just need to distinguish one from the next. You can suffix the name with a number -- like '001', for starters.
gacha321 gacha321

2019/6/4

#
But my game should named the animals or plant with us (input from the keyboard) and save to the file .txt and I dont know how to use :(
gacha321 gacha321

2019/6/4

#
Can you tell me how to use I/O in greenfoot? :( When I read a discussion about the saving and loading game in the txt files : www.greenfoot.org/topics/2295 But I don't understand where must place that code :( I think It would be same if I named the item that I buy with this code :'( but how can we input the name :(
danpost danpost

2019/6/4

#
gacha321 wrote...
how can we input the name :(
One way is to use the Greenfoot class ask method.
gacha321 gacha321

2019/6/4

#
Will it saved into the file .txt?
gacha321 gacha321

2019/6/4

#
I don't understand how can we save to the file formated .txt
danpost danpost

2019/6/4

#
When working with files, it is best to read in the entire file to begin with so that all the data is readily available. Although read in as a String array, it is best to convert it to a List of String objects (list sizes can be altered). When any change happens to the list, re-write the entire file (treating the List object as an Array object). You can use java.nio.files operations to facilitate the reading and writing of the file.
gacha321 gacha321

2019/6/5

#
I just try to code like this T_T
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * Write a description of class Button4 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Button4 extends Button
{
    /**
     * Act - do whatever the Button4 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this)){
            Counter counter = ((FarmWorld)getWorld()).getCounter();
            if(counter > 100){
                name();
                counter = (counter-100);
            }
        }
    }    
    
    public void name(){
    public static void main(String[] args) throws IOException {
         String Name;
         InputStreamReader isr = new InputStreamReader(System.in);
         BufferedReader br = new BufferedReader(isr);
         System.out.print("Input yoour Grape's plant name: ");
         Name = br.readLine();
         System.out.println("Your Cows Name : " + Name);
    }
}
}

But It's so fail :((( The buy session is when we click the buy button on the board. 1. Grape = 100 gold 2. Guava = 60 gold 3. Manggo = 20 gold so if we click the buy button on the board, I think it will like that code. we must make condition first because we need a gold to buy the item. but I don't know it's true if I code like this for input a name and save to the .txt files :(( I'm sorry I know I'm so bad :(
Super_Hippo Super_Hippo

2019/6/5

#
Never put a method into a method (main is in name). Never use the main method in your code.
gacha321 gacha321

2019/6/6

#
well, I tried to code like this :(( but this getting error too. and in the if condition it said it has bad operation :(( Helppp T^T
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * Write a description of class Button4 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Button4 extends Button
{
    /**
     * Act - do whatever the Button4 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this)){
            Counter counter = ((FarmWorld)getWorld()).getCounter();
            if(counter > 100){
                name();
                counter = (counter-100);
            }
        }
    }    
    
    public static void name (String[] args) throws IOException {
         String Name;
         InputStreamReader isr = new InputStreamReader(System.in);
         BufferedReader br = new BufferedReader(isr);
         System.out.print("Input yoour Grape's plant name: ");
         Name = br.readLine();
         System.out.println("Your Cows Name : " + Name);
    }
}
Super_Hippo Super_Hippo

2019/6/6

#
You compare a Counter object to a number. You will probably need something like this:
Counter counter = ((FarmWorld)getWorld()).getCounter();
if (counter.getValue() >= 100)
{
    counter.addValue(-100);
}
gacha321 gacha321

2019/6/6

#
It works, but when I clicked the button, it said error because buyPlants can't be cast to FarmWorld.. hmmm It's caused by the counter??
There are more replies on the next page.
1
2
3