Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can u please help me with this I have done the Item class import java.util.Scanner; /** * Write a description of class GroceryCheckoutWithItemClass here. *

can u please help me with this

I have done the Item class

import java.util.Scanner; /** * Write a description of class GroceryCheckoutWithItemClass here. * Use a single ARRAY OF ITEM Objects to store the data for the purchases * @author (your name) * @version (a version number or a date) */ public class GroceryCheckoutWithItemClass { public static void main(String [] args) { Scanner keyboard = new Scanner(System.in); int numItems;

System.out.println("How many items? "); numItems = keyboard.nextInt(); } }

##############################Item###############################

import java.util.*; /** * class Item represents a Grocery checkout item * * @author () * @version (a version number or a date) */ public class Item { // instance variables private String name; private double price; private int serialNumber; private static int count = 0;

/** * No-args Constructor for objects of class Item */ public Item() { name = "----"; price = 0.0; serialNumber = count++; }

// explicit constructor public Item(String item, double price) { name = "----"; this.price = price; serialNumber = count++; } // getName public String getName(){ return this.name; } // getPrice public double getPrice(){ return this.price; } // setName public void setName(String name){ this.name = name; } // setPrice public void setPrice(double price){ this.price = price; } public String toString(){ return name + " " + price +"s/n: " + serialNumber; } public int getSerialNumber(){ return serialNumber; }

public static void main(String [] args) { //*********** test 1 No-arg (default) Constructor, toString ********** Item one = new Item(); System.out.println("Item one = " + one); //*********** END test 1 ***********************************/ /*********** test 2 Explicit Constructor *******************/

Item two = new Item("peas",2.40); System.out.println("Item two = " + two); //*********** END test 2 ***********************************/ /*********** test 3 getters and setters ********************/ one.setName("peas and carrots"); System.out.println("Item one name = " + one.getName()); two.setPrice(2.59); System.out.println("Item two price = " + two.getPrice()); //*********** END test 3 ***********************************/ /*********** test 4 check if we have peas *******************/ // two ways to check if we have peas if (one.getName().equals("peas")) // comparing two Strings System.out.println("Item one: Nice peas there");

if (two.getName().equals("peas")) System.out.println("Item two: Nice peas there");

//*********** end test 4 *********************************/ /*********** test 5 read an Item object from Scanner ******/ Scanner keyboard = new Scanner(System.in); String name; double price; System.out.print("Enter an item (name and price): "); name = keyboard.next(); price = keyboard.nextDouble(); Item three = new Item(name, price); System.out.println("Item three = " + three); System.out.print("Enter a new name and price for Item three: "); name = keyboard.next(); price = keyboard.nextDouble(); three.setName(name); three.setPrice(price); System.out.println("Item three now = " + three); //*********** end test 5 *********************************/ } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions