Learning Java, troubleshooting an assignment [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
SO I have an assignment for one of my classes to create an Insurance Calculator that takes in some input for ones age, level of education, marriage status, and employment status and uses this to come up with an insurance payment. The default payment is set to 100 and if an individual is younger than 22 or older than 70, the insurance is increased by 25%. If the individual has a Bachelors, the cost is reduced by 10% (20% for masters), and if the individual is married it is further decreased by 20%. If their employment status is student, cost increases 10%, but if they are unemployed it decreases 5%. I have a huge block of code and Ive been running through it for the last few days and I seem to be stuck on my conditionals. Currently running in two classes, one for the Individual.Class and one for the user inputs.
Individual.java:
public class Individual {
private int age;
private String ed_stat;
private String mar_stat;
private String emp_stat;
private double default_Payment = 100;
public Individual(int suns, String edu, String mar, String employ, double payments) {
this.age = suns;
this.ed_stat = edu;
this.mar_stat = mar;
this.emp_stat = employ;
this.defaultPayment = payments;
}
public int getAge() {
return this.age;
}
public String getEdu() {
return this.ed_stat;
}
public String getMar() {
return this.mar_stat;
}
public String getEmploy() {
return this.emp_stat;
}
public double getPay(){
if (22 < age > 70) {
default_Payment = default_Payment + (default_Payment * .25);
}
if (ed_stat == "Bachelor's") {
default_Payment = default_Payment - (default_Payment * .10);
}
if (ed_stat == "Master's") {
default_Payment = default_Payment - (default_Payment * .20);
}
if (mar_stat == "yes") {
default_Payment = default_Payment - (default_Payment * .20);
}
if (emp_stat == "Student") {
default_Payment = default_Payment + (default_Payment * .10);
}
if (emp_stat == "Unemployed") {
default_Payment = default_Payment - (default_Payment * .05);
}
return this.default_Payment;
}
public void setAge(int suns) {
this.age = suns;
}
public void setEdu(String edu) {
this.ed_stat = edu;
}
public void setMar(String mar) {
this.mar_stat = mar;
}
public void setEmploy(String employ) {
this.emp_stat = employ;
}
public void setPayment(double payments) {
this.default_Payment = payments;
}
public String toString() {
return String("Age: " + age() + ", Education : " + ed_stat + ", Marital Status: " + mar_stat + ", Employment Status: " + emp_stat + ", Payment: " + default_Payment);
}
}
And the InsuranceCalculator.java
import java.util.Scanner;
public class InsuranceCalculator {
public static void main(String[] args) {
double insurance = 100;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your age: ");
int years = scan.nextInt();
System.out.println("Please enter your education level as High School, Bachelor's, or Master's");
String edu = scan.next();
System.out.println("Are you married? Yes or No");
String mar = scan.next();
System.out.println("Please indicate your employment status as Unemployed, Student, or Employed");
String employ = scan.next();
Individual me = new Individual(years, edu, mar, employ, insurance);
System.out.println(me.toString());
}
Note that I have errors in the Individual.java and I can't tell how the Calculator class will run once I fix the Individual.java.
Any help is greatly appreciated, I will be working on my own as well and will update as I solve

Rather than comparing Strings with the '==' operator, you want to use the .equals() method. For example:
if(ed_stat.equals("Bachelor's")){

Related

How to make private hashmap in one class visible to other class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
So everything works great in my program, but I read that making variable not private in class is a big mistake, because it can make problems with others part of big program.
Well I tried making HashMap airplane and flight private but I get error that "The field Airplane.airplane is not visible",which is of course true.
But how do I then make it visible in interface class?
Thanks in advance, I'm still learning and I got to this part in course.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner imeskanera = new Scanner(System.in);
Airplane airplane = new Airplane();
flight flight = new flight();
interface_aerodrom ui = new interface_aerodrom(imeskanera,airplane,flight);
ui.start();
}
}
/ Airplane class
import java.util.HashMap;
public class Airplane {
HashMap<String,Integer>airplane;
private String id;
private int capacity;
public Airplane() {
this.airplane = new HashMap<String,Integer>();
}
public void add(String id, int capacity) {
this.id = id;
this.capacity = capacity;
this.airplane.put(id, capacity);
}
public String id() {
return this.id;
}
public int capacity() {
return this.capacity;
}
public String airplaneinfo() {
return this.id + "( " + this.capacity + " )";
}
}
/interface class
import java.util.Scanner;
public class interface_aerodrom {
private Scanner imeskanera;
private Airplane airplane;
private flight flight;
public interface_aerodrom(Scanner scanner, Airplane airplane,flight flight) {
this.imeskanera = scanner;
this.airplane = airplane;
this.flight = flight;
}
public void start() {
System.out.println("Airport panel\r\n"
+ "--------------------");
System.out.println();
while(true) {
System.out.println("Choose operation:\r\n"
+ "[1] Add airplane\r\n"
+ "[2] Add flight\r\n"
+ "[x] Exit");
String input = this.imeskanera.nextLine();
input = input.toLowerCase();
input = input.trim();
if(input.equals("x")) {
flight_service();
break;
}
else if(input.equals("1")) {
addairplane();
}
else if(input.equals("2")){
addflight();
}
}
}
public void flight_service() {
System.out.println("Flight service\r\n"
+ "------------");
while(true) {
System.out.println("Choose operation:\r\n"
+ "[1] Print planes\r\n"
+ "[2] Print flights\r\n"
+ "[3] Print plane info\r\n"
+ "[x] Quit");
String input = this.imeskanera.nextLine();
input = input.toLowerCase();
input = input.trim();
if(input.equals("quit")){
break;
}
else if(input.equals("1")) {
for(String name : this.airplane.airplane.keySet()) {
int numberofseats = this.airplane.airplane.get(name);
String list = name + "( " + numberofseats + " )";
System.out.println(list);
}
}
else if(input.equals("2")){
for(String name : this.flight.flight.keySet()) {
String value = this.flight.flight.get(name);
String list = name + value;
System.out.println(list);
}
}
else if(input.equals("3")) {
System.out.println("Give plane ID: ");
String planeid = this.imeskanera.nextLine();
if(airplanecontains(planeid)) {
int numberofseats = this.airplane.airplane.get(planeid);
System.out.println(planeid + "( " + numberofseats + " )" );
} else {
System.out.println("That plane is not in our database");
}
}
}
}
public void addairplane() {
System.out.println("Give plane ID: ");
String ID = this.imeskanera.nextLine();
System.out.println("Give plane capacity: ");
int capacity = Integer.parseInt(this.imeskanera.nextLine());
this.airplane.add(ID, capacity);
}
public boolean airplanecontains(String ID) {
if(this.airplane.airplane.containsKey(ID)) {
return true;
}else {
return false;
}
}
public void addflight() {
System.out.println("Give plane ID: ");
String ID = this.imeskanera.nextLine();
if(airplanecontains(ID)) {
System.out.println("Give departure airport code: ");
String departure = this.imeskanera.nextLine();
System.out.println("Give destination airport code: ");
String destination = this.imeskanera.nextLine();
int seats = this.airplane.airplane.get(ID);
this.flight.flight.put(ID + " ( " + seats + " ) ",departure + "-" + destination);
}
else {
System.out.println("This plane is not in our database");
}
}
}
/ flight class
import java.util.HashMap;
public class flight {
HashMap<String,String>flight;
public flight() {
this.flight = new HashMap<String,String>();
}
public void add(String departure, String destination) {
this.flight.put(departure, destination);
}
}
Making a field private does not necessarily mean you can't share it. You can use a getter to return the HashMap.
private Map<String,Integer>airplane = new HashMap<>();
public Map<String,Integer> getAirPlaneMap() {
return airplane;
}
The reason being is that this hides implementation details and allows for future changes without affecting users of the class. Users don't need to know where the map comes from within your class. You could have retrieved it from some where yourself and the user wouldn't know.
You may also want to ensure a user can't change it. So you could do the following:
public Map<String,Integer> getAirPlaneMap() {
return Collections.unModifiableMap(airplane);
}
The above will prevent the user from adding or deleting map elements. But it won't prevent them from changing a retrieved object from the map unless that object is also immutable.
In general, setter and getters are the best way to allow users to set and retrieve values. And it is usually a good idea to make defensive copies of mutable items that they are retrieving to ensure that the retrieved values are consistent for all users during execution of the program.

Displaying a final price as currency format and reading from file in java

I'm nearly finished with a program I've been writing for quite some time for a school final assignment. It's 99% complete, but I'd like to add a couple things to make it as perfect as possible and need help.
I'd like to display the final price of an order in currency format, but have only figured out a way to format as currency if it's in my tostring method.
There's also bonus points to be had with this if I create a method that creates an order from a file. (ex: orders 1 Reece's Pieces, 6 sugar cookies, 1 chocolate chip cookie, and 1 scoop of caramel ice cream).
Here's my code. Any help is appreciated.
package dessertshop;
import java.text.NumberFormat;
import java.util.Scanner;
abstract class DessertItem
{
protected String name;
public DessertItem()
{
this.name = "";
}
public DessertItem( String name )
{
this.name = name;
}
public final String getName()
{
return name;
}
public abstract double getCost(int number);
}
class Candy extends DessertItem
{
private double pricePerPound;
public Candy( String name, double unitPrice )
{
super( name );
this.pricePerPound = unitPrice;
}
#Override
public double getCost(int amount)
{
return( amount * pricePerPound );
}
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( "Candy\t" + name + "\t # " + formatter.format( this.pricePerPound ) + " per pound");
}
}
class Cookie extends DessertItem
{
private double pricePerDozen;
public Cookie(String name, double pricePerDozen)
{
super(name);
this.pricePerDozen=pricePerDozen;
}
#Override
public double getCost(int amount)
{
return(amount * pricePerDozen)/12;
}
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( "Cookie\t" + name + "\t # " + formatter.format( this.pricePerDozen) + " per dozen");
}
}
class IceCream extends DessertItem
{
private double cost;
public IceCream(String name, double cost)
{
super(name);
this.cost=cost;
}
#Override
public double getCost(int amount)
{
return(amount * cost);
}
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( "Ice Cream\t" + name + "\t # " + formatter.format( this.cost ) + " per scoop");
}
}
public class DessertShop
{
private String name = "";
private DessertItem[] menu;
private int numberOfItems = 0;
public DessertShop(String name)
{
this.name = name;
menu = new DessertItem[200];
}
public DessertShop()
{
menu = new DessertItem[200];
}
public void addToMenu(DessertItem item)
{
menu[numberOfItems++] = item;
}
public void printMenu()
{
System.out.println(this.toString());
for (int i = 0; i < numberOfItems; i++)
{
System.out.print((i + 1) + ": " + menu[i].toString());
System.out.println("");
}
}
public double createNewOrder()
{
double totalCost = 0;
Scanner input = new Scanner(System.in);
while (true)
{
this.printMenu();
System.out.print("What would you like to purchase? (0 to checkout) > ");
int choice = input.nextInt();
if (choice == 0)
{
break;
}
System.out.print("How many (lbs. or amount) > ");
int amount = input.nextInt();
totalCost += menu[(choice - 1)].getCost(amount);
}
input.close();
return totalCost;
}
public String toString()
{
return ("Welcome to " + name);
}
public static void main( String[] args )
{
DessertShop shop01 = new DessertShop("Chuck D's Dessert Depot");
Candy candy01 = new Candy("Reece's Pieces", 3.99);
Candy candy02 = new Candy("Chocolate Covered Raisins", 4.99);
Cookie cookie01 = new Cookie("Peanut Butter", 5.99);
Cookie cookie02 = new Cookie("Chocolate Chip", 4.99);
Cookie cookie03 = new Cookie("Sugar", 4.50);
IceCream icecream01 = new IceCream("Cookie Dough", 3.00);
IceCream icecream02 = new IceCream("Vanilla", 2.00);
IceCream icecream03 = new IceCream("Caramel", 3.50);
IceCream icecream04 = new IceCream("Rocky Road", 2.99);
IceCream icecream05 = new IceCream("Mint Chocolate Chip", 3.99);
shop01.addToMenu(candy01);
shop01.addToMenu(candy02);
shop01.addToMenu(cookie01);
shop01.addToMenu(cookie02);
shop01.addToMenu(cookie03);
shop01.addToMenu(icecream01);
shop01.addToMenu(icecream02);
shop01.addToMenu(icecream03);
shop01.addToMenu(icecream04);
shop01.addToMenu(icecream05);
double frankOrder = shop01.createNewOrder();
System.out.println(frankOrder);
}
}
Depending on what you seek, you can format your price at your last line of code:
System.out.println(frankOrder)
have a look: here.
About the other question, reading in from file, I suggest you have a look at csv files: here and how to read them:here
If you don't want to hassle too much, you can get decent result with printf:
System.out.printf("Price is: €%,d", 12342353563623L);
Output is: Price is: €12,342,353,563,623
Formatter on "," flag:
The result will include locale-specific grouping separators

