Reputation:
I am trying to pass in the user Input into a parameter when instantiating a constructor. I am stuck on how to pass in the user input when we instantiate the constructor. We are using user input to create a custom taco while inheriting from another class.
import java.io.*;
class TacoSupreme extends Taco
{
private int sourCream;
private int blackOlives;
private int userInput;
public TacoSupreme()
{
super();
System.out.println("Making a default Taco Supreme...");
sourCream = 1;
blackOlives = 1;
}
public int weight()
{
return (sourCream + blackOlives + super.weight());
}
public void gutBomb()
{
super.gutBomb();
sourCream = sourCream * 2;
blackOlives = blackOlives * 2;
}
public void print_order()
{
super.print_order();
System.out.println("PLUS !!!!");
System.out.println(sourCream + " units of sour cream, and");
System.out.println(blackOlives + " units of black olives.");
}
public TacoSupreme (String n1,int b1,int b2,int c1,int l2, int s, int o) throws IOException
{
super(n1, b1, b2, c1, l2);
String user;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); <---this works but I don't know how to use the userInput when I instantiate -->
System.out.print ("Please enter your name: ");
user = br.readLine();
System.out.println("Making a custom Taco Supreme ...");
sourCream = s;
blackOlives = o;
n1 = user;
}
}
public class Lab8
{
public static void main (String args[]) throws IOException
{
int w;
TacoSupreme t0 = new TacoSupreme();
t0.print_order();
w = t0.weight();
System.out.println("The above taco weight is: " + w + "\n");
System.out.println("Invoking the gutBomb special!!!\n");
t0.gutBomb();
t0.print_order();
w = t0.weight();
System.out.println("The above taco weight is: " + w + "\n");
TacoSupreme t1 = new TacoSupreme("bob",2,3,4,5,6,7); <-----this is where I am having the problem---->I don't know how to add the user input here
t1.print_order();
w = t1.weight();
System.out.println("The above taco weight is: " + w + "\n");
System.out.println("Invoking the gutBomb special!!!\n");
t1.gutBomb();
t1.print_order();
w = t1.weight();
System.out.println("The above taco weight is: " + w + "\n");
Upvotes: 0
Views: 84
Reputation: 1907
You can use Scanner
class to take input from user and initialize your constructor
with them, add these changes:
Add this to Main():
Scanner scan = new Scanner(System.in);
System.out.print("Please enter name : ");
String name = scan.nextLine();
System.out.print("Please enter n1 : ");
int n1 = scan.nextInt();
System.out.print("Please enter b1 : ");
int b1 = scan.nextInt();
System.out.print("Please enter b2 : ");
int b2 = scan.nextInt();
System.out.print("Please enter c1 : ");
int c1 = scan.nextInt();
System.out.print("Please enter l1 : ");
int l1 = scan.nextInt();
System.out.print("Please enter o : ");
int o = scan.nextInt();
TacoSupreme t1 = new TacoSupreme(name,n1,b1,b2,c1,l1,o);
Add this to TacoSupreme():
super(n1, b1, b2, c1, l2);
System.out.println("Making a custom Taco Supreme ...");
sourCream = s;
blackOlives = o;
Upvotes: 1