Write a java program to calculate the income of sales person per week.
Itemcode
Item Name
Unit Price
Quantity
121
Shampoo
250.45
3
131
Soap
102.16
2
141
Talcum powder
98.23
3
145
Oxford Dictionary
500.24
4
6. Write a java application that inputs an integer containing only 0‘s and 1’s (i.e. a binary integer) and prints its decimal equivalent.
// task 1
import java.util.ArrayList;
public class Salesman {
private String name;
private ArrayList<Item> saledUnits;
private double income, count;
public Salesman(String n) {
name = n;
saledUnits = new ArrayList<>();
}
public void setSaled(Item i) {
saledUnits.add(i);
}
public double calculateWeeklyIncome() {
int in = 0;
for (Item i : saledUnits) {
in += i.unitPrice * i.quantity;
}
System.out.println("The weekly income of " + name + " is $" + in);
income += in;
return in;
}
public void nextWeek() {
count += income;
income = 0;
saledUnits.clear();
}
public static class Item {
private int itemCode;
private String itemName;
private double unitPrice;
private int quantity;
public Item(int code, String name, double price, int amount) {
itemCode = code;
itemName = name;
unitPrice = price >= 0 ? price : 0;
quantity = amount;
}
}
public static void main(String args[]) {
Salesman lewis = new Salesman("Lewis");
lewis.setSaled(new Item(121, "Shampoo", 250.45, 3));
lewis.setSaled(new Item(131, "Soap", 102.16, 2));
lewis.setSaled(new Item(141, "Talcum powder", 98.23, 3));
lewis.setSaled(new Item(145, "Oxford Dictionary", 500.24, 4));
lewis.calculateWeeklyIncome();
lewis.nextWeek();
}
}
// Is it correct output or it need be more detailed, for example,
// with the list of the saled?
// task 2
import java.util.Scanner;
public class BinaryString {
public static void main(String args[]) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Please input a binary integer, press s to stop.");
while (scanner.hasNext()) {
String input= scanner.next();
if (input.matches("[01]+")) {
int count = input.charAt(input.length()-1)==48 ? 0 : 1; // it means 0
for (int c = input.length() - 2, d = 2; c > -1; c--, d *= 2) {
if (input.charAt(c) == 49) // it means 1
count += d;
}
System.out.println(count);
} else if (input.matches("s"))
return;
else
System.out.println("Input is incorrect, try again.");
}
}
}
}
// thank you, this is much better
Comments
Leave a comment