Accepting User Input for Getter Class in Java

Java newbie here, I have one of those assignments where I need to write a class for creating student objects and then also write a driver.
I have all of the requirements done, except I am having trouble with this one - "Write method getTestScore that accepts test number and returns appropriate score."
I believe I've written the class correctly, but I am having trouble writing the code for the driver.
After the prompt for the user to enter the test number, it isn't returning anything and the program terminates insted of returning appropriate score .
Help is very appreciated!
Here is the class:
public class Student
{
private String firstName, lastName;
private String homeAddress, schoolAddress;
private int testScore1, testScore2, testScore3;
//Constructors
public Student()
{
firstName = "None";
lastName = "None";
homeAddress = "None";
schoolAddress = "None";
testScore1 = 0;
testScore2 = 0;
testScore3 = 0;
}
public Student(String first, String last, String home, String school, int score1, int score2, int score3)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
testScore1 = score1;
testScore2 = score2;
testScore3 = score3;
}
//Setter for test scores
public void setTestScore(int testNum, int score)
{
if (testNum == 1)
testScore1 = score;
else
if (testNum == 2)
testScore2 = score;
else
if (testNum == 3)
testScore3 = score;
else
throw new IllegalArgumentException(testNum + " is out of range");
}
//Getter for test scores
public int getTestScore(int testNum2)
{
if (testNum2 == 1)
return testScore1;
else
if (testNum2 == 2)
return testScore2;
else
if (testNum2 == 3)
return testScore3;
else
throw new IllegalArgumentException(testNum2 + " is out of range");
}
//Calculates average for each student
public int getAverage()
{
int average = (testScore1 + testScore2 + testScore3)/3;
return average;
}
//Returns a description of this Student object
public String toString()
{
String result;
result = firstName + " " + lastName + "\n";
result += "Home Address:\n" + homeAddress + "\n";
result += "School Address:\n" + schoolAddress + "\n";
result += "Test Score 1:\n" + testScore1 + "\n";
result += "Test Score 2:\n" + testScore2 + "\n";
result += "Test Score 3:\n" + testScore3 + "\n";
result += "Average Test Score:\n" + ((testScore1+testScore2+testScore3)/3);
return result;
}
}
And here is the driver:
package lab7;
import java.util.Scanner;
public class StudentBody {
public static void main(String[] args)
//Create student objects
{
Student snm24 = new Student("Sarah", "M", "18 79th Street", "5000 Forbes Ave.", 95, 80, 63);
Student adk28 = new Student("Andrew", "K", "16 Collins Ave.", "16401 NW 37th Ave.", 90, 82, 76);
//Get average for snm24
snm24.getAverage();
System.out.println("snm24 average initial: " + snm24.getAverage());
//Set new test score for test 3 for snm24 and see new average
snm24.setTestScore(3, 68);
System.out.println("snm24 average after adjustment: " +snm24.getAverage());
//Get test score for adk28
Scanner scan = new Scanner(System.in);
System.out.println("Which test score are you looking for?:");
int testNum2 = scan.nextInt();
adk28.getTestScore(testNum2);
}
}
it isn't returning anything and the program terminates. Help is very appreciated!
this here>
adk28.getTestScore(testNum2);
the returned value is getting lost.. do something with that, print it, assign it to a variable:
int result = adk28.getTestScore(testNum2);

