In: Computer Science
Please fix this code I am having issues compiling it all together there is 3 codes here and it's giving me errors in my main method..... I feel like it might be something simple but I can't seem to find it.
package assignement2;
import java.util.ArrayList;
import java.util.Scanner;
public class reg1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of items: ");
int number = input.nextInt();
input.nextLine();
for (int i = 0; i < number; i++) {
System.out.print("Enter item name: ");
int n = input.nextInt();
System.out.print("Enter cost of item: ");
double costs = input.nextDouble();
input.nextLine();
ArrayList<Item> items = new ArrayList<>();
ArrayList<Payment> paymentDone = new
ArrayList<>();
boolean add = items.add(new Item(n,costs));
}
double total = 0.0;
int items = input.nextInt();
for (Item i : items) {
total += i.getCost();
}
final double subT = total;
final double salesTax = 0.07 * subT;
final double totalPaymentToBeDone = subT + salesTax;
int itemNumber = 1;
for (Item i : items) {
System.out.println("Item " + itemNumber + ": " + i.getName() + ": "
+ i.getCost());
itemNumber += 1;
}
System.out.println("Total: " + totalPaymentToBeDone);
boolean fullPaymentDone = false;
double amountDue;
double total1 = totalPaymentToBeDone;
while (!fullPaymentDone) {
System.out.println("Please enter payment type.\n1. Cash\n2. Debit
Card\n3. Credit card\n4. Check");
int choice = input.nextInt();
input.nextLine();
System.out.println("Enter the amount to pay with this
type.");
double amount = input.nextDouble();
amountDue = total1 - amount;
double change = 0.0;
if (amountDue < 0)
change = Math.abs(amountDue);
System.out.println("Total after payment: " + amountDue +
"\n");
if (amountDue > 0.0) {
total = amountDue;
continue;
} else {
for (Item i : items) {
System.out.println(((Item) i).getName() + ": " +
i.getCost());
}
System.out.print("-----------------------------------------\n");
System.out.println("Subtotal:\t" + subT);
System.out.println("Tax: \t" + salesTax);
System.out.println("Total: \t" + totalPaymentToBeDone);
System.out.println("Change: \t" + change);
fullPaymentDone = true;
}
}
input.close();
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
package assignement2;
public class Payment {
private double amount;
private PaymentType paymentType;
public Payment(double amount, PaymentType paymentType)
{
this.amount = amount;
this.paymentType = paymentType;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public PaymentType getPaymentType() {
return paymentType;
}
public void setPaymentType(PaymentType paymentType)
{
this.paymentType = paymentType;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package assignement2;
public class Item {
private String name;
private double cost;
public Item(String name, double cost) {
this.name = name;
this.cost = cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
}
This is the code of your question which fix all the errors, I mentioned below with all the changes with comments so that you can easily understand the code. Copy and paste the code in java IDE or compiler and run it. I also provide the screenshots of output.
reg1.java :-
package assignement2;
import java.util.ArrayList;
import java.util.Scanner;
public class reg1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of items: ");
int number = input.nextInt();
/* declare arraylist outside the scope because
if you declare arraylist inside then it only accesible within scope
*/
ArrayList<Item> items = new ArrayList<Item>();
for (int i = 0; i < number; i++) {
System.out.print("Enter item name: ");
String n = input.next();
System.out.print("Enter cost of item: ");
double costs = input.nextDouble();
boolean add = items.add(new Item(n,costs));
}
double total = 0.0;
for (Item i : items) {
total += i.getCost();
}
final double subT = total;
final double salesTax = 0.07 * subT;
final double totalPaymentToBeDone = subT + salesTax;
int itemNumber = 1;
for (Item i : items) {
System.out.println("Item " + itemNumber + ": " + i.getName() + ": "
+ i.getCost());
itemNumber += 1;
}
System.out.println("Total: " + totalPaymentToBeDone);
boolean fullPaymentDone = false;
double amountDue;
double total1 = totalPaymentToBeDone;
while (!fullPaymentDone) {
System.out.println("Please enter payment type.\n1. Cash\n2. Debit
Card\n3. Credit card\n4. Check");
int choice = input.nextInt();
System.out.println("Enter the amount to pay with this
type.");
double amount = input.nextDouble();
Payment paymentdone=new Payment(amount,choice);
amountDue = total1 - amount;
double change = 0.0;
if (amountDue < 0)
change = Math.abs(amountDue);
System.out.println("Total after payment: " + amountDue +
"\n");
if (amountDue > 0.0) {
total = amountDue;
continue;
}
else {
for (Item i : items) {
System.out.println(((Item) i).getName() + ": " +
i.getCost());
}
System.out.print("-----------------------------------------\n");
System.out.println("Subtotal:\t" + subT);
System.out.println("Tax: \t" + salesTax);
System.out.println("Total: \t" + totalPaymentToBeDone);
System.out.println("Change: \t" + change);
fullPaymentDone = true;
}
}
input.close();
}
}
Item.java :-
package assignement2;
public class Item {
private String name;
private double cost;
public Item(String name, double cost) {
this.name = name;
this.cost = cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
}
Payment.java :-
package assignement2;
public class Payment {
private double amount;
// take return type of variable paymentType as int
private int paymentType;
public Payment(double amount, int paymentType) {
this.amount = amount;
this.paymentType = paymentType;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public int getPaymentType() {
return paymentType;
}
public void setPaymentType(int paymentType) {
this.paymentType = paymentType;
}
}
Screenshot of output :-