No suitable contstructors found java error..? - java

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);

Related

Android : How to calculate total in model array-list?

I want to create totalHaveMoney I was try to looping data income - expense in totalMoney method, but this failed. It is only getting income from one data, so the calculation doesn't work.
Model :
public class MoneyManager {
String name, category;
String date, description, key;
Long income, expense, totalMoney;
public MoneyManager(String name, String category, String date, Long income,Long expense , String description) {
this.name = name;
this.category = category;
this.date = date;
this.income = income;
this.expense = expense;
this.description = description;
}
public MoneyManager() {
}
public Long totalMoney(){
Long haveMoney = getIncome();
for (int i = 0; i < this.getIncome(); i++){
haveMoney -= getExpense();
}
return haveMoney;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Long getExpense() {
return expense;
}
public void setExpense(Long expense) {
this.expense = expense;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Long getIncome() {
return income;
}
public void setIncome(Long income) {
this.income = income;
}
public Long getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(Long totalMoney) {
this.totalMoney = totalMoney;
}
}
When im getting data:
MoneyManager money = moneyManagers.get(position);
Resources resources = context.getResources();
Locale rupiah = new Locale("id", "id");
holder.textName.setText(String.valueOf(money.totalMoney()));
holder.textDate.setText(money.getDate());
The result of money.totalMoney() is the same as income.
If you have a List of you can iterate through them and add
long sum = 0;
for (MoneyManager mm : listOfMoneyManager) { // or whatever your local variable is
sum += mm.getIncome() - mm.getExpense();
}
// after the iteration you have the total
System.out.println (sum);

error: incompatible types: int cannot be converted to Client - Java

I'm fairly new to Java and am working on a Spring Boot database application (MySQL) in which I am trying to create a method which will allow for a Client (Java object) to be deleted from my database using a Dao/CrudRepository and searching by the unique Id. I am receiving an error when running stating "error: incompatible types: int cannot be converted to Client"
I've compared to a similar program created in which I used the same syntax with no issue, so unsure why I am getting this error when processing.
My object is set up like so:
#Entity
public class Client {
#Id
#GeneratedValue
private int id;
#NotNull
#Size(min=1, message = "Must enter client name")
private String name;
#NotNull
#Size(min=1, message= "Must enter contact's name")
private String contact;
#NotNull
#Size(min=1, message="Must enter primary office location")
private String location;
#NotNull(message="Must enter contract start date")
private String startDate;
#NotNull(message="Must enter contract end date")
private String endDate;
#NotNull(message="Must enter employee count")
private Integer employeeCount;
private PhilanthropyInterest interest;
public Client(String name, String contact, String location, String startDate, String endDate, Integer employeeCount) {
this.name = name;
this.contact = contact;
this.location = location;
this.startDate = startDate;
this.endDate = endDate;
this.employeeCount = employeeCount;
}
public Client () { }
public int getId() { return id; }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public Integer getEmployeeCount() {
return employeeCount;
}
public void setEmployeeCount(Integer employeeCount) {
this.employeeCount = employeeCount;
}
public PhilanthropyInterest getInterest() { return interest; }
public void setInterest(PhilanthropyInterest interest) { this.interest = interest; }
And my controller method in which I am receiving the error is:
#RequestMapping(value = "remove", method = RequestMethod.GET)
public String displayRemoveAddClientForm(Model model) {
model.addAttribute("clients", clientDao.findAll());
model.addAttribute("title", "Remove Client");
return "clients/remove";
}
#RequestMapping(value = "remove", method = RequestMethod.POST)
public String processRemoveClientForm(#RequestParam int[] clientIds) {
for (int clientId : clientIds) {
clientDao.delete(clientId);
}
return "redirect:";
}
And my ClientDao class is:
#Repository
#Transactional
public interface ClientDao extends CrudRepository<Client, Integer> {
}
The error showing in IntelliJ states:
delete
(org.launchcode.spcdb.models.Client)
in CrudRepository cannot be applied
to
(int)
and when running, I am receiving:
error: incompatible types: int cannot be converted to Client
clientDao.delete(clientId);
here you can find the documentation for CRUDRepository : https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html
You will see that the method delete(T entity) method take an Object as argument.
In your case, it is a Client.
As you are using
public interface ClientDao extends CrudRepository<Client, Integer>
the method delete(T entity) that you use is expecting an Client.
So try :
public String processRemoveClientForm(#RequestParam int[] clientIds) {
List<Client> clientList = clientDAO.findByIds(clientIds);
for (Client client : clientList) {
clientDao.delete(client );
}
}
this should work. Nerver forget that you are manipulating Object in this case.

Java object scope between methods

I'm having issues with this code running, I'm trying to get the program to print the strings below by using input from the other classes. As you can see, the info put into the new Bride and Location objects are being put in to a Wedding Object and then I need to try and retrieve the details from the wedding object and display it on screen like so:
Wedding data:
Bride: Amy Cronos, age: 29
Location: South Rd, suburb: Tonsley
but I am instead met with 4 identical errors relating to the place.getName, place.getSuburb() etc. etc. that say
Main.java:6: error: cannot find symbol
System.out.println("Location"+place.getStreet()+", suburb:
"+place.getsuburb());
symbol: variable place
location: class Main
I'm pretty sure this has something to do with the scope, but cant work out what I need to do.
What is causing this error and how do I fix it?
Here is the code:
public class WeddingDetails {
public static void main(String[] args) {
Bride person = new Bride("Amy Cronos", 29);
Location place = new Location("Tonsley", "South Rd");
Wedding wed = new Wedding(person, place);
show(wed);
}
public static void show(Wedding wed) {
System.out.println("Wedding data:");
System.out.println("Bride: " + person.getName() + ", age: " + person.getAge());
System.out.println("Location: " + place.getStreet() + ", suburb: " + place.getSuburb());
}
public static class Location {
private String suburb;
private String street;
Location(String suburb, String street) {
this.suburb = suburb;
this.street = street;
}
public String getSuburb() {
return suburb;
}
public String getStreet() {
return street;
}
}
public static class Bride {
private String name;
private int age;
Bride(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public static class Wedding {
private Bride person;
private Location place;
Wedding(Bride person, Location place) {
this.person = person;
this.place = place;
}
public Bride getBride() {
return person;
}
public Location getPlace() {
return place;
}
}
}
The issue here is your println statements are trying to access methods within objects, but by calling those methods on the wrong object. You should be accessing the Bride and Location objects with the Wedding class' getters (getBride() and getPlace(). The complete call would be wed.getBride().getName() and wed.getPlace().getStreet() so on.
Corrected code is below. NOTE: for the purposes of being able to compile all of the code inside one class, I added the static keyword to the Bride, Location and Wedding class declarations. You can just remove the static and copy and paste each class back into your .java files.
public class WeddingDetails {
public static void main(String[] args) {
Bride person = new Bride("Amy Cronos", 29);
Location place = new Location("Tonsley", "South Rd");
Wedding wed = new Wedding(person, place);
show(wed);
}
public static void show(Wedding wed) {
System.out.println("Wedding data:");
System.out.println("Bride: " + wed.getBride().getName() + ", age: " + wed.getBride().getAge());
System.out.println("Location: " + wed.getPlace().getStreet() + ", suburb: " + wed.getPlace().getSuburb());
}
public static class Location {
private String suburb;
private String street;
Location(String suburb, String street) {
this.suburb = suburb;
this.street = street;
}
public String getSuburb() {
return suburb;
}
public String getStreet() {
return street;
}
}
public static class Bride {
private String name;
private int age;
Bride(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public static class Wedding {
private Bride person;
private Location place;
Wedding(Bride person, Location place) {
this.person = person;
this.place = place;
}
public Bride getBride() {
return person;
}
public Location getPlace() {
return place;
}
}
}

Class Return type when creating a method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
[UML Diagram][1]
I'm studying for the midterm exam next week and I'm practicing some given examples from my professor; however, I am having some trouble with class return type methods.
I attached UML diagram just in case.
What i'm trying to understand is getPerson method in Job class. I don't think i need a array list in Job class to store all the employee. Because I have an array list already in Company class. Also return type is Employee class that I'm not sure how to get person's info using this class return type.
My problems
public Employee getPerson() {} in Job class
public boolean isVacant() {} in Job class
Also would you mind checking getVacantJobs, getFilledJobs, and getAllJobs methods if those are correctly built?
I used iterator to display all the stored jobs.
---------------------------Employee Class -----------------------------
public class Employee {
private String name;
private int id;
public Employee(int id, String name) {
this.name = name;
this.id =id;
}
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
public final int getId() {
return id;
}
public final void setId(int id) {
this.id = id;
}
#Override
public String toString() {
return "Employee [name=" + name + ", id=" + id + "]";
}
}
----------------------------Job Class--------------------------------------
public class Job {
private String description;
private int id;
private double maxSalary;
public Job(int id, double maxSalary, String description) {
this.description = description;
this.id = id;
this.maxSalary = maxSalary;
}
public Job(int id, double maxSalary, String description, Employee e1) {
this.description = description;
this.id = id;
this.maxSalary = maxSalary;
}
#Override
public String toString() {
return "Job [description=" + description + ", id=" + id
+ ", maxSalary=" + maxSalary + "]";
}
public final String getDescription() {
return description;
}
public final void setDescription(String description) {
this.description = description;
}
public final double getMaxSalary() {
return maxSalary;
}
public final void setMaxSalary(double maxSalary) {
this.maxSalary = maxSalary;
}
public final int getId() {
return id;
}
public Employee getPerson() {
retrun
}
public final void setPerson(Employee person) {
this.id = person.getId();
}
}
--------------------------Company Class ---------------------------
import java.util.ArrayList;
import java.util.Iterator;
public class Company {
static ArrayList list = new ArrayList();
Iterator itr = list.iterator();
private String name;
public Company(String name) {
this.name = name;
}
public Company() {
// TODO Auto-generated constructor stub
}
public static void addJob(Job j1) {
list.add(j1);
}
public void removeJob(int id) {
list.remove(id);
}
public ArrayList<Job> getVacantJobs() {
while (itr.hasNext()) {
if ((itr == null)) {
System.out.println(itr);
}
}
return null;
}
public ArrayList<Job> getFilledJobs() {
while (itr.hasNext()) {
if (!(itr == null)) {
System.out.println(itr);
}
}
return null;
}
public ArrayList<Job> getAllJobs() {
while (itr.hasNext()) {
System.out.println(itr.next());
}
return null;
}
}
Add field person to Job class.
public class Job {
// .....
private Employee person;
public Employee getPerson() {
return person;
}
public final void setPerson(Employee person) {
this.person = person;
}
public boolean isVacant() {
return person == null;
}
}
And add jobs field to Company class.
public class Company {
// static ArrayList list = new ArrayList(); // You don't need this
// Iterator itr = list.iterator(); // You don't need this.
// .....
private ArrayList<Job> jobs = new ArrayList<>();
public ArrayList<Job> getVacantJobs() {
ArrayList<Job> result = new ArrayList<>();
for (Job job : jobs)
if (job.isVacant())
result.add(job);
return result;
}
public ArrayList<Job> getFilledJobs() {
ArrayList<Job> result = new ArrayList<>();
for (Job job : jobs)
if (!job.isVacant())
result.add(job);
return result;
}
public ArrayList<Job> getAllJobs() {
ArrayList<Job> result = new ArrayList<>();
for (Job job : jobs)
result.add(job);
return result;
}
}

How to use more than one method?

So, I've got to write an invoice for a video store that has a Customer class which takes six attributes, the customer name (string), the street address (string), the city(String), the state(string), the zip code(string), and the telephone number. I had to use a parameterized constructor that receives the attributes as parameters as well as provide getters and setters. I believe I did this correctly.
Next I had to make a Video class that had four attributes, the video name (string), the year the video was released(integer), the video copy number(integer), and the daily rental rate(double). I had to do a parameterized constructor and getters and setters for this as well.
The problems start on my Invoice class which is to represent the rental of a video to a given customer, it is not finished, but is supposed to have four attributes, the customer renting the video, the video being rented, the date it was rented(as a inputted string), and the daily rental rate(double). It was also supposed to have three methods, the subtotal, the tax and the total. My problem is I've got the preset methods for the customers and the videos setup, I just have no clue how to effectively use them in an if statement. I don't know what I would put in my fourth test class to allow this to work. I am all but lost at this point, any push in the right direction would be greatly appreciated. here are my classes.
Customer:
public class Customer {
private String customerName;
private String streetAddress;
private String custCity;
private String custState;
private String custZip;
private String custPhone;
public Customer(String customerName, String streetAddress, String custCity, String custState, String custZip,
String custPhone) {
super();
this.customerName = customerName;
this.streetAddress = streetAddress;
this.custCity = custCity;
this.custState = custState;
this.custZip = custZip;
this.custPhone = custPhone;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getCustCity() {
return custCity;
}
public void setCustCity(String custCity) {
this.custCity = custCity;
}
public String getCustState() {
return custState;
}
public void setCustState(String custState) {
this.custState = custState;
}
public String getCustZip() {
return custZip;
}
public void setCustZip(String custZip) {
this.custZip = custZip;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
}
Video:
public class Video {
private String videoName;
private int videoYear;
private int copyNum;
private double rentalRate;
public Video(String videoName, int videoYear, int copyNum, double rentalRate) {
super();
this.videoName = videoName;
this.videoYear = videoYear;
this.copyNum = copyNum;
this.rentalRate = rentalRate;
}
public String getVideoName() {
return videoName;
}
public void setVideoName(String videoName) {
this.videoName = videoName;
}
public int getVideoYear() {
return videoYear;
}
public void setVideoYear(int videoYear) {
this.videoYear = videoYear;
}
public int getCopyNum() {
return copyNum;
}
public void setCopyNum(int copyNum) {
this.copyNum = copyNum;
}
public double getRentalRate() {
return rentalRate;
}
public void setRentalRate(double rentalRate) {
this.rentalRate = rentalRate;
}
Invoice (incomplete) :
import java.util.Scanner;
public class Invoice {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Customer Brandon = new Customer("Brandon James" , "112 Oak Street"
, "CityVille" , "Alabama" , "18229",
"912-2248");
Customer Judy = new Customer("Judy Vermooth" , "8008 Ribbit Ln.",
"Metropolis" , "Pennsylvania" , "24057", "241-8009");
Video Easter = new Video("Easter 2", 2002, 4, 2.49);
Video DareDevil3 = new Video ("Dare Devil 3", 2012, 2, 3.62);
if( Prog4.newRental = "Brandon"){
Customer Brandon = newCust
}
}
}
Prog4(incomplete):
import java.util.*;
public class Prog4 {
private String newRental;
private String vidName;
private String rentalDate;
private String daysRented;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter Customer Name: ");
String newRental = in.nextLine();
System.out.println("Enter Video Name: ");
String vidName = in.nextLine();
System.out.println("Enter Rental date in mm/dd/yyyy format: ");
String rentalDate = in.nextLine();
System.out.println("Enter Number of Days Rented");
int daysRented = in.nextInt();
}
public String getNewRental() {
return newRental;
}
public void setNewRental(String newRental) {
this.newRental = newRental;
}
public String getVidName() {
return vidName;
}
public void setVidName(String vidName) {
this.vidName = vidName;
}
public String getRentalDate() {
return rentalDate;
}
public void setRentalDate(String rentalDate) {
this.rentalDate = rentalDate;
}
public String getDaysRented() {
return daysRented;
}
public void setDaysRented(String daysRented) {
this.daysRented = daysRented;
}
}

Categories

Resources