id appreciate anyones help here. Below is my class that contains all my setters and getters, in my main class, ive created 3 customers and in the value parameters, i have 3 different numbers. What i need to do is find the total value of all of those values, is there any way that i can create a method (See bookingValue below) that will calculate and add the the total of each customers value parameter? Bare in mind that 3 is not a fixed number, so the method should not be affected should i choose to add in more customers. This is probably really basic but if someone could get me on the right path, that'd be great, cheers
public class Customer
{
private int identity;
private String name;
private String address;
private double value;
public Customer()
{
identity = 0;
name = "";
address = "";
value = 0.0;
}
public void setIdentity(int identityParam)
{
identity = identityParam;
}
public int getIdentity()
{
return identity;
}
public void setName(String nameParam)
{
name = nameParam;
}
public String getName()
{
return name;
}
public void setAddress(String addressParam)
{
address = addressParam;
}
public String getAddress()
{
return address;
}
public void setValue(double valueParam)
{
value = valueParam;
}
public double getCarCost()
{
return value;
}
public void printCustomerDetails()
{
System.out.println("The identity of the customer is: " + identity);
System.out.println("The name of the customer is: " + name);
System.out.println("The address of the customer is: " + address);
System.out.println("The value of the customers car is: " + value + "\n");
}
public void bookingValue()
{
//Ive tried messing around with a for loop here but i cant seem to get it working
}
}
you can create an array of object of class customer and access the value in loop...
In the main function:
customer cus[]=new customer[num];
where num can be any number as 3 in your case
then get the "value" for each customer.. and then
public double bookingValue(customer []cus, int length)
{
double total=0.0;
for(int i=0;i<length;i++)
total+=a[i].value;
return total;
}'
return total value wherever you want to use.....
As in real life, one customer doesn't know anything about the other customers. If you would ask a customer in a store how much all customers spent, he will just look as confused as others reading this question.
I'd suggest implementing some CustomerManager, or Bookkeeper which hold all the customers internally (in a List for example). This CustomerManager needs to have methods to add and remove customers, the getBookingValue() method which loops over all customers in the CustomerManager's customers List and returns the total value and, if you please, some other comfort methods.
As an example:
public interface CustomerManager {
public void addCustomer(Customer customer);
public void removeCustomer(Customer customer);
public List<Customer> getCustomersByDate(long from, long to);
public double getBookingValue();
public double getBookingValue(List<Customer> customerList);
public List<Customer> getByAddress(String address);
public List<Customer> getByName(String name);
}
Related
I am new to java and learning basic concepts. I was learning abstraction and the most basic definitions i found was : Used to hide the complexity (hide how a process will be done and show what can we do?)
Fair enough. I got a basic idea of what is abstraction. But i am not clear in few things here:
Lets see the below example:
/* File name : Employee.java */
public abstract class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public double computePay() {
System.out.println("Inside Employee computePay");
return 0.0;
}
public void mailCheck() {
System.out.println("Mailing a check to " + this.name + " " + this.address);
}
public String toString() {
return name + " " + address + " " + number;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
Salary.java
/* File name : Salary.java */
public class Salary extends Employee {
private double salary; // Annual salary
public Salary(String name, String address, int number, double salary) {
super(name, address, number);
setSalary(salary);
}
public void mailCheck() {
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName() + " with salary " + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}
Main.java
/* File name : AbstractDemo.java */
public class AbstractDemo {
public static void main(String [] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using Employee reference--");
e.mailCheck();
}
}
My question here is , we cant even intsantiate an abstract class. So we have to extend it and overide the same method? When we override the abstract methos in the child class, the super class(abstract class method) is of no use. Also as we cant even intantiate , why cant we just write everything in one class instead of extending the abtsrcat class?
While extending the abstract class and overriding the same thing is it not a negative as the space will more for these waste abstract classes?
I know i dont have clarity and thats the reason i am confused. If anyone can clarify this (no stratight definions which are not useful for noobs like me) with explanation , i would really appreciate the time for that.
Aside of bad example you use, your understanding of Abstract class is not right:
So we have to extend it and overide the same method? When we override the abstract methos in the child class, the super class(abstract class method) is of no use
First: Abstract class and Interface both stay to provide abstract methods to be overridden by extending or implementing classes.
Second: Abstract class can have common methods implementation for all extending classes - then you do not need to implement or override them over and over again. Just use them. (Note: starting from Java 8 Interface also can have default implementation for methods)
Third: if you need another than common implementation - override needed method from super class.
Forth: if in your another implementation you need to run super method - do it at any time in the implementation by calling super.methodName(...)
Create and implement a class Person. A Person has a firstName and friends. Store the names of the friends as a String, separated by spaces. Provide a constructor that constructs a Person with a given name (passed through arguments) and no friends. Provide the following methods:
public void befriend(Person p)
public void unfriend(Person p)
public String getFriendNames()
public int getFriendCount()
*Hint - you can use p.name to access the name of the Person passed to a method as an argument.
Include a Tester class to make sure your Person has some friends.
How do I store the names of the friends as a String, separated by spaces. (I have to be able to input the names from the main method). I also have no idea how to get rid of already inputted name using the method "unfriend"
public class Person
{
private String firstName;
private String friendNames;
private int friendCount;
public Person(String name)
{
firstName = name;
friendCount = 0;
}
public String getFriendNames()
{
return friendNames;
}
public double getFriendCount()
{
return friendCount;
}
public void befriend(String name)
{
friendNames = friendNames + " " + name;
friendCount++;
}
public void unfriend(String name)
{
String[] parseNames = friendNames.split(name);
friendNames = parseNames[0] + parseNames[1];
friendCount--;
}
}
Main Method:
public class PersonTester {
public static void main(String[] args) {
Person p = new Person("Alex");
p.befriend("John");
p.befriend("Alice");
p.befriend("Mike");
p.befriend("Annette");
p.unfriend("Alice");
System.out.println(p.getFriendCount());
System.out.println(p.getFriendNames());
}
}
Expected output:
2
John Mike
The problems you are having with the methods using the parameter(Person p) are because you have two different variables: friendName (which exists) and name (which does not). Changing the variable friendName to name will take care of some of the errors you are receiving.
(Also the method getFriendCount() returns friendsCount, but should return friendCount (you have an extra s in there) and your assignment calls for a method called befriend, not bestFriend.)
How to delete friends:
You can delete a friend by parsing the friend out of the friendNames string and then concatenating the two resulting strings back together:
public void unfriend(String name)
{
String[] parseNames = friendNames.split(name);
friendNames = parseNames[0] + parseNames[1];
friendCount--;
}
I would suggest changing befriend and unfriends parameters to accept a String instead of a Person object. Person already has access to its own object and in your main you are trying to pass them Strings anyways. Here is what befriend should look like:
public void befriend(String name) //Changed to "befriend"
{
friendNames = friendNames + " " + name;
friendCount++;
}
Also, you only need one constructor for Person, which should look like this:
public Person(String name)
{
firstName = name;
friendCount = 0;
}
When I run your program (using these changes) I get the following output:
2.0
John Mike
I've been asked to program a small online sales aplication.
It sounds very simple in theory (but it's been a hell for me). I'm just supposed to have an arrayList with about 5 products and then have a client buy 1 to 5 products and print the sales total.
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String printInfo() {
return "Product: " + name + " Cost: " + price;
}
}
Then I have a client class:
public class Cliente {
private String name;
private int numPedido;
ArrayList<Producto> products = new ArrayList<Producto>();
public void listBuilder() {
Producto shirt = new Producto("Shirt", 30);
Producto tshirt = new Producto("T-Shirt", 40);
Producto sweater = new Producto("Sweater", 50);
}
public Cliente(String name, int numPedido) {
this.name = name;
this.numPedido = numPedido;
}
public Cliente() {
}
public String getName() {
return name;
}
public int getNumPedido() {
return (int) (Math.random() * 100);
}
public void addNewClient() {
name = JOptionPane.showInputDialog("Nombre: ");
}
public String printInfo() {
return "Nombre: " + name;
}
}
Right now I'm stuck thinking on how to make a client select a product and get that attached to him. I was thinking on making an arrayList of an arrayList but I'm sure that would complicate things. I know there is probably an easier way to connect them but I can't think of any. The option I have in mind is a method which shows numbers from 1 to 3(corresponding to each product) and when the user picks one it should return the price of the item.
Still not sure how to implement it in a way that the user can pick multiple products.
EDIT:
I also have an admin class that goes like this:
public class Admin {
private Client[] clientList;
public AdminPedidos() {
clientList = new Client[2];
}
public void AddContact() {
clienteList[0] = addProduct();
clienteList[1] = addProduct();
fillList();
}
public Cliente addProduct() {
String contactoString = JOptionPane.showInputDialog("Are you a new client? Press 1 if yes.");
if (contactoString.equals("1")) {
return new Cliente();
} else {
return new Cliente(); //just for testing
}
}
private void fillList() {
for (Client i : clientList) {
i.addNewClient();
}
}
public void printContact() {
for (Client i : clientList) {
System.out.println(i.printInfo());
}
}
}
You can have some purchaseProduct method attached to each Client.
public void purchaseProduct(Product product) { this.products.add(product); }
Then each Client you instantiate (Client client = new Client(name, id);) can add Products to his/her cart with the purchaseProduct method.
I'm assuming you are using some kind of user input method (Scanner). With that you can read the user's input of which Product they want and accordingly call the function with the right Product.
The listBuilder function doesn't quite make sense to me btw (and after your edit, it's really hard to make sense of what the Admin class should be/represent).
Edit: You would probably want to create an ArrayList<Product> which will be attached to each client, which you already have. I sense that you have a difficulty deciding where to put your actual Products. You should not put them inside your Client class for sure.
You should think about who/where they are going to be used. Probably in main right? So just instantiate them there first and then the Client could choose which one to purchase (via the method I introduced before):
client.purchaseProduct(product);
I try to run a programm but i get those four errors.
TestCusomer.java:25: error: toString() in Invoice cannot override toString() in Object (same thing at line 49)
and
line 59 cannot find symbol . myCustomer.setTrn(112233778)
line 60 cannot find symbol . myCustomer.setPersentage(150)
My programm is the following:
class Invoice
{
int trn; //TAX REGISTRATION NUMBER
int persentage;
public Invoice{}
public int setTrn(int trn){
this.trn = trn;
}
public int getTrn(){
return trn;
}
public void setPersentage(int persentage){
this.persentage = persentage;
}
public int getPersentage(){
return persentage;
}
String toString(){
System.out.println(trn+" : "+persentage);
}
}
class Customer{
int trn;
int charging= 0;
public Customer(int trn){
this.trn = trn;
}
public int charge(int amount){
charging = charging + amount;
}
public int charge(int amount , int trn){
if (this.trn == trn){
charging = charging + amount;
}
}
String toString(){
System.out.println(trn+" : "+charging);
}
}
class TestCustomer
{
public static void main(String[] args){
Customer myCustomer = new Customer(112233778);
myCustomer.charge(100);
myCustomer.setTrn(112233778);
myCustomer.setPersentage(150);
System.out.println(myCustomer);
}
}
few things,
You need to declare the toString method public
You need to return a String in your toString methods
I strongly suggest you add the #Override notation when you override a method to ensure that you actually override the method
The cannot find symbol... happen because those methods are not defined in Customer, you have those in Invoice
Your toString() methods need to return String objects. You are outputting a string in them but not returning a string. Also make them public.
For example, your toString() method for the Invoice class should be:
public String toString()
{
return trn + " : " + persentage;
}
And for your second problem (cannot find symbols), those methods are in the Invoice class and not in the Customer class so they cannot be called on a Customer object.
(using Netbeans)
I want to make something with an index like an array does, that when called will return multiple pieces of information which were previously inserted by the user.
e.g
name = Luke;
age = 20;
favouriteColour = red;
I have tried multiple arrays, maps/hashmaps (please someone explain these aswell, I have no clue), and despite it kind of working, it's like I've just thrown spaghetti at the screen.
Any ideas?
I think what you want is an Object, just create a class yourself. For example:
public class Person
{
private String name;
private int age;
private Color favoriteColor;
public Person(){
// perhaps add some defaults here
}
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
public void setAge(int a){
age = a;
}
public int getAge(){
return age;
}
public void setFavoriteColor(Color c){
favoriteColor = c;
}
public Color getFavoriteColor(){
return favoriteColor;
}
}
In your main app you can then create this Object like so:
Person person = new Person();
Store the user inputs in it. So when the user adds a name:
person.setName(nameThatWasInputtedByTheUser);
You can store all of these UserInputContainers in a list:
ArrayList<Person> list = new ArrayList<Person>(); // or "new ArrayList<>();" when you use Java 8.0+
Then at the end you can get all this again like so:
System.out.println("Person at index " + index);
Person currentPerson = list.get(index);
System.out.println("Name: " + currentPerson.getName());
System.out.println("Age: " + currentPerson.getAge());
System.out.println("Color: " + currentPerson.getColor());