import java.util.ArrayList;
import java.util.Scanner;
public class StudentList {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
int choice;
printMenu();
do {
Scanner input = new Scanner(System.in);
choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("\nAdd a student\n");
students.add(addStudent());
printMenu();
break;
case 2:
System.out.println("\nFind a student\n");
findStudent(students);
printMenu();
break;
case 3:
System.out.println("\nDelete a student\n");
displayAllStudents(students);
printMenu();
break;
case 4:
System.out.println("\nDispay all students\n");
displayAllStudents(students);
printMenu();
break;
case 5:
System.out.println("\nDisplay the total number of students\n");
studentSize(students);
printMenu();
break;
case 6:
System.out.println("\nGoodbye!\n");
break;
default:
System.out
.println("\nYour choice," + choice + ", is invalid\n");
break;
}
} while (choice != 6);
}
public static void printMenu() {
System.out
.println("\nPlease select from the following menu:\n"
+ "\t1. Add a student\n" + "\t2. Find a student\n"
+ "\t3. Delete a student\n"
+ "\t4. Display all students\n"
+ "\t5. Display the total number of students\n"
+ "\t6. Exit\n");
System.out.print("Your choice: ");
}
public static Student addStudent() {
Scanner inputS = new Scanner(System.in);
System.out.println("First Name:");
String fName = inputS.nextLine();
System.out.println("Last Name:");
String lName = inputS.nextLine();
System.out.println("Major Name:");
String major = inputS.nextLine();
System.out.println("Student Name:");
Integer sNumber = inputS.nextInt();
System.out.println("gpa:");
double grade = inputS.nextDouble();
return new Student(fName, lName, Major, sNumber, grade);
}
public static void displayAllStudents(ArrayList<Student> students) {
for (Student s : students) {
System.out.print(s.toString());
}
}
public static void findStudent(ArrayList<Student> students) {
Scanner inputN = new Scanner(System.in);
String name = inputN.nextLine();
for (Student s : students) {
if (s.getFName().equals(name)) {
System.out.println(s);
break;
}
}
}
public static void deleteStudent(ArrayList<Student> students) {
Scanner inputS = new Scanner(System.in);
String sNum = inputS.next();
for (Student s : students) {
if (s.FName().equals(sNum)) {
students.remove(s);
break;
}
}
}
public static void studentSize(ArrayList<Student> students) {
System.out.printf("Size: %\n", students.size());
}
}
i cannot figure out what is wrong with my code
it gives me an error on these three things when i try to compile
return new Student(fName, lName, Major, sNumber, grade);
if(s.FName().equals(sNum))
if(s.getFName().equals(name))
First:
return new Student(fName, lName, Major, sNumber, grade);
Major is not a variable, its major.
Second:
if(s.FName().equals(sNum))
it should be
if(s.getFName().equals(sNum))
UPDATE
public static Student addStudent() {
Scanner inputS = new Scanner(System.in);
System.out.println("First Name:");
String fName = inputS.nextLine();
System.out.println("Last Name:");
String lName = inputS.nextLine();
System.out.println("Major Name:");
String major = inputS.nextLine(); //variable name: major
System.out.println("Student Name:");
Integer sNumber = inputS.nextInt();
System.out.println("gpa:");
double grade = inputS.nextDouble();
//variable sent: Major, it should be major
return new Student(fName, lName, Major, sNumber, grade);
}
at one place you are
s.FName()
and at another
s.getFName()
Also following would give you error at rumtime
for(Student s: students)
{
if(s.FName().equals(sNum))
{
students.remove(s);
break;
}
}
You can't delete an object from array, while looking at it.
You should use iterator.
Like this:
for (Iterator<Student> it = students.iterator(); it.hasNext(); )
{
Student student = (Student)it.next();
if (!s.getFName().equals(sNum))
{
tmp.add(student);
}
lines.clear();
for (Student t: tmp)
students.add(t);
tmp.clear();
}
Related
I am trying to access the accOpBalance parameter from the Account method stored in the HashMap. I want to be able to change this value based on transaction type and store it again. I also want to save the the 6 most recent transactions fot the account but not sure how to approach the problem. I hope this describes the problem well enough. Im still very new to this and would love feedback on my code aswell.
public class BankingSystem{
static int accNum;
static String accName;
static String accAdd;
static String accOpDate;
static double accOpBalance;
int option = 0;
static Scanner keyboard = new Scanner(System.in);
static HashMap<Integer, Account> accounts = new HashMap<Integer, Account>(Map.of(
accNum, new Account(accNum, accName, accAdd, accOpDate, accOpBalance)
));
public BankingSystem() {
}
public static void main(String[] args) {
BankingSystem bankingSystem = new BankingSystem();
int option;
do{
option = mainMenu(keyboard);
System.out.println();
if(option == 1){
bankingSystem.createAccount(keyboard);
} else if (option == 2) {
bankingSystem.showAccounts();
} else if (option == 4) {
bankingSystem.deleteAccount();
}
}while(option != 5);
}
public void createAccount(Scanner keyboard) {
do{
System.out.println("-----------------------------------------------");
System.out.print("Please enter 1 to continue or -1 to exit: ");
option = keyboard.nextInt();
switch (option) {
case 1 -> {
Account newAccount = new Account(accNum, accName, accAdd, accOpDate, accOpBalance);
System.out.println("-----------------------------------------------");
System.out.print("Please enter the new account number: ");
accNum = keyboard.nextInt();
newAccount.setAccNum(accNum);
System.out.print("Please enter the name of the account holder: ");
accName = keyboard.next();
newAccount.setAccName(accName);
System.out.print("Please enter the address of the account holder: ");
accAdd = keyboard.next();
newAccount.setAccAdd(accAdd);
System.out.print("Please enter the accounts open date: ");
accOpDate = keyboard.next();
newAccount.setAccOpDate(accOpDate);
System.out.print("Please enter the starting balance: ");
accOpBalance = keyboard.nextDouble();
newAccount.setAccOpBalance(accOpBalance);
accounts.put(accNum, new Account(accNum, accName, accAdd, accOpDate, accOpBalance));
}
case 2 -> {
System.out.println("Please type -1 the exit create account menu: ");
option = keyboard.nextInt();
}
}
}while (option != -1);
}
public void showAccounts() {
for (Map.Entry<Integer, Account> accountEntry : accounts.entrySet()) {
System.out.println("Account key(number) = " + accountEntry.getKey() + accountEntry.getValue());
}
}
public void deleteAccount(){
int key;
option = 0;
do {
System.out.println("-----------------------------------------------");
System.out.print("Please enter 1 to continue or -1 to exit: ");
option = keyboard.nextInt();
switch (option) {
case 1 -> {
System.out.print("-----------------------------------------------");
System.out.print("Please enter the Account number of the" + "\n" +
" the account you wish to delete");
System.out.print("-----------------------------------------------");
key = keyboard.nextInt();
accounts.remove(key);
}
case 2 -> {
System.out.println("Please type -1 the exit delete account menu: ");
option = keyboard.nextInt();
}
}
}while (option != -1);
}
public static int mainMenu(Scanner keyboard){
int menuOption;
System.out.println("Welcome to the bank of Cal");
System.out.println("-----------------------------------------------");
System.out.println("Main Menu" + "\n" + "Please select option:");
System.out.println("1. Create Account");
System.out.println("2. Display Accounts");
System.out.println("3. Deposit/Withdraw");
System.out.println("4. Delete Account");
System.out.println("5. Exit Program");
System.out.println("-----------------------------------------------");
do{
menuOption = keyboard.nextInt();
}while(menuOption <1 || menuOption >5);
return menuOption;
}
public class Account {
private int accNum;
private String accName;
private String accAdd;
private String accOpDate;
private double accOpBalance;
Scanner getDetails = new Scanner(System.in);
public Account(int theAccNum, String theAccName, String theAccAdd, String theAccOpDate, double theAccOpBalance){
accNum = theAccNum;
accName = theAccName;
accAdd = theAccAdd;
accOpDate = theAccOpDate;
accOpBalance = theAccOpBalance;
}
public void setAccNum(int accNum){
this.accNum = accNum;
}
public int getAccNum(){
return accNum;
}
public void setAccName(String accName){
this.accName = accName;
}
public String getAccName(){
return accName;
}
public void setAccAdd(String accAdd){
this.accAdd = accAdd;
}
public String getAccAdd(){
return accAdd;
}
public void setAccOpDate(String accOpDate){
this.accOpDate = accOpDate;
}
public String getAccOpDate(){
return accOpDate;
}
public void setAccOpBalance(double accOpBalance){
this.accOpBalance = accOpBalance;
}
public double getAccOpBalance(){
return accOpBalance;
}
#Override
public String toString() {
return "\n" + "Account Number: " + accNum + "\n" + "Name: " + accName +
"\n" + "Account Holder Address: " + accAdd + "\n" +"Account open date: "
+ accOpDate + "\n" + "Account balance: " + accOpBalance;
}
}
I'm having a problem with case 4. I'm trying to find a voter using ID. It's public and it says Cannot invoke contains(int) on the primitive type int. Why is that? The Id is literally public but I don't know the problem.
All I'm trying to do is to find the Id of the voter.
public class Voters {
public static HashSet<Voter> voters = new HashSet<>();
public static void main(String[] args) throws IOException {
while (true) {
System.out.println("1. Add New Voter");
System.out.println("2. List All Voters");
System.out.println("3. Find a Voter By Name");
System.out.println("4. Get information about specific voter");
System.out.println("5. Exit");
Scanner scanner = new Scanner(System.in);
int chosen = scanner.nextInt();
switch (chosen) {
case 1:
System.out.print("National ID Number: ");
int idNum = scanner.nextInt();
System.out.print("\nName: ");
String name = scanner.next();
System.out.print("\nMale Relative: ");
String maleRelative = scanner.next();
System.out.print("\nAge: ");
int age = scanner.nextInt();
System.out.print("\nAddress: ");
String address = scanner.next();
System.out.print("\nProvince: ");
String province = scanner.next();
System.out.println("");
Voter voter = new Voter(idNum, name, maleRelative, age, address, province);
voters.add(voter);
FileWriter fileWriter = new FileWriter("C:\\Users\\YourPcName\\Desktop\\voters.txt");
fileWriter.append("\n").append(voter.toString());
fileWriter.flush();
fileWriter.close();
break;
case 2:
for (Voter v : voters) {
System.out.println(v);
}
break;
case 3:
System.out.print("Name: ");
Scanner ss = new Scanner(System.in);
String n = ss.nextLine();
System.out.println();
for (Voter v : voters) {
if (v.name.contains(n)) {
System.out.println(v);
break;
}
}
break;
case 4:
System.out.print("Id: ");
Scanner nn = new Scanner(System.in);
int s = nn.nextInt();
System.out.println();
for (Voter v : voters) {
if (v.nationalIdNumber.contains(s)) {
System.out.println(v);
break;
}
}
break;
case 5:
System.exit(0);
break;
default:
}
}
}
static class Voter {
public int nationalIdNumber;
public String name;
public String maleRelativeName;
public int age;
public String address;
public String province;
public Voter(int nationalIdNumber, String name, String maleRelativeName, int age, String address,
String province) {
this.nationalIdNumber = nationalIdNumber;
this.name = name;
this.maleRelativeName = maleRelativeName;
this.age = age;
this.address = address;
this.province = province;
}
public String toString() {
return "Id: " + nationalIdNumber + " Name: " + name + " Male Relative: " + maleRelativeName + " Age: "
+ age + " Address: " + address + " Province: " + province;
}
}
nationalIdNumber is of type int It does not have any methods. What are you trying to do? Maybe that line should read
if (v.nationalIdNumber == s) {
? I dont't know. Give us some more context.
I am trying to search an arraylist of objects for an ID code, but I am stuck.
import java.util.Scanner;
import java.util.ArrayList;
public class Homework01{
public static void main(String[] args){
ArrayList<Transaction> argList = new ArrayList<Transaction>();
Scanner input = new Scanner(System.in);
System.out.println("Transaction List Menu");
System.out.println("=====================");
System.out.println("1) Add Transaction.");
System.out.println("2) Search Transactions.");
System.out.println("3) Filter.");
System.out.println("4) Display All Transactions.");
System.out.println("5) Exit.");
int menu = input.nextInt();
while (menu != 5) {
switch (menu) {
case 1:
addTransaction(argList);
break;
case 2:
;// Search Transaction
break;
case 3:
;// Filter Withdraws and Deposits
break;
case 4:
;// Display transactions
break;
case 5:
System.out.println("End");
break;
default:
System.out.println("Invalid response");
break;
}
menu = input.nextInt();
}
}
public static void addTransaction(ArrayList<Transaction> argList) {
Scanner input = new Scanner(System.in);
int tempId;
double tempAmount;
char tempType;
String tempDescription;
System.out.println("Enter in an ID for the transaction: ");
tempId = input.nextInt();
System.out.println("Enter in the amount of money: ");
tempAmount = input.nextDouble();
System.out.println("W for withdraw, D for deposit: ");
tempType = input.next(".").charAt(0);
System.out.println("Give transaction a description: ");
tempDescription = input.next();
//add transaction
argList.add(new Transaction(tempId, tempAmount, tempType, tempDescription) ); }
public static void searchTransactions(ArrayList<Transaction> argList){
Scanner input = new Scanner(System.in);
System.out.println("Please type in transaction ID: ");
int searchId = input.nextInt();
for(int i=0;i<argList.size();i++){
if(argList.argId.get(i).contains(searchId)){
System.out.println("Yes");
}
}
}
}
My second file contains this
public class Transaction {
int id;
char type;
double amount;
String description;
public Transaction(int argId, double argAmount,char argType, String
argDescription){
id = argId;
type = argType;
amount = argAmount;
description = argDescription;
}
public void getId(int id){
}
public void getAmount(double amount){
}
public void getType(char type){
}
public void getDescription(String description){
}
}
And i get the error message: argId cannot be resolved or is not a field on line 58. I think my error is that argId is not part of the ArrayList, and i need to find the right tern to search the ID codes in the ArrayList.
Thanks
Earlier, before you edited your question, you had wrong getter methods.
Instead of
public void getId(int id){
}
you should write this:
public int getId() {
return id;
}
Declare your fields in Transaction class as private.
Then change your other getters in the similar way.
About your actual question, you can use for-each loop:
public static void searchTransactions(ArrayList<Transaction> argList) {
try (Scanner input = new Scanner(System.in)) {
System.out.println("Please type in transaction ID: ");
int searchId = input.nextInt();
for (Transaction transaction : argList) {
if (transaction.getId() == searchId) {
System.out.println("Yes");
break;
}
}
}
}
If you insist for i loop, change it this way:
for (int i = 0; i < argList.size(); i++) {
if(argList.get(i).getId() == searchId){
System.out.println("Yes");
break;
}
}
ArgId is a property of the objects in the list, not a property of the list itself which is why the compiler is giving you an error.
Looks like a typo:
You have:
argList.argId.get(i).contains(searchId)
try using:
argList.get(i).argId.contains(searchId)
argList is a collection, then you get object, read argId and check if it contains searchId
I would suggest to encapsulate class Transaction:
public class Transaction {
private int id;
private char type;
private double amount;
private String description;
public Transaction(int argId, double argAmount, char argType, String argDescription) {
id = argId;
type = argType;
amount = argAmount;
description = argDescription;
}
public int getId() {
return id;
}
public char getType() {
return type;
}
public double getAmount() {
return amount;
}
public String getDescription() {
return description;
}
}
Then main class will look in this way:
import java.util.Scanner;
import java.util.ArrayList;
public class Homework01{
public static void main(String[] args){
ArrayList<Transaction> argList = new ArrayList<Transaction>();
Scanner input = new Scanner(System.in);
System.out.println("Transaction List Menu");
System.out.println("=====================");
System.out.println("1) Add Transaction.");
System.out.println("2) Search Transactions.");
System.out.println("3) Filter.");
System.out.println("4) Display All Transactions.");
System.out.println("5) Exit.");
int menu = input.nextInt();
while (menu != 5) {
switch (menu) {
case 1: ; addTransaction(argList);
break;
case 2: ;// Search Transaction
break;
case 3: ;// Filter Withdraws and Deposits
break;
case 4: ;// Display transactions
break;
case 5: System.out.println("End");
break;
default: System.out.println("Invalid response");
break;
}
menu = input.nextInt();
}
}
public static void addTransaction(ArrayList<Transaction> argList) {
Scanner input = new Scanner(System.in);
int tempId;
double tempAmount;
char tempType;
String tempDescription;
System.out.println("Enter in an ID for the transaction: ");
tempId = input.nextInt();
System.out.println("Enter in the amount of money: ");
tempAmount = input.nextDouble();
System.out.println("W for withdraw, D for deposit: ");
tempType = input.next(".").charAt(0);
System.out.println("Give transaction a description: ");
tempDescription = input.next();
//add transaction
argList.add(new Transaction(tempId, tempAmount, tempType, tempDescription)
);
}
public static void searchTransactions(ArrayList<Transaction> argList){
Scanner input = new Scanner(System.in);
System.out.println("Please type in transaction ID: ");
int searchId = input.nextInt();
for(int i=0;i<argList.size();i++){
if(argList.get(i).getId()==searchId){
System.out.println("Yes");
}
}
}
}
I have problem when print output list of people by using ArrayList
package data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Manager {
List<Person> p = new ArrayList();
Scanner sc = new Scanner(System.in);
public void addStudent() {
String id;
String name;
int yob;
double point1;
double point2;
System.out.println("Input student id:");
id = sc.nextLine();
System.out.println("Input student name:");
name = sc.nextLine();
System.out.println("Input yob:");
yob = Integer.parseInt(sc.nextLine());
System.out.println("Input point 1:");
point1 = Double.parseDouble(sc.nextLine());
System.out.println("Input point 2:");
point2 = Double.parseDouble(sc.nextLine());
p.add(new Student(id,name,yob,point1,point2));
}
public void addEmployee() {
String id;
String name;
int yob;
double salaryRatio;
double salary;
System.out.println("Input employee id:");
id = sc.nextLine();
System.out.println("Input employee name:");
name = sc.nextLine();
System.out.println("Input employee yob:");
yob = Integer.parseInt(sc.nextLine());
System.out.println("Input salary ratio:");
salaryRatio = Double.parseDouble(sc.nextLine());
System.out.println("Input salary:");
salary = Double.parseDouble(sc.nextLine());
p.add(new Employee(id, name, yob,salaryRatio, salary));
}
public void addCustomer() {
String id;
String name;
int yob;
String companyName;
double bill;
System.out.println("Input customer id:");
id = sc.nextLine();
System.out.println("Input customer name:");
name = sc.nextLine();
System.out.println("Input customer yob:");
yob = Integer.parseInt(sc.nextLine());
System.out.println("Input compnay name:");
companyName = sc.nextLine();
System.out.println("Input bill:");
bill = Double.parseDouble(sc.nextLine());
p.add(new Customer(id,name,yob,companyName,bill));
}
public void addWho() {
int choice;
do {
System.out.println("1.Add Student");
System.out.println("2.Add Employee");
System.out.println("3.Add Customer");
System.out.println("4.Back to menu");
System.out.println("==============");
System.out.println("Choice:");
choice = Integer.parseInt(sc.nextLine());
switch(choice) {
case 1:
addStudent();
break;
case 2:
addEmployee();
break;
case 3:
addCustomer();
break;
case 4:
break;
}
}
while(choice != 4);
}
public void printPersonById() {
Collections.sort(p, Comparator.comparing(Person::getId));
for (int i = 0; i < p.size(); i++) {
System.out.println(p.get(i));
}
}
Student,Employee, and Customer are sub classes of class Person.When I try to print list,it has an error:
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
How can I fix it?
Try this in your code:
System.out.println("Input yob:");
yob = sc.nextInt();
System.out.println("Input point 1:"); point1 =sc.nextDouble();
System.out.println("Input point 2:"); point2 = sc.nextDouble();
p.add(new Student(id,name,yob,point1,point2));
use System.out.println(p.get(i).someMethodOrVariableOfPerson); instead of System.out.println(p.get(i));
like below
public void printPersonById() {
Collections.sort(p, Comparator.comparing(Person::getId));
for (int i = 0; i < p.size(); i++) {
System.out.println("id :"+p.get(i).id);
System.out.println("name :"+p.get(i).name);
}
}
you can not directly print Person class object in out.println() method.
out.println() allow only string value
Heres the code:
Driver:
package myschool;
import java.util.ArrayList;
import java.util.Scanner;
public class MySchool {
public static void main(String[] args) {
ArrayList<Student> listStudent = new ArrayList<>();
ArrayList<Course> listCourse = new ArrayList<>();
Student s = new Student();
Course c = new Course();
boolean continueLoop = true;
Scanner userInput = new Scanner(System.in);
Scanner courseAddInput = new Scanner(System.in);
int option;
do{
try {
System.out.println(" What would you like to do?");
System.out.println(" 1) Add a student");
System.out.println(" 2) View students");
System.out.println(" 3) Remove a student");
System.out.println(" 4) Exit");
System.out.print("--> ");
option = userInput.nextInt();
switch( option ){
case 1:
Scanner inputs = new Scanner(System.in);
String fName, lName;
int sID;
double sGPA;
System.out.print(" First Name:");
fName = inputs.nextLine();
s.setStudentFirstName( fName );
System.out.print(" Last Name:");
lName = inputs.nextLine();
s.setStudentLastName( lName );
System.out.print(" ID Number:");
sID = inputs.nextInt();
s.setStudentID( sID );
System.out.print(" GPA:");
sGPA = inputs.nextDouble();
s.setStudentGPA( sGPA );
String cName, instructor, cBeginTime, cEndTime, cDay;
int i = 0, cID, cCred;
boolean continueAdd = true;
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
String addCourse;
do{
System.out.println("Would you like to add a course? Y/N");
addCourse = input2.nextLine();
if( "N".equals(addCourse)|| "n".equals(addCourse))
continueAdd = false;
if(continueAdd){
System.out.print(" CourseName:");
cName = input.nextLine();
c.setCourseName( cName );
System.out.print(" Instructor:");
instructor = input.nextLine();
c.setCourseInstructor( instructor );
System.out.print(" CourseID:");
cID = input.nextInt();
c.setCourseID( cID );
System.out.print(" CourseCredit:");
cCred = input.nextInt();
c.setCourseCred( cCred );
/*System.out.print(" StartTime:");
cBeginTime = input.nextLine();
c.setCourseBeginTime(cBeginTime);
System.out.print(" EndTime:");
cEndTime = input2.nextLine();
c.setCourseEndTime(cEndTime);*/
listCourse.add( new Course( c.getCourseName(), c.getCourseInstructor(), c.getCourseCred(), c.getCourseBeginTime(), c.getCourseEndTime(), c.getCourseID() ));
}
}while( continueAdd );
listStudent.add( new Student( s.getStudentFirstName(),s.getStudentLastName(), s.getStudentID(), s.getStudentGPA(), listCourse));
break;
case 2:
if(!listStudent.isEmpty()){
for(Student l:listStudent) {
System.out.println(l);
for(Course n:listCourse) {
System.out.println(n);
}
System.out.println();
}
}else
System.out.println("There are no students to view\n");
break;
case 3:
Scanner removeChoice = new Scanner(System.in);
try {
if(!listStudent.isEmpty()){
int j = 0;
System.out.println("Which student do you want to remove?");
for(Student l:listStudent) {
System.out.print(j+1 + ")");
System.out.println(l);
j++;
}
int remove = removeChoice.nextInt();
listStudent.remove( remove - 1 );
System.out.println("Student has been removed\n");
}else
System.out.println("There are no students to remove\n");
} catch (Exception e) {
System.out.println("There are no students to remove\n");
}
break;
case 4:
continueLoop = false;
break;
}
} catch (Exception e) {
System.out.println("That is not a valid option!!!");
continueLoop = false;
}
}while( continueLoop );
}
}
///////////Student Class///////////
package myschool;
import java.util.ArrayList;
public class Student {
String studentFirstName, studentLastName;
int studentID;
double studentGPA;
ArrayList<Course> listCourse = new ArrayList<>();
Student(){}
public Student(String studentFirstName, String studentLastName, int studentID, double studentGPA, ArrayList courseList) {
this.studentFirstName = studentFirstName;
this.studentLastName = studentLastName;
this.studentID = studentID;
this.studentGPA = studentGPA;
this.listCourse = courseList;
}
public void setListCourse(ArrayList<Course> listCourse) {
this.listCourse = listCourse;
}
public ArrayList<Course> getListCourse() {
return listCourse;
}
public void setStudentFirstName(String studentFirstName) {
this.studentFirstName = studentFirstName;
}
public String getStudentFirstName() {
return studentFirstName;
}
public void setStudentLastName(String studentLastName) {
this.studentLastName = studentLastName;
}
public String getStudentLastName() {
return studentLastName;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public int getStudentID() {
return studentID;
}
public void setStudentGPA(double studentGPA) {
this.studentGPA = studentGPA;
}
public double getStudentGPA() {
return studentGPA;
}
#Override
public String toString() {
return ("FirstName:" + this.getStudentFirstName() +
" LastName:" + this.getStudentLastName() +
" ID:" + this.getStudentID() +
" GPA:" + this.getStudentGPA());
}
}
///////////Course Class//////////////
package myschool;
public class Course {
private String courseName, courseInstructor, courseBeginTime, courseEndTime;
int courseID, courseCred;
Course(){}
public Course(String courseName, String courseInstructor, int courseCred, String courseBeginTime, String courseEndTime, int courseID) {
this.courseName = courseName;
this.courseInstructor = courseInstructor;
this.courseCred = courseCred;
this.courseBeginTime = courseBeginTime;
this.courseEndTime = courseEndTime;
this.courseID = courseID;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseName() {
return courseName;
}
public void setCourseInstructor(String courseInstructor) {
this.courseInstructor = courseInstructor;
}
public String getCourseInstructor() {
return courseInstructor;
}
public void setCourseCred( int courseCred) {
this.courseCred = courseCred;
}
public int getCourseCred() {
return courseCred;
}
public String getCourseBeginTime() {
return courseBeginTime;
}
public void setCourseBeginTime(String courseBeginTime) {
this.courseBeginTime = courseBeginTime;
}
public void setCourseEndTime(String courseEndTime) {
this.courseEndTime = courseEndTime;
}
public String getCourseEndTime() {
return courseEndTime;
}
public void setCourseID(int courseID) {
this.courseID = courseID;
}
public int getCourseID() {
return courseID;
}
#Override
public String toString() {
return( "Course Name:" + this.getCourseName()
+ " Course ID:" + this.getCourseBeginTime()
+ " Instructor:" + this.getCourseInstructor()
+ " Credit:" + this.getCourseCred()
+ " Begin Time:" + this.getCourseBeginTime()
+ " End Time:" + this.getCourseEndTime());
}
}
This is what happens at runtime
What would you like to do?
1) Add a student
2) View students
3) Remove a student
4) Exit
--> 1
First Name:Mike
Last Name:Smith
ID Number:2345
GPA:4
Would you like to add a course? Y/N
y
CourseName:MAth
Instructor:get
CourseID:123
CourseCredit:3
Would you like to add a course? Y/N
n
What would you like to do?
1) Add a student
2) View students
3) Remove a student
4) Exit
--> 1
First Name:Sarah
Last Name:Smoith
ID Number:42342
GPA:3
Would you like to add a course? Y/N
y
CourseName:Science
Instructor:tra;l
CourseID:345
CourseCredit:4
Would you like to add a course? Y/N
n
What would you like to do?
1) Add a student
2) View students
3) Remove a student
4) Exit
--> 2
FirstName:Mike LastName:Smith ID:2345 GPA:4.0
Course Name:MAth Course ID:null Instructor:get Credit:3 Begin Time:null End Time:null
Course Name:Science Course ID:null Instructor:tra;l Credit:4 Begin Time:null End Time:null
FirstName:Sarah LastName:Smoith ID:42342 GPA:3.0
Course Name:MAth Course ID:null Instructor:get Credit:3 Begin Time:null End Time:null
Course Name:Science Course ID:null Instructor:tra;l Credit:4 Begin Time:null End Time:null
Your toString method has a problem, check comments:
public String toString() {
return( "Course Name:" + this.getCourseName()
+ " Course ID:" + this.getCourseBeginTime() // getCourseID should be used here
+ " Instructor:" + this.getCourseInstructor()
+ " Credit:" + this.getCourseCred()
+ " Begin Time:" + this.getCourseBeginTime()
+ " End Time:" + this.getCourseEndTime());
}
so update all the getters call appropriately:
Please look at the toString() method of Course class.
Course ID:" + this.getCourseBeginTime() // should be getCourseId