I have 2 classes Student and Finals.
Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class Student {
#Id
private int id;
private String name;
private int marks;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name, int marks) {
this.id = id;
this.name = name;
this.marks = marks;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
}
Finals.java
#Entity
public class Finals {
#Id
private int id;
private int marks;
private int totalMarks;
public Finals() {
super();
// TODO Auto-generated constructor stub
}
public Finals(int id, int marks, int totalMarks) {
super();
this.id = id;
this.marks = marks;
this.totalMarks = totalMarks;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
public int getTotalMarks() {
return totalMarks;
}
public void setTotalMarks(int totalMarks) {
this.totalMarks = totalMarks;
}
}
ObjWrapper.java
public class ObjWrapper{
private Student student;
private Finals finals;
public ObjWrapper() {
super();
// TODO Auto-generated constructor stub
}
public ObjWrapper(Student student, Finals finals) {
super();
this.student = student;
this.finals = finals;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Finals getFinals() {
return finals;
}
public void setFinals(Finals finals) {
this.finals = finals;
}
}
I would like to take the marks of a particular student from student class and add some marks given by user and update it in the marks column of the Student.
I would also like to add a new row to finals table with id,marks,newmarks.
My code
#Autowired
public StudentRepo studentrepo;
#Autowired
public StudentService stuservice;
#Autowired
public FinalsRepo finalsrepo;
#PutMapping("/updatemarks/{id}/{marks}")
public void putTables(#RequestBody ObjWrapper wrapper,#PathVariable(value = "id") int id,#PathVariable(value = "marks") int marks) {
Student student = wrapper.getStudent();
student = studentrepo.getOne(id);
int studentMark = student.getMarks();
int newMarks = studentMark + marks;
Finals f = wrapper.getFinals();
f.setId(id);
f.setMarks(marks);
f.setTotalMarks(newMarks);
finalsrepo.save(f);
student.setMarks(newMarks);
studentrepo.save(student);
}
Error :
"message": "Required request body is missing: public void com.example.demo.controller.MainController.putTables(com.example.demo.entities.ObjWrapper,int,int)",
"path": "/updatemarks/1/50"
I am not able to unwrap through object mapper I think, I dont know what is wrong in my code...can anyone help me out....Thanks
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?
public class Student {
private String name;
private long id;
private String grade;
private int[] test;
private int NUM_TESTS;
public Student(){
name="Un";
id=0;
grade="Un";
test=new int[0];
NUM_TESTS=5;
}
public Student(String x, long z) {
name=x;
id=z;
}
public void setName(String n) {
name=n;
}
public void setID(long i) {
id=i;
}
public void setGrade(String g) {
grade=g;
}
/*public void setTestScore(int t,int s) {
test=t;
test=s;
}
public int getTestScore(int) {
return test;
}*/
public int getNumTests() {
return NUM_TESTS;
}
public String getName() {
return name;
}
public long getID() {
return id;
}
public String getGrade() {
return grade;
}
public String toString() {
return getTestScore()+getNumTests()+getName()+getID()+getGrade();
}
/*public void calculateResult() {
int sum=0;
for (int t:test)sum+=t;
double average= 1.0t*sum/5;*/
}
}
Here is my code I have spaced out the places where I am having the issues. I am writing a Student subclass with subclasses undergrad and postgrad.
Here is the UML
I don't understand how to correctly implement testScore if it is not one of the variables? Nevermind the calculate result I'll fix that myself. I am also unsure if my constructors are accurate. All the students do five exams that's a constant
setTestScore(int t, int s)... I do recommend to use carefully chosen names (identifiers). For example if you just rename the parameters to: setTestScore(int testNumber, int score) you can be more familiar what should you inplement.
test = new int[0];isn't what you want. You want test = new int[NUM_TESTS]
Try to reconsider method setTestScore(int testNumber, int score)
first parameter is actually the index in the array of test and the second is the value.
So, your method should be something like this:
public void setTestScore(int testNumber, int score) {
test[testNumber] = score;
}
I just gave you some guidance for your own implementation...
First of all, It seems that Student class should be abstract. because each student is UnderGraduate or PostGraduate.
Secondly, you should extend the child classes from Student class.
I hope the below code be helpful:
abstract class Student {
private String name;
private long id;
private String grade;
private int[] test;
private final int NUM_TESTS = 5;
public Student(){
name = "UN";
id = 0;
grade = "UN";
test = new int[NUM_TESTS];
}
public Student(String name, long id){
this.name = name;
this.id = id;
}
#Override
public String toString() {
//TODO: write your desire toString method
return getNUM_TESTS()+getName()+getId()+getGrade();
}
abstract void claculateResult();
public int getTestScore(int testNumber){
if(testNumber >= NUM_TESTS)
return 0;
return test[testNumber];
}
public void setTestScore(int testNumber, int score){
if(testNumber >= NUM_TESTS)
return;
test[testNumber] = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public int[] getTest() {
return test;
}
public void setTest(int[] test) {
this.test = test;
}
public int getNUM_TESTS() {
return NUM_TESTS;
}
}
and the UnderGraduate class would be:
public class UnderGraduate extends Student{
public UnderGraduate(){
}
public UnderGraduate(String name, long id){
super();
}
#Override
void claculateResult() {
//TODO: DO whatever you want
}
}
remember that the PostGraduate class is same as UnderGraduate.
So I'm supposed to use a "Summable" interface to add up the populations of the cities. I've been staring at it for an hour but still can't find my error. Please Help!
This is my tester
public class SummableTester extends ConsoleProgram
{
public void run()
{
City cookie = new City("Coookie", 20000);
City taco = new City("Taco", 10000);
System.out.println(taco.getValue());
}
}
City Class:
public class City
{
private String name;
private int population;
public City(String name, int population)
{
this.name = name;
this.population = population;
}
public String getName()
{
return this.name;
}
public int getValue()
{
return this.population;
}
public int add(Summable other)
{
return getValue() + other.getValue();
}
}
Summable:
public interface Summable
{
public int add(Summable other);
public int getValue();
}
You implemented Summable interface in your City class, but forgot to include implements Summable in city class.
public class City implements Summable {
}
I'm creating the grid view with array list of category and un categorized product model class. Now I want to sort the list by date or name. See below my code.
Here this is my adapter.
public class CommonAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater inflator = null;
private List<Object> list;
public CommonAdapter(Context mContext, List<Object> list) {
super();
this.mContext = mContext;
this.list = list;
inflator = LayoutInflater.from(mContext);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflator.inflate(R.layout.row_categories, null);
holder = new ViewHolder();
holder.layout_bg = (RelativeLayout) convertView.findViewById(R.id.grid_bg);
holder.titleTextView = (TextView) convertView.findViewById(R.id.grid_item_title);
holder.txt_price = (TextView) convertView.findViewById(R.id.txt_price);
holder.img_notifier = (ImageView) convertView.findViewById(R.id.img_notifier);
holder.titleTextView.setTextColor(Color.WHITE);
holder.titleTextView.setTextSize(27);
holder.titleTextView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
holder.titleTextView.setLayoutParams(new RelativeLayout.LayoutParams(200, 200));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (list.get(position) instanceof Product) {
holder.titleTextView.setText(((Product) list.get(position)).getShortCode());
holder.img_notifier.setVisibility(ImageView.GONE);
holder.txt_price.setVisibility(TextView.VISIBLE);
NumberFormat format = NumberFormat.getCurrencyInstance();
double amount = Double.parseDouble(((Product) list.get(position)).getPrice()toString());
String formatAmount = NumberFormat.getCurrencyInstance().format(amount / 100);
holder.txt_price.setText(formatAmount);
}
if (list.get(position) instanceof Category) {
holder.titleTextView.setText(((CategoryWithProduct) list.get(position)).getShortCode());
holder.img_notifier.setVisibility(ImageView.VISIBLE);
holder.txt_price.setVisibility(TextView.GONE);
if (((Category) list.get(position)).getColor() != null) {
holder.layout_bg.setBackgroundColor(Color.parseColor(((Category) list.get(position)).getColor()));
} else {
}
}
return convertView;
}
static class ViewHolder {
RelativeLayout layout_bg;
TextView titleTextView, txt_price;
ImageView img_notifier;
}
This is product model classes
public class Product {
String id;
String name;
String price;
String createAt;
public Product(String id, String name, String price, String createAt) {
this.id = id;
this.name = name;
this.price = price;
this.createAt = createAt;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt;
}
}
This is Category Model
public class Category {
String id;
String name;
String createAt;
public Category(String id, String name, String createAt) {
this.id = id;
this.name = name;
this.createAt = createAt;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt;
}
}
In MainActivity.java
CommonAdapter commonAdapter = new CommonAdapter(getActivity(), commonArrayList);
grid_common.setAdapter(commonAdapter);
Here I tried with comparator, it's comes with object only!
Collections.sort(commonArrayList, new Comparator<Object>() {
#Override
public int compare(Object o1, Object o2) {
return 0;
}
});
See here both models have createAt and name fields, So I want to sort by createAt or by name in this ArrayList.
Create another object model class and add all method and variable there is in two separate class...
and set data manually then... using for loop and any other ..that suitable for you...
and you this third created object model for sorting your data...
Edited
Eg:
first class
class first{
String f_name,l_name;
public String getF_name() {
return f_name;
}
public void setF_name(String f_name) {
this.f_name = f_name;
}
public String getL_name() {
return l_name;
}
public void setL_name(String l_name) {
this.l_name = l_name;
}
}
Second class
public class second {
String f_name,l_name,m_name;
public String getF_name() {
return f_name;
}
public void setF_name(String f_name) {
this.f_name = f_name;
}
public String getL_name() {
return l_name;
}
public void setL_name(String l_name) {
this.l_name = l_name;
}
public String getM_name() {
return m_name;
}
public void setM_name(String m_name) {
this.m_name = m_name;
}
}
third class
public class third{
String f_name,l_name,m_name;
public String getF_name() {
return f_name;
}
public void setF_name(String f_name) {
this.f_name = f_name;
}
public String getL_name() {
return l_name;
}
public void setL_name(String l_name) {
this.l_name = l_name;
}
public String getM_name() {
return m_name;
}
public void setM_name(String m_name) {
this.m_name = m_name;
}
}
set all value of first and second into third...
and use third class for setup data and sorting data
Here is my advice:
public class Category {
String id;
String name;
String createAt;
...
}
public class Product extends Category{
String price;
....
}
Collections.sort(commonArrayList, new Comparator<Category>() {
#Override
public int compare(Category o1, Category o2) {
if(o1.getCreateAt()>o2.getCreateAt()){
return 1;
}else{
...
}
return 0;
}
});
Create an abstract class, put fields common in Product and Category and compare that class.
public abstract class BaseClass {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Your Public class:
public class Product extends BaseClass {
...
public Product(String id, String name, String price, String createAt) {
setId(id);
setName(name);
this.price = price;
this.createAt = createAt;
}
}
Category class:
public class Category extends BaseClass {
...
public Category(String id, String name, String createAt) {
setId(id);
setName(name);
this.createAt = createAt;
}
}
And compare like this:
Collections.sort("ArrayList<BaseClass>()", new Comparator<BaseClass>() {
#Override
public int compare(BaseClass baseClass, BaseClass t1) {
return baseClass.getName().compareTo(t1.getName());
}
});
If you wanna sort by date put date field to BaseClass.
Thanks for your advice. I found the answer. I just make class casting on the object inside the comparator.
See the code below,
Collections.sort(commonArrayList, new Comparator<Object>() {
#Override
public int compare(Object o1, Object o2) {
int res = 0;
if (o1 instanceof Category && o2 instanceof Category) {
res = (((Category) o1).getName().compareTo(((Category) o2).getName()));
} else if (o1 instanceof Product && o2 instanceof Product) {
res = (((Product) o1).getName().compareTo(((Product) o2).getName()));
} else if (o1 instanceof Category && o2 instanceof Product) {
res = (((Category) o1).getName().compareTo(((Product) o2).getName()));
} else if (o1 instanceof Product && o2 instanceof Category) {
res = (((Product) o1).getName().compareTo(((Category) o2).getName()));
}
return res;
}
});
If you have any simplified ideas, kindly post here..
Hope this sample solution in java may help :
Create an interface let say Data as follows
public interface Data {
}
Create the model classes as follows :
Product
public class Product implements Data{
String id;
String name;
String price;
String createAt;
public Product(String id, String name, String price, String createAt) {
this.id = id;
this.name = name;
this.price = price;
this.createAt = createAt;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt;
}
}
Category
public class Category implements Data{
String id;
String name;
String createAt;
public Category(String id, String name, String createAt) {
this.id = id;
this.name = name;
this.createAt = createAt;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt;
}
}
Now in main class of the project
public class TestSorting {
public static void main(String args[]) {
ArrayList<Data> categories = new ArrayList<>();
ArrayList<Data> products = new ArrayList<Data>();
// For Product
for (int i = 0; i < 10; i++) {
Product product = new Product("Prod" + i, "Product " + i, "" + i, System.currentTimeMillis() + "");
products.add(product);
}
// For category
for (int i = 10; i >=0; i--) {
Category category = new Category("Cat" + i, "Category " + i, System.currentTimeMillis() + "");
categories.add(category);
}
Collections.sort(categories, new Comparator<Data>() {
#Override
public int compare(Data data, Data data2) {
if(data instanceof Category)
{
int result=(((Category) data).getId().compareTo((((Category) data2).getId())));
return result;
}else if(data instanceof Product)
{
int result= (((Product) data).getId().compareTo(((Product) data2).getId()));
return result;
}else {
return 0;
}
}
});
System.out.println("******PRODUCT****************");
// For Product
for (int i = 0; i < products.size(); i++) {
Product product=((Product)products.get(i));
System.out.println(product.id+ " "+product.name);
}
System.out.println("\n\n"+"******Caterogy****************");
// For category
for (int i = 0; i < categories.size(); i++) {
Category category=((Category)categories.get(i));
System.out.println(category.id+ " "+category.name);
}
}
}