Question
can i get an example of a java code using a class and bulit in methods inculding everything else in the picture
20% Control structures decision: if. else if.... Else switch ... case (optional) repetition (at least 2 iterations) while, do
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Item.java

public class Item {
   private String name;

   private int price;

   public Item() {
       this.name = "none";
       this.price = 0;
   }

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

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the price
   */
   public int getPrice() {
       return price;
   }

   /**
   * @param price
   * the price to set
   */
   public void setPrice(int price) {
       if (price < 0)
           this.price = 0;
       else
           this.price = price;
   }

}

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

// Customer.java

public class Customer {
   private String name;
   private int promotion;
   private Item basket[] = null;
   private int cnt;
private double calculatedPayment;
private double discountedPrice;
  
   public Customer() {
       this.basket = new Item[30];
       cnt = 0;
       this.promotion = 100;
       this.name = "Unknown";
   }

   public Customer(String name) {
       this.basket = new Item[30];
       cnt = 0;
       this.promotion = 100;
       this.name = name;
   }

   /**
   * @param name
   * @param promotion
   * @param basket
   */
   public Customer(String name, int promotion) {

       this.name = name;
       setPromotion(promotion);
       this.basket = new Item[30];
       this.cnt = 0;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the promotion
   */
   public int getPromotion() {
       return promotion;
   }

   /**
   * @param promotion
   * the promotion to set
   */
   public void setPromotion(int promotion) {
       if (promotion < 0 || promotion > 100)
           this.promotion = 100;
       else
           this.promotion = promotion;
   }

   public void addItem(Item i) {
       this.basket[cnt] = i;
       cnt++;
   }

   public void deleteItem(String itemName) {
       int pos = -1;
       for (int i = 0; i < cnt; i++) {
           if (basket[i].getName().equalsIgnoreCase(itemName)) {
               pos = i;
               break;
           }
       }
       if (pos != -1) {
           // remove specific indexed element in array
           // left shift array
           for (int i = pos; i < cnt - 1; i++) {
               basket[i] = basket[i + 1];
           }
           cnt--;
       } else {
           System.out.println("** Item Not found **");
       }

   }

   public void listItem() {
      
       for (int i = 0; i < cnt; i++) {
           System.out.println((cnt + 1) + " " + basket[i].getName() + " "
                   + basket[i].getPrice());
           calculatedPayment += basket[i].getPrice();
       }
       System.out.println("Total Price :" + calculatedPayment);
       discountedPrice = (calculatedPayment * ((double) getPromotion() / 100));
       System.out.printf("Discounted Price :%.2f\n",discountedPrice);

   }
}

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

// Test.java

import java.util.Scanner;

public class Test {
   /*
   * Creating an Scanner class object which is used to get the inputs entered
   * by the user
   */
   static Scanner sc = new Scanner(System.in);

   public static void main(String[] args) {
       Customer c1 = new Customer("Ali", 90);

       Customer c2 = new Customer("Ayse");

       Customer c3 = new Customer();

       c3.setPromotion(70);

       int choice;
       while (true) {
           System.out.println("\n(1) " + c1.getName());
           System.out.println("(2) " + c2.getName());
           System.out.println("(3) " + c3.getName());
           System.out.println("(4) Terminate");
           System.out.print("Enter Choice :");
           choice = sc.nextInt();
           switch (choice) {
           case 1: {
               choice = menu();
               switch (choice) {
               case 1: {
                   System.out.print("Enter the name of the item :");
                   String name = sc.next();
                   System.out.print("Enter the Price of the item :");
                   int price = sc.nextInt();
                   Item i = new Item(name, price);
                   c1.addItem(i);

                   break;
               }
               case 2: {
                   System.out.print("Enter the name of the item to delete:");
                   String name = sc.next();
                   c1.deleteItem(name);
                   break;
               }
               case 3: {
                   c1.listItem();
                   break;
               }

               }

               continue;
           }
           case 2: {
               choice = menu();
               switch (choice) {
               case 1: {
                   System.out.print("Enter the name of the item :");
                   String name = sc.next();
                   System.out.print("Enter the Price of the item :");
                   int price = sc.nextInt();
                   Item i = new Item(name, price);
                   c2.addItem(i);
                   break;
               }
               case 2: {
                   System.out.print("Enter the name of the item to delete:");
                   String name = sc.next();
                   c2.deleteItem(name);
                   break;
               }
               case 3: {
                   c2.listItem();
                   break;
               }

               }

               continue;
           }
           case 3: {
               choice = menu();
               switch (choice) {
               case 1: {
                   System.out.print("Enter the name of the item :");
                   String name = sc.next();
                   System.out.print("Enter the Price of the item :");
                   int price = sc.nextInt();
                   Item i = new Item(name, price);
                   c3.addItem(i);
                   break;
               }
               case 2: {
                   System.out.print("Enter the name of the item to delete:");
                   String name = sc.next();
                   c3.deleteItem(name);
                   break;
               }
               case 3: {
                   c3.listItem();
                   break;
               }

               }

               continue;
           }
           case 4: {

               break;
           }
           default: {
               System.out.println("** Invalid Choice **");
               continue;
           }

           }
           break;

       }

   }

