Accessing a pojo object using arraylist [closed] - java

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 5 years ago.
Improve this question
i am trying to solve the following project where I need to choose out the best customer's based on their purchases. All the necessary details like name,and amount purchased is included in my POJO object(Customer).
I made an ArrayList of Cusomers and trying to access getName()/getAmount method in a for Loop.
import java.util.ArrayList;
import java.util.Scanner;
public class Store {
ArrayList<Customer> Customers = new ArrayList<>();
Customer[] csa = new Customer[1000];
public void addSale(String customerName, double amount) {
String cn = customerName;
double am = amount;
Customer cs = new Customer(cn, am);
Customers.add(cs);
}
public String nameOfBestCustomer() {
String name = null;
//Customer csa=new Customer();
double largest = csa[0].getAmount();
// gives me:java.lang.NullPointerException
for (int i = 1; i < Customers.size(); i++) {
if (largest < csa[i].getAmount()) {
largest = csa[i].getAmount();
name = csa[i].getName();
}
}
// return name+""+largest;
return name;
}
public static void main(String[] args) {
Store s = new Store();
double am;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter Customer name:");
String cn = scanner.next();
if (cn.equals("done")) {
am = 0;
scanner.close();
break;
} else {
System.out.println("Enter Amount:");
am = scanner.nextDouble();
s.addSale(cn, am);
}
}
System.out.println("Best customer " + s.nameOfBestCustomer());
}
}
Is it okay to use Customer[] csa = new Customer[1000]? when I already have an Arraylist for Customers?
Can anybody tell me how to access customer-methods using Array or ArrayList? And also why is largest = csa[0].getAmount() giving me a NullPointerException?

