简体   繁体   中英

Java runner class won't accept variables

Okay so I have this Java object class that has methods to create a backpack object with options. I got all the methods working but the final method which displays the info isn't working correctly. It seems to not be accepting the parameters that are being inputted. I've included both the object class and the runner class.

/* Variables and parameters
 * Intro "Welcome to BackPackMaker, the ultimate backpack making experience. NOW LET'S MAKE SOME BACKPACKS!!"
 * Color(red,indigo,yellow,green,purple,orange,black,white)
 * No. of straps(1,2)
 * Size (small,medium,large,gigantic)
 * Pouches(1,2,3,4,5)
 * Water Bottle Slot(True,False)
 * Do you like Spongebob(True,False) if yes, "Hello SpongeBob, my name is PatBack!"
 * 
 * Use or statements in the if loop to keep things simple
 * The if loop consists of checking to see whether one of the variables was used.
 * 
 * All methods must be commented
 */
import java.util.Scanner;
public class Backpack {
//Setting up the private variables
private String color; //color of the backpack
private String cchoice;
private int straps; //amount of straps(some backpacks only have one)
private int strapsans;
private String size; //how big it is
private String tsize; //how big it is
private int pouches; //how many pouches there are
private int tpouch; //how big it is
private boolean slot; //if there is a water bottle slot on the side
private boolean waterslot;
private static int cost; //The cost based off of number of pouches and straps.

public Backpack(){
color = "red"; //color of the backpack
cchoice = "red";
straps = 2; //amount of straps(some backpacks only have one)
strapsans = 2;
size = "big"; //how big it is
tsize = "big";
pouches = 2; //how many pouches there are
tpouch = 2;
slot = true; //if there is a water bottle slot on the side
waterslot = true;
cost = 100; //The cost based off of number of pouches and straps.   
}


public String pickc(){
    String color = "blank";
    Scanner input = new Scanner(System.in);
    System.out.println("Please choose a color.");
    System.out.println("You can have red, blue, yellow, green, purple, or orange");
    color = input.nextLine();
    String cchoice = color;
    if(cchoice.equals("red") || cchoice.equals("blue") || cchoice.equals("yellow") || cchoice.equals("green") || cchoice.equals("purple") || cchoice.equals("orange"))
    {
        return cchoice;
    }
    else
    {
        System.out.println("please enter a valid choice");
        return pickc();
    }   
    }
public String getcolor(){
    return cchoice;
}

public String picks(){
    String size = "blank";
    Scanner input5 = new Scanner(System.in);
    System.out.println("What size do you want? Available sizes are small, medium, and large");
    size = input5.nextLine();
    String tsize = size;
    if(tsize.equals("small") || tsize.equals("medium") || tsize.equals("large")){
        return tsize;
    }
    else
    {
        System.out.println("please enter a valid choice");
        return picks();
    }

}

public String getsize(){

    return tsize;
}
public int pouchnum(){
    pouches = 0;
    Scanner input2 = new Scanner(System.in);
    System.out.println("How many pouches do you want?");
    pouches = input2.nextInt();
    int tpouch = pouches;
    if(tpouch == 1 || tpouch == 2 || tpouch == 3 || tpouch == 4 || tpouch == 5){
        System.out.println(tpouch);
        return tpouch;
    }
    else
    {
        System.out.println("Enter a valid number between 0 and 5");
        return pouchnum();
    }
}
public int getpouchnum(){
    return tpouch;
}

public boolean slotyes(){
    boolean slots = true;
    Scanner input4 = new Scanner(System.in);
    System.out.println("Do you want a water bottle space? Enter 1 for yes or anything else for no");
    int answer = input4.nextInt();
    boolean waterslot = slot;
    if(answer == 1){
        slot = true;
        return slot;

    }
    else if (answer == 2){
        slot = false;
        return slot;
    }
    return false;
}

public boolean getslot(){
    return waterslot;
}

public int straps(){
    straps = 0;
    Scanner input3 = new Scanner(System.in);
    System.out.println("How many straps do you want? You can have up to 2");
    straps = input3.nextInt();
    int strapsans = straps;
    if(strapsans == 1 || strapsans == 2){
        System.out.println(straps);
        return strapsans;
    }
    else
    {
        System.out.println("1 or 2 straps only");
        return straps();
    }


}
public int getstraps(){
    return strapsans;
}

public void displayinfo(){ //Displays the various values of the backpack.

    System.out.println("Your backpack is a " + tsize + ", " + cchoice + " backpack with " + tpouch + "pouch(s) and " + strapsans + "strap(s).");
}
}

import java.util.Scanner;
public class BackPackMaker {

    public static void main(String[] args)
{
        System.out.println("Welcome to BackPack Maker, prepare for the ultimate midterm experience");
        System.out.println("NOW LET'S MAKE SOME BACKPACKS!");
        Backpack B1 = new Backpack();
        B1.pickc();
        B1.getcolor();
        B1.picks();
        B1.getsize();
        B1.pouchnum();
        B1.getpouchnum();
        B1.straps();
        B1.getstraps();
        System.out.println(B1.slotyes());
        B1.getslot();
        B1.displayinfo();


}
}

You need to assign inputed value to the variable of your backpack object. After passing input in method -> this.pouches = pouches;

if(tpouch == 1 || tpouch == 2 || tpouch == 3 || tpouch == 4 || tpouch == 5){
            System.out.println(tpouch);
            this.pouches = tpouch;
            return tpouch;
        }

You receive the same values every time because you have created object of Backpack class and never changed its original values.

Also you need to display proper values, I don't really know why do you even care about "pouches" variable if there is no use for it. Try to keep it as simple as possible - if there is something you don't use - get rid of this.

You are just taking the input and not setting it to instance variable. For example while taking bag size String tsize = size; this sets the size to local variable tsize not the instance variable. It should be just this.tsize=size . And also in other methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM