Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I will give a +1 for who solve it correctly. (Its easy and so basic, please read

ID: 3683478 • Letter: I

Question

I will give a +1 for who solve it correctly. (Its easy and so basic, please read it carefully)

Level:

objects, classes, getters and setters, access and non-access modifiers, composition and arrays.

Problem:

implement a system to store some information about the local fruit shops. The system will accept a few fields, such as shop name, balance, owner name, owner age, and how many apples and the price per apple each store has.

When a user opens your program, they are greeted with a menu with 7 different possible operations:

List all fruit shop

Add a new fruit shop

Delete a fruit shop

Modify the quantity of apples in a fruit shop

Modify the price per apple in a fruit shop

Change the owner of the fruit shop

The last option is the exit option. This menu will repeat until the user presses ‘e’ or ‘E’ to exit the program.

You must use the skeleton as your starting point. (the Skelton is provided at the end of this document)

The skeleton gives you the minimum of four classes:

The App class will be the entry point of your program and where most of the logic and execution happens.

The FruitShop class holds information about each fruit shop. It must have references to two other classes using what you learned in class regarding composition: Apples and OwnerInfo. You are responsible for creating constructors, methods, getters, setters and all fields. All instance variables must be private. That means you must take care of setters and getters for any variable you declare.

The OwnerInfo class has two fields: owner name and age. These two fields must be private.

The Apples class also has two fields. Quantity of apples and price per apple. These two fields must be private.

You are free to add any additional classes or utility functions that you think will be necessary.

To get input from the user, write the following code

Scanner in = new Scanner(System.in);

Then, for integers:

int a = Integer.parseInt(in.next());

For doubles: double b = Double.parseDouble(in.next());

For strings String t = in.next();

Finally, don’t forget to

in.close();

*Detailed description of each selection item:

1- List all available fruit shops in the system

This will simply print out all the fruit shops in the system. The columns your customers are interesting in seeing are: ID of store, name of store, balance of store, owner name, age, and “stock worth”. The ID must be automatically generated using a static field that increments automatically every time you add a fruit shop. The name is a string that does not contain spaces, the balance is the current balance of the store in USD, name of owner is a string that does not contain any spaces, age is an integer, and finally

the “stock worth” is simply the amount of apples that store has times the price of each apple.

Here is an example of an output of this command:

ID       Name           Balance              Owner       Age      Stock worth

--           ----                 -------                 -----           ---       -----------

1       FruitShop        342.33           Abdulla         23      720.30

2       MegaStore      33.33              Sara              36      3.30

3      BigFruit          83432.30        Ahmad         52       1957.20

2- Add a new fruit shop

You are required to enter information to create a new fruit shop in the system. The user must be asked to input the following: a. Name of fruit shop b. Current balance c. Owner name d. Age of owner e. How many apples are in stock f. The price of each apple

The ID must be auto-generated using a static variable/method. The user will not be asked for it. Try to keep the spacing as close as possible to what you see above.

3- Delete a fruit shop

A user is asked to enter an ID of the store to delete. If found, that store is erased from the record with a “Success message”. Otherwise, you must print a “not found” message.

4- Modify quantity of apples in fruit shop

Here a user is asked to enter the ID of the store to change. If not found, a proper message must be printed, otherwise your program asks for the user to enter the new quantity of apples for this store and your program must update that store’s quantity.

5-Modify price of apples in a fruit shop

Same as 4, except for the price.

6- Change owner of fruit shop

A user is asked to enter the ID of the fruit shop to change the owner of. If not found, a proper message must be indicated. Otherwise, the user enters the name and age of the new owner. Your program must reflect that change the next time the user prints the list of fruit shops.

****The Skelton***(for each one):

APP:

package homework2;

import homework2.model.Apples;

import homework2.model.FruitShop;

import homework2.model.OwnerInfo;

import java.util.ArrayList;

import java.util.Scanner;

public class App {

                public static void main(String[] args) {

                               

                                new App().run();

                }

               

               

                private void run() {

                }

               

               

}

Apples:

package homework2.model;

public class Apples {

                private int quantity;

                private double price;

               

               

                public Apples(int quantity, double price) {

                                this.quantity = quantity;

                                this.price = price;

                }

}

FruitShop:

package homework2.model;

public class FruitShop {

                public FruitShop() {

                               

                }

}

OwnerInfo:

package homework2.model;

public class OwnerInfo {

               

               

               

}

Explanation / Answer

FruitShop.java:

package fr.ybonnel;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

public class FruitShop {

private static class Article {

private int price;
private int lotSize;
private int reducPrice;
private String cat;

public Article(int price, int lotSize, int reducPrice, String cat) {
this.price = price;
this.lotSize = lotSize;
this.reducPrice = reducPrice;
this.cat = cat;
}
}

private ConcurrentHashMap<String, Article> database = new ConcurrentHashMap<>();

public FruitShop() {
database.put("Pommes", new Article(100, 1, 0, "P"));
database.put("Mele", new Article(100, 2, 100, "P"));
database.put("Apples", new Article(100, 3, 100, "P"));
database.put("Bananes", new Article(150, 2, 150, "B"));
database.put("Cerises", new Article(75, 2, 20, "C"));
}

public static void main(String[] args) {
new FruitShop().caisse(System.in, System.out);

}

private ConcurrentHashMap<String, AtomicInteger> counters = new ConcurrentHashMap<>();

public void caisse(InputStream in, PrintStream out) {

out.println("Caisse prête...");

Scanner scanner = new Scanner(in);

AtomicInteger sum = new AtomicInteger(0);

while (true) {
String article = scanner.next();

if (article.equals("exit")) {
break;
}

out.println(sum.addAndGet(Stream.of(article.split(","))
.map(this::traiteArticle)
.mapToInt(Integer::valueOf)
.sum()));

}

out.println("Good bye");
}

private int traiteArticle(String articleName) {
Article article = database.get(articleName);

int counter = counters.computeIfAbsent(articleName, (key) -> new AtomicInteger(0)).incrementAndGet();
int counterCat = counters.computeIfAbsent(article.cat, (key) -> new AtomicInteger(0)).incrementAndGet();
int counterFruit = counters.computeIfAbsent("Fruit", (key) -> new AtomicInteger(0)).incrementAndGet();

int articlePrice = article.price;

if (counter == article.lotSize) {
counters.get(articleName).set(0);
articlePrice -= article.reducPrice;
}

if (article.cat.equals("P") && counterCat == 4) {
articlePrice -= 100;
counters.get(article.cat).set(0);
}
if (counterFruit == 5) {
articlePrice -= 200;
counters.get("Fruit").set(0);

}
return articlePrice;

}
}

FruitShopTest.java:

package fr.ybonnel;

import org.apache.commons.io.input.ReaderInputStream;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.assertEquals;

public class FruitShopTest {

private void oneTest(int expectedResult, String... articles) {
InputStream reader = new ReaderInputStream(new StringReader(
Stream.of(articles).collect(Collectors.joining(" ")) + " exit "
));

ByteArrayOutputStream stream = new ByteArrayOutputStream();

new FruitShop().caisse(reader, new PrintStream(stream));

String result = new String(stream.toByteArray());

String[] lines = result.split(" ");

String lastTotal = lines[lines.length - 2];

System.out.println(result);

assertEquals(expectedResult, Integer.parseInt(lastTotal));


}

@Test
public void testeSimpleCaisse() {

oneTest(410, "Cerises", "Pommes", "Cerises", "Bananes", "Cerises", "Cerises", "Pommes");

}


@Test
public void testeCsv() {
oneTest(410, "Cerises,Pommes,Cerises", "Bananes", "Cerises,Cerises", "Pommes");
}

@Test
public void testeBananes() {


oneTest(355, "Cerises", "Pommes", "Cerises", "Bananes", "Pommes", "Bananes", "Cerises");
}

@Test
public void testeMultiLang() {


oneTest(380, "Cerises", "Apples", "Cerises", "Bananes", "Pommes", "Mele");
}

@Test
public void testeMultiLangPlus() {


oneTest(230, "Mele", "Apples", "Apples", "Pommes", "Apples", "Mele", "Cerises", "Cerises");
}

@Test
public void testeMultiLangPlusPlus() {


oneTest(380, "Mele", "Apples", "Apples", "Pommes", "Apples", "Mele", "Cerises", "Cerises", "Bananes");
}

@Test
public void testeMultiLangPlusPlusPlus() {


oneTest(380, "Cerises,Apples", "Cerises", "Apples,Pommes,Bananes", "Apples,Pommes","Mele", "Pommes");
}

@Test
public void testeApoteose() {


oneTest(150, "Mele,Apples,Apples,Mele", "Bananes", "Mele,Apples,Apples,Pommes,Mele");
}

@Test
public void testeApoteosePlus() {


oneTest(250, "Mele,Apples,Apples,Pommes,Mele", "Bananes");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote