import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class AccontOperator {
ArrayList<Account> cutAccounts = new ArrayList<>();
Scanner sc = new Scanner(System.in);
Random random = new Random();
Account account = new Account("光头强", 0, 80,0);
public void start() {
while (true) {
System.out.println("请输入(砍价)开始砍价");
String cut = sc.next();
if (cut.equals("砍价")) {
System.out.println("恭喜你,成功开始砍价");
break;
} else {
System.out.println("输入错误");
}
}
while (true) {
System.out.println("=====砍价帮=====");
System.out.println("1. 开始砍价");
System.out.println("2. 查看砍价记录");
System.out.println("请输入您要选择的功能:");
int Choice = sc.nextInt();
switch (Choice) {
case 1:
// 开始砍价
cut();
break;
case 2:
// 查看砍价记录
look();
break;
default:
System.out.println("输入错误,请重新输入");
}
}
}
//查看砍价记录
private void look() {
System.out.println("==========>砍价记录<-===========");
for (Account cutAccount : cutAccounts) {
System.out.println(cutAccount.getUsername() + "帮你砍了" + cutAccount.getCutPrice() + "元");
}
}
//开始砍价
private void cut() {
int i = 0;
System.out.println("***************************************");
System.out.println("已砍" + account.getCutPrice() + "元" + ",还需砍" + (account.getTotalPrice() - account.getCutPrice()) + "元");
while (true) {
Account cutPerson = new Account();
System.out.println("==============->砍价帮<-================");
System.out.println("请输入砍价人的姓名:");
String name = sc.next();
//检查是否重复
if (judgeName(cutAccounts, name)) {
System.out.println("该用户已参与过砍价");
continue;
}
cutPerson.setUsername(name);
i++;
//随机砍价
if (i == 10) {
cutPerson.setCutPrice(account.getTotalPrice() - account.getTotalCutPrice());
} else {
int money = random.nextInt(10);
cutPerson.setCutPrice(money);
}
//更新总金额
if (account.getTotalPrice() - account.getTotalCutPrice() < cutPerson.getCutPrice()) {
account.setTotalCutPrice(80);
}else {
account.setTotalCutPrice(account.getTotalCutPrice() + cutPerson.getCutPrice());
}
//放入集合去
cutAccounts.add(cutPerson);
System.out.println("恭喜" + cutPerson.getUsername() + "成功帮你砍了" + cutPerson.getCutPrice() + "元"+"");
System.out.println("已砍" + account.getTotalCutPrice() + "元" + ",还需砍" + (account.getTotalPrice() - account.getTotalCutPrice()) + "元");
//总金额为零就结束
if (account.getTotalCutPrice() == 80) {
System.out.println("恭喜你,砍价成功");
break;
}
}
}
//比较是否重复
private boolean judgeName(ArrayList<Account> cutAccounts,String name) {
boolean judge = false;
for (Account cutAccount : cutAccounts) {
if (cutAccount.getUsername().equals(name)) {
judge = true;
break;
}
}
return judge;
}
}
public class Account {
private String username;
private int totalPrice;
private int cutPrice;
private int totalCutPrice;
public Account() {
}
public Account(String username, int cutPrice, int totalPrice,int totalCutPrice) {
this.username = username;
this.totalPrice = totalPrice;
this.cutPrice = cutPrice;
this.totalCutPrice = totalCutPrice;
}
public int getTotalCutPrice() {
return totalCutPrice;
}
public void setTotalCutPrice(int totalCutPrice) {
this.totalCutPrice = totalCutPrice;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(int totalPrice) {
this.totalPrice = totalPrice;
}
public int getCutPrice() {
return cutPrice;
}
public void setCutPrice(int cutPrice) {
this.cutPrice = cutPrice;
}
}
public class Test {
public static void main(String[] args) {
AccontOperator accontOperator = new AccontOperator();
accontOperator.start();
}
}
作为学Java开始的第一个项目。