Java Ticket program with inheritance

I have a program with different types of tickets. I have the Ticket, AdvanceTicket, and a StudentAdvanceTicket. I am printing the price of the ticket $50, advance ticket both options $30 and $40), and both options for student advance tickets $15 and $20.
Both options with the condition being if daysAhead >= 10.
I'v spent hours trying to solve this, I need help.
Ticket.java
public class Ticket {
private int number;
public Ticket(int number) {
this.number = number;
}
public double getPrice() {
return 50.0;
}
public String toString() {
return "Ticket #" + this.number + ", Price: $" + this.getPrice();
}
}
TicketMain.java
public class TicketMain {
public static void main(String[] args) {
Ticket[] tickets = new Ticket[5];
tickets[0] = new WalkupTicket(1);
tickets[1] = new AdvanceTicket(2,12);
tickets[2] = new AdvanceTicket(3,8);
tickets[3] = new StudentAdvanceTicket(4,17);
tickets[4] = new StudentAdvanceTicket(5,7);
for (int i = 0; i<5; i++) {
System.out.println(tickets[i]);
//System.out.println(" ");
}
}
}
AdvanceTicket.java
public class AdvanceTicket extends Ticket {
private int daysAhead;
public AdvanceTicket(int number, int daysAhead) {
super(number);
this.daysAhead = daysAhead;
}
public double getPrice() {
if (daysAhead >= 10) {
return 30.00;
} else {
return 40.00;
}
}
public String toString() {
if (daysAhead >= 10) {
return super.toString() + " (" + this.daysAhead + " days ahead, you got a great deal!)";
} else {
return super.toString() + " (" + this.daysAhead + " days ahead, you could have saved a bit more)";
}
}
}
And finally the issue...
StudentAdvanceTicket.java
public class StudentAdvanceTicket extends AdvanceTicket {
public StudentAdvanceTicket(int number, int daysAhead) {
super(number, daysAhead);
super.price = super.getPrice() / 2;
}
public String toString() {
return super.toString() + " (ID Required) ";
}
}
This is my desired output.
Ticket #1, Price: $50.0
Ticket #2, Price: $30.0 (12 days ahead, you got a great deal!)
Ticket #3, Price: $40.0 (8 days ahead, you could have saved a bit more)
Ticket #4, Price: $15.0 (ID Required)
Ticket #5, Price: $20.0 (ID Required)
When you call getPrice() for a StudentAdvanceTicket, your program actually calls the implementation from AdvanceTicket, which will return $30 or $40.
Now when you call toString() for a StudentAdvanceTicket, it calls toString from the super, which is the one for AdvanceTicket, and that one calls the object's getPrice() method, which, again, just returns $30 or $40 and doesn't make use of the price member variable.
The correct solution is to override getPrice for the StudentAdvanceTicket class. In that method, you call super.getPrice() and then do the price manipulation with that.
In StudentAdvanceTicket there is a compile error because super.price cannot be resolved.

