In my EMR Class, I am trying increment the patient counter data field after my constructor with 2 variables. I am also trying to increment the patient counter datafield after my construct with 5 variables.
In the Main the end goal is to Create 5 new patient records (which I did) and Print the number of patient records available before and after.
I am pretty sure I have to use public static long numofpatients in my EMR class, but don't know where to go from there.
Main code:
package studenthealthservices;
public class Studenthealthservices {
public static void main(String[] args) {
EMR p1 = new EMR("Colin", "10-22-74", "Strained Ankle", 99, 110, "hurt during football", "ankle brace");
System.out.println(p1.toString());
EMR p2 = new EMR("Anquan", "9-30-77", "stomach ache", 98, 120, "stress", "Tylenol");
System.out.println(p2.toString());
EMR p3 = new EMR("Buster", "3-27-1987", "Broken ankle", 99, 113, "Scott Cousins", "None");
System.out.println(p3.toString());
EMR P4 = new EMR("Frank The Tank", "4/1/89");
EMR p5 = new EMR("Merton Hanks", "03-12-1968");
}
}
EMR class code:
package studenthealthservices;
public class EMR {
private String name;
private String dob;
private String rfv;
private double bodyt;
private double hr;
private String diag;
private String pmeds;
public void setName(String name) {
this.name = name;
}
public EMR(String name, String dob) {
this.name = name;
this.dob = dob;
}
public String getName() {
return name;
}
public EMR(String name, String dob, String rfv, double bodyt, double hr, String diag, String pmeds) {
this.name = name;
this.dob = dob;
this.rfv = rfv;
this.bodyt = bodyt;
this.hr = hr;
this.diag = diag;
this.pmeds = pmeds;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getRfv() {
return rfv;
}
public void setRfv(String rfv) {
this.rfv = rfv;
}
public double getBodyt() {
return bodyt;
}
public void setBodyt(double bodyt) {
this.bodyt = bodyt;
}
public double getHr() {
return hr;
}
public void setHr(double hr) {
this.hr = hr;
}
public String getDiag() {
return diag;
}
public void setDiag(String diag) {
this.diag = diag;
}
public String getPmeds() {
return pmeds;
}
public void setPmeds(String pmeds) {
this.pmeds = pmeds;
}
public void redFlags() {
String help = "get help!";
if (bodyt >= 97.3 && bodyt <= 99.1)
this.bodyt = bodyt;
if (hr >= 60 && hr <= 100)
this.hr = hr;
else {
System.out.printf(help);
}
}
#Override
public String toString() {
return "\nname : " + this.name
+ "\nDate of Birth: " + this.dob
+ "\nReason for visit: " + this.rfv
+ "\nBody Temperature: " + this.bodyt
+ "\nHeart Rate " + this.hr
+ "\nDiagnosis: " + this.diag
+ "\nPrescribed Meds " + this.pmeds;
}
}
I would avoid incrementing a static variable in the constructor, and instead push that responsibility into the StudentHealthServices class, like this:
public class StudentHealthServices {
private int numOfPatients;
public static void main(String[] args) {
StudentHealthServices services = new StudentHealthServices();
EMR p1 = new EMR("Colin", "10-22-74", "Strained Ankle", 99, 110, "hurt during football", "ankle brace");
services.add(p1);
EMR p2 = new EMR("Anquan", "9-30-77", "stomach ache", 98, 120, "stress", "Tylenol");
services.add(p2);
EMR p3 = new EMR("Buster", "3-27-1987", "Broken ankle", 99, 113, "Scott Cousins", "None");
services.add(p3);
EMR p4 = new EMR("Frank The Tank", "4/1/89");
services.add(p4);
EMR p5 = new EMR("Merton Hanks", "03-12-1968");
services.add(p5);
}
private void add(EMR emr) {
// maybe you want to store the patients in a list here?
System.out.println(emr);
System.out.println("there are now " + ++numOfPatients + " patients");
}
}
see here (simplified your class):
package studenthealthservices;
public class EMR
{
private static /*volatile*/ long numofpatients; // when using different threads, volatile is needed.
private String name;
private String dob;
public EMR(String name, String dob) {
numofpatients++; //increment here
this.name = name;
this.dob = dob;
}
public EMR(String name, String dob, String rfv, double bodyt, double hr, String diag, String pmeds) {
numofpatients++; //increment here
this.name = name;
this.dob = dob;
//simplified
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
#Override
public String toString() {
return "simplified";
}
public static long getInstanceCount()
{
return numofpatients;
}
}
Related
getting this error which I don't understand.
I have made constructors to the best of my knowledge.
Error:(12, 46) java: constructor FitnessEmployees in class com.company.FitnessEmployees cannot be applied to given types;
required: java.lang.String,java.lang.String,int,double,java.lang.String,java.lang.String
found: no arguments
reason: actual and formal argument lists differ in length
My Class:
package com.company;
public class FitnessEmployees {
private String Name;
private String CPR;
private int Hours;
private double Salary;
private String Vacation;
private String EmployeeType;
public FitnessEmployees(String name, String CPR, int hours, double salary, String vacation, String employeeType) {
this.Name = name;
this.CPR = CPR;
this.Hours = hours;
this.Salary = salary;
this.Vacation = vacation;
this.EmployeeType = employeeType;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getCPR() {
return CPR;
}
public void setCPR(String CPR) {
this.CPR = CPR;
}
public int getHours() {
return Hours;
}
public void setHours(int hours) {
Hours = hours;
}
public double getSalary() {
return Salary;
}
public void setSalary(double salary) {
Salary = salary;
}
public String getVacation() {
return Vacation;
}
public void setVacation(String vacation) {
Vacation = vacation;
}
public String getEmployeeType() {
return EmployeeType;
}
public void setEmployeeType(String employeeType) {
EmployeeType = employeeType;
}
#Override
public String toString() {
return "FitnessEmployees{" +
"Name='" + Name + '\'' +
", CPR='" + CPR + '\'' +
", Hours=" + Hours +
", Salary=" + Salary +
", Vacation=" + Vacation +
", EmployeeType='" + EmployeeType + '\'' +
'}';
}
}
My Main:
package com.company;
public class FitnessMain {
public static void main(String[] args) {
int Salaryadmin = 23000;
int SalaryInstructor = 456;
FitnessEmployees FitnessEmployees1 = new FitnessEmployees();
String Name1 = "Claus";
String CPR1 = "221175-1011";
int Hours1 = 37;
double Salary1 = Salaryadmin;
String Vacation1 = "5";
String EmployeeType1 = "Administrative";
FitnessEmployees ObjectEmployee2 = new FitnessEmployees();
String Name2 = "Tove";
String CPR2 = "011080-1014";
int Hours2 = 20;
double Salary2 = (SalaryInstructor * Hours2);
String Vacation2 = " ";
String EmployeeType2 = "Instructor";
FitnessEmployees ObjectEmployee3 = new FitnessEmployees();
String Name3 = "Anna";
String CPR3 = "011080-1012";
int Hours3 = 37;
double Salary3 = Salaryadmin;
String Vacation3 = "5";
String EmployeeType3 = "Administrative";
FitnessEmployees ObjectEmployee4 = new FitnessEmployees();
String Name4 = "Henning";
String CPR4 = "011080-1014";
int Hours4 = 20;
double Salary4 = (SalaryInstructor * Hours2);
String Vacation4 = " ";
String EmployeeType4 = "Instructor";
}
}
Just add a default constructor as mentioned in the comments by #BugsForBreakfast
package com.company;
public class FitnessEmployees {
private String Name;
private String CPR;
private int Hours;
private double Salary;
private String Vacation;
private String EmployeeType;
public FitnessEmployees(){
}
public FitnessEmployees(String name, String CPR, int hours, double salary, String vacation, String employeeType) {
this.Name = name;
this.CPR = CPR;
this.Hours = hours;
this.Salary = salary;
this.Vacation = vacation;
this.EmployeeType = employeeType;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getCPR() {
return CPR;
}
public void setCPR(String CPR) {
this.CPR = CPR;
}
public int getHours() {
return Hours;
}
public void setHours(int hours) {
Hours = hours;
}
public double getSalary() {
return Salary;
}
public void setSalary(double salary) {
Salary = salary;
}
public String getVacation() {
return Vacation;
}
public void setVacation(String vacation) {
Vacation = vacation;
}
public String getEmployeeType() {
return EmployeeType;
}
public void setEmployeeType(String employeeType) {
EmployeeType = employeeType;
}
#Override
public String toString() {
return "FitnessEmployees{" +
"Name='" + Name + '\'' +
", CPR='" + CPR + '\'' +
", Hours=" + Hours +
", Salary=" + Salary +
", Vacation=" + Vacation +
", EmployeeType='" + EmployeeType + '\'' +
'}';
}
}
FitnessEmployees ObjectEmployee1 = new FitnessEmployees();
FitnessEmployees ObjectEmployee2 = new FitnessEmployees();
FitnessEmployees ObjectEmployee3 = new FitnessEmployees();
FitnessEmployees ObjectEmployee4 = new FitnessEmployees();
These objects call the default constructor of your FitnessEmployees class. All you need to do is add default constructor :
public FitnessEmployees {
}
The problem I'm having is with the superclass. I can't change the new Property(...) to another class called House. The only difference is the information in them, but once I change it I am getting an error stating that the constructor doesn't match but I thought that the super() should pass the information on.
This is how my Array is set up:
public static void main(String[] args) {
Property [] Properties = new Property[25];
Properties[0] = new Property (1001, 200, "58 Lackagh Park", "Dungiven", "BT47 4ND", new MyDate(15,05,2018), 5000.00, 800.50, 40.50);
Properties[1] = new Property (1002, 500, "24 Jop Lane", "Kelflar", "VT57 5LP", new MyDate(10,06,2018), 12000.00, 1000.00, 60.00);
Properties[2] = new Property (1003, 100, "Lot B", "Bandlebop", "LU49 3JU", new MyDate(01,07,2018), 450.00, 800.00, 50.50);
Properties[3] = new Property (1004, 200, "12 Back Lane", "Galafray", "GA1 1Fy", new MyDate(05,12,2018), 50000.00, 1000.00, 80.00);
Properties[4] = new Property (1005, 200, "43 Pop Street", "Jundar", "BY78 1JH", new MyDate(11,12,2018), 2500.00, 600.50, 32.90);
Properties[5] = new Property (1006, 200, "123 Fake Street", "Dunlem", "BL09 4PL", new MyDate(21,03,2018), 65000.00, 700.50, 56.50);
Properties[6] = new Property (1007, 200, "09012 Bakers Field", "Bristol", "LO87 D0N", new MyDate(15,05,2018), 5000.00, 200.50, 40.50);
Properties[7] = new Property (1008, 200, "Unit B high Street", "LA", "LA58 7NL", new MyDate(18,11,2018), 20000.00, 1000.00, 90.00);
Properties[8] = new Property (1009, 200, "1 Flabbergast Park", "Nubb", "HJ98 7NH", new MyDate(10,03,2018), 4958.00, 900.20, 20.50);
Properties[9] = new Property (1010, 200, "29 Bakers Field", "London Town", "HU84 2JO", new MyDate(02,05,2018), 1234.00, 800.00, 40.50);
Properties[10] = new Property (1011, 200, "20 Peliper Lane", "Dungiven", "BT47 4FE", new MyDate(15,05,2018), 4321.00, 900.00, 20.00);
Here is the Property class:
public class Property {
private int PropertyNumber, SquareFoot;
private String Address,Town,PostCode;
private MyDate DateListed;
public double Price;
private double Rates;
private double PricePerSquareFoot;
public Property() {
PropertyNumber = 0;
SquareFoot = 0;
Address = "";
Town = "";
PostCode = "";
DateListed = new MyDate();
Price = 0.0;
Rates = 0.0;
PricePerSquareFoot = 0.0;
}
public Property(int PropertyNumber, int SquareFoot, String Address, String Town, String PostCode, MyDate DateListed, double Price, double rates, double PricePerSquareFoot)
{
super();
this.PropertyNumber = PropertyNumber;
this.SquareFoot = SquareFoot;
this.Address = Address;
this.Town = Town;
this.PostCode = PostCode;
this.DateListed = DateListed;
this.Price = Price;
this.Rates = Rates;
this.PricePerSquareFoot = PricePerSquareFoot;
}
//--------Getters and setters----------------//
public void setPropertyNumber(int PropertyNumber)
{
this.PropertyNumber = PropertyNumber;
}
public int getPropertyNumber()
{
return PropertyNumber;
}
public void setSquareFoot(int SquareFoot)
{
this.SquareFoot = SquareFoot;
}
public int getSquareFoot()
{
return SquareFoot;
}
public void setAddress(String Address)
{
this.Address = Address;
}
public String getAddress()
{
return Address;
}
public void setTown( String Town)
{
this.Town = Town;
}
public String getTown()
{
return Town;
}
public void setPostcode( String PostCode)
{
this.PostCode = PostCode;
}
public String GetPostcode()
{
return PostCode;
}
public void setDateListed(MyDate DateListed)
{
this.DateListed = DateListed;
}
public MyDate getDateListed()
{
return DateListed;
}
public void setPrice(double Price)
{
this.Price = Price;
}
public double getPrice()
{
return Price;
}
public void setRates(double Rates)
{
this.Rates = Rates;
}
public double getRates()
{
return Rates;
}
public void setPricePerSquareFoot(double PricePerSqaureFoot)
{
this.PricePerSquareFoot = PricePerSquareFoot;
}
public double getPricePerSquareFoot()
{
return PricePerSquareFoot;
}
And this is what I am trying to put into the array along with the property information such as Property number price etc:
public class House extends Residential {
private int Garden;
private int Garage;
public House() {
super();
Garage = 0;
Garden = 0;
}
public House(int Garage, int garden) {
super();
this.Garage = 0;
this.Garden = 0;
}
public void setGarage(int Garage)
{
this.Garage = Garage;
}
public int getGarage()
{
return Garage;
}
public void setGarden(int Garden)
{
this.Garden = Garden;
}
public int getGarden()
{
return Garden;
}
.......
This is the residential class
public class Residential extends Property
{
private int NumberOfBedrooms;
private int NumberOfBathrooms;
public Residential()
{
super();
NumberOfBedrooms = 0;
NumberOfBathrooms = 0;
}
public void setNumberOfBedrooms(int NumberOfBedrooms)
{
this.NumberOfBedrooms = NumberOfBedrooms;
}
public int getNumberOfBedrooms()
{
return NumberOfBedrooms;
}
public void setNumberOfBathrooms(int NumberOfBathrooms)
{
this.NumberOfBathrooms = NumberOfBathrooms;
}
public int getNumberOfBathrooms()
{
return NumberOfBathrooms;
}
public Residential(int NumberOfBedrooms, int NumberOfBathrooms)
{
super();
NumberOfBedrooms = 0;
NumberOfBathrooms = 0;
}
I need to display an ArrayList with a list of college degree/s that an individual may have.
In another ArrayList, I need display the list of individual/s that meet the requirement of college degree that a company is asking.
Tried a system.out inside the loops and it's not even accessing the for loops.
*The 2 methods with the for loops are at the end of this class
import java.util.ArrayList;
public class RecruitingCompany {
private String companyName;
private int phoneNumber;
private String address;
private ArrayList<CollegeDegree> collegeDegreeList = new ArrayList<>();
private ArrayList<Candidate> candidateList = new ArrayList<>();
private Candidate candidate;
private Requirement academicDegree;
public RecruitingCompany(){
/*main constructor*/
}
public RecruitingCompany(String companyName, int phoneNumber, String address){
this.companyName = companyName;
this.phoneNumber = phoneNumber;
this.address = address;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public int getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public ArrayList<CollegeDegree> getCollegeDegreeList() {
return collegeDegreeList;
}
public void setCollegeDegreeList(ArrayList<CollegeDegree> collegeDegreeList) {
this.collegeDegreeList = collegeDegreeList;
}
public ArrayList<Candidate> getCandidateList() {
return candidateList;
}
public void setCandidateList(ArrayList<Candidate> candidateList) {
this.candidateList = candidateList;
}
public Candidate getCandidate() {
return candidate;
}
public void setCandidate(Candidate candidate) {
this.candidate = candidate;
}
public Requirement getAcademicDegree() {
return academicDegree;
}
public void setAcademicDegree(Requirement academicDegree) {
this.academicDegree = academicDegree;
}
public String showCandidateCollegeDegrees(){
String degree = "Candidato: " + candidate.getName() + "\n";
for (CollegeDegree cd: this.collegeDegreeList){
degree += cd.toString();
}
return degree;
}
public String selectByCollegeDegree(){
String person = "Título: " + academicDegree.getDegree() + "\n";
for (Candidate c: this.candidateList){
person += c.toString();
}
return person;
}
}
The Tester class
public class Tester {
public static void main(String[] args) {
AvailableJob availableJob = new AvailableJob(2005, 2011, "Contador", 444464444, "Metalco", "del poste de luz, 50m oeste", 550.000);
Candidate candidate = new Candidate("Asdrubal", 888888888, "Asdrubal#yahoo.com", "Bachillerato");
CollegeDegree collegeDegree = new CollegeDegree("Bachillerato Administracion", 2003, "Universidad Aguila Calva");
RecruitingCompany recruitingCo = new RecruitingCompany();
Requirement requirement = new Requirement("Bachillerato", 4);
recruitingCo.setCandidate(candidate);
recruitingCo.setAcademicDegree(requirement);
availableJob.setRequirement(requirement);
System.out.println(recruitingCo.showCandidateCollegeDegrees());
System.out.println();
System.out.println(recruitingCo.selectByCollegeDegree());
System.out.println();
System.out.println(availableJob.showRequirement());
//System.out.print(recruitingCo.getCandidateList());
}
}
You are not adding the new candidate or degree to the arraylists. Change the following methods it will work,
public void setAcademicDegree(Requirement academicDegree) {
this.academicDegree = academicDegree;
collegeDegreeList.add(academicDegree);
}
and
public void setCandidate(Candidate candidate) {
this.candidate = candidate;
candidateList.add(candidate);
}
Now when you set a new Candidate object or CollegeDegree object it will be automatically added to the lists.
In your code, you need to do some changes:
setCollegeDegreeList(), setCandidateList() and setCandidate() methods
should be changed to addCollegDegree() and addCandidate()
recruitingCo.setCandidate(candidate) should be replaced to
recruitingCo.addCandidate(candidate);
Need to add recruitingCo.addCollegeDegree(collegeDegree); in main();
And you can get following code:
import java.util.ArrayList;
public class RecruitingCompany {
private String companyName;
private int phoneNumber;
private String address;
private ArrayList<CollegeDegree> collegeDegreeList = new ArrayList<>();
private ArrayList<Candidate> candidateList = new ArrayList<>();
private Requirement academicDegree;
public RecruitingCompany(){
/*main constructor*/
}
public RecruitingCompany(String companyName, int phoneNumber, String address){
this.companyName = companyName;
this.phoneNumber = phoneNumber;
this.address = address;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public int getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public ArrayList<CollegeDegree> getCollegeDegreeList() {
return collegeDegreeList;
}
public ArrayList<Candidate> getCandidateList() {
return candidateList;
}
public void addCollegeDegree(CollegeDegree collegeDegree) {
this.collegeDegreeList.add(collegeDegree);
}
public void addCandidate(Candidate candidate) {
this.candidateList.add(candidate);
}
public Requirement getAcademicDegree() {
return academicDegree;
}
public void setAcademicDegree(Requirement academicDegree) {
this.academicDegree = academicDegree;
}
public String showCandidateCollegeDegrees(){
String degree = "Candidato: " + candidate.getName() + "\n";
for (CollegeDegree cd: this.collegeDegreeList){
degree += cd.toString();
}
return degree;
}
public String selectByCollegeDegree(){
String person = "Título: " + academicDegree.getDegree() + "\n";
for (Candidate c: this.candidateList){
person += c.toString();
}
return person;
}
}
Tester:
public class Tester {
public static void main(String[] args) {
AvailableJob availableJob = new AvailableJob(2005, 2011, "Contador", 444464444, "Metalco", "del poste de luz, 50m oeste", 550.000);
Candidate candidate = new Candidate("Asdrubal", 888888888, "Asdrubal#yahoo.com", "Bachillerato");
CollegeDegree collegeDegree = new CollegeDegree("Bachillerato Administracion", 2003, "Universidad Aguila Calva");
RecruitingCompany recruitingCo = new RecruitingCompany();
Requirement requirement = new Requirement("Bachillerato", 4);
recruitingCo.addCandidate(candidate);
recruitingCo.addСollegeDegree(collegeDegree);
recruitingCo.setAcademicDegree(requirement);
availableJob.setRequirement(requirement);
System.out.println(recruitingCo.showCandidateCollegeDegrees());
System.out.println();
System.out.println(recruitingCo.selectByCollegeDegree());
System.out.println();
System.out.println(availableJob.showRequirement());
//System.out.print(recruitingCo.getCandidateList());
}
}
Can someone help solving this. I want to print all the object once please
public class Student {
private static String firstName;
private static String lastName;
private static int studentId;
private static String major;
private static double balance;
public Student (String fName, String lName,int id,String mjr,double blce) {
firstName = new String(fName);
lastName = new String(lName);
studentId = id;
major = new String(mjr);
balance = blce;
}
public String toString () {
return firstName + "\t" + lastName + "\t" + studentId + "\t" + major + "\t$" + balance;
}
public boolean equals (Object obj) {
if (obj instanceof Student) {
Student collegeStud = (Student) obj;
return (this.firstName.equals(collegeStud.firstName));
} else
return false;
}
public static String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public static String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public static int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public static String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public static double getBalance() {
return balance;
}
/*
* .commmm
*/
public void setBalance(double balance) {
this.balance = balance;
}
public static void main (String[] args) {
Student Mike = new Student ("Mike","Versace", 99, "CS",0.00);
Student John = new Student ("John","Sling" ,97, "Maths", 20.00);
Student Bob = new Student ("Bob","Tomson" ,57, "Physic",5.00);
System.out.println (Mike.toString() + "\n" + John.toString());
if (Mike.equals(John))
System.out.println ("Mike is John");
else
System.out.println ("Mike is NOT John");
}
}
import java.io.ObjectInputStream.GetField;
public class StudentList {
private int numberOfStudents=0;
private Student[] studentListArray;
//private int studentCount = 0;
StudentList () {
numberOfStudents=0;
studentListArray = new Student[100];
}
public void createStudent(String firstName, String lastName,int studentId, String major, double balance){
Student collegeStud = new Student(firstName, lastName, studentId, major, balance);
addStudent(collegeStud);
numberOfStudents++;
}
public void addStudent (Student collegeStud) {
studentListArray[numberOfStudents++]=new Student(collegeStud.getFirstName(), collegeStud.getLastName(),
collegeStud.getStudentId(), collegeStud.getMajor(),collegeStud.getBalance());
}
public String toString() {
String result = "";
for (int i=0; i<numberOfStudents; i++) {
result += studentListArray[i].toString() + "\n";
}
return result;
}
public Student[] getList() {
return studentListArray;
}
public int listSize() {
return numberOfStudents;
}
public Student searchForStudent (String firstName){
int index = 0;
while (index < numberOfStudents) {
if (studentListArray[index].equals(new Student(Student.getFirstName(),Student.getLastName(),Student.getStudentId(),Student.getMajor(),Student.getBalance()))) {
return studentListArray[index];
}
index++;
}
return null;
}
public static void main(String args[]) {
StudentList theList = new StudentList();
theList.addStudent (new Student ("John","Sling" ,97, "Maths", 20.00));
theList.addStudent (new Student ("Mike","Versace", 99, "CS",0.00));
theList.addStudent (new Student ("Bob","Tomson" ,57, "Physic",5.00));
//theList.createStudent(new Student(Student.getFirstName(),Student.getLastName(),Student.getStudentId(),Student.getMajor(),Student.getBalance()));
//theList.searchForStudent(new String());
System.out.println (theList.toString());
}
}
The problem is that you marked your fields as static. Remove it and the method will work as expected.
public class Student {
//non-static fields
private String firstName;
private String lastName;
private int studentId;
private String major;
private double balance;
//similar for getters, setters and toString method
}
Static members are shared amongst all objects in a class rather than being one per object. Hence each new object you create is overwriting the data of the previous one.
More info:
What does the 'static' keyword do in a class?
What would be the simplest method to print this array broken down into each mobile phone as a product number, name department etc, and then re print the same information sorted by product name. I have tried a couple different methods and am already passed the turn in date for the assignment but still need to figure it out for upcoming assignment this weekend. When I try to implement the comparator on MobilePhone class it forces me to make it abstract or use #override but I can't figure out where or what to override to make it work because the abstract class causes a multitude of other problems.
package InventoryPro2;
import java.util.*;
class MobilePhone {
private double productNumber; // Variables
private String name;
private String department;
private double unitsInStock;
private double unitPrice;
public MobilePhone() {
this(0.0, "", "", 0.0, 0.0);
}
public MobilePhone(double productNumber, String name, String department,
double unitsInStock, double unitPrice) { //assign variables
this.productNumber = productNumber;
this.name = name;
this.department = department;
this.unitsInStock = unitsInStock;
this.unitPrice = unitPrice;
}
public double getproductNumber() { // retrieve values
return productNumber;
}
public String getname() {
return name;
}
public String getdepartment() {
return department;
}
public double getunitPrice() {
return unitPrice;
}
public double getunitsInStock() {
return unitsInStock;
}
public void setproductNumber(double productNumber) {
this.productNumber = productNumber;
}
public void setname(String name) {
this.name = name;
}
public void setdepartment(String department) {
this.department = department;
}
public void setunitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public void setunitsInStock(double unitsInStock) {
this.unitsInStock = unitsInStock;
}
public double gettotalInv() {
return getunitPrice() * getunitsInStock();
}
}
public class InventoryPro2 {
MobilePhone mobilephone = new MobilePhone();
public static void main(String args[]) {
System.out.println("Mobile Phone Inventory Program");
System.out.println();//skips a line
MobilePhone[] phones = new MobilePhone[5];
phones[0] = new MobilePhone();
phones[0].setproductNumber(1);
phones[0].setname("Motorola");
phones[0].setdepartment("Electronics");
phones[0].setunitPrice(150.10);
phones[0].setunitsInStock(98);
phones[1] = new MobilePhone();
phones[1].setproductNumber(2);
phones[1].setname("Samsung");
phones[1].setdepartment("Electronics");
phones[1].setunitPrice(199.99);
phones[1].setunitsInStock(650);
phones[2] = new MobilePhone();
phones[2].setproductNumber(3);
phones[2].setname("Nokia");
phones[2].setdepartment("Electronics");
phones[2].setunitPrice(200.25);
phones[2].setunitsInStock(125);
phones[3] = new MobilePhone();
phones[3].setproductNumber(4);
phones[3].setname("LG");
phones[3].setdepartment("Electronics");
phones[3].setunitPrice(100.05);
phones[3].setunitsInStock(200);
phones[4] = new MobilePhone();
phones[4].setproductNumber(5);
phones[4].setname("IPhone");
phones[4].setdepartment("Electronics");
phones[4].setunitPrice(299.99);
phones[4].setunitsInStock(150);
System.out.println("Order of inventory before sorting:");
System.out.println();
}
}
(Also, what is the best way to take just one piece of information out of each part of the array such as the totalInv and total all of those numbers to print?) Do I have unnecessary code here or have I done everything right thus far? I have to say that learning this coding language in an online format has not been a very enjoyable experience thus far..
Here is how to sort by name
import java.util.Arrays;
import java.util.Comparator;
public class AppInventoryPro2 {
public static void main(String... args) {
System.out.println("Mobile Phone Inventory Program");
System.out.println();// skips a line
MobilePhone[] phones = new MobilePhone[5];
phones[0] = new MobilePhone();
phones[0].setproductNumber(1);
phones[0].setname("Motorola");
phones[0].setdepartment("Electronics");
phones[0].setunitPrice(150.10);
phones[0].setunitsInStock(98);
phones[1] = new MobilePhone();
phones[1].setproductNumber(2);
phones[1].setname("Samsung");
phones[1].setdepartment("Electronics");
phones[1].setunitPrice(199.99);
phones[1].setunitsInStock(650);
phones[2] = new MobilePhone();
phones[2].setproductNumber(3);
phones[2].setname("Nokia");
phones[2].setdepartment("Electronics");
phones[2].setunitPrice(200.25);
phones[2].setunitsInStock(125);
phones[3] = new MobilePhone();
phones[3].setproductNumber(4);
phones[3].setname("LG");
phones[3].setdepartment("Electronics");
phones[3].setunitPrice(100.05);
phones[3].setunitsInStock(200);
phones[4] = new MobilePhone();
phones[4].setproductNumber(5);
phones[4].setname("IPhone");
phones[4].setdepartment("Electronics");
phones[4].setunitPrice(299.99);
phones[4].setunitsInStock(150);
System.out.println("Order of inventory before sorting:");
System.out.println(Arrays.toString(phones));
Arrays.sort(phones, new Comparator<MobilePhone>() {
#Override
public int compare(MobilePhone mp1, MobilePhone mp2) {
return mp1.getname().compareTo(mp2.getname());
}
});
System.out.println("Order of inventory after sorting by name:");
System.out.println(Arrays.toString(phones));
}
}
class MobilePhone {
private double productNumber; // Variables
private String name;
private String department;
private double unitsInStock;
private double unitPrice;
public MobilePhone() {
this(0.0, "", "", 0.0, 0.0);
}
public MobilePhone(double productNumber, String name, String department,
double unitsInStock, double unitPrice) { // assign variables
this.productNumber = productNumber;
this.name = name;
this.department = department;
this.unitsInStock = unitsInStock;
this.unitPrice = unitPrice;
}
public double getproductNumber() { // retrieve values
return productNumber;
}
public String getname() {
return name;
}
public String getdepartment() {
return department;
}
public double getunitPrice() {
return unitPrice;
}
public double getunitsInStock() {
return unitsInStock;
}
public void setproductNumber(double productNumber) {
this.productNumber = productNumber;
}
public void setname(String name) {
this.name = name;
}
public void setdepartment(String department) {
this.department = department;
}
public void setunitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public void setunitsInStock(double unitsInStock) {
this.unitsInStock = unitsInStock;
}
public double gettotalInv() {
return getunitPrice() * getunitsInStock();
}
#Override
public String toString() {
return "MobilePhone [productNumber=" + productNumber + ", name=" + name
+ ", department=" + department + ", unitsInStock="
+ unitsInStock + ", unitPrice=" + unitPrice + "]";
}
}
1 - To print content of MobilePhone class: Override default toString method like this:
#Override
public String toString() {
return "MobilePhone [productNumber=" + productNumber +
", name=" + name + ']'; // add more info if needed
}
2 - To allow sorting by name: Have MobilePhone class implement Comparable interface like
this:
class MobilePhone implements Comparable {
...
#Override
public int compareTo(Object o) {
MobilePhone m = (MobilePhone) o;
return (this.name.compareTo(o.name));
}
}
EDIT: To print your array of MobilePhone object you can do:
System.out.printf("Phones: %s%n", Arrays.toString(phones));