Question

design two java classes The example of output would be this: user puts the amount of...

design two java classes

The example of output would be this:

user puts the amount of money: (for example 9000)
I'm going to get three things
1. Cellphone cost: 3000
2. Laptop cost: 6000
3. Macbook cost: 4000

From now on, my balance would be (+balance) and I bought cellphone, laptop,
(since there is not enough amount of money, I can only buy cellphone and laptop. if there is enough money then it can buy all things)

so I have to create two classes that are person and product
here is my main code :

class Cellphone extends Product {
public Cellphone(int price, String name) {
super(price, name);
}
}

class Computer extends Product {
public Computer(int price, String name) {
super(price, name);
}
}

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
       items[0] = new Cellphone(3000, "Cellphone");
       items[1] = new Computer(5000, "laptop");
       items[2] = new Computer(4000, "Macbook");
int balance = 0;
       Product[] items = new Product[3];
      
       System.out.print("amount of money: ");
       balance = sc.nextInt();
       Person p = new Person(balance);
      
       System.out.println("\nI'm going to get three things");
       for (int i = 0; i < 3; i++)
       System.out.println((i + 1) + ". " + items[i].name + ": " + items[i].price);

       for (int i = 0; i < 3; i++)
           p.buy(items[i]);
      
       System.out.println("\nFrom now on, my balance would be " + p.getBalance() + " and I bought");
       for (int i = 0; i < p.getItemsCount(); i++)
           System.out.print(p.myItems[i] + ", ");
      
       sc.close();
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here are all the 5 classes.

=========================================================================================

public class Product {

    private String name;
    private int price;

    public Product( int price, String name) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return name;
    }
}

=========================================================================================

public class Cellphone extends Product {


    public Cellphone(int price, String name) {
        super(price, name);
    }
}

=========================================================================================

public class Computer extends Product {


    public Computer(int price, String name) {
        super(price, name);
    }
}

=========================================================================================

public class Person {


    private int balance;
    public Product[] myItems;
    private int counter;

    public Person(int bal) {
        this.balance = bal;
        myItems = new Product[3];
        counter = 0;

    }

    public void buy(Product product) {

        if (product.getPrice() < this.balance) {
            myItems[counter++] = product;
            this.balance -= product.getPrice();
        }

    }

    public int getBalance() {
        return balance;
    }

    public int getItemsCount() {
        return counter;
    }
}

=========================================================================================

import java.util.Scanner;


class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Product[] items = new Product[3];
        items[0] = new Cellphone(3000, "Cellphone");
        items[1] = new Computer(5000, "laptop");
        items[2] = new Computer(4000, "Macbook");
        int balance = 0;

        System.out.print("amount of money: ");
        balance = sc.nextInt();
        Person p = new Person(balance);

        System.out.println("\nI'm going to get three things");
        for (int i = 0; i < 3; i++)
            System.out.println((i + 1) + ". " + items[i].getName() + ": " + items[i].getPrice());

        for (int i = 0; i < 3; i++)
            p.buy(items[i]);

        System.out.println("\nFrom now on, my balance would be " + p.getBalance() + " and I bought");
        for (int i = 0; i < p.getItemsCount(); i++)
            System.out.print(p.myItems[i] + ", ");

        sc.close();
    }
}

=========================================================================================

Add a comment
Know the answer?
Add Answer to:
design two java classes The example of output would be this: user puts the amount of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java Use the classes given in the previous question and write the output tha t is...

    java Use the classes given in the previous question and write the output tha t is generated when this program is run. You may write your answer as "Elaine 2 Jerry 1" or "Compiler Error" or "Runtime Error" If there is an error then explain why. public class Main{ public static void main(String args[]){ Object o = new Kramer(); ((George)(o)).method1(); ((Jerry)(o)). method1(); 0.method2(); public class George extends Elaine {! public void method1() { System.out.print("George 1 "); public class Jerry {...

  • ​I have to create two classes one class that accepts an object, stores the object in...

    ​I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList {    private ShoppingItem [] list;    private int amtItems = 0;       public ShoppingList()    {       list=new ShoppingItem[8];    }       public void add(ShoppingItem item)    {       if(amtItems<8)       {          list[amtItems] = ShoppingItem(item);...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • Write a java programm that calculates the total of a retail sale should ask the user...

    Write a java programm that calculates the total of a retail sale should ask the user for the following: The retail price of the item being purchased The sales tax rate Once these items have been entered, the program should calculate and display the following: The sales tax for the purchase The total of the sale I tried doing it here. but it is not giving me the right answer. kindly help correct the errors. import java.util.Scanner; public class SalesTax...

  • Please make the following code modular. Therefore contained in a package with several classes and not...

    Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q {    public static LinkedList<String> dogs = new LinkedList<String> ();    public static LinkedList<String> cats = new LinkedList<String> ();    public static LinkedList<String> animals = new LinkedList<String> ();    public static void enqueueCats(){        System.out.println("Enter name");        Scanner sc=new Scanner(System.in);        String name = sc.next();        cats.addLast(name);        animals.addLast(name);    }   ...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • Create a java class that user put two inputs and first input generate n length array...

    Create a java class that user put two inputs and first input generate n length array of randomly generated numbers. and the second input changes that number of multiples in the array into zero. for example, if the user puts 3 and 5 then it generates 34 60 10 and since the second input is 5 then the multiple of 5 eliminates so it generates 34 0 0 here is the main method that I'm going to use class Main...

  • *JAVA Prompt the user for a movie name. Output the three movies that come alphabetically before...

    *JAVA Prompt the user for a movie name. Output the three movies that come alphabetically before the one entered. I need help correcting my code. I'm unsure why I am getting a name, year, and genre in my output. Input: Jericho Output: Similar title finder. Enter a movie name.\n Here are the 3 movies that are listed before the one you entered\n Jeremy Paxman Interviews...\n Jeremy Taylor\n Jeremy Vine Meets...\n Current output: Similar title finder. Enter a movie name.\n Here...

  • I am creating a program where a user will enter in a price amount. And then...

    I am creating a program where a user will enter in a price amount. And then the program will determine how many 20, 10, 5, 1, etc.. they get back. I keep getting "error: unexpected type" in my findValue method. Not too sure why I am getting this. There also might be more bugs in there that I am not aware of because it wont compile. Any help is appreciated. Here is my code: import java.util.Scanner; public class prac1 {...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT