I have 3 Classes:Account,Customer and Main.the Main Class has the main method:
these Classes are the some parts of a programm.
public class Account {
private static ArrayList<Account> allAccounts=new ArrayList<>();
private Bank bank;
private int id;
private int money;
private int remainingDuration;
private int interest;
private Customer customer;
public Account(Bank bank, Customer customer,int id, int money,int duration,int interest) {
this.bank = bank;
this.customer=customer;
this.id=id;
this.money = money;
this.remainingDuration=duration;
this.interest = interest;
allAccounts.add(this);
}
public int getId() {
return id;
}
public Bank getBank() {
return bank;
}
}
public class Customer {
private static ArrayList<Customer> allCustomers=new ArrayList<>();
private String name;
private double moneyInSafe;
private ArrayList<Account> allActiveAccounts;
private int totalNumberOfAccountsCreated;
private int negativeScore;
public Customer(String name, double moneyInSafe) {
this.name=name;
this.moneyInSafe=moneyInSafe;
totalNumberOfAccountsCreated=0;
allCustomers.add(this);
}
public static Customer getCustomerByName(String name){
for (Customer customer:allCustomers){
if(customer.getName().equals(name)){
return customer;
}
}
return null;
}
public String getName() {
return name;
}
public void createNewAccount(Bank bank,int money,int duration,int interest){
totalNumberOfAccountsCreated++;
allActiveAccounts.add(new Account(bank,this, totalNumberOfAccountsCreated, money, duration, interest));
}
public double getMoneyInSafe() {
return moneyInSafe;
}
public void setMoneyInSafe(double moneyInSafe) {
this.moneyInSafe = moneyInSafe;
}
public boolean hasActiveAccountBank(Bank bank){
}
private Account getAccountWithId(int id){
for(Account account:allActiveAccounts){
if(account.getId()==id){
return account;
}
}
return null;
}
}
public class Bank {
private static ArrayList<Bank> allBanks=new ArrayList<>();
private String name;
public Bank(String name) {
this.name = name;
allBanks.add(this);
}
public static Bank getBankWithName(String name){
for (Bank bank:allBanks){
if(bank.getName().equals(name)){
return bank;
}
}
return null;
}
public static boolean isThereBankWithName(String name){
return allBanks.contains(getBankWithName(name));
}
public static int getAccountInterestFromName (String name){
if(name.equals("KOOTAH")){
return 10;
}else if(name.equals("BOLAN")){
return 30;
}else{
return 50;
}
}
public String getName() {
return name;
}
}
So my question is How do I Define the hasActiveAccountBank method in Customer Class to Check Is there any Account with this Account id or not in Main Class.
the part of the Main Class has a matcher that returns Customer's name and the id so they are given.Here is the part:
if (!getCustomerByName(matcher.group(1)).hasActiveAccountBank()) {
System.out.println("Chizi zadi?!");
}
So How do i Write in the hasActiveAccountBank() argument?
Here's MVCE: https://github.com/neo4j-examples/movies-java-spring-data-neo4j
If you change one test to:
#Test
public void testFindByTitle() {
String title = "The Matrix";
Movie result = movieRepository.findByTitle(title);
Person p = personRepository.findByName("Keanu Reeves");
assertNotNull(result);
assertEquals(1999, result.getReleased());
}
You can see in debug mode that object p does not have any movies.
Person entity is:
#NodeEntity
public class Person {
#Id
#GeneratedValue
private Long id;
private String name;
private int born;
#Relationship(type = "ACTED_IN")
private List<Movie> movies = new ArrayList<>();
public Person() {
}
public Person(String name, int born) {
this.name = name;
this.born = born;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public int getBorn() {
return born;
}
public List<Movie> getMovies() {
return movies;
}
}
This is offical example from neo4j. How can i store entity Person with movies in database and also have Movie entity with roles ?
Edit: What i can do is add in Person entity method:
public void addMovie(Movie movie) {
if (this.movies == null) {
this.movies = new ArrayList<>();
}
this.movies.add(movie);
}
And in the test add:
p.addMovie(matrix);
personRepository.save(p);
But i don't like this - cause i setting it manually from two sites.
You do not need to set the references manually from two sides. Expand your code snippet slightly by a single line movie.setPerson(this); and you are done:
public void addMovie(#NotNull Movie movie) {
if (this.movies == null)
this.movies = new ArrayList<>();
this.movies.add(movie);
movie.setPerson(this);
}
I have a problem in realisation of Builder pattern.
I have 2 classes:
package course_2;
import java.util.Date;
public class Student {
private static int idStart = 0;
private final int id = idStart++;
private String name;
private String surname;
private String secondName;
private Date birthDate;
private String address;
private String phone;
private int course;
private int group;
public static class Builder {
// Обязательные параметры
private final String name;
private final String surname;
private final Date birthDate;
// Необязательные параметры, инициализация по умолчанию
private String secondName = "";
private String address = "";
private String phone = "";
private int course = 1;
private int group = 1;
public Builder(String name, String surname, Date birthDate) {
this.name = name;
this.surname = surname;
this.birthDate = (Date) birthDate.clone();
}
public Builder SecondName(String secondName) {
this.secondName = secondName;
return this;
}
public Builder address(String address) {
this.address = address;
return this;
}
public Builder phone(String phone) {
this.phone = phone;
return this;
}
public Builder course(int course) {
this.course = course;
return this;
}
public Builder group(int group) {
this.group = group;
return this;
}
}
private Student(Builder builder) {
this.name = builder.name;
this.surname = builder.surname;
this.secondName = builder.secondName;
this.birthDate = builder.birthDate;
this.address = builder.address;
this.phone = builder.phone;
this.course = builder.course;
this.group = builder.group;
}
}
The problem is when I'm trying to call a Builder from my client code:
Student studentOne = new Student.Builder("Andrue", "Booble", /*Date variable here*/);
I'm getting a compiler problem :
Error:(24, 30) java: incompatible types: course_2.Student.Builder
cannot be converted to course_2.Student
Can somebody help me with understanding, why does this happen and how I can solve it? Thanks!
You need to add the following to your Builder:
public Student build(){
return new Student(this);
}
And call it like this:
Student studentOne = new Student.Builder("Andrue", "Booble", null).build();
new Student.Builder("Andrue", "Booble", /*Date variable here*/); returns you builder object not student.
Your factory is missing method create which invoke Student constructor
it should looks like this
public Student create(){
return new student (this);
}
and be implemented inside Builder class
now if you want to create Student, you call
Student studentOne = new Student.Builder("Andrue", "Booble", /*Date variable here*/).create();
public class CustomerTest
{
private static Object CustomerType;
public static void main(String args[])
{
String msg = "";
ArrayList(Customer) cList;
ArrayList<Customer> customerList = new ArrayList<Customer>();
Customer c1 = new Customer ("Jones", new Address("Cooper","Arlington", "Texas", 76019), 12345);
Customer c2 = new Customer ("Smith", new Address("Bowen","Arlington", "Texas", 76006), 65489);
Customer c3 = new Customer ("willis", new Address("Bowen","Arlington", "Texas",75550), 27589);
customerList.add(c1);
customerList.add(c2);
customerList.add(c3);
ArrayList<Course> courseList = new ArrayList<Course>();
Course co1 = new Course ("OnlineCourse",("Java 1","PROGRAMMING", "Davis", 125.00, new Date(1,1,2015), new Date(1,15,2015), "Uta" 2015);
Course co2 = new Course ("OnlineCourse","Java 2","Jones", 125.00, new Date(1,1,2015), new Date(1,15,2015));
Course co3 = new Course ("InClassCourse","CanonPictures", "Long", 75.00, new Date(2,5,2015), new Date(3,2,2015));
courseList.add(co1);
courseList.add(co2);
courseList.add(co3);
c1.setCType(Customer.customerType.STUDENT);
c2.setCType(Customer.customerType.FACULTY);
c3.setCType(Customer.customerType.GOVERNMENT);
for (Customer c: customerList)
{
cList = c.getCustomerList();
for (Customer c: cList)
{
msg += c.calculateCharge();
}
}
JOptionPane.showMessageDialog(null, msg);
}
}
Getting "No suitable constructors found" In Course c01, co2.
It's also not letting me set STUDENT, FACULTY, etc.. it's giving me a "cannot find symbol" I have 6 classes and this is the main class. I declared all the variables, sets, gets in other classes and now I'm trying to execute the main class(this one) OKAY I ADDED THE CLASSES THE MAIN USES.
public class Course
{
private String title;
private String instructor;
private double price;
public enum CourseType{PROGRAMMING, MATHEMATICS, PHOTOGRAPHY, MUSIC, PAINTING, MISC};
private CourseType cType;
private Date startDate;
private Date endDate;
public Course()
{
setTitle("");
setInstructor("");
setPrice(0.0);
setCType(CourseType.PROGRAMMING);
setStartDate(new Date());
setEndDate(new Date());
}
public Course(String title, String instructor, double price, Date startDate, Date endDate)
{
setTitle(title);
setInstructor(instructor);
setPrice(price);
setStartDate(startDate);
setEndDate(endDate);
}
public void setTitle(String title)
{
this.title = title;
}
public void setInstructor(String instructor)
{
this.instructor = instructor;
}
public void setPrice(double price)
{
this.price = price;
}
public void setCType(CourseType cType)
{
this.cType = cType;
}
public void setStartDate(Date startDate)
{
this.startDate = startDate;
}
public void setEndDate(Date endDate)
{
this.endDate = endDate;
}
public String getTitle()
{
return title;
}
public String getInstructor()
{
return instructor;
}
public double getPrice()
{
return price;
}
public CourseType getCType()
{
return cType;
}
public Date getStartDate()
{
return startDate;
}
public Date getEndDate()
{
return endDate;
}
public double calculateCharge(Customer.CustomerType c)
{
return 0.0;
}
public String toString()
{
return("title" + title + "instructor" + instructor + "price" + price + "cType" + cType);
}
}
public class Customer
{
private String name;
private Address address;
private int accountNumber;
public enum CustomerType{STUDENT, FACULTY, GOVERNMENT};
private CustomerType cType;
private ArrayList<Course> courseList = new ArrayList<Course>();
public Customer()
{
setName("");
setAddress(new Address());
setAccountNumber(0);
}
public Customer(String name, Address address, int accountNumber)
{
setName(name);
setAddress(address);
setAccountNumber(accountNumber);
}
public void setName(String name)
{
this.name = name;
}
public void setAddress(Address address)
{
this.address = address;
}
public void setAccountNumber(int accountNumber)
{
this.accountNumber = accountNumber;
}
public void setCType(CustomerType cType)
{
this.cType = cType;
}
public void addCourse(Course course)
{
courseList.add(course);
}
public String getName()
{
return name;
}
public Address getAddress()
{
return address;
}
public CustomerType getCType()
{
return cType;
}
public String getCourseList()
{
return courseList.toString();
}
public String toString()
{
return("name" + name + "Address" + address + "accountnumber" + accountNumber + "CustomerType" + cType + "courseList" + courseList);
}
}
These are the constructors you have supplied.
public Course(String title, String instructor, double price, Date startDate, Date endDate)
public Course()
And based on the Course c01 object it looks like you have a couple of issues. First a misplaced parentheses (, and "Uta" 2015, aren't valid for the constructor either.
Course co1 = new Course ("OnlineCourse",("Java 1","PROGRAMMING", "Davis", 125.00, new Date(1,1,2015), new Date(1,15,2015), "Uta" 2015);
I am not sure if it was just a typo, but there is no suitable constructors, just as the errors states.
Try to replace that with this and see what you get.
Course co1 = new Course ("Java 1","Davis", 125.00, new Date(1,1,2015), new Date(1,15,2015));
For the cannot find symbol error it looks like you might not be referring to the enum correctly.
c1.setCType(Customer.customerType.STUDENT);
When it looks like it should be,
c1.setCType(Customer.CustomerType.STUDENT);
it's been a while since I've done some java coding.
I need to build an application for a business which requires automation (part of a workshop), which is however irrelevant to my question...
I'm stuck on the line : customerList.add(customer); //(part of the addCustomer method in the WCIA class)
Also it's the first time I'm told to "Assign return value to new Variable" as part of an error, so not too sure what that means.
Code: Main
import java.util.ArrayList;
public class WCIA {
private final ArrayList customerList = null;
public static void main(String[] args) {
short s =002;
Customer arno = new Customer();
arno.setName("Arno");
arno.setId(s);
arno.setEmail("arnomeye#gmail.com");
arno.setAddress("Somewhere");
arno.setPhoneNum("0727855201");
System.out.printf("%s",arno.getEmail());
WCIA wcia = new WCIA();
wcia.addCustomer(arno);
wcia.displayCustomers();
}
public void addCustomer (Customer customer)
{
customerList.add(customer); // <---Problem over here
}
public void displayCustomers()
{
for(int x=0;x<customerList.size();x++)
{
Customer cus = (Customer) customerList.get(x);
cus.DisplayCustomer();
}
}
}
Code: Customer class:
public class Customer {
private short id;
private String name;
private String email;
private String phoneNum;
private String address;
public Customer()
{
System.out.println("Class initiated");
}
public void DisplayCustomer()
{
System.out.append("Name : "+ name+"\n");
System.out.append("ID : "+ id+"\n");
System.out.append("Email : "+ email+"\n");
System.out.append("Phone Number : "+ phoneNum+"\n");
System.out.append("address : "+ address+"\n");
}
public void setId(short id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public void setAddress(String address) {
this.address = address;
}
public short getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getPhoneNum() {
return phoneNum;
}
public String getAddress() {
return address;
}
}
You need to instantiate your ArrayList before you can assign elements to it. You're probably getting a NullPointerException, is my guess.
Change this line:
private final ArrayList customerList = null;
to
private final ArrayList customerList = new ArrayList();
Should solve at least this problem. I did not read the rest of your code so I'm not sure if other problems exist.
customerList is null and never initialized. Create an object of type ArrayList and assign it to that variable before you try to add to it.
You should declare the List with an explicit definition of the type of its elements (parametrized list):
private final List<Customer> customerList;
This way you can get rid of casting to Customer in:
Customer cus = customerList.get(x);
Finally, as good practice, initialize it in the constructor:
public WCIA()
{
customerList = new ArrayList<>();
}