When I run this program these errors come up: error: duplicate class: classProg.Client class error: class Main is public, should be declared in a file named Main.java How do I fix it? package classProg; import java.util.Scanner; class Client { String name; int amount; Client(String name, int amount) { this.name = name; this.amount = amount; } public String toString() { return "\n Customer: \n" + name + " " + amount + " cents"; } } public class Main { Client client[]; static Scanner sc = new Scanner(System.in); int totalFifty, totalTen, totalFive; int currentIndex; Main(int no) { client = new Client[no]; currentIndex = 0; totalFifty = totalTen = totalFive = 0; } /** * Method to search a client name in the array of object * @param name - name to search * @return * If found returns the found index position * If not found returns -1 */ int findClient(String name) { for(int c = 0; c < currentIndex; c++) if(client[c].name.equalsIgnoreCase(name)) return c; return -1; } /** * Method to add a client to array of object * @param name - name of the client * @param amount - amount having */ void addClient(String name, int amount) { int pos = findClient(name); if(pos == -1) client[currentIndex++] = new Client(name, amount); else client[pos].amount += amount; } /** * Method to display the change for each fifty, ten and five * @param amount - having the amount */ void showChange(int amount) { int fiftyCent = amount / 50; amount = amount % 50; int tenCent = amount / 10; amount = amount % 10; int fiveCent = amount / 5; amount = amount % 5; totalFifty += fiftyCent; totalTen += tenCent; totalFive += fiveCent; if(fiftyCent != 0) System.out.println("50 cents: " + fiftyCent); if(tenCent != 0) System.out.println("10 cents: " + tenCent); if(fiveCent != 0) System.out.println("5 cents: " + fiveCent); } void findSmallestAmount() { int smallAmt = client[0].amount; int pos = 0; for(int c = 1; c < currentIndex; c++) { if(client[c].amount < smallAmt) { smallAmt = client[c].amount; pos = c; break; } } System.out.println("\n Smallest Amount Clent" + client[pos]); showChange(client[pos].amount); } void findLargestAmount() { int largAmt = client[0].amount; int pos = 0; for(int c = 1; c < currentIndex; c++) { if(client[c].amount > largAmt) { largAmt = client[c].amount; pos = c; break; } } System.out.println("\n Largest Amount Clent" + client[pos]); showChange(client[pos].amount); } void showTotal() { System.out.print("\n Total number of coins for Fifty: " + totalFifty); System.out.print("\n Total number of coins for Ten: " + totalTen); System.out.print("\n Total number of coins for five: " + totalFive); System.out.print("\n Total of all coins: " + (totalFifty + totalTen + totalFive)); } int menu() { System.out.print("\n\n ********************** Change Menu **********************"); System.out.print("\n 1. Enter a name and display change to be " + "given for each denomination"); System.out.print("\n 2. Find the name with the smallest amount and " + "display change to be given for each denomination"); System.out.print("\n 3. Find the name with the largest amount and " + "display change to be given for each denomination"); System.out.print("\n 4. Calculate and display the total number of coins " + "for each denomination, and the sum of these totals"); System.out.print("\n 5. Exit"); System.out.print("\n\t What is your choice: "); return sc.nextInt(); } int getAmount() { int amount; do { System.out.print("\n Please enter the coin value for the person \n" + "(range 5 to 95, multiple of 5): "); amount = sc.nextInt(); if((amount >= 5 && amount <= 95) && amount % 5 == 0) return amount; }while(true);// End of do - while loop } public static void main(String ss[]) { String name; System.out.print("\n Please enter number of persons: "); int person = sc.nextInt(); Main cc = new Main(person); for(int c = 0; c < person; c++) { System.out.print("\n Please enter the name of the person: "); name = sc.next(); cc.addClient(name, cc.getAmount()); } do { switch(cc.menu()) { case 1: System.out.print("\n Please enter the name of the person: "); name = sc.next(); int pos = cc.findClient(name); if(pos == -1) System.out.println("\n No client found with name: " + name); else { System.out.println(cc.client[pos]); cc.showChange(cc.client[pos].amount); } break; case 2: cc.findSmallestAmount(); break; case 3: cc.findLargestAmount(); break; case 4: cc.showTotal(); break; case 5: System.out.print("\n\n\t\t Thanks."); System.exit(0); default: System.out.print("\n\n\t\t Invalid Choice!!"); } }while(true); } }
When I run this program these errors come up:
error: duplicate class: classProg.Client class
error: class Main is public, should be declared in a file named Main.java
How do I fix it?
package classProg;
import java.util.Scanner;
class Client
{
String name;
int amount;
Client(String name, int amount)
{
this.name = name;
this.amount = amount;
}
public String toString()
{
return "\n Customer: \n" + name + " " + amount + " cents";
}
}
public class Main
{
Client client[];
static Scanner sc = new Scanner(System.in);
int totalFifty, totalTen, totalFive;
int currentIndex;
Main(int no)
{
client = new Client[no];
currentIndex = 0;
totalFifty = totalTen = totalFive = 0;
}
/**
* Method to search a client name in the array of object
* @param name - name to search
* @return
* If found returns the found index position
* If not found returns -1
*/
int findClient(String name)
{
for(int c = 0; c < currentIndex; c++)
if(client[c].name.equalsIgnoreCase(name))
return c;
return -1;
}
/**
* Method to add a client to array of object
* @param name - name of the client
* @param amount - amount having
*/
void addClient(String name, int amount)
{
int pos = findClient(name);
if(pos == -1)
client[currentIndex++] = new Client(name, amount);
else
client[pos].amount += amount;
}
/**
* Method to display the change for each fifty, ten and five
* @param amount - having the amount
*/
void showChange(int amount)
{
int fiftyCent = amount / 50;
amount = amount % 50;
int tenCent = amount / 10;
amount = amount % 10;
int fiveCent = amount / 5;
amount = amount % 5;
totalFifty += fiftyCent;
totalTen += tenCent;
totalFive += fiveCent;
if(fiftyCent != 0)
System.out.println("50 cents: " + fiftyCent);
if(tenCent != 0)
System.out.println("10 cents: " + tenCent);
if(fiveCent != 0)
System.out.println("5 cents: " + fiveCent);
}
void findSmallestAmount()
{
int smallAmt = client[0].amount;
int pos = 0;
for(int c = 1; c < currentIndex; c++)
{
if(client[c].amount < smallAmt)
{
smallAmt = client[c].amount;
pos = c;
break;
}
}
System.out.println("\n Smallest Amount Clent" + client[pos]);
showChange(client[pos].amount);
}
void findLargestAmount()
{
int largAmt = client[0].amount;
int pos = 0;
for(int c = 1; c < currentIndex; c++)
{
if(client[c].amount > largAmt)
{
largAmt = client[c].amount;
pos = c;
break;
}
}
System.out.println("\n Largest Amount Clent" + client[pos]);
showChange(client[pos].amount);
}
void showTotal()
{
System.out.print("\n Total number of coins for Fifty: " + totalFifty);
System.out.print("\n Total number of coins for Ten: " + totalTen);
System.out.print("\n Total number of coins for five: " + totalFive);
System.out.print("\n Total of all coins: " +
(totalFifty + totalTen + totalFive));
}
int menu()
{
System.out.print("\n\n ********************** Change Menu **********************");
System.out.print("\n 1. Enter a name and display change to be " +
"given for each denomination");
System.out.print("\n 2. Find the name with the smallest amount and " +
"display change to be given for each denomination");
System.out.print("\n 3. Find the name with the largest amount and " +
"display change to be given for each denomination");
System.out.print("\n 4. Calculate and display the total number of coins " +
"for each denomination, and the sum of these totals");
System.out.print("\n 5. Exit");
System.out.print("\n\t What is your choice: ");
return sc.nextInt();
}
int getAmount()
{
int amount;
do
{
System.out.print("\n Please enter the coin value for the person \n" +
"(range 5 to 95, multiple of 5): ");
amount = sc.nextInt();
if((amount >= 5 && amount <= 95) && amount % 5 == 0)
return amount;
}while(true);// End of do - while loop
}
public static void main(String ss[])
{
String name;
System.out.print("\n Please enter number of persons: ");
int person = sc.nextInt();
Main cc = new Main(person);
for(int c = 0; c < person; c++)
{
System.out.print("\n Please enter the name of the person: ");
name = sc.next();
cc.addClient(name, cc.getAmount());
}
do
{
switch(cc.menu())
{
case 1:
System.out.print("\n Please enter the name of the person: ");
name = sc.next();
int pos = cc.findClient(name);
if(pos == -1)
System.out.println("\n No client found with name: " + name);
else
{
System.out.println(cc.client[pos]);
cc.showChange(cc.client[pos].amount);
}
break;
case 2:
cc.findSmallestAmount();
break;
case 3:
cc.findLargestAmount();
break;
case 4:
cc.showTotal();
break;
case 5:
System.out.print("\n\n\t\t Thanks.");
System.exit(0);
default:
System.out.print("\n\n\t\t Invalid Choice!!");
}
}while(true);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 6 images