   private static int menu() {
       int choice;
       System.out.println("(1) AddItem");
       System.out.println("(2) DeleteItem");
       System.out.println("(3) ListItem");
       while (true) {
           choice = sc.nextInt();
           if (choice < 1 || choice > 3) {
               System.out.println("** Invalid.Must be between 1-3 **");
           } else {
               break;
           }
       }

       return choice;
   }

}

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

Output:


(1) Ali
(2) Ayse
(3) Unknown
(4) Terminate
Enter Choice :1
(1) AddItem
(2) DeleteItem
(3) ListItem
1
Enter the name of the item :Computer
Enter the Price of the item :3000

(1) Ali
(2) Ayse
(3) Unknown
(4) Terminate
Enter Choice :1
(1) AddItem
(2) DeleteItem
(3) ListItem
1
Enter the name of the item :rose
Enter the Price of the item :20

(1) Ali
(2) Ayse
(3) Unknown
(4) Terminate
Enter Choice :2
(1) AddItem
(2) DeleteItem
(3) ListItem
1
Enter the name of the item :Skirt
Enter the Price of the item :250

(1) Ali
(2) Ayse
(3) Unknown
(4) Terminate
Enter Choice :1
(1) AddItem
(2) DeleteItem
(3) ListItem
3
3 Computer 3000
3 rose 20
Total Price :3020
Discounted Price :2718.00

(1) Ali
(2) Ayse
(3) Unknown
(4) Terminate
Enter Choice :4

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
can i get an example of a java code using a class and bulit in methods...
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
  • can i get an example of a java code using a class and user defined menthod...

    can i get an example of a java code using a class and user defined menthod with everything else listed in the picture 20% Control structures decision: if. else if.... Else switch ... case (optional) repetition (at least 2 iterations) while, do...while for 20% Classes (at least 2 classes) 10% Methods (at least 2 methods) user-defined and/or built-in methods 10% Arrays (at least 1 set of array) 20% User Interface Menu like starting point User friendliness Error free

  • ***Please give the java code for the below and add comments for different areas for a...

    ***Please give the java code for the below and add comments for different areas for a dummy to understand*** This java program will combine the techniques of handling arrays, decision control statements, and loops. Your program should accomplish the following: Build an array that holds characters (size 35) Load the array with random capital letters (use random generator) Your program should present a menu to the user allowing for the following “actions”. To “search” for a target letter of the...

  • can i get an example of a simple code using the following picture in java 20%...

    can i get an example of a simple code using the following picture in java 20% Basic Java usage of comments next to the program variables declaration: input/output data types

  • Please help me code the following in: JAVA Please create the code in as basic way...

    Please help me code the following in: JAVA Please create the code in as basic way as possible, so I can understand it better :) Full points will be awarded, thanks in advance! Program Description Write a program that demonstrates the skills we've learned throughout this quarter. This type of project offers only a few guidelines, allowing you to invest as much time and polish as you want, as long as you meet the program requirements described below. You can...

  • Note: According to the question, please write source code in java only using the class method....

    Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors. Exercise #2: Design and implement a program (name it compareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array...

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

  • Need assistance with this problem. This is for a java class and using eclipse. For this...

    Need assistance with this problem. This is for a java class and using eclipse. For this assignment we’ll be doing problems 21.3 and 21.4 from your textbook. Problem 21.3 asks you to write a generic method that removes duplicate items from an ArrayList. This should be fairly straightforward. Problem 21.4 asks you to implement insertion sort within a generic method. Insertion sort is described in detail on pages 250-252 of your textbook. You can write your two methods in a...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Code a complete Java program for the following payroll application: First, hard code the following data...

    Code a complete Java program for the following payroll application: First, hard code the following data for the object ‘Employee’ into 4 separate arrays: SSN: 478936762, 120981098, 344219081, 390846789, 345618902, 344090917 First name      : Robert, Thomas, Tim, Lee, Young, Ropal Last name       : Donal, Cook, Safrin, Matlo, Wang, Kishal Hourly rate     : 12.75, 29.12, 34.25, 9.45,   20.95, 45.10 Hours worked: 45,        40,        39,       20,      44,        10 These 4 arrays must be declared inside the class and not within any method....

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

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