Java: Error for no reason [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi,
I need help in the field of validating data. For some reason i keep getting a incompatible error. I checked a couple times now that I have the right type. What is wrong, 2 classes. The error is int the driver class keep bringing incompatibale tyoes in "name = student.setName( input);". Please explain why?
/*
* Joel Gordon
* per.4
*/
import java.util.Scanner;
public class P5A
{
public static void main (String args[])
{
Scanner reader = new Scanner(System.in);
student student = new student();
String name, validate, valtests;
int tests, score, count = 100;
String input = reader.nextLine();
System.out.println( "Please enter the students Name: " + input);
name = student.setName( input);
validate = student.validateData( name );
System.out.print( "Please enter Test " + count + "(As number 1-3 for test number, then test score): ");
tests = student.getScore( reader.nextInt(), reader.nextInt());
valtests = student.validateTests(tests);
System.out.println( stu.toString());
}//end of main mthod
}//end of main class/Driver
End of student Class driver.
public class student
{
private String name, result;
private int test1, test2, test3;
public student ()
{
name = "";
test1 = 0;
test2 = 0;
test3 = 0;
result = "";
}//constructor
public void setName (String nm)
{
name = nm;
}//setting the name in the program
public String getName ()
{
return name;
}//getting the name
public int getScore (int i, int score)
{
if (i == 1) test1 = score;
else if( i == 2)test2 = score;
else test3 = score;
if ( i == 1 )return test1;
else if ( i == 2 ) return test2;
else return test3;
}//getting score of tests
public int getAverage ()
{
int average;
average = (int) Math.round((test1 + test2 + test3)/ 3.0);
return average;
}//getting a average of all the scores
public int getHighScore()
{
int highscore;
highscore = test1;
if (test2 > highscore) highscore = test2;
if (test3 > highscore)highscore = test3;
return highscore;
}//getting the highscores of all three
public String validateData(String name)
{
if (name.equals("") ) result = "You have entered an invalid name, Please restart." ;
return result;
}
public String validateTests ( int tests )
{
if (test1 >= 0 && test1 <= 100 || test2 >= 0 && test2 <= 100 || test3 >= 0 && test3 <= 100) result = " You have entered an invalid number, between 1-100. " +
"Please restart!" ;
return result;
}//getting the test scores and tesing each one against method
public String toString()
{
String str;
str = "Name: " + name +
"\nTest1: " + test1 +
"\nTest2: " + test2 +
"\nTest3: " + test3 +
"\nAverage: " + getAverage() +
"\nHighscore: " + getHighScore();
return str;
}//putting all the tests together to view in termainal
}
You are struggling with control flow.
What is happening is that when you call your teacher constructor, no matter what, the following lines of code are always executed:
System.out.println ( "I don't have a teacher in that room." );
System.out.println("Always show");
There are a few ways to fix this:
First, you can return; inside your if statements.
Or you can use if then else if then else if then else as the last one for each of your conditions.
Or you can use a switch statement.
You should make Teacher a class representing teacher. Then, you create instances of Teacher objects outside teacher class.
public class Teacher{
private String name;
private String catchPhrase;
private int roomNumber;
public(String name, String catchPhrase, int roomNumber){
this.name = name;
this.catchPhrase = catchPhrase;
this.roomNumber = roomNumber;
}
//put setters and getters here...
public String toString(){
return "Name: "+this.getName()+"\nCatch Phrase: "+this.getCatchPhrase() + "\nRoom Number:
"+this.getRoomNumber();
}
}
Then, you can create Teacher objects. For instance, you can put them in TeacherDriver class.
public class TeacherDriver{
public static void main (String[] args){
//create array of teachers (you can use list and other data structures if you want)
Teacher[] myTeachers = new Teacher[]{
new Teacher("Mr. Clark", "Just do it", 225),
new Teacher("Mr. Harris", "Do the essays and you will pass.", 123)
};
//ask for room number input here...
//mark this truE if teacher is found
boolean found = false;
//search the array of teachers for a match
for(Teacher chosenTeacher : myTeachers)
//if the room number of the teacher matches your target room number
//then display teacher info and stop reading the array
//note: replace <your input room number> with your input variable name
if(chosenTeacher.getRoomNumber() == <your input room number>){
found = true;
System.out.println(chosenTeacher.toString());
break; //stop the loop
}
}
if(!found){
System.out.println ( "I don't have a teacher in that room." );
}
}
A better way to do this though is to create another class that holds collection/array of Teacher objects.
Example:
public class TeacherList{
List<Teacher> teachers;
public TeacherList(){
teachers = new ArrayList<>();
//you can put initial entries like:
teacher.add(new Teacher("Mr. Clark", "Just do it", 225));
teacher.add( new Teacher("Mr. Harris", "Do the essays and you will pass.", 123));
}
public void add(Teacher e){
teachers.add(e);
}
public void findTeacher(int roomNumber){
//mark this tru if teacher is found
boolean found = false;
//search
for(Teacher chosenTeacher : teachers)
//if the room number of the teacher matches your target room number
//then display teacher info and stop reading the array
//note: replace <your input room number> with your input variable name
if(chosenTeacher.getRoomNumber() == <your input room number>){
found = true;
System.out.println(chosenTeacher.toString());
break; //stop the loop
}
}
if(!found){
System.out.println ( "I don't have a teacher in that room." );
}
}
}
Then, in your main:
//create Teacher list
TeacherList myTeachers = new TeacherList();
//input room number here...
//replace <room number? with input variable name
myTeachers.findTeacher(<room number>);
If you want to accept multiple room numbers, then you can put the last two lines of codes above inside a loop.

Categories