there are many mistakes in your source code like
Customer class constructor was wrong assigned
You were not stored any objects on array so objects are null.
Refer below code i have updated source code, just copy paste it will work...
package demo;
import java.util.ArrayList;
import java.util.Scanner;
public class Store {
ArrayList<Customer> Customers = new ArrayList<Customer>();
Customer[] csa = new Customer[1000];
public void addSale(String customerName, double amount) {
String cn = customerName;
double am = amount;
Customer cs = new Customer(cn, am);
Customers.add(cs);
}
public String nameOfBestCustomer() {
String name = null;
//Customer csa=new Customer();
double largest = Customers.get(0).getAmount();
// gives me:java.lang.NullPointerException
for (int i = 1; i < Customers.size(); i++) {
if (largest < Customers.get(i).getAmount()) {
largest = Customers.get(i).getAmount();
name = Customers.get(i).getName();
}
}
// return name+""+largest;
return name;
}
public static void main(String[] args) {
Store s = new Store();
double am;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter Customer name:");
String cn = scanner.next();
if (cn.equals("done")) {
am = 0;
scanner.close();
break;
} else {
System.out.println("Enter Amount:");
am = scanner.nextDouble();
s.addSale(cn, am);
}
}
System.out.println("Best customer " + s.nameOfBestCustomer());
}
}
class Customer {
private String name;
private double amount;
public Customer(){
}
//#SuppressWarnings("null")
public Customer(String name,double price) {
this.name=name;
this.amount= price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
Output :
Enter Customer name:
ramesh
Enter Amount:
100
Enter Customer name:
pramod
Enter Amount:
200
Enter Customer name:
done
Best customer pramod

Related

What is the relationship between two classes they are tied to a Third class?

Getting stuck with my Travail System Project, Confusing a little bit about understanding that if there Classes called Bookable, Hotel and BookingSystem.
Hotel class is implements Bookable. Furthermore, BookingSystem Class is composition from Bookable, So, I need to create method at BookingSystem class which called addHotel.
what I must do about it to make a relationship between Hotel Class and BookingSystem Class.
Thanks In Advance.
Israa
Hotal Class:
public class Hotel implements Bookable {
private String name, location;
private int noOfRooms;
private double roomPrice;
private Date bookingDate;
private ArrayList<Integer> bookedRooms = new ArrayList<Integer>();
private ArrayList<Integer> numberOfrooms = new ArrayList<Integer>();
public Hotel() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getNoOfRooms() {
return noOfRooms;
}
public void setNoOfRooms(int noOfRooms) {
this.noOfRooms = noOfRooms;
}
public double getRoomPrice() {
return roomPrice;
}
public void setRoomPrice(double roomPrice) {
this.roomPrice = roomPrice;
}
public Date getBookingDate() {
return bookingDate;
}
public void setBookingDate(Date bookingDate) {
this.bookingDate = bookingDate;
}
public ArrayList<Integer> getBookedRooms() {
return bookedRooms;
}
public void setBookedRooms(ArrayList<Integer> bookedRooms) {
this.bookedRooms = bookedRooms;
}
public String Book() {
if ( numberOfrooms.size() != (bookedRooms.size())) {
for (int i = 0; i < bookedRooms.size(); i++) {
int oldVal = bookedRooms.get(i);
int newVal = oldVal + 1;
bookedRooms.add(bookedRooms.set(i, newVal));
}
}
return null;
}
}
Bookable class:
public interface Bookable {
public String Book();
}
BookingSytsem Class:
public class BookingSystem {
private ArrayList<Customer> customer = new ArrayList<Customer>();
private ArrayList<Bookable> bookable = new ArrayList<Bookable>();
private ArrayList<Operation> operation = new ArrayList<Operation>();
public BookingSystem() {
}
// **
public void addCustomer(String name, int id) {
Customer customers = new Customer(id, name);
customer.add(customers);
System.out.println("new customer " + customers.getName() + " added");
}
// **
public void deleteCustomer(String name, int id) {
Customer customers = new Customer(id, name);
if (customer.contains(name)) {
customer.remove(name);
}
System.out.println("Customer " + customers.getName() + " deleted");
}
public Customer findCustomer(int id) {
for (Customer c : customer) {
if (c.getId() == id) {
return c;
}
}
return null;
}
public void addHotel() {
Hotel H1 = new Hotel();
Scanner name = new Scanner(System.in);
System.out.println("Please Enter the name of Hotel: ");
String n1 = name.nextLine();
bookable.add(H1);
System.out.println("The Hotel " + name + "added");
}
public void makeABooking(Customer c, Bookable b) {
Scanner input =new Scanner(System.in);
System.out.println("Please Enter your name: ");
String name = input.nextLine();
System.out.println("Please Enter your ID: ");
int ID = input.nextInt();
while(true) {
if(ID == -1 && ID == 0) {
System.out.println("Invalid ID. Enter again: ");
name = input.nextLine();
System.out.println("Please Enter your ID: ");
ID = input.nextInt();
}
}
}
}
(Your question is more suitable to https://softwareengineering.stackexchange.com/ - recommend asking it there...)
General speaking it wouldn't make sense to have a Hotel without a name, location or number of rooms so I'd recommend adding a constructor with minimal required information:
public Hotel (String name, String location, int rooms) {
this.name = name;
this.location = location;
this.noOfRooms = rooms;
}
bookingDate makes no sense as a single property of a hotel but rather a property of each booked room so you have a design issue - this is not addressed here.
Again, roomPrice usually varies by room so in a robust solution this would be a property of a room not a hotel - not addressed here.
Why is there a noOfRooms and a numberOfRooms list. In fact, the numberOfRooms list doesn't make sense as a list. I'd just keep the noOfRooms and get rid of numberOfRooms.
An implied property, nbrOfAvailableRooms can be derived from noOfRooms - bookedRooms.size();
I would assume your bookedRooms is a list of room numbers which are booked but that's not possible to tell from your implementation. You should focus on what you want Book to do.
The Book interface method is not documented but it looks like it should simply take an available asset (room) and consider it booked. It should return a boolean success not a String - especially not null.
I recommend writing (in pseudo code) what you want a Book implementation to do - include that in question. That is the core issue you are having.

How do I get user input from separate classes

My goal is to get an amount for an item and a description of that item. I want to get 10 inputs and their values. I don't know how to store their info without asking for one then the other. I want to be able to ask for the user to input the item then price and then store each of those values.
I was trying to create an array of objects to enter and then was suggested to use arraylists which I kind of understand but don't know how to implement in this case.
import java.util.ArrayList;
public class Invoice {
private ArrayList<Item> listOfItems;
public Invoice() {
listOfItems = new ArrayList<Item>();
}
public void addItem(Item item) {
listOfItems.add(item);
}
public double calculateNetItemCost() {
double netCost = 0;
for(Item currentItem : listOfItems) {
netCost += currentItem.getCost();
}
return netCost;
}
public double calculateTax(double taxRateAsADecimal) {
return calculateNetItemCost() * taxRateAsADecimal;
}
public double calculateGST() {
double GST = calculateNetItemCost() * 0.05;
return GST;
}
public double calculatePST() {
double PST = calculateNetItemCost() * 0.07;
return PST;
}
public double calculateTotalCost() {
double total = calculateGST() + calculatePST() + calculateNetItemCost();
return total;
}
}
public class Item {
private double amount;
private String description;
public Item(String description, double amount) {
this.amount = amount;
this.description = description;
}
public String toString() {
return description + ", $" + String.format("%.2f", amount);
}
public double getCost() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
import java.util.Scanner;
import java.util.ArrayList;
public class CustomerBuild {
public static void main(String[] args) {
ArrayList<String> description = new ArrayList<String>();
ArrayList<Double> amount = new ArrayList<Double>();
Scanner input = new Scanner(System.in);
String userInput;
Item testItem = new Item("Apples", 4.00);
System.out.println(testItem);
Invoice testInvoice = new Invoice();
testInvoice.addItem(testItem);
double subTotal = testInvoice.calculateNetItemCost();
System.out.println(subTotal);
double GST = testInvoice.calculateGST();
System.out.println("GST: " + GST);
double PST = testInvoice.calculatePST();
System.out.println("PST: " + PST);
System.out.println("Total cost: " + testInvoice.calculateTotalCost());
}
}
This code snippet is what are you looking for. Asking the user for input then initializing the object using the values
Scanner input = new Scanner(System.in);
System.out.println("Please enter description(e.g.apple,banana,orange):");
String description =input.nextLine();
System.out.println("Please enter Price(e.g.4.0,5.99):");
Double price = scanner.nextDouble();
Item testItem = new Item(description, price);
For Looping:
for(int i =1;i<10;i++) {
System.out.println("Please enter description(e.g.apple,banana,orange):");
String description =input.nextLine();
System.out.println("Please enter Price(e.g.4.0,5.99):");
Double price = scanner.nextDouble();
testInvoice.addItem(new Item(description, price));
}

Java - Adding 2 objects in an ArrayList

I'm pretty new to programming so I need help. I wanna add the SubjectGrades to the studentList ArrayList. But I think I'm doing the wrong way. What should I do for me to add the SubjectGrades to the ArrayList? Thanks
Here's my partial Main class.
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
private static Scanner in;
public static void main(String[] args) {
ArrayList<Student> studentList = new ArrayList<Student>();
//ArrayList<SubjectGrades> Grades = new ArrayList<SubjectGrades>();
in = new Scanner(System.in);
String search, inSwitch1, inSwitch2;
int inp;
do {
SubjectGrades sGrade = new SubjectGrades();
Student student = new Student();
System.out.println("--------------------------------------");
System.out.println("What do you want to do?");
System.out.println("[1]Add Student");
System.out.println("[2]Find Student");
System.out.println("[3]Exit Program");
System.out.println("--------------------------------------");
inSwitch1 = in.next();
switch (inSwitch1) {
case "1":
System.out.println("Input student's Last Name:");
student.setLastName(in.next());
System.out.println("Input student's First Name:");
student.setFirstName(in.next());
System.out.println("Input student's course:");
student.setCourse(in.next());
System.out.println("Input student's birthday(mm/dd/yyyy)");
student.setBirthday(in.next());
System.out.println("Input Math grade:");
student.subjectGrade.setMathGrade(in.nextDouble());
System.out.println("Input English grade:");
student.subjectGrade.setEnglishGrade(in.nextDouble());
System.out.println("Input Filipino grade:");
student.subjectGrade.setFilipinoGrade(in.nextDouble());
System.out.println("Input Java grade:");
student.subjectGrade.setJavaGrade(in.nextDouble());
System.out.println("Input SoftEng grade:");
student.subjectGrade.setSoftEngGrade(in.nextDouble());
studentList.add(student);
studentList.add(student.setSubjectGrade(sGrade)); //Here it is that I want to add
break;
//end case 1
Here is my Student Class.
package santiago;
public class Student {
private String lastName;
private String firstName;
private String course;
private String birthday;
SubjectGrades subjectGrade = new SubjectGrades();
public SubjectGrades getSubjectGrade() {
return subjectGrade;
}
public void setSubjectGrade(SubjectGrades subjectGrade) {
this.subjectGrade = subjectGrade;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
And my SubjectGrades class
package santiago;
public class SubjectGrades{
Double mathGrade, englishGrade, filipinoGrade, javaGrade, softEngGrade, weightedAverage;
public Double getMathGrade() {
return mathGrade;
}
public void setMathGrade(Double mathGrade) {
this.mathGrade = mathGrade;
}
public Double getEnglishGrade() {
return englishGrade;
}
public void setEnglishGrade(Double englishGrade) {
this.englishGrade = englishGrade;
}
public Double getFilipinoGrade() {
return filipinoGrade;
}
public void setFilipinoGrade(Double filipinoGrade) {
this.filipinoGrade = filipinoGrade;
}
public Double getJavaGrade() {
return javaGrade;
}
public void setJavaGrade(Double javaGrade) {
this.javaGrade = javaGrade;
}
public Double getSoftEngGrade() {
return softEngGrade;
}
public void setSoftEngGrade(Double softEngGrade) {
this.softEngGrade = softEngGrade;
}
public Double getWeightedAverage(){
weightedAverage = ((mathGrade + englishGrade + filipinoGrade + javaGrade + softEngGrade)*3) / 15;
return weightedAverage;
}
public String getScholarStatus(){
String status = "";
if(weightedAverage <= 1.5) {
status = "full-scholar";
} else if (weightedAverage <= 1.75){
status = "half-scholar" ;
} else {
status = "not a scholar";
}
return status;
}
}
Your mistake:
studentList.add(student);
studentList.add(student.setSubjectGrade(sGrade));
You are adding the student, then trying to add a void. The return value of setSubjectGrade is void, so nothing will be added:
Just do:
student.setSubjectGrade(sGrade);
studentList.add(student);
Where sGrade is an Object of type SubjectGrades, which was populated in the same way
student.subjectGrade.setSoftEngGrade(in.nextDouble()); was populated.
Use
ArrayList <SubjectGrades> list;
in student class instead SubjectGrades subjectGrade = new SubjectGrades();.
and generate getters and setters
Just remove this line:
studentList.add(student.setSubjectGrade(sGrade)); //Here it is that I want to add
The way you have done it, the student object already has the subjectGrade attribute with its values set.
You can access it with studentList.get(0).getSubjectGrade()

Creating and Returning a List of Employees in Java

Good evening all. I'm just trying to create a class with a method that allows you to create a list of users, their ages, and their departments (although I haven't coded the department part yet!). I want to be able to return the list of employees SORTED by age. I haven't coded Java in a long while, and I'm a little rusty. Most of my time has been spent in Perl and C++ lately, so any help would be greatly appreciated. Thanks, guys!
Here's my (broken) code:
import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
public class Department{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many employees?\n");
int noOfEmployees = in.nextInt();
String[][] employeeInformation;
employeeInformation = new String[noOfEmployees][3];
for (int row = 0; row < employeeInformation.length; row++){
System.out.print("Enter employee name: ");
try {
employeeInformation[0][row] = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print("Enter employee age: ");
try {
employeeInformation[1][row] = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
Arrays.sort(employeeInformation, new Comparator<String[]>() {
public int compare(String[] entry1, String[] entry2){
String name = entry1[0];
String age = entry2[0];
return name.compareTo(age);
}
});
for(int i=0;i<employeeInformation.length;i++){
System.out.println(employeeInformation[i][i]);
}
}
}
using your code
employeeInformation[0][row] = br.readLine();
employeeInformation[1][row] = br.readLine();
should be
employeeInformation[row][0] = br.readLine();
employeeInformation[row][1] = br.readLine();
and why are you comparing age with name in your sort
return name.compareTo(age); // this does not make sense.
Sorry to be rough, but try using a debugger or print statements
OK I have some thing for you. I took your class and modified it to make work and make little better. You may still want to make more error checking and other optimizations as you see fit. This is just a working sample.
Code explanation:
I have created a inner class called employee. But feel free to tear that out into a separate class. I just did for convenience sake to post a sample. I hope it helps you.I have tested and it works.
package com.vkg;
import java.util.Arrays;
import java.util.Scanner;
public class Department {
public static void main(String[] args) {
Department department = new Department();
Scanner in = new Scanner(System.in);
System.out.println("How many employees?\n");
int noOfEmployees = in.nextInt();
Employee[] employees = new Employee[noOfEmployees];
for (int i = 0; i < employees.length; i++) {
Employee employee = department.new Employee();
System.out.print("Enter employee name: ");
String employeeName = in.next();
employee.setName(employeeName);
System.out.print("Enter employee age: ");
int age = in.nextInt();
employee.setAge(age);
employees[i] = employee;
}
Arrays.sort(employees);
for (Employee employee : employees) {
System.out.println(employee.getName() + "'s age = " + employee.getAge());
}
}
public class Employee implements Comparable<Employee> {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
#Override
public int compareTo(Employee o) {
if(this == o || this.age == o.age) {
return 0;
} else if (this.age < o.age) {
return -1;
} else {
return 1;
}
}
}
}

(non-static method Change(String,String,String) cannot be referenced from a static context

I've searched, but didn't understand how to incorporate previous answers with my own code. I have an assigment to create a draft of a program for the schools "bank/scholarship"-system. Here we need to create a lot of different methods, that should be called instead of just doing the change directly.
The thing is, I keep getting the error-message in the topic along with a few others, and I don't know how to fix it. I would really appreciate if someone could explain for me how and why to change one of my methods, then I assume I will be able to rewrite all the others myself. Here is my code so far:
class Address
public class Address {
public String street, zip, post;
public Address (String street, String zip, String post) {
this.street = street;
this.zip = zip;
this.post = post;
}
// these are setters (mutators)
public void setStreet (String street) {
this.street = street; }
public void setZip (String zip) {
this.zip = zip; }
public void setPost (String post) {
this.post = post; }
// these are getters
public String getStreet () {
return this.street; }
public String getZip () {
return zip; }
public String getPost () {
return post; }
// methods
//public Address Change(Address newAddress) {
//return newAddress;
//}
// output
public String toString() {
return (street + ", " + zip + ", "+ post); }
}
class Customer
import java.text.DecimalFormat;
public class Customer {
DecimalFormat twoD = new DecimalFormat("0.00");
Address address = new Address("Vik ", "6393 ", "Tomrefjord");
public String name, campus, newCampus, newAddress;
public double loan, increase,decrease,regMaster;
public boolean finished;
public Customer (String name,Address address, String campus, double loan, boolean finished) {
this.name = name;
this.campus = campus;
this.loan = loan;
this.address = address;
this.finished = finished;
}
// these are setters
public void setName (String name) {
this.name = name; }
public void setCampus (String campus) {
this.campus = campus; }
public void setLoan (double loan) {
this.loan = loan; }
public void setFinished (boolean finished) {
this.finished = finished; }
// these are getters
public String getName () {
return name; }
public String getCampus () {
return campus; }
public double getLoan () {
return loan; }
// methods
public void RegMaster () {
this.loan = loan * 0.90;}
//public void changeAddress (Address newAddress) {
//Address.Change(newAddress); }
public double decreaseLoan (double currentBalance, double decrease) {
currentBalance = currentBalance - decrease;
return currentBalance; }
public double getNewLoan (double currentBalance, double newLoan) {
newLoan = currentBalance + newLoan;
return newLoan; }
public String getNewCampus (String newCampus) {
campus = newCampus;
return campus; }
public static boolean Ended () {
return true; }
// output
public String toString() {
return (name + "\t\n" + address + "\t\n" + campus + "\t\n" + twoD.format(loan) + "\t\n" + finished); }
}
class TestCustomer
import java.util.Scanner;
public class TestCustomer {
public static void main (String[] args) {
String student, school, answer, street, zip, post;
double balance, master;
boolean finished = false;
double done = 0; //this is for the loop
Scanner scan = new Scanner(System.in);
// a new student receives a loan
System.out.println("Write your name");
student = scan.nextLine();
System.out.println("Enter your street name and number");
street = scan.nextLine();
System.out.println("What is your zip code?");
zip = scan.nextLine();
System.out.println("What is your post area?");
post = scan.nextLine();
System.out.println("Where do you study?");
school = scan.nextLine();
System.out.println("Input your current loan");
balance = scan.nextDouble();
Address residence = new Address(street, zip, post);
Customer customer = new Customer(student, residence, school, balance, finished);
while(done < 1) { //the variable done will have the value 0 until you are finished.
// change address
System.out.println("Do you wish to change your address? (yes/no)");
answer = scan.nextLine();
if (answer.equalsIgnoreCase("yes")) { //this made it case-insensitive, and now it is not skipping anymore...
System.out.println("Write your street name and house number");
street = scan.nextLine();
System.out.println("Write your zip code");
zip = scan.nextLine();
System.out.println("Write your post area");
post = scan.nextLine();
//Address newAddress = new Address(street, zip, post);
//residence = Customer.changeAddress(newAddress);
}
// increase the loan
System.out.println("Do you want to increase your loan? (yes/no)");
answer = scan.nextLine();
if (answer.equalsIgnoreCase("yes")) {
System.out.println("How much do you need?");
double increaseLoan = scan.nextDouble();
//customer.balance = getNewLoan(customer.balance, moreLoan);
}
// decrease the loan
System.out.println("Do you want to make a downpayment on your loan?");
answer = scan.nextLine();
if (answer.equalsIgnoreCase("yes")) {
System.out.println("How much do you intend to pay?");
double downpayment = scan.nextDouble();
//customer.balance = decreaseLoan(customer.balance, downpayment);
}
// change school
System.out.println("Do you study at the same school? (yes/no");
answer = scan.nextLine();
if (answer.equalsIgnoreCase("no")) {
System.out.println("Write the name of the new school");
school = scan.nextLine(); }
//school.getNewCampus(customer.campus);
// master check
System.out.println("Have you finished your master? (yes/no)");
answer = scan.nextLine();
if (answer.equalsIgnoreCase("yes")) {
customer.finished = Customer.Ended() ; //this will set finished to true, using the method like intended
//customer.balance = Customer.RegMaster(); // dont know how to invoke it... me neither -_-
}
System.out.println("Are you done? yes/no");
answer = scan.nextLine();
if (answer.equalsIgnoreCase("yes")) {
done = 1; } // this adds 1 to the variable done, thus ending the loop.
}
// output
System.out.println("");
System.out.println("The data we have so far is:");
System.out.println(customer);
}}
Here are all three of my classes. Some of the functions are commented away, others are just not working properly (the code is also a little extra messy, I was changing back and forth to try to get it working yesterday). Any help will be appreciated! :)
Okay, from you comment then your problem comes from the (helpfully) commented out lines
//public void changeAddress (Address newAddress) {
//Address.Change(newAddress); }
This is because your Customer done not "have an" Address, you need an address instance and not a String:
private Address address;
public Customer (final Address address) {
this.address = address;
}
I have redacted other variables for clarity. Incidentally I would use a builder pattern rathen than one enormous constructor.
Now that you have an Address in your Customer:
public void changeAddress (Some parameters to change) {
address.setStuff();
}
I would recommend that you read this link about the difference between static and instance variables.

Categories

Resources